That's right, Redis is working!

- Fix end date for bans not being correct
- Add a lot of debug information
- Add ban reasons
This commit is contained in:
Telesphoreo 2022-02-04 02:18:07 -06:00
parent 753dba8986
commit 93a8a10212
13 changed files with 128 additions and 54 deletions

View File

@ -18,6 +18,7 @@ repositories {
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
mavenCentral()
}
dependencies {
@ -26,7 +27,7 @@ dependencies {
library 'org.json:json:20211205'
library 'commons-io:commons-io:2.11.0'
library 'dev.morphia.morphia:morphia-core:2.2.3'
library 'redis.clients:jedis:4.0.1'
library 'redis.clients:jedis:4.1.0'
library 'org.mariadb.jdbc:mariadb-java-client:2.7.4'
library 'org.apache.httpcomponents:httpclient:4.5.13'
compileOnly 'io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT'

View File

@ -101,6 +101,14 @@ public class Plex extends JavaPlugin
Metrics metrics = new Metrics(this, 14143);
PlexLog.log("Enabled Metrics");
if (redisConnection.isEnabled())
{
redisConnection.openPool();
}
else
{
PlexLog.log("Redis is disabled in the configuration file, not connecting.");
}
if (storageType == StorageType.MONGODB)
{
@ -139,11 +147,11 @@ public class Plex extends JavaPlugin
@Override
public void onDisable()
{
/*if (redisConnection.getJedis().isConnected())
if (redisConnection.isEnabled() && redisConnection.getJedis().isConnected())
{
PlexLog.log("Disabling Redis/Jedis. No memory leaks in this Anarchy server !");
PlexLog.log("Disabling Redis/Jedis. No memory leaks in this Anarchy server!");
redisConnection.getJedis().close();
}*/
}
}
private void generateWorlds()

View File

@ -13,12 +13,14 @@ import dev.plex.player.PunishedPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -38,42 +40,51 @@ public class BanCMD extends PlexCommand
return usage(getUsage());
}
if (args.length == 1)
UUID targetUUID = PlexUtils.getFromName(args[0]);
String reason;
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
UUID targetUUID = PlexUtils.getFromName(args[0]);
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
Player player = Bukkit.getPlayer(targetUUID);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
if (isAdmin(plexPlayer))
{
if (!isConsole(sender))
{
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (isAdmin(plexPlayer))
{
if (!isConsole(sender))
PlexPlayer plexPlayer1 = getPlexPlayer(playerSender);
if (!plexPlayer1.getRankFromString().isAtLeast(plexPlayer.getRankFromString()))
{
PlexPlayer plexPlayer1 = getPlexPlayer((Player)sender);
if (!plexPlayer1.getRankFromString().isAtLeast(plexPlayer.getRankFromString()))
{
return tl("higherRankThanYou");
}
return tl("higherRankThanYou");
}
}
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(targetUUID) == null ? new PunishedPlayer(targetUUID) : PlayerCache.getPunishedPlayer(targetUUID);
Punishment punishment = new Punishment(targetUUID, getUUID(sender));
punishment.setType(PunishmentType.BAN);
punishment.setReason("");
punishment.setPunishedUsername(plexPlayer.getName());
punishment.setEndDate(new Date(Instant.now().plusSeconds(PlexUtils.hoursToSeconds(24)).getEpochSecond()));
punishment.setCustomTime(false);
plugin.getPunishmentManager().doPunishment(punishedPlayer, punishment);
PlexUtils.broadcast(tl("banningPlayer", sender.getName(), plexPlayer.getName()));
if (Bukkit.getPlayer(targetUUID) != null)
{
Bukkit.getPlayer(targetUUID).kick(componentFromString("&cYou've been banned."));
}
}
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(targetUUID) == null ? new PunishedPlayer(targetUUID) : PlayerCache.getPunishedPlayer(targetUUID);
Punishment punishment = new Punishment(targetUUID, getUUID(sender));
punishment.setType(PunishmentType.BAN);
if (args.length > 1)
{
reason = StringUtils.join(args, " ", 1, args.length);
punishment.setReason(reason);
}
else
{
punishment.setReason("No reason provided.");
}
punishment.setPunishedUsername(plexPlayer.getName());
Date date = new Date();
punishment.setEndDate(DateUtils.addDays(date, 1));
punishment.setCustomTime(false);
plugin.getPunishmentManager().doPunishment(punishedPlayer, punishment);
PlexUtils.broadcast(tl("banningPlayer", sender.getName(), plexPlayer.getName()));
if (player != null)
{
player.kick(componentFromString("&cYou've been banned."));
}
PlexLog.debug("(From /ban command) PunishedPlayer UUID: " + punishedPlayer.getUuid());
return null;
}

View File

@ -11,11 +11,11 @@ import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.time.DateUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -36,7 +36,8 @@ public class FreezeCMD extends PlexCommand
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId());
Punishment punishment = new Punishment(UUID.fromString(punishedPlayer.getUuid()), getUUID(sender));
punishment.setCustomTime(false);
punishment.setEndDate(new Date(Instant.now().plusSeconds(10).toEpochMilli()));
Date date = new Date();
punishment.setEndDate(DateUtils.addDays(date, 1));
punishment.setType(PunishmentType.FREEZE);
punishment.setPunishedUsername(player.getName());
punishment.setReason("");

