- Change indefinite bans config system to be more organized

- Store indefinite bans through an object instead
- Add debug redis messages
- Make loading keys from the jar optional
This commit is contained in:
Taah 2022-03-05 15:44:38 -08:00
parent aa5e184a56
commit 2804f6bc41
5 changed files with 82 additions and 94 deletions

View File

@ -1,5 +1,6 @@
package dev.plex; package dev.plex;
import com.google.gson.Gson;
import dev.plex.admin.Admin; import dev.plex.admin.Admin;
import dev.plex.admin.AdminList; import dev.plex.admin.AdminList;
import dev.plex.cache.DataUtils; import dev.plex.cache.DataUtils;
@ -69,7 +70,6 @@ public class Plex extends JavaPlugin
config = new Config(this, "config.yml"); config = new Config(this, "config.yml");
messages = new Config(this, "messages.yml"); messages = new Config(this, "messages.yml");
indefBans = new Config(this, "indefbans.yml"); indefBans = new Config(this, "indefbans.yml");
sqlConnection = new SQLConnection(); sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection(); mongoConnection = new MongoConnection();
redisConnection = new RedisConnection(); redisConnection = new RedisConnection();
@ -81,6 +81,7 @@ public class Plex extends JavaPlugin
config.load(); config.load();
messages.load(); messages.load();
indefBans.load(); indefBans.load();
system = config.getString("commands.permissions"); system = config.getString("commands.permissions");
try try

View File

@ -37,7 +37,7 @@ public class PlexCMD extends PlexCommand
send(sender, "Reloaded config file"); send(sender, "Reloaded config file");
plugin.messages.load(); plugin.messages.load();
send(sender, "Reloaded messages file"); send(sender, "Reloaded messages file");
plugin.indefBans.load(); plugin.indefBans.load(false);
plugin.getPunishmentManager().mergeIndefiniteBans(); plugin.getPunishmentManager().mergeIndefiniteBans();
send(sender, "Reloaded indefinite bans"); send(sender, "Reloaded indefinite bans");
plugin.getRankManager().importDefaultRanks(); plugin.getRankManager().importDefaultRanks();

View File

