-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/eu/endermite/censura/listener/CommandColonListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |