From 321c766da249b963139700ca4079999f7414ee10 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 22 Jun 2021 14:51:28 +0200 Subject: [PATCH] added option to excape command by starting a message by \/command --- .../purpurextras/config/PurpurConfig.java | 10 ++++--- .../listeners/EscapeCommandSlashListener.java | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/purpurextras/listeners/EscapeCommandSlashListener.java diff --git a/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java b/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java index aaecc52..544c1f3 100644 --- a/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java +++ b/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java @@ -2,6 +2,7 @@ import me.youhavetrouble.purpurextras.PurpurExtras; import me.youhavetrouble.purpurextras.listeners.BeehiveLoreListener; +import me.youhavetrouble.purpurextras.listeners.EscapeCommandSlashListener; import me.youhavetrouble.purpurextras.listeners.RespawnAnchorNeedsChargeListener; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.HandlerList; @@ -14,7 +15,7 @@ public class PurpurConfig { Logger logger; FileConfiguration config; File configPath; - public final boolean beeHiveLore, respawnAnchorNeedsCharges; + public final boolean beeHiveLore, respawnAnchorNeedsCharges, escapeEscapedCommands; public final String beeHiveLoreBees, beeHiveLoreHoney; public PurpurConfig(PurpurExtras plugin) { @@ -36,6 +37,10 @@ public PurpurConfig(PurpurExtras plugin) { if (!respawnAnchorNeedsCharges) plugin.registerListener(RespawnAnchorNeedsChargeListener.class); + this.escapeEscapedCommands = getBoolean("settings.chat.escape-commands", false); + if (!escapeEscapedCommands) + plugin.registerListener(EscapeCommandSlashListener.class); + saveConfig(); } @@ -45,8 +50,7 @@ public void saveConfig() { try { config.save(configPath); } catch (IOException e) { - logger.error("Failed to save configuration file!"); - e.printStackTrace(); + logger.error("Failed to save configuration file! - "+e.getLocalizedMessage()); } } diff --git a/src/main/java/me/youhavetrouble/purpurextras/listeners/EscapeCommandSlashListener.java b/src/main/java/me/youhavetrouble/purpurextras/listeners/EscapeCommandSlashListener.java new file mode 100644 index 0000000..0525253 --- /dev/null +++ b/src/main/java/me/youhavetrouble/purpurextras/listeners/EscapeCommandSlashListener.java @@ -0,0 +1,27 @@ +package me.youhavetrouble.purpurextras.listeners; + +import io.papermc.paper.event.player.AsyncChatEvent; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +public class EscapeCommandSlashListener implements Listener { + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onCommandEscape(AsyncChatEvent event) { + String message = PlainComponentSerializer.plain().serialize(event.message()); + String[] messageSplit = message.split(" "); + String command = messageSplit[0].substring(1); + Component component = event.message().replaceText( + TextReplacementConfig.builder() + .match("(\\\\/\\S*)") + .replacement(command) + .once() + .build()); + event.message(component); + } + +}