@ -2,16 +2,16 @@ package dev.plex.config;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File; import java.io.File;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.bukkit.configuration.file.YamlConfiguration;
/** /**
* Creates a custom Config object * Creates a custom Config object
*/ */
public class Config extends YamlConfiguration public class Config extends YamlConfiguration {
{
/** /**
* The plugin instance * The plugin instance
*/ */
@ -38,35 +38,35 @@ public class Config extends YamlConfiguration
* @param plugin The plugin instance * @param plugin The plugin instance
* @param name The file name * @param name The file name
*/ */
public Config(Plex plugin, String name) public Config(Plex plugin, String name) {
{
this.plugin = plugin; this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), name); this.file = new File(plugin.getDataFolder(), name);
this.name = name; this.name = name;
if (!file.exists()) if (!file.exists()) {
{
saveDefault(); saveDefault();
} }
} }
public void load()
{
this.load(true);
}
/** /**
* Loads the configuration file * Loads the configuration file
*/ */
public void load() public void load(boolean loadFromFile) {
{ try {
try if (loadFromFile) {
{
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file); YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
InputStreamReader internalConfigFileStream = new InputStreamReader(Plex.get().getResource(name), StandardCharsets.UTF_8); InputStreamReader internalConfigFileStream = new InputStreamReader(Plex.get().getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream); YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
// Gets all the keys inside the internal file and iterates through all of it's key pairs // Gets all the keys inside the internal file and iterates through all of it's key pairs
for (String string : internalYamlConfig.getKeys(true)) for (String string : internalYamlConfig.getKeys(true)) {
{
// Checks if the external file contains the key already. // Checks if the external file contains the key already.
if (!externalYamlConfig.contains(string)) if (!externalYamlConfig.contains(string)) {
{
// If it doesn't contain the key, we set the key based off what was found inside the plugin jar // If it doesn't contain the key, we set the key based off what was found inside the plugin jar
externalYamlConfig.setComments(string, internalYamlConfig.getComments(string)); externalYamlConfig.setComments(string, internalYamlConfig.getComments(string));
externalYamlConfig.set(string, internalYamlConfig.get(string)); externalYamlConfig.set(string, internalYamlConfig.get(string));
@ -74,16 +74,14 @@ public class Config extends YamlConfiguration
added = true; added = true;
} }
} }
if (added) if (added) {
{
externalYamlConfig.save(file); externalYamlConfig.save(file);
PlexLog.log("Saving new file..."); PlexLog.log("Saving new file...");
added = false; added = false;
} }
super.load(file);
} }
catch (Exception ex) super.load(file);
{ } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@ -91,14 +89,10 @@ public class Config extends YamlConfiguration
/** /**
* Saves the configuration file * Saves the configuration file
*/ */
public void save() public void save() {
{ try {
try
{
super.save(file); super.save(file);
} } catch (Exception ex) {
catch (Exception ex)
{
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@ -106,8 +100,7 @@ public class Config extends YamlConfiguration
/** /**
* Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/) * Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/)
*/ */
private void saveDefault() private void saveDefault() {
{
plugin.saveResource(name, false); plugin.saveResource(name, false);
} }
} }

View File

@ -19,10 +19,13 @@ import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.Data;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -33,43 +36,26 @@ import redis.clients.jedis.Jedis;
public class PunishmentManager extends PlexBase public class PunishmentManager extends PlexBase
{ {
private final List<String> bannedIPs = Lists.newArrayList(); private final List<IndefiniteBan> indefiniteBans = Lists.newArrayList();
private final List<String> bannedUsernames = Lists.newArrayList();
private final List<UUID> bannedUUIDs = Lists.newArrayList();
public void mergeIndefiniteBans() public void mergeIndefiniteBans()
{ {
this.bannedUsernames.clear(); this.indefiniteBans.clear();
this.bannedIPs.clear(); Plex.get().indefBans.getKeys(false).forEach(key -> {
this.bannedUUIDs.clear(); IndefiniteBan ban = new IndefiniteBan();
ban.ips.addAll(Plex.get().getIndefBans().getStringList(key + ".ips"));
ban.usernames.addAll(Plex.get().getIndefBans().getStringList(key + ".users"));
ban.uuids.addAll(Plex.get().getIndefBans().getStringList(key + ".uuids").stream().map(UUID::fromString).toList());
this.indefiniteBans.add(ban);
});
this.bannedUUIDs.addAll(Plex.get().indefBans.getStringList("uuids").stream().filter(string -> PlexLog.log("Loaded {0} UUID(s), {1} IP(s), and {2} username(s) as indefinitely banned", this.indefiniteBans.stream().map(IndefiniteBan::getUuids).mapToLong(Collection::size).sum(), this.indefiniteBans.stream().map(IndefiniteBan::getIps).mapToLong(Collection::size).sum(), this.indefiniteBans.stream().map(IndefiniteBan::getUsernames).mapToLong(Collection::size).sum());
{
try
{
UUID uuid = UUID.fromString(string);
return true;
} catch (IllegalArgumentException e)
{
return false;
}
}).map(UUID::fromString).toList());
this.bannedIPs.addAll(Plex.get().indefBans.getStringList("ips"));
this.bannedUsernames.addAll(Plex.get().indefBans.getStringList("usernames"));
PlexLog.log("Loaded {0} UUID(s), {1} IP(s), and {2} username(s) as indefinitely banned", this.bannedUUIDs.size(), this.bannedIPs.size(), this.bannedUsernames.size());
if (Plex.get().getRedisConnection().isEnabled()) if (Plex.get().getRedisConnection().isEnabled())
{ {
PlexLog.log("Asynchronously uploading all indefinite bans to Redis"); PlexLog.log("Asynchronously uploading all indefinite bans to Redis");
Plex.get().getRedisConnection().runAsync(jedis -> { Plex.get().getRedisConnection().runAsync(jedis -> {
jedis.set("indefbanned-uuids", new Gson().toJson(this.bannedUUIDs)); jedis.set("indefbans", new Gson().toJson(indefiniteBans));
jedis.set("indefbanned-ips", new Gson().toJson(this.bannedIPs));
jedis.set("indefbanned-users", new Gson().toJson(this.bannedUsernames));
this.bannedIPs.clear();
this.bannedUsernames.clear();
this.bannedUUIDs.clear();
}); });
} }
} }
@ -78,30 +64,33 @@ public class PunishmentManager extends PlexBase
{ {
if (Plex.get().getRedisConnection().isEnabled()) if (Plex.get().getRedisConnection().isEnabled())
{ {
List<UUID> uuids = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-uuids"), new TypeToken<List<UUID>>(){}.getType()); PlexLog.debug("Checking if UUID is banned in Redis");
return uuids.contains(uuid); List<IndefiniteBan> bans = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbans"), new TypeToken<List<IndefiniteBan>>(){}.getType());
return bans.stream().anyMatch(indefiniteBan -> indefiniteBan.getUuids().contains(uuid));
} }
return this.bannedUUIDs.contains(uuid); return this.indefiniteBans.stream().anyMatch(indefiniteBan -> indefiniteBan.getUuids().contains(uuid));
} }
public boolean isIndefIPBanned(String ip) public boolean isIndefIPBanned(String ip)
{ {
if (Plex.get().getRedisConnection().isEnabled()) if (Plex.get().getRedisConnection().isEnabled())
{ {
List<String> ips = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-ips"), new TypeToken<List<String>>(){}.getType()); PlexLog.debug("Checking if IP is banned in Redis");
return ips.contains(ip); List<IndefiniteBan> bans = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbans"), new TypeToken<List<IndefiniteBan>>(){}.getType());
return bans.stream().anyMatch(indefiniteBan -> indefiniteBan.getIps().contains(ip));
} }
return this.bannedIPs.contains(ip); return this.indefiniteBans.stream().anyMatch(indefiniteBan -> indefiniteBan.getIps().contains(ip));
} }
public boolean isIndefUserBanned(String username) public boolean isIndefUserBanned(String username)
{ {
if (Plex.get().getRedisConnection().isEnabled()) if (Plex.get().getRedisConnection().isEnabled())
{ {
List<String> users = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-users"), new TypeToken<List<String>>(){}.getType()); PlexLog.debug("Checking if username is banned in Redis");
return users.contains(username); List<IndefiniteBan> bans = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbans"), new TypeToken<List<IndefiniteBan>>(){}.getType());
return bans.stream().anyMatch(indefiniteBan -> indefiniteBan.getUsernames().contains(username));
} }
return this.bannedUsernames.contains(username); return this.indefiniteBans.stream().anyMatch(indefiniteBan -> indefiniteBan.getUsernames().contains(username));
} }
public void insertPunishment(PunishedPlayer player, Punishment punishment) public void insertPunishment(PunishedPlayer player, Punishment punishment)
@ -320,4 +309,12 @@ public class PunishmentManager extends PlexBase
issuePunishment(player, punishment); issuePunishment(player, punishment);
insertPunishment(player, punishment); insertPunishment(player, punishment);
} }
@Data
public static class IndefiniteBan
{
private final List<String> usernames = Lists.newArrayList();
private final List<UUID> uuids = Lists.newArrayList();
private final List<String> ips = Lists.newArrayList();
}
} }

View File

@ -1,16 +1,13 @@
# Plex Indefinite Bans File # Plex Indefinite Bans File
# Players with their UUID / IP / Usernames in here will be indefinitely banned until removed # Players with their UUID / IP / Usernames in here will be indefinitely banned until removed
# List of permanently banned UUIDs
# If you want to get someone's UUID, use https://api.ashcon.app/mojang/v2/user/<username> # If you want to get someone's UUID, use https://api.ashcon.app/mojang/v2/user/<username>
uuids: griefers:
- 1dac0e92-f565-4479-afd5-38c7df5f9732 # badplayer123 users:
- "Taahh"
# List of permanently banned IP addresses - "Telesphoreo"
ips: uuids: []
- 169.254.69.69 ips: []
0:
# List of permanently banned UUIDs users:
# If you want to get someone's username, use https://api.ashcon.app/mojang/v2/user/<uuid>, or just remember it - "videogamesm12"
usernames:
- badplayer123