Skip to content

Commit

Permalink
added option to excape command by starting a message by \/command
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Jun 22, 2021
1 parent ec1b3c7 commit 321c766
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}

Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 321c766

Please sign in to comment.