Skip to content

Commit

Permalink
feat: ✨ remove prefix from command
Browse files Browse the repository at this point in the history
  • Loading branch information
konhi committed Dec 21, 2021
1 parent fa96e48 commit db6f225
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public CachedConfig() {
if (config.getBoolean("checks.nametag-use", true))
registerListener(EntityRenameListener.class);

registerListener(CommandColonListener.class);

ConfigurationSection filter = config.getConfigurationSection("filter");
if (filter == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.endermite.censura.listener;

import eu.endermite.censura.Censura;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class CommandColonListener implements Listener {
@EventHandler
public void onChatEven(PlayerCommandPreprocessEvent event) {
event.setMessage(getMessageWithoutPrefix(event));
}

/** /minecraft:xp -> /xp */
private String getMessageWithoutPrefix(PlayerCommandPreprocessEvent event) {
String message = event.getMessage();
String[] messageAsArray = message.split(" ");
String commandName = messageAsArray[0];

if (!isInvalidCommand(message) && commandName.contains(":")) {
String commandNameWithoutPrefix = commandName.split(":")[1];
messageAsArray[0] = commandNameWithoutPrefix;

String messageWithoutPrefix = messageAsArray.toString();
Censura.getPlugin().getLogger().info(String.format("Censura: Prefix removed: %s", commandName));

return messageWithoutPrefix;
}

return message;
}

private boolean isInvalidCommand(String command) {
// only syntax like minecraft:xp should work
if (colonCount(command) > 1) {
return true;
}

return false;
}

private long colonCount(String message) {
return message.chars().filter(i -> i == ':').count();
}
}

0 comments on commit db6f225

Please sign in to comment.