From 7ad63246f3ec3232e57f7ab273974bc158445883 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Wed, 8 Feb 2023 17:52:24 -0500 Subject: [PATCH] fix mysql, entityhider ignore temp players --- pom.xml | 2 - .../hideAndSeek/database/Database.java | 7 +- .../hideAndSeek/database/GameDataTable.java | 2 +- .../hideAndSeek/database/InventoryTable.java | 2 +- .../hideAndSeek/database/NameDataTable.java | 2 +- .../database/connections/MySQLConnection.java | 4 +- .../hideAndSeek/game/EntityHider.java | 77 +++++++++++++++---- 7 files changed, 72 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 6a8df6e..d110881 100644 --- a/pom.xml +++ b/pom.xml @@ -47,8 +47,6 @@ *:* META-INF/** - sqlite-jdbc.properties - mariadb.properties diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java index dca2de0..1c56fe7 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java @@ -14,6 +14,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.UUID; +import static net.tylermurphy.hideAndSeek.configuration.Config.databasePort; import static net.tylermurphy.hideAndSeek.configuration.Config.databaseType; public class Database { @@ -25,12 +26,14 @@ public class Database { public Database(){ - if(databaseType.equals("SQLITE")) { + if(databaseType.equalsIgnoreCase("SQLITE")) { Main.getInstance().getLogger().info("SQLITE database chosen"); connection = new SQLiteConnection(); - } else { + } else if(databaseType.equalsIgnoreCase("MYSQL")) { Main.getInstance().getLogger().info("MYSQL database chosen"); connection = new MySQLConnection(); + } else { + throw new IllegalArgumentException("Invalid database type: " + databaseType); } playerInfo = new GameDataTable(this); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java index c01f615..87378f9 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java @@ -201,7 +201,7 @@ public class GameDataTable { } protected void updateInfo(byte[] uuid, int hider_wins, int seeker_wins, int hider_games, int seeker_games, int hider_kills, int seeker_kills, int hider_deaths, int seeker_deaths){ - String sql = "INSERT OR REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; + String sql = "REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setBytes(1, uuid); statement.setInt(2, hider_wins); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/InventoryTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/InventoryTable.java index 0ff2225..b023801 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/InventoryTable.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/InventoryTable.java @@ -59,7 +59,7 @@ public class InventoryTable { } public void saveInventory(@NotNull UUID uuid, @NotNull ItemStack[] itemArray) { - String sql = "INSERT OR REPLACE INTO hs_inventory (uuid, inventory) VALUES (?,?)"; + String sql = "REPLACE INTO hs_inventory (uuid, inventory) VALUES (?,?)"; String data = itemStackArrayToBase64(itemArray); try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setBytes(1, database.encodeUUID(uuid)); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java index 02d90cf..4cc77fc 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java @@ -96,7 +96,7 @@ public class NameDataTable { } public boolean update(@NotNull UUID uuid, @NotNull String name){ - String sql = "INSERT OR REPLACE INTO hs_names (uuid, name) VALUES (?,?)"; + String sql = "REPLACE INTO hs_names (uuid, name) VALUES (?,?)"; try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { statement.setBytes(1, database.encodeUUID(uuid)); statement.setString(2, name); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java index 56f53f6..e237e71 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java @@ -25,6 +25,7 @@ import net.tylermurphy.hideAndSeek.Main; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import static net.tylermurphy.hideAndSeek.configuration.Config.*; @@ -43,7 +44,8 @@ public class MySQLConnection implements DatabaseConnection { Main.getInstance().getLogger().info("Database name: " + databaseName); - config.setJdbcUrl("jdbc:mariadb://"+databaseHost+":"+databasePort+"/"+databaseName); + config.setDriverClassName(org.mariadb.jdbc.Driver.class.getName()); + config.setJdbcUrl("jdbc:mariadb://"+databaseHost+":"+databasePort+"/"+databaseName.trim()); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/EntityHider.java b/src/main/java/net/tylermurphy/hideAndSeek/game/EntityHider.java index fb98008..755e89d 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/EntityHider.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/EntityHider.java @@ -84,26 +84,38 @@ public class EntityHider implements Listener { /** * Add or remove the given entity and observer entry from the table. * @param observer - the player observer. - * @param entityID - ID of the entity. + * @param newEntityId - ID of the entity. * @param member - TRUE if they should be present in the table, FALSE otherwise. * @return TRUE if they already were present, FALSE otherwise. */ - protected boolean setMembership(Player observer, int entityID, boolean member) { + protected boolean setMembership(Player observer, int newEntityId, boolean member) { + int entityID; + try { + entityID = observer.getEntityId(); + } catch (Exception e) { + return member; + } if (member) { - return observerEntityMap.put(observer.getEntityId(), entityID, true) != null; + return observerEntityMap.put(newEntityId, entityID, true) != null; } else { - return observerEntityMap.remove(observer.getEntityId(), entityID) != null; + return observerEntityMap.remove(newEntityId, entityID) != null; } } /** * Determine if the given entity and observer is present in the table. * @param observer - the player observer. - * @param entityID - ID of the entity. + * @param newEntityID - ID of the entity. * @return TRUE if they are present, FALSE otherwise. */ - protected boolean getMembership(Player observer, int entityID) { - return observerEntityMap.contains(observer.getEntityId(), entityID); + protected boolean getMembership(Player observer, int newEntityID) { + int entityID; + try { + entityID = observer.getEntityId(); + } catch (Exception e) { + return false; + } + return observerEntityMap.contains(entityID, newEntityID); } /** @@ -124,7 +136,12 @@ public class EntityHider implements Listener { * @param entity - the entity to remove. */ protected void removeEntity(Entity entity) { - int entityID = entity.getEntityId(); + int entityID; + try { + entityID = entity.getEntityId(); + } catch (Exception e) { + return; + } for (Map maps : observerEntityMap.rowMap().values()) { maps.remove(entityID); @@ -136,8 +153,13 @@ public class EntityHider implements Listener { * @param player - the player that jused logged out. */ protected void removePlayer(Player player) { - // Cleanup - observerEntityMap.rowMap().remove(player.getEntityId()); + int entityID; + try { + entityID = player.getEntityId(); + } catch (Exception e) { + return; + } + observerEntityMap.rowMap().remove(entityID); } /** @@ -194,7 +216,13 @@ public class EntityHider implements Listener { */ @SuppressWarnings("unused") public final boolean toggleEntity(Player observer, Entity entity) { - if (isVisible(observer, entity.getEntityId())) { + int entityID; + try { + entityID = observer.getEntityId(); + } catch (Exception e) { + return true; + } + if (isVisible(observer, entityID)) { return hideEntity(observer, entity); } else { return !showEntity(observer, entity); @@ -209,7 +237,13 @@ public class EntityHider implements Listener { */ public final boolean showEntity(Player observer, Entity entity) { validate(observer, entity); - boolean hiddenBefore = !setVisibility(observer, entity.getEntityId(), true); + int entityID; + try { + entityID = entity.getEntityId(); + } catch (Exception e) { + return false; + } + boolean hiddenBefore = !setVisibility(observer, entityID, true); // Resend packets if (manager != null && hiddenBefore) { @@ -226,12 +260,18 @@ public class EntityHider implements Listener { */ public final boolean hideEntity(Player observer, Entity entity) { validate(observer, entity); - boolean visibleBefore = setVisibility(observer, entity.getEntityId(), false); + int entityID; + try { + entityID = entity.getEntityId(); + } catch (Exception e) { + return true; + } + boolean visibleBefore = setVisibility(observer, entityID, false); if (visibleBefore) { PacketContainer destroyEntity = new PacketContainer(ENTITY_DESTROY); try { - destroyEntity.getIntegerArrays().write(0, new int[]{entity.getEntityId()}); + destroyEntity.getIntegerArrays().write(0, new int[]{entityID}); } catch (Exception e){ return false; } // Make the entity disappear manager.sendServerPacket(observer, destroyEntity); @@ -252,8 +292,13 @@ public class EntityHider implements Listener { @SuppressWarnings("unused") public final boolean canSee(Player observer, Entity entity) { validate(observer, entity); - - return isVisible(observer, entity.getEntityId()); + int entityID; + try { + entityID = entity.getEntityId(); + } catch (Exception e) { + return true; + } + return isVisible(observer, entityID); } private void validate(Player observer, Entity entity) {