UUID definite bans

This commit is contained in:
Seth 2020-08-09 22:25:04 -07:00
parent 00f94e47f1
commit aab4f55b7d
No known key found for this signature in database
GPG Key ID: A7BAB4E14F089CF3
5 changed files with 90 additions and 17 deletions

View File

@ -10,6 +10,7 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
@ -27,6 +28,9 @@ public class Ban
@Setter @Setter
private String username = null; private String username = null;
@Getter @Getter
@Setter
private UUID uuid = null;
@Getter
private final List<String> ips = Lists.newArrayList(); private final List<String> ips = Lists.newArrayList();
@Getter @Getter
@Setter @Setter
@ -45,9 +49,10 @@ public class Ban
{ {
} }
public Ban(String username, String ip, String by, Date at, Date expire, String reason) public Ban(String username, UUID uuid, String ip, String by, Date at, Date expire, String reason)
{ {
this(username, this(username,
uuid,
Arrays.asList(ip), Arrays.asList(ip),
by, by,
at, at,
@ -55,9 +60,10 @@ public class Ban
reason); reason);
} }
public Ban(String username, List<String> ips, String by, Date at, Date expire, String reason) public Ban(String username, UUID uuid, List<String> ips, String by, Date at, Date expire, String reason)
{ {
this.username = username; this.username = username;
this.uuid = uuid;
if (ips != null) if (ips != null)
{ {
this.ips.addAll(ips); this.ips.addAll(ips);
@ -78,12 +84,12 @@ public class Ban
public static Ban forPlayerIp(Player player, CommandSender by, Date expiry, String reason) public static Ban forPlayerIp(Player player, CommandSender by, Date expiry, String reason)
{ {
return new Ban(null, Arrays.asList(FUtil.getIp(player)), by.getName(), Date.from(Instant.now()), expiry, reason); return new Ban(null, null, Arrays.asList(FUtil.getIp(player)), by.getName(), Date.from(Instant.now()), expiry, reason);
} }
public static Ban forPlayerIp(String ip, CommandSender by, Date expiry, String reason) public static Ban forPlayerIp(String ip, CommandSender by, Date expiry, String reason)
{ {
return new Ban(null, ip, by.getName(), Date.from(Instant.now()), expiry, reason); return new Ban(null, null, ip, by.getName(), Date.from(Instant.now()), expiry, reason);
} }
// //
@ -96,7 +102,8 @@ public class Ban
public static Ban forPlayerName(String player, CommandSender by, Date expiry, String reason) public static Ban forPlayerName(String player, CommandSender by, Date expiry, String reason)
{ {
return new Ban(player, return new Ban(player,
new ArrayList<String>(), null,
new ArrayList<>(),
by.getName(), by.getName(),
Date.from(Instant.now()), Date.from(Instant.now()),
expiry, expiry,
@ -113,6 +120,7 @@ public class Ban
public static Ban forPlayer(Player player, CommandSender by, Date expiry, String reason) public static Ban forPlayer(Player player, CommandSender by, Date expiry, String reason)
{ {
return new Ban(player.getName(), return new Ban(player.getName(),
player.getUniqueId(),
FUtil.getIp(player), FUtil.getIp(player),
by.getName(), by.getName(),
Date.from(Instant.now()), Date.from(Instant.now()),
@ -123,6 +131,7 @@ public class Ban
public static Ban forPlayerFuzzy(Player player, CommandSender by, Date expiry, String reason) public static Ban forPlayerFuzzy(Player player, CommandSender by, Date expiry, String reason)
{ {
return new Ban(player.getName(), return new Ban(player.getName(),
player.getUniqueId(),
FUtil.getFuzzyIp(FUtil.getIp(player)), FUtil.getFuzzyIp(FUtil.getIp(player)),
by.getName(), by.getName(),
Date.from(Instant.now()), Date.from(Instant.now()),
@ -135,6 +144,11 @@ public class Ban
return username != null && !username.isEmpty(); return username != null && !username.isEmpty();
} }
public boolean hasUUID()
{
return uuid != null;
}
public boolean addIp(String ip) public boolean addIp(String ip)
{ {
return ips.add(ip); return ips.add(ip);

View File

@ -12,6 +12,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.player.PlayerData;
@ -27,8 +28,9 @@ public class BanManager extends FreedomService
{ {
private final Set<Ban> bans = Sets.newHashSet(); private final Set<Ban> bans = Sets.newHashSet();
private final Map<String, Ban> ipBans = Maps.newHashMap();
private final Map<String, Ban> nameBans = Maps.newHashMap(); private final Map<String, Ban> nameBans = Maps.newHashMap();
private final Map<UUID, Ban> uuidBans = Maps.newHashMap();
private final Map<String, Ban> ipBans = Maps.newHashMap();
private final List<String> unbannableUsernames = Lists.newArrayList(); private final List<String> unbannableUsernames = Lists.newArrayList();
// //
@ -44,12 +46,18 @@ public class BanManager extends FreedomService
while (banSet.next()) while (banSet.next())
{ {
String name = banSet.getString("name"); String name = banSet.getString("name");
UUID uuid = null;
String strUUID = banSet.getString("uuid");
if (strUUID != null)
{
uuid = UUID.fromString(strUUID);
}
List<String> ips = FUtil.stringToList(banSet.getString("ips")); List<String> ips = FUtil.stringToList(banSet.getString("ips"));
String by = banSet.getString("by"); String by = banSet.getString("by");
Date at = new Date(banSet.getLong("at")); Date at = new Date(banSet.getLong("at"));
Date expires = new Date(banSet.getLong("expires")); Date expires = new Date(banSet.getLong("expires"));
String reason = banSet.getString("reason"); String reason = banSet.getString("reason");
Ban ban = new Ban(name, ips, by, at, expires, reason); Ban ban = new Ban(name, uuid, ips, by, at, expires, reason);
bans.add(ban); bans.add(ban);
} }
} }
@ -136,6 +144,18 @@ public class BanManager extends FreedomService
return null; return null;
} }
public Ban getByUUID(UUID uuid)
{
final Ban directBan = uuidBans.get(uuid);
if (directBan != null && !directBan.isExpired())
{
return directBan;
}
return null;
}
public Ban unbanIp(String ip) public Ban unbanIp(String ip)
{ {
final Ban ban = getByIp(ip); final Ban ban = getByIp(ip);
@ -224,14 +244,20 @@ public class BanManager extends FreedomService
public void onPlayerLogin(PlayerLoginEvent event) public void onPlayerLogin(PlayerLoginEvent event)
{ {
final String username = event.getPlayer().getName(); final String username = event.getPlayer().getName();
final UUID uuid = event.getPlayer().getUniqueId();
final String ip = FUtil.getIp(event); final String ip = FUtil.getIp(event);
// Regular ban // Regular ban
Ban ban = getByUsername(username); Ban ban = getByUsername(username);
if (ban == null)
{
ban = getByUUID(uuid);
if (ban == null) if (ban == null)
{ {
ban = getByIp(ip); ban = getByIp(ip);
} }
}
if (ban != null && !ban.isExpired()) if (ban != null && !ban.isExpired())
{ {
@ -279,6 +305,7 @@ public class BanManager extends FreedomService
} }
nameBans.clear(); nameBans.clear();
uuidBans.clear();
ipBans.clear(); ipBans.clear();
for (Ban ban : bans) for (Ban ban : bans)
{ {
@ -287,6 +314,11 @@ public class BanManager extends FreedomService
nameBans.put(ban.getUsername().toLowerCase(), ban); nameBans.put(ban.getUsername().toLowerCase(), ban);
} }
if (ban.hasUUID())
{
uuidBans.put(ban.getUuid(), ban);
}
if (ban.hasIps()) if (ban.hasIps())
{ {
for (String ip : ban.getIps()) for (String ip : ban.getIps())

View File

@ -121,7 +121,17 @@ public class Command_ban extends FreedomCommand
} }
// Ban player // Ban player
Ban ban = Ban.forPlayerName(username, sender, null, reason); Ban ban;
if (player != null)
{
ban = Ban.forPlayer(player, sender, null, reason);
}
else
{
ban = Ban.forPlayerName(username, sender, null, reason);
}
for (String ip : ips) for (String ip : ips)
{ {
ban.addIp(ip); ban.addIp(ip);

View File

@ -104,7 +104,18 @@ public class Command_tempban extends FreedomCommand
msg("Quietly temporarily banned " + player.getName() + "."); msg("Quietly temporarily banned " + player.getName() + ".");
} }
Ban ban = Ban.forPlayerName(username, sender, expires, reason);
Ban ban;
if (player != null)
{
ban = Ban.forPlayer(player, sender, null, reason);
}
else
{
ban = Ban.forPlayerName(username, sender, null, reason);
}
for (String ip : ips) for (String ip : ips)
{ {
ban.addIp(ip); ban.addIp(ip);

View File

@ -70,7 +70,7 @@ public class SQLite extends FreedomService
{ {
try try
{ {
connection.createStatement().execute("CREATE TABLE `bans` (`name` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);"); connection.createStatement().execute("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);");
} }
catch (SQLException e) catch (SQLException e)
{ {
@ -378,13 +378,19 @@ public class SQLite extends FreedomService
{ {
try try
{ {
PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?)"); PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, ban.getUsername()); statement.setString(1, ban.getUsername());
statement.setString(2, FUtil.listToString(ban.getIps())); String uuid = null;
statement.setString(3, ban.getBy()); if (ban.hasUUID())
statement.setLong(4, ban.getAt().getTime()); {
statement.setLong(5, ban.getExpiryUnix()); uuid = ban.getUuid().toString();
statement.setString(6, ban.getReason()); }
statement.setString(2, uuid);
statement.setString(3, FUtil.listToString(ban.getIps()));
statement.setString(4, ban.getBy());
statement.setLong(5, ban.getAt().getTime());
statement.setLong(6, ban.getExpiryUnix());
statement.setString(7, ban.getReason());
statement.executeUpdate(); statement.executeUpdate();
} }
catch (SQLException e) catch (SQLException e)