mirror of
https://github.com/barkeser2002/CatServer.git
synced 2026-09-25 05:19:55 +03:00
240 lines
11 KiB
Diff
240 lines
11 KiB
Diff
--- ../src-base/minecraft/net/minecraft/tileentity/CommandBlockBaseLogic.java
|
|
+++ ../src-work/minecraft/net/minecraft/tileentity/CommandBlockBaseLogic.java
|
|
@@ -1,23 +1,41 @@
|
|
package net.minecraft.tileentity;
|
|
|
|
+import com.google.common.base.Joiner;
|
|
import io.netty.buffer.ByteBuf;
|
|
import java.text.SimpleDateFormat;
|
|
+import java.util.ArrayList;
|
|
import java.util.Date;
|
|
+import java.util.logging.Level;
|
|
import javax.annotation.Nullable;
|
|
+
|
|
+import net.minecraft.advancements.FunctionManager;
|
|
+import net.minecraft.command.CommandException;
|
|
import net.minecraft.command.CommandResultStats;
|
|
+import net.minecraft.command.CommandSenderWrapper;
|
|
+import net.minecraft.command.EntitySelector;
|
|
+import net.minecraft.command.ICommandListener;
|
|
import net.minecraft.command.ICommandSender;
|
|
import net.minecraft.crash.CrashReport;
|
|
import net.minecraft.crash.CrashReportCategory;
|
|
import net.minecraft.crash.ICrashReportDetail;
|
|
+import net.minecraft.entity.item.EntityMinecartCommandBlock;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
+import net.minecraft.network.rcon.RConConsoleSource;
|
|
import net.minecraft.server.MinecraftServer;
|
|
+import net.minecraft.server.dedicated.DedicatedServer;
|
|
import net.minecraft.util.ReportedException;
|
|
import net.minecraft.util.text.ITextComponent;
|
|
import net.minecraft.util.text.TextComponentString;
|
|
+import net.minecraft.util.text.TextComponentTranslation;
|
|
+import net.minecraft.util.text.TextFormatting;
|
|
import net.minecraft.world.World;
|
|
+import net.minecraft.world.WorldServer;
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
+import org.bukkit.command.CommandSender;
|
|
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
|
|
+import org.bukkit.event.server.ServerCommandEvent;
|
|
|
|
public abstract class CommandBlockBaseLogic implements ICommandSender
|
|
{
|
|
@@ -30,6 +48,7 @@
|
|
private String commandStored = "";
|
|
private String customName = "@";
|
|
private final CommandResultStats resultStats = new CommandResultStats();
|
|
+ protected org.bukkit.command.CommandSender sender = new org.bukkit.craftbukkit.command.CraftBlockCommandSender(this);
|
|
|
|
public int getSuccessCount()
|
|
{
|
|
@@ -152,7 +171,8 @@
|
|
try
|
|
{
|
|
this.lastOutput = null;
|
|
- this.successCount = minecraftserver.getCommandManager().executeCommand(this, this.commandStored);
|
|
+// this.successCount = minecraftserver.getCommandManager().executeCommand(this, this.commandStored);
|
|
+ this.successCount = executeSafely(this, sender, this.commandStored);
|
|
}
|
|
catch (Throwable throwable)
|
|
{
|
|
@@ -198,6 +218,176 @@
|
|
}
|
|
}
|
|
|
|
+ public static int executeSafely(ICommandSender sender, org.bukkit.command.CommandSender bSender, String command) {
|
|
+ try {
|
|
+ return executeCommand(sender, bSender, command);
|
|
+ } catch (CommandException commandexception) {
|
|
+ // Taken from CommandHandler
|
|
+ TextComponentTranslation chatmessage = new TextComponentTranslation(commandexception.getMessage(), commandexception.getErrorObjects());
|
|
+ chatmessage.getStyle().setColor(TextFormatting.RED);
|
|
+ sender.sendMessage(chatmessage);
|
|
+ }
|
|
+
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ public static int executeCommand(ICommandSender sender, org.bukkit.command.CommandSender bSender, String command) throws CommandException {
|
|
+ org.bukkit.command.SimpleCommandMap commandMap = sender.getEntityWorld().getServer().getCommandMap();
|
|
+ Joiner joiner = Joiner.on(" ");
|
|
+ if (command.startsWith("/")) {
|
|
+ command = command.substring(1);
|
|
+ }
|
|
+
|
|
+ ServerCommandEvent event = new ServerCommandEvent(bSender, command);
|
|
+ org.bukkit.Bukkit.getPluginManager().callEvent(event);
|
|
+ if (event.isCancelled()) {
|
|
+ return 0;
|
|
+ }
|
|
+ command = event.getCommand();
|
|
+
|
|
+ String[] args = command.split(" ");
|
|
+ ArrayList<String[]> commands = new ArrayList<String[]>();
|
|
+
|
|
+ String cmd = args[0];
|
|
+ if (cmd.startsWith("minecraft:")) cmd = cmd.substring("minecraft:".length());
|
|
+ if (cmd.startsWith("bukkit:")) cmd = cmd.substring("bukkit:".length());
|
|
+
|
|
+ // Block disallowed commands
|
|
+ if (cmd.equalsIgnoreCase("stop") || cmd.equalsIgnoreCase("kick") || cmd.equalsIgnoreCase("op")
|
|
+ || cmd.equalsIgnoreCase("deop") || cmd.equalsIgnoreCase("ban") || cmd.equalsIgnoreCase("ban-ip")
|
|
+ || cmd.equalsIgnoreCase("pardon") || cmd.equalsIgnoreCase("pardon-ip") || cmd.equalsIgnoreCase("reload")) {
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ // Handle vanilla commands;
|
|
+ org.bukkit.command.Command commandBlockCommand = commandMap.getCommand(args[0]);
|
|
+ if (sender.getEntityWorld().getServer().getCommandBlockOverride(args[0])) {
|
|
+ commandBlockCommand = commandMap.getCommand("minecraft:" + args[0]);
|
|
+ }
|
|
+ if (commandBlockCommand instanceof VanillaCommandWrapper) {
|
|
+ command = command.trim();
|
|
+ if (command.startsWith("/")) {
|
|
+ command = command.substring(1);
|
|
+ }
|
|
+ String as[] = command.split(" ");
|
|
+ as = VanillaCommandWrapper.dropFirstArgument(as);
|
|
+ if (!sender.getEntityWorld().getServer().getPermissionOverride(sender) && !((VanillaCommandWrapper) commandBlockCommand).testPermission(bSender)) {
|
|
+ return 0;
|
|
+ }
|
|
+ return ((VanillaCommandWrapper) commandBlockCommand).dispatchVanillaCommand(bSender, sender, as);
|
|
+ }
|
|
+
|
|
+ // Make sure this is a valid command
|
|
+ if (commandMap.getCommand(args[0]) == null) {
|
|
+ // CatServer start - try to handle mod commands
|
|
+ commandMap = sender.getEntityWorld().getServer().getCraftCommandMap();
|
|
+ commandBlockCommand = commandMap.getCommand(args[0]);
|
|
+ if (commandBlockCommand == null) {
|
|
+ return 0;
|
|
+ }
|
|
+ // CatServer end
|
|
+ }
|
|
+
|
|
+ commands.add(args);
|
|
+
|
|
+ // Find positions of command block syntax, if any
|
|
+ WorldServer[] prev = MinecraftServer.getServerInst().worlds;
|
|
+ MinecraftServer server = MinecraftServer.getServerInst();
|
|
+ server.worlds = new WorldServer[server.worldServerList.size()];
|
|
+ server.worlds[0] = (WorldServer) sender.getEntityWorld();
|
|
+ int bpos = 0;
|
|
+ for (int pos = 1; pos < server.worlds.length; pos++) {
|
|
+ WorldServer world = server.worldServerList.get(bpos++);
|
|
+ if (server.worlds[0] == world) {
|
|
+ pos--;
|
|
+ continue;
|
|
+ }
|
|
+ server.worlds[pos] = world;
|
|
+ }
|
|
+ try {
|
|
+ ArrayList<String[]> newCommands = new ArrayList<String[]>();
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
+ if (EntitySelector.isSelectorDefault(args[i])) {
|
|
+ for (int j = 0; j < commands.size(); j++) {
|
|
+ newCommands.addAll(buildCommands(sender, commands.get(j), i));
|
|
+ }
|
|
+ ArrayList<String[]> temp = commands;
|
|
+ commands = newCommands;
|
|
+ newCommands = temp;
|
|
+ newCommands.clear();
|
|
+ }
|
|
+ }
|
|
+ } finally {
|
|
+ MinecraftServer.getServerInst().worlds = prev;
|
|
+ }
|
|
+
|
|
+ int completed = 0;
|
|
+
|
|
+ // Now dispatch all of the commands we ended up with
|
|
+ for (int i = 0; i < commands.size(); i++) {
|
|
+ try {
|
|
+ if (commandMap.dispatch(bSender, joiner.join(java.util.Arrays.asList(commands.get(i))))) {
|
|
+ completed++;
|
|
+ }
|
|
+ } catch (Throwable exception) {
|
|
+ if (sender.getCommandSenderEntity() instanceof EntityMinecartCommandBlock) {
|
|
+ MinecraftServer.getServerInst().server.getLogger().log(Level.WARNING, String.format("MinecartCommandBlock at (%d,%d,%d) failed to handle command", sender.getPosition().getX(), sender.getPosition().getY(), sender.getPosition().getZ()), exception);
|
|
+ } else if (sender instanceof CommandBlockBaseLogic) {
|
|
+ CommandBlockBaseLogic listener = (CommandBlockBaseLogic) sender;
|
|
+ MinecraftServer.getServerInst().server.getLogger().log(Level.WARNING, String.format("CommandBlock at (%d,%d,%d) failed to handle command", listener.getPosition().getX(), listener.getPosition().getY(), listener.getPosition().getZ()), exception);
|
|
+ } else {
|
|
+ MinecraftServer.getServerInst().server.getLogger().log(Level.WARNING, String.format("Unknown CommandBlock failed to handle command"), exception);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return completed;
|
|
+ }
|
|
+
|
|
+ private static ArrayList<String[]> buildCommands(ICommandSender sender, String[] args, int pos) throws CommandException {
|
|
+ ArrayList<String[]> commands = new ArrayList<String[]>();
|
|
+ java.util.List<EntityPlayer> players = (java.util.List<EntityPlayer>) EntitySelector.matchEntitiesDefault(sender, args[pos], EntityPlayer.class);
|
|
+
|
|
+ if (players != null) {
|
|
+ for (EntityPlayer player : players) {
|
|
+ if (player.world != sender.getEntityWorld()) {
|
|
+ continue;
|
|
+ }
|
|
+ String[] command = args.clone();
|
|
+ command[pos] = player.getName();
|
|
+ commands.add(command);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return commands;
|
|
+ }
|
|
+
|
|
+ public static CommandSender unwrapSender(ICommandSender listener) {
|
|
+ org.bukkit.command.CommandSender sender = null;
|
|
+ // CatServer start
|
|
+ if (listener instanceof DedicatedServer) {
|
|
+ sender = ((DedicatedServer) listener).console;
|
|
+ } else if (listener instanceof RConConsoleSource) {
|
|
+ sender = new org.bukkit.craftbukkit.command.CraftRemoteConsoleCommandSender((RConConsoleSource) listener);
|
|
+ } else if (listener instanceof CommandBlockBaseLogic) {
|
|
+ sender = ((CommandBlockBaseLogic) listener).sender;
|
|
+ } else if (listener instanceof FunctionManager.CustomFunctionListener) {
|
|
+ sender = ((FunctionManager.CustomFunctionListener) listener).sender;
|
|
+ } else if (listener instanceof CommandSenderWrapper) {
|
|
+ sender = unwrapSender(((CommandSenderWrapper) listener).delegate); // Search deeper
|
|
+ } else if (VanillaCommandWrapper.lastSender != null) {
|
|
+ sender = VanillaCommandWrapper.lastSender;
|
|
+ } else if (listener.getCommandSenderEntity() != null) {
|
|
+ sender = listener.getCommandSenderEntity().getBukkitEntity();
|
|
+ } else if (listener instanceof net.minecraft.entity.Entity) {
|
|
+ sender = ((net.minecraft.entity.Entity) sender).getBukkitEntity();
|
|
+ } else {
|
|
+ throw new RuntimeException("Unhandled executor " + listener.getClass().getSimpleName());
|
|
+ }
|
|
+ // CatServer end
|
|
+ return sender;
|
|
+ }
|
|
+
|
|
public String getName()
|
|
{
|
|
return this.customName;
|