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

View File

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

View File

@ -2,16 +2,16 @@ package dev.plex.config;
import dev.plex.Plex;
import dev.plex.util.PlexLog;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.bukkit.configuration.file.YamlConfiguration;
/**
* Creates a custom Config object
*/
public class Config extends YamlConfiguration
{
public class Config extends YamlConfiguration {
/**
* The plugin instance
*/
@ -38,52 +38,50 @@ public class Config extends YamlConfiguration
* @param plugin The plugin instance
* @param name The file name
*/
public Config(Plex plugin, String name)
{
public Config(Plex plugin, String name) {
this.plugin = plugin;
this.file = new File(plugin.getDataFolder(), name);
this.name = name;
if (!file.exists())
{
if (!file.exists()) {
saveDefault();
}
}
public void load()
{
this.load(true);
}
/**
* Loads the configuration file
*/
public void load()
{
try
{
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
InputStreamReader internalConfigFileStream = new InputStreamReader(Plex.get().getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
public void load(boolean loadFromFile) {
try {
if (loadFromFile) {
YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file);
InputStreamReader internalConfigFileStream = new InputStreamReader(Plex.get().getResource(name), StandardCharsets.UTF_8);
YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream);
// Gets all the keys inside the internal file and iterates through all of it's key pairs
for (String string : internalYamlConfig.getKeys(true))
{
// Checks if the external file contains the key already.
if (!externalYamlConfig.contains(string))
{
// 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.set(string, internalYamlConfig.get(string));
PlexLog.log("Setting key: " + string + " in " + this.name + " to the default value(s) since it does not exist!");
added = true;
// Gets all the keys inside the internal file and iterates through all of it's key pairs
for (String string : internalYamlConfig.getKeys(true)) {
// Checks if the external file contains the key already.
if (!externalYamlConfig.contains(string)) {
// 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.set(string, internalYamlConfig.get(string));
PlexLog.log("Setting key: " + string + " in " + this.name + " to the default value(s) since it does not exist!");
added = true;
}
}
if (added) {
externalYamlConfig.save(file);
PlexLog.log("Saving new file...");
added = false;
}
}
if (added)
{
externalYamlConfig.save(file);
PlexLog.log("Saving new file...");
added = false;
}
super.load(file);
}
catch (Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}
@ -91,14 +89,10 @@ public class Config extends YamlConfiguration
/**
* Saves the configuration file
*/
public void save()
{
try
{
public void save() {
try {
super.save(file);
}
catch (Exception ex)
{
} catch (Exception ex) {
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/)
*/
private void saveDefault()
{
private void saveDefault() {
plugin.saveResource(name, false);
}
}

View File

@ -19,10 +19,13 @@ import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -33,43 +36,26 @@ import redis.clients.jedis.Jedis;
public class PunishmentManager extends PlexBase
{
private final List<String> bannedIPs = Lists.newArrayList();
private final List<String> bannedUsernames = Lists.newArrayList();
private final List<UUID> bannedUUIDs = Lists.newArrayList();
private final List<IndefiniteBan> indefiniteBans = Lists.newArrayList();
public void mergeIndefiniteBans()
{
this.bannedUsernames.clear();
this.bannedIPs.clear();
this.bannedUUIDs.clear();
this.indefiniteBans.clear();
Plex.get().indefBans.getKeys(false).forEach(key -> {
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 ->
{
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());
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());
if (Plex.get().getRedisConnection().isEnabled())
{
PlexLog.log("Asynchronously uploading all indefinite bans to Redis");
Plex.get().getRedisConnection().runAsync(jedis -> {
jedis.set("indefbanned-uuids", new Gson().toJson(this.bannedUUIDs));
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();
jedis.set("indefbans", new Gson().toJson(indefiniteBans));
});
}
}
@ -78,30 +64,33 @@ public class PunishmentManager extends PlexBase
{
if (Plex.get().getRedisConnection().isEnabled())
{
List<UUID> uuids = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-uuids"), new TypeToken<List<UUID>>(){}.getType());
return uuids.contains(uuid);
PlexLog.debug("Checking if UUID is banned in Redis");
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)
{
if (Plex.get().getRedisConnection().isEnabled())
{
List<String> ips = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-ips"), new TypeToken<List<String>>(){}.getType());
return ips.contains(ip);
PlexLog.debug("Checking if IP is banned in Redis");
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)
{
if (Plex.get().getRedisConnection().isEnabled())
{
List<String> users = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-users"), new TypeToken<List<String>>(){}.getType());
return users.contains(username);
PlexLog.debug("Checking if username is banned in Redis");
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)
@ -320,4 +309,12 @@ public class PunishmentManager extends PlexBase
issuePunishment(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
# 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>
uuids:
- 1dac0e92-f565-4479-afd5-38c7df5f9732 # badplayer123
# List of permanently banned IP addresses
ips:
- 169.254.69.69
# List of permanently banned UUIDs
# If you want to get someone's username, use https://api.ashcon.app/mojang/v2/user/<uuid>, or just remember it
usernames:
- badplayer123
griefers:
users:
- "Taahh"
- "Telesphoreo"
uuids: []
ips: []
0:
users:
- "videogamesm12"