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); + } + +}