From 355a930c89671dcab6bd9406308e1beb744643a4 Mon Sep 17 00:00:00 2001 From: Taah Date: Mon, 21 Feb 2022 22:55:59 -0800 Subject: [PATCH] - Create tag command - Fix bug fixes --- src/main/java/dev/plex/Plex.java | 29 +++++-- .../java/dev/plex/command/PlexCommand.java | 2 +- .../java/dev/plex/command/impl/ListCMD.java | 3 +- .../java/dev/plex/command/impl/TagCMD.java | 80 +++++++++++++++++++ .../dev/plex/handlers/CommandHandler.java | 2 +- .../dev/plex/listener/impl/ChatListener.java | 2 +- .../plex/listener/impl/ServerListener.java | 1 + src/main/resources/config.yml | 3 + 8 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 src/main/java/dev/plex/command/impl/TagCMD.java diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index d21c899..411e3d5 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -21,13 +21,14 @@ import dev.plex.storage.StorageType; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import dev.plex.world.CustomWorld; -import java.util.UUID; import lombok.Getter; import lombok.Setter; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.util.UUID; + @Getter @Setter public class Plex extends JavaPlugin @@ -81,8 +82,7 @@ public class Plex extends JavaPlugin { PlexUtils.testConnections(); PlexLog.log("Connected to " + storageType.name().toUpperCase()); - } - catch (Exception e) + } catch (Exception e) { PlexLog.error("Failed to connect to " + storageType.name().toUpperCase()); e.printStackTrace(); @@ -96,8 +96,7 @@ public class Plex extends JavaPlugin { redisConnection.getJedis(); PlexLog.log("Connected to Redis!"); - } - else + } else { PlexLog.log("Redis is disabled in the configuration file, not connecting."); } @@ -105,8 +104,7 @@ public class Plex extends JavaPlugin if (storageType == StorageType.MONGODB) { mongoPlayerData = new MongoPlayerData(); - } - else + } else { sqlPlayerData = new SQLPlayerData(); } @@ -139,6 +137,23 @@ public class Plex extends JavaPlugin @Override public void onDisable() { + Bukkit.getOnlinePlayers().forEach(player -> + { + PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId()); //get the player because it's literally impossible for them to not have an object + + if (plugin.getRankManager().isAdmin(plexPlayer)) + { + plugin.getAdminList().removeFromCache(UUID.fromString(plexPlayer.getUuid())); + } + + if (mongoPlayerData != null) //back to mongo checking + { + mongoPlayerData.update(plexPlayer); //update the player's document + } else if (sqlPlayerData != null) //sql checking + { + sqlPlayerData.update(plexPlayer); + } + }); if (redisConnection.isEnabled() && redisConnection.getJedis().isConnected()) { PlexLog.log("Disabling Redis/Jedis. No memory leaks in this Anarchy server!"); diff --git a/src/main/java/dev/plex/command/PlexCommand.java b/src/main/java/dev/plex/command/PlexCommand.java index 2a9cfce..4012171 100644 --- a/src/main/java/dev/plex/command/PlexCommand.java +++ b/src/main/java/dev/plex/command/PlexCommand.java @@ -260,7 +260,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC PlexPlayer plexPlayer = getPlexPlayer(player); if (plugin.getSystem().equalsIgnoreCase("ranks")) { - if (!plexPlayer.getRankFromString().isAtLeast(getLevel())) + if (!plexPlayer.getRankFromString().isAtLeast(rank)) { throw new CommandFailException(PlexUtils.tl("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage()))); } diff --git a/src/main/java/dev/plex/command/impl/ListCMD.java b/src/main/java/dev/plex/command/impl/ListCMD.java index 410ac1f..74ad92a 100644 --- a/src/main/java/dev/plex/command/impl/ListCMD.java +++ b/src/main/java/dev/plex/command/impl/ListCMD.java @@ -8,6 +8,7 @@ import dev.plex.rank.enums.Rank; import java.util.List; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -37,7 +38,7 @@ public class ListCMD extends PlexCommand for (int i = 0; i < players.size(); i++) { Player player = players.get(i); - component = component.append(Component.text(getPlexPlayer(player).getRankFromString().getPrefix())).append(Component.space()).append(Component.text(player.getName()).color(NamedTextColor.WHITE)); + component = component.append(LegacyComponentSerializer.legacyAmpersand().deserialize(getPlexPlayer(player).getRankFromString().getPrefix())).append(Component.space()).append(Component.text(player.getName()).color(NamedTextColor.WHITE)); if (i != players.size() - 1) { component = component.append(Component.text(",")).append(Component.space()); diff --git a/src/main/java/dev/plex/command/impl/TagCMD.java b/src/main/java/dev/plex/command/impl/TagCMD.java new file mode 100644 index 0000000..557f9ab --- /dev/null +++ b/src/main/java/dev/plex/command/impl/TagCMD.java @@ -0,0 +1,80 @@ +package dev.plex.command.impl; + +import dev.plex.cache.DataUtils; +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.command.source.RequiredCommandSource; +import dev.plex.player.PlexPlayer; +import dev.plex.rank.enums.Rank; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +//TODO: Redo Messages and have customizable messages +@CommandPermissions(level = Rank.OP, permission = "plex.tag", source = RequiredCommandSource.ANY) +@CommandParameters(name = "tag", aliases = "prefix", description = "Manages your prefix", usage = "/ ") +public class TagCMD extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) + { + if (args.length == 0) + { + if (sender instanceof ConsoleCommandSender) + { + return usage("/tag clear "); + } + return usage(); + } + + if (args[0].equalsIgnoreCase("set")) + { + if (sender instanceof ConsoleCommandSender) + { + return tl("noPermissionConsole"); + } + PlexPlayer player = DataUtils.getPlayer(playerSender.getUniqueId()); + if (args.length < 2) + { + return usage("/tag set "); + } + String prefix = StringUtils.join(args, " ", 1, args.length); + if (ChatColor.stripColor(prefix).length() > plugin.config.getInt("chat.max-tag-length", 16)) + { + return componentFromString("The max length for a tag may only be " + plugin.config.getInt("chat.max-tag-length", 16)); + } + player.setPrefix(prefix); + return Component.text("Your prefix has been set to ").color(NamedTextColor.AQUA).append(componentFromString(prefix)); + } + + if (args[0].equalsIgnoreCase("clear")) + { + if (args.length == 1) + { + if (sender instanceof ConsoleCommandSender) + { + return tl("noPermissionConsole"); + } + + PlexPlayer player = DataUtils.getPlayer(playerSender.getUniqueId()); + player.setPrefix(""); + return Component.text("Your prefix has been cleared.").color(NamedTextColor.AQUA); + } + + checkRank(sender, Rank.ADMIN, "plex.tag.clearother"); + + Player target = getNonNullPlayer(args[1]); + PlexPlayer plexTarget = DataUtils.getPlayer(target.getUniqueId()); + plexTarget.setPrefix(""); + return Component.text("You have cleared " + target.getName() + "'s prefix.").color(NamedTextColor.AQUA); + } + return usage(); + } +} diff --git a/src/main/java/dev/plex/handlers/CommandHandler.java b/src/main/java/dev/plex/handlers/CommandHandler.java index 2a75b28..39244be 100644 --- a/src/main/java/dev/plex/handlers/CommandHandler.java +++ b/src/main/java/dev/plex/handlers/CommandHandler.java @@ -32,7 +32,6 @@ public class CommandHandler extends PlexBase commands.add(new BanCMD()); commands.add(new CommandSpyCMD()); commands.add(new CreativeCMD()); - commands.add(new FlatlandsCMD()); commands.add(new FreezeCMD()); commands.add(new ListCMD()); @@ -43,6 +42,7 @@ public class CommandHandler extends PlexBase commands.add(new PunishmentsCMD()); commands.add(new SpectatorCMD()); commands.add(new SurvivalCMD()); + commands.add(new TagCMD()); commands.add(new UnbanCMD()); commands.add(new UnfreezeCMD()); commands.add(new WorldCMD()); diff --git a/src/main/java/dev/plex/listener/impl/ChatListener.java b/src/main/java/dev/plex/listener/impl/ChatListener.java index e539594..38a5d48 100644 --- a/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -41,7 +41,7 @@ public class ChatListener extends PlexListener { if (hasPrefix) { - return LegacyComponentSerializer.legacyAmpersand().deserialize(prefix) + return Component.empty().append(LegacyComponentSerializer.legacyAmpersand().deserialize(prefix)) .append(Component.space()) .append(sourceDisplayName) .append(Component.space()) diff --git a/src/main/java/dev/plex/listener/impl/ServerListener.java b/src/main/java/dev/plex/listener/impl/ServerListener.java index 4be0b13..e3a633c 100644 --- a/src/main/java/dev/plex/listener/impl/ServerListener.java +++ b/src/main/java/dev/plex/listener/impl/ServerListener.java @@ -36,6 +36,7 @@ public class ServerListener extends PlexListener List samples = plugin.config.getStringList("server.sample"); if (!samples.isEmpty()) { + event.getPlayerSample().clear(); event.getPlayerSample().addAll(samples.stream().map(string -> string.replace("&", "ยง")).map(Bukkit::createProfile).collect(Collectors.toList())); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index cd35309..47a0121 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -16,6 +16,9 @@ titles: banning: ban_url: "https://forum.plex.us.org" +chat: + max-tag-length: 16 + name-color: 'f' #Color code for name color # Settings for commands relating to Plex commands: