From 6086f761eb2be7dfd10647a21885939419994c49 Mon Sep 17 00:00:00 2001 From: Taah Date: Sat, 9 Apr 2022 22:10:35 -0700 Subject: [PATCH] add /say fix /tag --- .../blocker/CommandBlockerManager.java | 14 +-------- .../java/dev/plex/command/impl/DebugCMD.java | 13 ++++++++ .../java/dev/plex/command/impl/RawSayCMD.java | 2 +- .../java/dev/plex/command/impl/SayCMD.java | 31 +++++++++++++++++++ .../java/dev/plex/command/impl/TagCMD.java | 8 +++-- .../dev/plex/listener/impl/ChatListener.java | 7 +++-- .../plex/listener/impl/ServerListener.java | 2 ++ src/main/java/dev/plex/rank/RankManager.java | 3 +- src/main/java/dev/plex/util/PlexUtils.java | 20 ++++++++++-- src/main/resources/config.yml | 4 +-- src/main/resources/messages.yml | 5 ++- 11 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 src/main/java/dev/plex/command/impl/SayCMD.java diff --git a/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java b/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java index c47c79a..f44feab 100644 --- a/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java +++ b/src/main/java/dev/plex/command/blocker/CommandBlockerManager.java @@ -23,19 +23,7 @@ public class CommandBlockerManager extends PlexBase private static CommandMap getCommandMap() { - try - { - SimplePluginManager spm = (SimplePluginManager) Bukkit.getServer().getPluginManager(); - Field cmf = SimplePluginManager.class.getDeclaredField("commandMap"); - cmf.setAccessible(true); - return (SimpleCommandMap)cmf.get(spm); - } - catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) - { - PlexLog.error("Unable to get command map for command blocker."); - e.printStackTrace(); - } - return null; + return plugin.getServer().getCommandMap(); } public void syncCommands() diff --git a/src/main/java/dev/plex/command/impl/DebugCMD.java b/src/main/java/dev/plex/command/impl/DebugCMD.java index b22326f..bf00bc5 100644 --- a/src/main/java/dev/plex/command/impl/DebugCMD.java +++ b/src/main/java/dev/plex/command/impl/DebugCMD.java @@ -8,12 +8,15 @@ import dev.plex.command.annotation.System; import dev.plex.rank.enums.Rank; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; + +import java.util.Arrays; import java.util.List; import java.util.Locale; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -59,6 +62,16 @@ public class DebugCMD extends PlexCommand } return mmString("Re-applied game all the game rules!"); } + if (args[0].equalsIgnoreCase("aliases")) + { + String commandName = args[1]; + Command command = plugin.getServer().getCommandMap().getCommand(commandName); + if (command == null) + { + return mmString("That command could not be found!"); + } + return mmString("Aliases for " + commandName + " are: " + Arrays.toString(command.getAliases().toArray(new String[0]))); + } return null; } diff --git a/src/main/java/dev/plex/command/impl/RawSayCMD.java b/src/main/java/dev/plex/command/impl/RawSayCMD.java index caa269b..d942fc9 100644 --- a/src/main/java/dev/plex/command/impl/RawSayCMD.java +++ b/src/main/java/dev/plex/command/impl/RawSayCMD.java @@ -14,7 +14,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @CommandPermissions(level = Rank.SENIOR_ADMIN, permission = "plex.rawsay", source = RequiredCommandSource.ANY) -@CommandParameters(name = "rawsay", usage = "/ ", description = "Displays a message to everyone") +@CommandParameters(name = "rawsay", usage = "/ ", description = "Displays a raw message to everyone") public class RawSayCMD extends PlexCommand { @Override diff --git a/src/main/java/dev/plex/command/impl/SayCMD.java b/src/main/java/dev/plex/command/impl/SayCMD.java new file mode 100644 index 0000000..10c1493 --- /dev/null +++ b/src/main/java/dev/plex/command/impl/SayCMD.java @@ -0,0 +1,31 @@ +package dev.plex.command.impl; + +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.rank.enums.Rank; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import org.apache.commons.lang.StringUtils; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@CommandPermissions(level = Rank.ADMIN, permission = "plex.say", source = RequiredCommandSource.ANY) +@CommandParameters(name = "say", usage = "/ ", description = "Displays a message to everyone") +public class SayCMD extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) + { + if (args.length == 0) + { + return usage(); + } + + PlexUtils.broadcast(PlexUtils.messageComponent("sayCommand", sender.getName(), StringUtils.join(args, " "))); + return null; + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/command/impl/TagCMD.java b/src/main/java/dev/plex/command/impl/TagCMD.java index 0f05322..78110bd 100644 --- a/src/main/java/dev/plex/command/impl/TagCMD.java +++ b/src/main/java/dev/plex/command/impl/TagCMD.java @@ -10,6 +10,8 @@ import dev.plex.rank.enums.Rank; import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.apache.commons.lang.StringUtils; @@ -48,15 +50,15 @@ public class TagCMD extends PlexCommand return usage("/tag set "); } String prefix = StringUtils.join(args, " ", 1, args.length); - Component convertedComponent = removeEvents(noColorComponentFromString(prefix)); - convertedComponent = removeEvents(PlexUtils.mmDeserialize(LegacyComponentSerializer.legacySection().serialize(convertedComponent))); + + Component convertedComponent = removeEvents(PlexUtils.mmCustomDeserialize(prefix = prefix.replace("", "").replace("
", ""), StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())); //noColorComponentFromString(prefix) if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16)) { return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16)); } - player.setPrefix(MiniMessage.miniMessage().serialize(convertedComponent)); + player.setPrefix(prefix); DataUtils.update(player); return messageComponent("prefixSetTo", MiniMessage.miniMessage().serialize(convertedComponent)); } diff --git a/src/main/java/dev/plex/listener/impl/ChatListener.java b/src/main/java/dev/plex/listener/impl/ChatListener.java index c5ae208..11b8205 100644 --- a/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -9,7 +9,10 @@ import io.papermc.paper.event.player.AsyncChatEvent; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.apache.commons.lang.StringUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; @@ -51,14 +54,14 @@ public class ChatListener extends PlexListener return Component.empty() .append(prefix) .append(Component.space()) - .append(LegacyComponentSerializer.legacyAmpersand().deserialize("&" + plugin.config.getString("chat.name-color") + LegacyComponentSerializer.legacyAmpersand().serialize(sourceDisplayName))) + .append(MiniMessage.miniMessage().deserialize(plugin.config.getString("chat.name-color", ""))).append(sourceDisplayName) .append(Component.space()) .append(Component.text("»").color(NamedTextColor.GRAY)) .append(Component.space()) .append(message); } return Component.empty() - .append(LegacyComponentSerializer.legacyAmpersand().deserialize("&" + plugin.config.getString("chat.name-color") + LegacyComponentSerializer.legacyAmpersand().serialize(sourceDisplayName))) + .append(MiniMessage.miniMessage().deserialize(plugin.config.getString("chat.name-color", ""))).append(sourceDisplayName) .append(Component.space()) .append(Component.text("»").color(NamedTextColor.GRAY)) .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 584d6d1..3bb025f 100644 --- a/src/main/java/dev/plex/listener/impl/ServerListener.java +++ b/src/main/java/dev/plex/listener/impl/ServerListener.java @@ -1,6 +1,7 @@ package dev.plex.listener.impl; import com.destroystokyo.paper.event.server.PaperServerListPingEvent; +import com.destroystokyo.paper.event.server.ServerTickEndEvent; import dev.plex.listener.PlexListener; import dev.plex.util.PlexUtils; import java.util.List; @@ -8,6 +9,7 @@ import java.util.stream.Collectors; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerKickEvent; public class ServerListener extends PlexListener { diff --git a/src/main/java/dev/plex/rank/RankManager.java b/src/main/java/dev/plex/rank/RankManager.java index 194e6dd..5ccf246 100644 --- a/src/main/java/dev/plex/rank/RankManager.java +++ b/src/main/java/dev/plex/rank/RankManager.java @@ -15,6 +15,7 @@ import lombok.SneakyThrows; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; @@ -95,7 +96,7 @@ public class RankManager { if (!player.getPrefix().equals("")) { - return PlexUtils.mmDeserialize(player.getPrefix()); + return PlexUtils.mmCustomDeserialize(player.getPrefix(), StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition()); } if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName())) { diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 4ac0db1..218abb3 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -37,6 +37,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.time.Instant; import java.time.LocalDateTime; +import java.time.Month; import java.time.ZoneId; import java.util.*; import java.util.concurrent.ThreadLocalRandom; @@ -180,14 +181,27 @@ public class PlexUtils extends PlexBase public static Component mmDeserialize(String input) { - Calendar calendar = Calendar.getInstance(); + /*Calendar calendar = Calendar.getInstance(); MiniMessage mm = (calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1 && (!plugin.config.contains("april_fools") || plugin.config.getBoolean("april_fools"))) ? eggMessage : safeMessage; - return mm.deserialize(PlainTextComponentSerializer.plainText().serialize(LegacyComponentSerializer.legacySection().deserialize(input))); + return mm.deserialize(PlainTextComponentSerializer.plainText().serialize(LegacyComponentSerializer.legacySection().deserialize(input)));*/ + boolean aprilFools = plugin.config.getBoolean("april_fools"); + LocalDateTime date = LocalDateTime.now(); + if (aprilFools && date.getMonth() == Month.APRIL && date.getDayOfMonth() == 1) + { + Component component = PlainTextComponentSerializer.plainText().deserialize(input); + return MiniMessage.miniMessage().deserialize("" + PlainTextComponentSerializer.plainText().serialize(component)); + } + return MiniMessage.miniMessage().deserialize(input); + } + + public static Component mmCustomDeserialize(String input, TagResolver... resolvers) + { + return MiniMessage.builder().tags(TagResolver.builder().resolvers(resolvers).build()).build().deserialize(input); } public static Component messageComponent(String entry, Object... objects) { - return mmDeserialize(messageString(entry, objects)); + return MiniMessage.miniMessage().deserialize(messageString(entry, objects)); } public static String messageString(String entry, Object... objects) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 12f9c74..e3955a7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -24,8 +24,8 @@ chat: # The maximum amount of characters a player can have for their tag # This does not include color tags such as or max-tag-length: 64 - # Color code for name color - name-color: 'f' + # Color tag for name color + name-color: '' # Should Plex use a "true op" system with ranks or only permission nodes # Options are "permissions" or "ranks" diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 2870cc6..2894798 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -168,4 +168,7 @@ autoWipeDisabled: "Item wiping is currently disabled in the config!" allowDropsDisabled: "No longer allowing drops from players." allowDropsEnabled: "Now allowing drops from players." blockedCommandColor: "" -commandBlocked: "That command is blocked." \ No newline at end of file +commandBlocked: "That command is blocked." +# 0 - The command sender +# 1 - The message being said +sayCommand: "[Server: {0}] {1}" \ No newline at end of file