Skip to content

Commit

Permalink
Attempt to fix wrong and broken duplicated player data
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro committed Aug 17, 2024
1 parent d7ed16a commit 305ef00
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.CommonPlayer;
import me.confuser.banmanager.common.configs.PluginInfo;
import me.confuser.banmanager.common.data.DuplicatePlayerData;
import me.confuser.banmanager.common.data.PlayerData;
import me.confuser.banmanager.common.data.PlayerNoteData;
import me.confuser.banmanager.common.ipaddr.IPAddress;
Expand Down Expand Up @@ -129,12 +130,12 @@ public static IPAddress getIp(String ipStr) {
IPAddress ip = null;

if (isName) {
List<PlayerData> players = BanManagerPlugin.getInstance().getDuplicatePlayerStorage().retrieve(ipStr);
List<DuplicatePlayerData> players = BanManagerPlugin.getInstance().getDuplicatePlayerStorage().retrieve(ipStr);
if (players.isEmpty()) {
return null;
}

PlayerData player = players.get(0);
DuplicatePlayerData player = players.get(0);

if (player == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class LocalDatabaseConfig extends DatabaseConfig {

public static HashMap<String, Class> types = new HashMap<String, Class>() {{
put("players", PlayerData.class);
put("duplicatePlayers", PlayerData.class);
put("duplicatePlayers", DuplicatePlayerData.class);

put("playerBans", PlayerBanData.class);
put("playerABans", PlayerBanData.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package me.confuser.banmanager.common.data;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import me.confuser.banmanager.common.ipaddr.AddressStringException;
import me.confuser.banmanager.common.ipaddr.IPAddress;
import me.confuser.banmanager.common.ipaddr.IPAddressString;
import me.confuser.banmanager.common.ormlite.field.DatabaseField;
import me.confuser.banmanager.common.ormlite.table.DatabaseTable;
import me.confuser.banmanager.common.storage.mysql.ByteArray;
import me.confuser.banmanager.common.storage.mysql.IpAddress;
import me.confuser.banmanager.common.util.UUIDUtils;

import java.util.UUID;

@DatabaseTable
@ToString
public class DuplicatePlayerData {

@DatabaseField(id = true, persisterClass = ByteArray.class, columnDefinition = "BINARY(16) NOT NULL")
@Getter
private byte[] id;
@DatabaseField(index = true, width = 16, columnDefinition = "VARCHAR(16) NOT NULL")
@Getter
@Setter
private String name;
@Getter
@DatabaseField(index = true, persisterClass = IpAddress.class, columnDefinition = "VARBINARY(16) NOT NULL")
private IPAddress ip;
@Getter
@DatabaseField(columnDefinition = "BIGINT UNSIGNED NOT NULL")
private long lastSeen = System.currentTimeMillis() / 1000L;

private UUID uuid = null;

DuplicatePlayerData() {

}

public DuplicatePlayerData(UUID uuid, String name) {
this.uuid = uuid;
this.id = UUIDUtils.toBytes(uuid);
this.name = name;

try {
this.ip = new IPAddressString("127.0.0.1").toAddress();
} catch (AddressStringException e) {
e.printStackTrace();
}

this.lastSeen = System.currentTimeMillis() / 1000L;
}

public DuplicatePlayerData(UUID uuid, String name, IPAddress ip) {
this.uuid = uuid;
this.id = UUIDUtils.toBytes(uuid);
this.name = name;
this.ip = ip;
this.lastSeen = System.currentTimeMillis() / 1000L;
}

public DuplicatePlayerData(UUID uuid, String name, IPAddress ip, long lastSeen) {
this.uuid = uuid;
this.id = UUIDUtils.toBytes(uuid);
this.name = name;
this.ip = ip;
this.lastSeen = lastSeen;
}

public UUID getUUID() {
if (uuid == null) {
uuid = UUIDUtils.fromBytes(id);
}

return uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.CommonPlayer;
import me.confuser.banmanager.common.commands.NotesCommand;
import me.confuser.banmanager.common.data.IpBanData;
import me.confuser.banmanager.common.data.IpRangeBanData;
import me.confuser.banmanager.common.data.NameBanData;
import me.confuser.banmanager.common.data.PlayerBanData;
import me.confuser.banmanager.common.data.PlayerData;
import me.confuser.banmanager.common.data.PlayerMuteData;
import me.confuser.banmanager.common.data.PlayerNoteData;
import me.confuser.banmanager.common.data.PlayerWarnData;
import me.confuser.banmanager.common.data.*;
import me.confuser.banmanager.common.google.guava.cache.Cache;
import me.confuser.banmanager.common.google.guava.cache.CacheBuilder;
import me.confuser.banmanager.common.ipaddr.IPAddress;
Expand Down Expand Up @@ -319,7 +312,7 @@ public void onServerJoin(CommonPlayer player, InetAddress address, String server
String name = player.getName();
IPAddress ip = IPUtils.toIPAddress(address);

PlayerData playerData = new PlayerData(id, name, ip);
DuplicatePlayerData playerData = new DuplicatePlayerData(id, name, ip);

try {
plugin.getDuplicatePlayerStorage().createOrUpdate(playerData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

import lombok.Getter;
import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.data.DuplicatePlayerData;
import me.confuser.banmanager.common.data.PlayerData;
import me.confuser.banmanager.common.ipaddr.IPAddress;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.ormlite.stmt.QueryBuilder;
import me.confuser.banmanager.common.ormlite.field.DataType;
import me.confuser.banmanager.common.ormlite.stmt.SelectArg;
import me.confuser.banmanager.common.ormlite.stmt.Where;
import me.confuser.banmanager.common.ormlite.support.ConnectionSource;
import me.confuser.banmanager.common.ormlite.table.DatabaseTableConfig;
import me.confuser.banmanager.common.ormlite.table.TableUtils;
import me.confuser.banmanager.common.util.UUIDProfile;
import me.confuser.banmanager.common.util.UUIDUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DuplicatePlayerStorage extends BaseDaoImpl<PlayerData, byte[]> {
public class DuplicatePlayerStorage extends BaseDaoImpl<DuplicatePlayerData, byte[]> {

@Getter
private BanManagerPlugin plugin;

public DuplicatePlayerStorage(BanManagerPlugin plugin) throws SQLException {
super(plugin.getLocalConn(), (DatabaseTableConfig<PlayerData>) plugin.getConfig().getLocalDb()
super(plugin.getLocalConn(), (DatabaseTableConfig<DuplicatePlayerData>) plugin.getConfig().getLocalDb()
.getTable("duplicatePlayers"));

this.plugin = plugin;
Expand All @@ -36,10 +35,10 @@ public DuplicatePlayerStorage(BanManagerPlugin plugin) throws SQLException {
}

public DuplicatePlayerStorage(ConnectionSource connection, DatabaseTableConfig<?> table) throws SQLException {
super(connection, (DatabaseTableConfig<PlayerData>) table);
super(connection, (DatabaseTableConfig<DuplicatePlayerData>) table);
}

public List<PlayerData> retrieve(String name) {
public List<DuplicatePlayerData> retrieve(String name) {
try {
return queryForEq("name", new SelectArg(name));
} catch (SQLException e) {
Expand All @@ -49,51 +48,44 @@ public List<PlayerData> retrieve(String name) {
return null;
}

public List<PlayerData> getDuplicatesInTime(IPAddress ip, long timeDiff) {
ArrayList<PlayerData> players = new ArrayList<>();
public List<PlayerData> getDuplicatesInTime(IPAddress address, long timeDiff) {
List<PlayerData> players = new ArrayList<>();

if (plugin.getConfig().getBypassPlayerIps().contains(ip.toString())) {
if (plugin.getConfig().getBypassPlayerIps().contains(address.toString())) {
return players;
}

QueryBuilder<PlayerData, byte[]> query = queryBuilder();
try {
query.leftJoin(plugin.getPlayerBanStorage().queryBuilder());

Where<PlayerData, byte[]> where = query.where();

where.eq("ip", ip);

if (timeDiff != 0) {
long currentTime = System.currentTimeMillis() / 1000L;
CloseableIterator<Object[]> itr = null;
try {
String playerTableName = getTableName();
String banTableName = plugin.getPlayerBanStorage().getTableName();
itr = queryRaw("SELECT *\n" +
"FROM " + playerTableName + " \n" +
"LEFT JOIN " + banTableName + " ON " + playerTableName + ".id = " + banTableName + ".player_id\n" +
"WHERE " + playerTableName + ".ip = UNHEX('" + address.toHexString(false) + "') \n" +
"LIMIT 300;", new DataType[]{DataType.BYTE_ARRAY, DataType.STRING, DataType.BYTE_ARRAY, DataType.LONG}).closeableIterator();

while (itr.hasNext()) {
Object[] data = itr.next();
PlayerData player = new PlayerData(UUIDUtils.fromBytes((byte[]) data[0]), (String) data[1], address, (Long) data[3]);

if (!plugin.getExemptionsConfig().isExempt(player, "alts")) {
players.add(player);
}
}

where.and().ge("lastSeen", (currentTime - timeDiff));
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (itr != null) itr.closeQuietly();
}

query.setWhere(where);
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
return players;
}

CloseableIterator<PlayerData> itr = null;
try {
itr = query.limit(300L).iterator();

while (itr.hasNext()) {
PlayerData player = itr.next();

if (!plugin.getExemptionsConfig().isExempt(player, "alts")) {
players.add(player);
}
}

} catch (SQLException e) {
e.printStackTrace();
} finally {
if (itr != null) itr.closeQuietly();
}

return players;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.confuser.banmanager.common.ipaddr.IPAddress;
import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.ormlite.field.DataType;
import me.confuser.banmanager.common.ormlite.stmt.QueryBuilder;
import me.confuser.banmanager.common.ormlite.stmt.StatementBuilder;
import me.confuser.banmanager.common.ormlite.stmt.Where;
Expand Down Expand Up @@ -259,40 +260,58 @@ public CloseableIterator<PlayerBanData> findBans(long fromTime) throws SQLExcept

}

public List<PlayerData> getDuplicates(IPAddress ip) {
public List<PlayerData> getDuplicates(IPAddress address) {
ArrayList<PlayerData> players = new ArrayList<>();

if (plugin.getConfig().getBypassPlayerIps().contains(ip.toString())) {
if (plugin.getConfig().getBypassPlayerIps().contains(address.toString())) {
return players;
}

QueryBuilder<PlayerBanData, Integer> query = queryBuilder();
try {
QueryBuilder<PlayerData, byte[]> playerQuery = plugin.getDuplicatePlayerStorage().queryBuilder();
CloseableIterator<Object[]> itr = null;
try {
String banTableName = getTableName();
String playerTableName = plugin.getDuplicatePlayerStorage().getTableName();
itr = queryRaw("SELECT *\n" +
"FROM " + banTableName + " \n" +
"LEFT JOIN " + playerTableName + " ON " + playerTableName + ".id = " + banTableName + ".player_id\n" +
"WHERE " + playerTableName + ".ip = UNHEX('" + address.toHexString(false) + "') \n" +
";", new DataType[]{
DataType.INTEGER,
DataType.BYTE_ARRAY,
DataType.STRING,
DataType.BYTE_ARRAY,
DataType.LONG,
DataType.LONG,
DataType.LONG,
DataType.BOOLEAN,
DataType.BYTE_ARRAY,
DataType.STRING,
DataType.BYTE_ARRAY,
DataType.LONG
}).closeableIterator();

while (itr.hasNext()) {
Object[] data = itr.next();

PlayerData player = new PlayerData(UUIDUtils.fromBytes((byte[]) data[9]), (String) data[9], address, (Long) data[11]);

if (!plugin.getExemptionsConfig().isExempt(player, "alts")) {
players.add(player);
}
}

Where<PlayerData, byte[]> where = playerQuery.where();
where.eq("ip", ip);
playerQuery.setWhere(where);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (itr != null) itr.closeQuietly();
}

query.leftJoin(playerQuery);
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
return players;
}

CloseableIterator<PlayerBanData> itr = null;
try {
itr = query.iterator();

while (itr.hasNext()) {
players.add(itr.next().getPlayer());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (itr != null) itr.closeQuietly();
}

return players;
}

Expand Down
Loading

0 comments on commit 305ef00

Please sign in to comment.