fix mysql, entityhider ignore temp players

This commit is contained in:
Tyler Murphy 2023-02-08 17:52:24 -05:00
parent c3eaf2eccd
commit 7ad63246f3
7 changed files with 72 additions and 24 deletions

View file

@ -47,8 +47,6 @@
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/**</exclude>
<exclude>sqlite-jdbc.properties</exclude>
<exclude>mariadb.properties</exclude>
</excludes>
</filter>
</filters>

View file

@ -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);

View file

@ -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);

View file

@ -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));

View file

@ -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);

View file

@ -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");

View file

@ -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<Integer, Boolean> 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) {