View File

@ -6,6 +6,7 @@ import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandArgumentException;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.rank.enums.Rank;
import java.util.List;
@ -28,7 +29,7 @@ public class PlexCMD extends PlexCommand
send(sender, ChatColor.LIGHT_PURPLE + "Plex. The long awaited TotalFreedomMod rewrite starts here...");
return componentFromString(ChatColor.LIGHT_PURPLE + "Plugin version: " + ChatColor.GOLD + "1.0");
}
if (args[0].equals("reload"))
if (args[0].equalsIgnoreCase("reload"))
{
checkRank(sender, Rank.SENIOR_ADMIN, "plex.reload");
Plex.get().config.load();
@ -39,6 +40,17 @@ public class PlexCMD extends PlexCommand
send(sender, "Imported ranks");
send(sender, "Plex successfully reloaded.");
}
else if (args[0].equalsIgnoreCase("redis"))
{
checkRank(sender, Rank.SENIOR_ADMIN, "plex.redis");
if (!plugin.getRedisConnection().isEnabled())
{
throw new CommandFailException("&cRedis is not enabled.");
}
plugin.getRedisConnection().getJedis().set("test", "123");
send(sender, "Set test to 123. Now outputting key test...");
send(sender, plugin.getRedisConnection().getJedis().get("test"));
}
else
{
throw new CommandArgumentException();
@ -49,6 +61,6 @@ public class PlexCMD extends PlexCommand
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
return ImmutableList.of("reload");
return ImmutableList.of("reload", "redis");
}
}

View File

@ -1,6 +1,9 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import dev.plex.player.PunishedPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.util.PlexLog;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
@ -14,6 +17,16 @@ public class LoginListener extends PlexListener
{
if (plugin.getBanManager().isBanned(event.getUniqueId()))
{
PunishedPlayer player = new PunishedPlayer(event.getUniqueId());
Punishment punishment = player.getPunishments().get(player.getPunishments().size() - 1);
PlexLog.debug("This player is banned. Outputting information:");
PlexLog.debug("UUID: " + player.getUuid());
PlexLog.debug("Username: " + punishment.getPunishedUsername());
PlexLog.debug("Punisher: " + punishment.getPunisher());
PlexLog.debug("Reason: " + punishment.getReason());
PlexLog.debug("End date: " + punishment.getEndDate());
PlexLog.debug("IPs: " + punishment.getIPS());
PlexLog.debug("Type: " + punishment.getType());
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED,
LegacyComponentSerializer.legacyAmpersand().deserialize(banMessage));
}

View File

