Refactor command checker

This commit is contained in:
mathiascode 2019-12-14 20:47:26 +02:00
parent 8ad68807d0
commit f7e86e696b
5 changed files with 178 additions and 198 deletions

View file

@ -14,10 +14,9 @@ class EntityExplosion implements Listener {
event.setRadius(20);
}
if (event.getEntity().getWorld().getEntitiesByClass(Fireball.class).size() > 40) {
if (event.getRadius() > 1) {
if (event.getEntity().getWorld().getEntitiesByClass(Fireball.class).size() > 30 &&
event.getRadius() > 1) {
event.setRadius(1);
}
}
}
}

View file

@ -67,7 +67,7 @@ class EntitySpawn implements Listener {
if (entity instanceof LivingEntity) {
final LivingEntity mob = (LivingEntity) entity;
checkIllegalEquipment(mob);
//checkIllegalEquipment(mob);
limitFollowAttribute(mob);
}
@ -102,7 +102,7 @@ class EntitySpawn implements Listener {
return false;
}
private void checkIllegalEquipment(LivingEntity mob) {
/*private void checkIllegalEquipment(LivingEntity mob) {
try {
for (ItemStack item : mob.getEquipment().getArmorContents()) {
if (isIllegalItem(item)) {
@ -136,7 +136,7 @@ class EntitySpawn implements Listener {
} catch (Exception exception) {
mob.getEquipment().setItemInOffHand(null);
}
}
}*/
private boolean checkShouldRemoveEntities(World world) {
final int worldEntityCount = world.getEntities().size();
@ -169,7 +169,7 @@ class EntitySpawn implements Listener {
return false;
}
public static boolean isIllegalItem(ItemStack item) {
/*public static boolean isIllegalItem(ItemStack item) {
try {
if (item != null &&
item.hasItemMeta()) {
@ -188,7 +188,7 @@ class EntitySpawn implements Listener {
return true;
}
return false;
}
}*/
private void limitAreaEffectCloudRadius(AreaEffectCloud cloud) {
if (cloud.getRadius() > 40) {
@ -244,7 +244,7 @@ class EntitySpawn implements Listener {
limitAreaEffectCloudRadius(event.getEntity());
}
@EventHandler
/*@EventHandler
void onBlockDispense(BlockDispenseEvent event) {
try {
event.getBlock().getState();
@ -252,7 +252,7 @@ class EntitySpawn implements Listener {
} catch (Exception exception) {
event.setCancelled(true);
}
}
}*/
@EventHandler
void onEntityAddToWorld(EntityAddToWorldEvent event) {
@ -293,16 +293,16 @@ class EntitySpawn implements Listener {
applyEntityChanges(entity);
}
@EventHandler
/*@EventHandler
void onItemSpawn(ItemSpawnEvent event) {
final ItemStack item = event.getEntity().getItemStack();
if (isIllegalItem(item)) {
event.setCancelled(true);
}
}
}*/
@EventHandler
/*@EventHandler
void onPlayerDropItem(PlayerDropItemEvent event) {
final Inventory inventory = event.getPlayer().getInventory();
@ -311,7 +311,7 @@ class EntitySpawn implements Listener {
inventory.clear();
}
}
}
}*/
@EventHandler
void onPreCreatureSpawn(PreCreatureSpawnEvent event) {

View file

@ -7,6 +7,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -22,8 +23,6 @@ class PlayerCommand implements Listener {
@EventHandler
void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
final String[] arr = event.getMessage().split(" ");
final String command = event.getMessage();
final UUID playerUuid = event.getPlayer().getUniqueId();
if (commandMillisList.get(playerUuid) != null) {
@ -40,98 +39,15 @@ class PlayerCommand implements Listener {
return;
}
if (("/minecraft:execute".equals(arr[0].toLowerCase()) ||
"/execute".equals(arr[0].toLowerCase())) &&
arr.length >= 2) {
int asAtCount = 0;
final CommandSender sender = event.getPlayer();
final String command = event.getMessage();
final boolean isConsoleCommand = false;
final String checkedCommand = ServerCommand.checkCommand(sender, command, isConsoleCommand);
for (int i = 1; i < arr.length; i++) {
if ("run".equalsIgnoreCase(arr[i])) {
if (i+1 < arr.length) {
if ("execute".equalsIgnoreCase(arr[i+1]) ||
"particle".equalsIgnoreCase(arr[i+1]) ||
"save-off".equalsIgnoreCase(arr[i+1]) ||
"stop".equalsIgnoreCase(arr[i+1])) {
Command.broadcastCommandMessage(event.getPlayer(), "Forbidden execute command detected");
event.setCancelled(true);
return;
} else if (i+3 < arr.length &&
"gamerule".equalsIgnoreCase(arr[i+1])) {
if ("randomTickSpeed".equalsIgnoreCase(arr[i+2]) &&
Double.parseDouble(arr[i+3]) > 6) {
event.setMessage(command.replaceFirst("(?i)" + "randomTickSpeed " + arr[i+3], "randomTickSpeed 6"));
return;
} else if ("spawnRadius".equalsIgnoreCase(arr[i+2]) &&
Double.parseDouble(arr[i+3]) > 100) {
event.setMessage(command.replaceFirst("(?i)" + "spawnRadius " + arr[i+3], "spawnRadius 100"));
}
}
}
break;
}
if ("as".equalsIgnoreCase(arr[i]) ||
"at".equalsIgnoreCase(arr[i])) {
asAtCount++;
}
}
if (asAtCount >= 2) {
Command.broadcastCommandMessage(event.getPlayer(), "Forbidden execute pattern detected");
if (checkedCommand != null) {
event.setMessage(checkedCommand);
} else {
event.setCancelled(true);
}
} else if (("/minecraft:gamerule".equalsIgnoreCase(arr[0]) ||
"/gamerule".equalsIgnoreCase(arr[0])) &&
arr.length >= 3) {
if ("randomTickSpeed".equalsIgnoreCase(arr[1]) &&
Double.parseDouble(arr[2]) > 6) {
event.setMessage(command.replaceFirst(arr[2], "6"));
} else if ("spawnRadius".equalsIgnoreCase(arr[1]) &&
Double.parseDouble(arr[2]) > 100) {
event.setMessage(command.replaceFirst(arr[2], "100"));
}
} else if (("/minecraft:particle".equalsIgnoreCase(arr[0]) ||
"/particle".equalsIgnoreCase(arr[0])) &&
arr.length >= 10) {
if (Double.parseDouble(arr[9]) > 10) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 9; i++) {
stringBuilder.append(arr[i]).append(" ");
}
stringBuilder.append("10 ");
for (int i = 10; i < arr.length; i++) {
stringBuilder.append(arr[i]).append(" ");
}
event.setMessage(stringBuilder.toString());
}
} else if (("/bukkit:reload".equalsIgnoreCase(arr[0]) ||
"/bukkit:rl".equalsIgnoreCase(arr[0]) ||
"/reload".equalsIgnoreCase(arr[0]) ||
"/rl".equalsIgnoreCase(arr[0])) &&
event.getPlayer().hasPermission("bukkit.command.reload")) {
if (arr.length >= 2 &&
"confirm".equalsIgnoreCase(arr[1])) {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getPlayer(), ChatColor.RED + "Please note that this command is not supported and may cause issues when using some plugins.");
Command.broadcastCommandMessage(event.getPlayer(), ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server.");
Command.broadcastCommandMessage(event.getPlayer(), ChatColor.GREEN + "Reload complete.");
}
} else if (("/restart".equalsIgnoreCase(arr[0]) ||
"/spigot:restart".equalsIgnoreCase(arr[0])) &&
event.getPlayer().hasPermission("bukkit.command.restart")) {
event.setCancelled(true);
} else if (("/minecraft:save-off".equalsIgnoreCase(arr[0]) ||
"/save-off".equalsIgnoreCase(arr[0])) &&
event.getPlayer().hasPermission("minecraft.command.save.disable")) {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getPlayer(), "Automatic saving is now disabled");
} else if (("/minecraft:stop".equalsIgnoreCase(arr[0]) ||
"/stop".equalsIgnoreCase(arr[0])) &&
event.getPlayer().hasPermission("minecraft.command.stop")) {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getPlayer(), "Stopping the server");
}
}
}

View file

@ -40,14 +40,14 @@ class PlayerConnection implements Listener {
}
}
@EventHandler
/*@EventHandler
void onInventoryClose(InventoryCloseEvent event) {
for (ItemStack item : event.getInventory().getContents()) {
if (EntitySpawn.isIllegalItem(item)) {
event.getInventory().clear();
}
}
}
}*/
@EventHandler
void onPlayerJoin(PlayerJoinEvent event) {

View file

@ -4,6 +4,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
@ -15,66 +16,83 @@ import org.bukkit.event.server.RemoteServerCommandEvent;
import org.bukkit.event.server.ServerCommandEvent;
class ServerCommand implements Listener {
@EventHandler
void onServerCommand(ServerCommandEvent event) {
final String[] arr = event.getCommand().split(" ");
final String command = event.getCommand();
public static String checkCommand(CommandSender sender, String command, boolean isConsoleCommand) {
final String[] arr = command.split(" ");
String commandName = arr[0].toLowerCase();
if (event.getSender() instanceof BlockCommandSender) {
if (Main.consoleCommandBlacklist.contains(arr[0].toLowerCase())) {
event.setCancelled(true);
}
if (isConsoleCommand) {
commandName = "/" + arr[0].toLowerCase();
}
if (("minecraft:execute".equals(arr[0].toLowerCase()) ||
"execute".equals(arr[0].toLowerCase())) &&
arr.length >= 2) {
switch (commandName) {
case "/minecraft:execute":
case "/execute":
if (arr.length >= 2) {
int asAtCount = 0;
for (int i = 1; i < arr.length; i++) {
if ("as".equalsIgnoreCase(arr[i]) ||
"at".equalsIgnoreCase(arr[i])) {
for (int i2 = i+1; i2 < arr.length; i2++) {
if ("at".equalsIgnoreCase(arr[i2]) ||
"as".equalsIgnoreCase(arr[i2])) {
Command.broadcastCommandMessage(event.getSender(), "Forbidden execute pattern detected");
event.setCancelled(true);
break;
}
}
} else if (i+1 < arr.length &&
"run".equalsIgnoreCase(arr[i])) {
if ("run".equalsIgnoreCase(arr[i])) {
if (i+1 < arr.length) {
if ("execute".equalsIgnoreCase(arr[i+1]) ||
"particle".equalsIgnoreCase(arr[i+1]) ||
"save-off".equalsIgnoreCase(arr[i+1]) ||
"stop".equalsIgnoreCase(arr[i+1])) {
Command.broadcastCommandMessage(event.getSender(), "Forbidden execute command detected");
event.setCancelled(true);
break;
Command.broadcastCommandMessage(sender, "Forbidden execute command detected");
} else if (i+3 < arr.length &&
"gamerule".equalsIgnoreCase(arr[i+1])) {
if ("randomTickSpeed".equalsIgnoreCase(arr[i+2]) &&
Double.parseDouble(arr[i+3]) > 6) {
event.setCommand(command.replaceFirst("(?i)" + "randomTickSpeed " + arr[i+3], "randomTickSpeed 6"));
break;
return command.replaceFirst("(?i)" + "randomTickSpeed " + arr[i+3], "randomTickSpeed 6");
} else if ("spawnRadius".equalsIgnoreCase(arr[i+2]) &&
Double.parseDouble(arr[i+3]) > 100) {
event.setCommand(command.replaceFirst("(?i)" + "spawnRadius " + arr[i+3], "spawnRadius 100"));
return command.replaceFirst("(?i)" + "spawnRadius " + arr[i+3], "spawnRadius 100");
}
} else if (i+5 < arr.length &&
"spreadplayers".equalsIgnoreCase(arr[i+1])) {
if (Double.parseDouble(arr[i+5]) > 75) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i2 = 0; i2 < i+5; i2++) {
stringBuilder.append(arr[i2]).append(" ");
}
stringBuilder.append("75 ");
for (int i2 = i+6; i2 < arr.length; i2++) {
stringBuilder.append(arr[i2]).append(" ");
}
return stringBuilder.toString();
}
}
}
break;
}
} else if (("minecraft:gamerule".equalsIgnoreCase(arr[0]) ||
"gamerule".equalsIgnoreCase(arr[0])) &&
arr.length >= 3) {
if ("as".equalsIgnoreCase(arr[i]) ||
"at".equalsIgnoreCase(arr[i])) {
asAtCount++;
}
}
if (asAtCount >= 2) {
Command.broadcastCommandMessage(sender, "Forbidden execute pattern detected");
}
}
break;
case "/minecraft:gamerule":
case "/gamerule":
if (arr.length >= 3) {
if ("randomTickSpeed".equalsIgnoreCase(arr[1]) &&
Double.parseDouble(arr[2]) > 6) {
event.setCommand(command.replaceFirst(arr[2], "6"));
return command.replaceFirst(arr[2], "6");
} else if ("spawnRadius".equalsIgnoreCase(arr[1]) &&
Double.parseDouble(arr[2]) > 100) {
event.setCommand(command.replaceFirst(arr[2], "100"));
return command.replaceFirst(arr[2], "100");
}
} else if (("minecraft:particle".equalsIgnoreCase(arr[0]) ||
"particle".equalsIgnoreCase(arr[0])) &&
arr.length >= 10) {
}
break;
case "/minecraft:particle":
case "/particle":
if (arr.length >= 10) {
if (Double.parseDouble(arr[9]) > 10) {
final StringBuilder stringBuilder = new StringBuilder();
@ -86,31 +104,78 @@ class ServerCommand implements Listener {
stringBuilder.append(arr[i]).append(" ");
}
event.setCommand(stringBuilder.toString());
return stringBuilder.toString();
}
} else if ("bukkit:reload".equalsIgnoreCase(arr[0]) ||
"bukkit:rl".equalsIgnoreCase(arr[0]) ||
"reload".equalsIgnoreCase(arr[0]) ||
"rl".equalsIgnoreCase(arr[0])) {
if (arr.length >= 2 &&
}
break;
case "/bukkit:reload":
case "/bukkit:rl":
case "/reload":
case "/rl":
if (sender.hasPermission("bukkit.command.reload") &&
arr.length >= 2 &&
"confirm".equalsIgnoreCase(arr[1])) {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getSender(), ChatColor.RED + "Please note that this command is not supported and may cause issues when using some plugins.");
Command.broadcastCommandMessage(event.getSender(), ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server.");
Command.broadcastCommandMessage(event.getSender(), ChatColor.GREEN + "Reload complete.");
Command.broadcastCommandMessage(sender, ChatColor.RED + "Please note that this command is not supported and may cause issues when using some plugins.");
Command.broadcastCommandMessage(sender, ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server.");
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Reload complete.");
}
} else if ("restart".equalsIgnoreCase(arr[0]) ||
"spigot:restart".equalsIgnoreCase(arr[0])) {
break;
case "/spigot:restart":
case "/restart":
if (sender.hasPermission("bukkit.command.restart")) {
}
break;
case "/minecraft:save-off":
case "/save-off":
if (sender.hasPermission("minecraft.command.save.disable")) {
Command.broadcastCommandMessage(sender, "Automatic saving is now disabled");
}
break;
case "/minecraft:spreadplayers":
case "/spreadplayers":
if (arr.length >= 5 &&
Double.parseDouble(arr[4]) > 75) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 4; i++) {
stringBuilder.append(arr[i]).append(" ");
}
stringBuilder.append("75 ");
for (int i = 5; i < arr.length; i++) {
stringBuilder.append(arr[i]).append(" ");
}
return stringBuilder.toString();
}
break;
case "/minecraft:stop":
case "/stop":
if (sender.hasPermission("minecraft.command.stop")) {
Command.broadcastCommandMessage(sender, "Stopping the server");
}
}
return null;
}
@EventHandler
void onServerCommand(ServerCommandEvent event) {
final CommandSender sender = event.getSender();
final String[] arr = event.getCommand().split(" ");
if (sender instanceof BlockCommandSender) {
if (Main.consoleCommandBlacklist.contains(arr[0].toLowerCase())) {
event.setCancelled(true);
} else if ("minecraft:save-off".equalsIgnoreCase(arr[0]) ||
"save-off".equalsIgnoreCase(arr[0])) {
}
}
final String command = event.getCommand();
final boolean isConsoleCommand = true;
final String checkedCommand = checkCommand(sender, command, isConsoleCommand);
if (checkedCommand != null) {
event.setCommand(checkedCommand);
} else {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getSender(), "Automatic saving is now disabled");
} else if (("minecraft:stop".equalsIgnoreCase(arr[0]) ||
"stop".equalsIgnoreCase(arr[0])) &&
!(event.getSender() instanceof BlockCommandSender)) {
event.setCancelled(true);
Command.broadcastCommandMessage(event.getSender(), "Stopping the server");
}
}
}