| 1 | package net.minecraft.command.server;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.text.SimpleDateFormat;
|
| 5 | import java.util.Date;
|
| 6 | import java.util.concurrent.Callable;
|
| 7 | import net.minecraft.command.CommandResultStats;
|
| 8 | import net.minecraft.command.ICommandManager;
|
| 9 | import net.minecraft.command.ICommandSender;
|
| 10 | import net.minecraft.crash.CrashReport;
|
| 11 | import net.minecraft.crash.CrashReportCategory;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.nbt.NBTTagCompound;
|
| 14 | import net.minecraft.server.MinecraftServer;
|
| 15 | import net.minecraft.util.ChatComponentText;
|
| 16 | import net.minecraft.util.IChatComponent;
|
| 17 | import net.minecraft.util.ReportedException;
|
| 18 | import net.minecraft.world.World;
|
| 19 |
|
| 20 | public abstract class CommandBlockLogic implements ICommandSender
|
| 21 | {
|
| 22 | /** The formatting for the timestamp on commands run. */
|
| 23 | private static final SimpleDateFormat timestampFormat = new SimpleDateFormat("HH:mm:ss");
|
| 24 |
|
| 25 | /** The number of successful commands run. (used for redstone output) */
|
| 26 | private int successCount;
|
| 27 | private boolean trackOutput = true;
|
| 28 |
|
| 29 | /** The previously run command. */
|
| 30 | private IChatComponent lastOutput = null;
|
| 31 |
|
| 32 | /** The command stored in the command block. */
|
| 33 | private String commandStored = "";
|
| 34 |
|
| 35 | /** The custom name of the command block. (defaults to "@") */
|
| 36 | private String customName = "@";
|
| 37 | private final CommandResultStats resultStats = new CommandResultStats();
|
| 38 |
|
| 39 | /**
|
| 40 | * returns the successCount int.
|
| 41 | */
|
| 42 | public int getSuccessCount()
|
| 43 | {
|
| 44 | return this.successCount;
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * Returns the lastOutput.
|
| 49 | */
|
| 50 | public IChatComponent getLastOutput()
|
| 51 | {
|
| 52 | return this.lastOutput;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * Stores data to NBT format.
|
| 57 | */
|
| 58 | public void writeDataToNBT(NBTTagCompound tagCompound)
|
| 59 | {
|
| 60 | tagCompound.setString("Command", this.commandStored);
|
| 61 | tagCompound.setInteger("SuccessCount", this.successCount);
|
| 62 | tagCompound.setString("CustomName", this.customName);
|
| 63 | tagCompound.setBoolean("TrackOutput", this.trackOutput);
|
| 64 |
|
| 65 | if (this.lastOutput != null && this.trackOutput)
|
| 66 | {
|
| 67 | tagCompound.setString("LastOutput", IChatComponent.Serializer.componentToJson(this.lastOutput));
|
| 68 | }
|
| 69 |
|
| 70 | this.resultStats.writeStatsToNBT(tagCompound);
|
| 71 | }
|
| 72 |
|
| 73 | /**
|
| 74 | * Reads NBT formatting and stored data into variables.
|
| 75 | */
|
| 76 | public void readDataFromNBT(NBTTagCompound nbt)
|
| 77 | {
|
| 78 | this.commandStored = nbt.getString("Command");
|
| 79 | this.successCount = nbt.getInteger("SuccessCount");
|
| 80 |
|
| 81 | if (nbt.hasKey("CustomName", 8))
|
| 82 | {
|
| 83 | this.customName = nbt.getString("CustomName");
|
| 84 | }
|
| 85 |
|
| 86 | if (nbt.hasKey("TrackOutput", 1))
|
| 87 | {
|
| 88 | this.trackOutput = nbt.getBoolean("TrackOutput");
|
| 89 | }
|
| 90 |
|
| 91 | if (nbt.hasKey("LastOutput", 8) && this.trackOutput)
|
| 92 | {
|
| 93 | this.lastOutput = IChatComponent.Serializer.jsonToComponent(nbt.getString("LastOutput"));
|
| 94 | }
|
| 95 |
|
| 96 | this.resultStats.readStatsFromNBT(nbt);
|
| 97 | }
|
| 98 |
|
| 99 | /**
|
| 100 | * Returns {@code true} if the CommandSender is allowed to execute the command, {@code false} if not
|
| 101 | */
|
| 102 | public boolean canCommandSenderUseCommand(int permLevel, String commandName)
|
| 103 | {
|
| 104 | return permLevel <= 2;
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Sets the command.
|
| 109 | */
|
| 110 | public void setCommand(String command)
|
| 111 | {
|
| 112 | this.commandStored = command;
|
| 113 | this.successCount = 0;
|
| 114 | }
|
| 115 |
|
| 116 | /**
|
| 117 | * Returns the command of the command block.
|
| 118 | */
|
| 119 | public String getCommand()
|
| 120 | {
|
| 121 | return this.commandStored;
|
| 122 | }
|
| 123 |
|
| 124 | public void trigger(World worldIn)
|
| 125 | {
|
| 126 | if (worldIn.isRemote)
|
| 127 | {
|
| 128 | this.successCount = 0;
|
| 129 | }
|
| 130 |
|
| 131 | MinecraftServer minecraftserver = MinecraftServer.getServer();
|
| 132 |
|
| 133 | if (minecraftserver != null && minecraftserver.isAnvilFileSet() && minecraftserver.isCommandBlockEnabled())
|
| 134 | {
|
| 135 | ICommandManager icommandmanager = minecraftserver.getCommandManager();
|
| 136 |
|
| 137 | try
|
| 138 | {
|
| 139 | this.lastOutput = null;
|
| 140 | this.successCount = icommandmanager.executeCommand(this, this.commandStored);
|
| 141 | }
|
| 142 | catch (Throwable throwable)
|
| 143 | {
|
| 144 | CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Executing command block");
|
| 145 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Command to be executed");
|
| 146 | crashreportcategory.addCrashSectionCallable("Command", new Callable<String>()
|
| 147 | {
|
| 148 | public String call() throws Exception
|
| 149 | {
|
| 150 | return CommandBlockLogic.this.getCommand();
|
| 151 | }
|
| 152 | });
|
| 153 | crashreportcategory.addCrashSectionCallable("Name", new Callable<String>()
|
| 154 | {
|
| 155 | public String call() throws Exception
|
| 156 | {
|
| 157 | return CommandBlockLogic.this.getName();
|
| 158 | }
|
| 159 | });
|
| 160 | throw new ReportedException(crashreport);
|
| 161 | }
|
| 162 | }
|
| 163 | else
|
| 164 | {
|
| 165 | this.successCount = 0;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | /**
|
| 170 | * Get the name of this object. For players this returns their username
|
| 171 | */
|
| 172 | public String getName()
|
| 173 | {
|
| 174 | return this.customName;
|
| 175 | }
|
| 176 |
|
| 177 | /**
|
| 178 | * Get the formatted ChatComponent that will be used for the sender's username in chat
|
| 179 | */
|
| 180 | public IChatComponent getDisplayName()
|
| 181 | {
|
| 182 | return new ChatComponentText(this.getName());
|
| 183 | }
|
| 184 |
|
| 185 | public void setName(String p_145754_1_)
|
| 186 | {
|
| 187 | this.customName = p_145754_1_;
|
| 188 | }
|
| 189 |
|
| 190 | /**
|
| 191 | * Send a chat message to the CommandSender
|
| 192 | */
|
| 193 | public void addChatMessage(IChatComponent component)
|
| 194 | {
|
| 195 | if (this.trackOutput && this.getEntityWorld() != null && !this.getEntityWorld().isRemote)
|
| 196 | {
|
| 197 | this.lastOutput = (new ChatComponentText("[" + timestampFormat.format(new Date()) + "] ")).appendSibling(component);
|
| 198 | this.updateCommand();
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | /**
|
| 203 | * Returns true if the command sender should be sent feedback about executed commands
|
| 204 | */
|
| 205 | public boolean sendCommandFeedback()
|
| 206 | {
|
| 207 | MinecraftServer minecraftserver = MinecraftServer.getServer();
|
| 208 | return minecraftserver == null || !minecraftserver.isAnvilFileSet() || minecraftserver.worldServers[0].getGameRules().getBoolean("commandBlockOutput");
|
| 209 | }
|
| 210 |
|
| 211 | public void setCommandStat(CommandResultStats.Type type, int amount)
|
| 212 | {
|
| 213 | this.resultStats.setCommandStatScore(this, type, amount);
|
| 214 | }
|
| 215 |
|
| 216 | public abstract void updateCommand();
|
| 217 |
|
| 218 | public abstract int func_145751_f();
|
| 219 |
|
| 220 | public abstract void func_145757_a(ByteBuf p_145757_1_);
|
| 221 |
|
| 222 | public void setLastOutput(IChatComponent lastOutputMessage)
|
| 223 | {
|
| 224 | this.lastOutput = lastOutputMessage;
|
| 225 | }
|
| 226 |
|
| 227 | public void setTrackOutput(boolean shouldTrackOutput)
|
| 228 | {
|
| 229 | this.trackOutput = shouldTrackOutput;
|
| 230 | }
|
| 231 |
|
| 232 | public boolean shouldTrackOutput()
|
| 233 | {
|
| 234 | return this.trackOutput;
|
| 235 | }
|
| 236 |
|
| 237 | public boolean tryOpenEditCommandBlock(EntityPlayer playerIn)
|
| 238 | {
|
| 239 | if (!playerIn.capabilities.isCreativeMode)
|
| 240 | {
|
| 241 | return false;
|
| 242 | }
|
| 243 | else
|
| 244 | {
|
| 245 | if (playerIn.getEntityWorld().isRemote)
|
| 246 | {
|
| 247 | playerIn.openEditCommandBlock(this);
|
| 248 | }
|
| 249 |
|
| 250 | return true;
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | public CommandResultStats getCommandResultStats()
|
| 255 | {
|
| 256 | return this.resultStats;
|
| 257 | }
|
| 258 | }
|
| 259 |
|