@ -2,9 +2,11 @@ package dev.plex.player;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.event.PunishedPlayerFreezeEvent;
import dev.plex.event.PunishedPlayerMuteEvent;
import dev.plex.punishment.Punishment;
import dev.plex.util.PlexLog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -21,7 +23,7 @@ import org.json.JSONObject;
import org.json.JSONTokener;
@Getter
public class PunishedPlayer
public class PunishedPlayer extends PlexBase
{
//everything in here will be stored in redis
@Setter(AccessLevel.NONE)
@ -85,12 +87,27 @@ public class PunishedPlayer
{
List<Punishment> punishments = Lists.newArrayList();
if (plugin.getRedisConnection().isEnabled())
{
PlexLog.debug("Getting punishments from Redis...");
String strObj = plugin.getRedisConnection().getJedis().get(uuid);
JSONTokener tokener = new JSONTokener(strObj);
JSONObject object = new JSONObject(tokener);
object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj ->
{
Punishment punishment = Punishment.fromJson(obj.toString());
punishments.add(punishment);
});
return punishments;
}
File file = getPunishmentsFile();
if (isNotEmpty(file))
{
try
{
PlexLog.debug("Getting punishments from locally stored JSON files...");
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
JSONObject object = new JSONObject(tokener);
object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj ->

View File

@ -12,7 +12,6 @@ import lombok.Setter;
@Setter
public class Punishment
{
private final UUID punished;
private final UUID punisher;
@ -47,5 +46,4 @@ public class Punishment
{
return new Gson().fromJson(json, Punishment.class);
}
}

View File

@ -3,8 +3,10 @@ package dev.plex.punishment;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.banning.Ban;
import dev.plex.player.PunishedPlayer;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.io.File;
import java.io.FileInputStream;
@ -22,9 +24,8 @@ import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;
import org.json.JSONTokener;
public class PunishmentManager
public class PunishmentManager extends PlexBase
{
public void insertPunishment(PunishedPlayer player, Punishment punishment)
{
File file = player.getPunishmentsFile();
@ -36,6 +37,11 @@ public class PunishmentManager
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
JSONObject object = new JSONObject(tokener);
object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON());
if (plugin.getRedisConnection().isEnabled())
{
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
}
FileWriter writer = new FileWriter(file);
writer.append(object.toString(8));
@ -52,13 +58,17 @@ public class PunishmentManager
punishments.put("punishments", punishmentList);
object.put(punishment.getPunished().toString(), punishments);
if (plugin.getRedisConnection().isEnabled())
{
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
}
FileWriter writer = new FileWriter(file);
writer.append(object.toString(8));
writer.flush();
writer.close();
}
}
catch (IOException e)
{
@ -107,8 +117,6 @@ public class PunishmentManager
Bukkit.getLogger().info("Unfroze");
}
}.runTaskLater(Plex.get(), 20 * seconds);
}
else if (punishment.getType() == PunishmentType.MUTE)
{
@ -132,5 +140,4 @@ public class PunishmentManager
issuePunishment(player, punishment);
insertPunishment(player, punishment);
}
}

View File

@ -3,5 +3,4 @@ package dev.plex.punishment;
public enum PunishmentType
{
MUTE, FREEZE, BAN;
}

View File

@ -1,11 +1,12 @@
package dev.plex.storage;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.util.PlexLog;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisConnection
public class RedisConnection extends PlexBase
{
private JedisPool pool;
private Jedis jedis;
@ -14,19 +15,24 @@ public class RedisConnection
{
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(RedisConnection.class.getClassLoader());
this.pool = new JedisPool(new JedisPoolConfig(), Plex.get().getConfig().getString("data.side.hostname"), Plex.get().getConfig().getInt("data.side.port"));
this.pool = new JedisPool(plugin.config.getString("data.side.hostname"), Plex.get().getConfig().getInt("data.side.port"));
Thread.currentThread().setContextClassLoader(previous);
PlexLog.log("Connected to Redis!");
return pool;
}
public Jedis getJedis()
{
this.jedis = pool.getResource();
if (Plex.get().getConfig().getBoolean("data.side.auth"))
if (plugin.config.getBoolean("data.side.auth"))
{
jedis.auth(Plex.get().getConfig().getString("data.side.password"));
jedis.auth(plugin.config.getString("data.side.password"));
}
return jedis;
}
public final boolean isEnabled()
{
return plugin.config.getBoolean("data.side.enabled");
}
}

View File

@ -1,6 +1,5 @@
package dev.plex.util;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.config.Config;

View File

@ -1,4 +1,5 @@
# Plex Configuration File
# For documentation, please visit: https://docs.plex.us.org
server:
name: "Plexus"
@ -24,6 +25,7 @@ data:
port: 27017
db: "plex"
side: # This is Redis, leave password blank if auth is false
enabled: false
auth: true
hostname: 127.0.0.1
port: 6379