| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.nbt.NBTTagCompound;
|
| 5 | import net.minecraft.scoreboard.Score;
|
| 6 | import net.minecraft.scoreboard.ScoreObjective;
|
| 7 | import net.minecraft.scoreboard.Scoreboard;
|
| 8 | import net.minecraft.util.BlockPos;
|
| 9 | import net.minecraft.util.IChatComponent;
|
| 10 | import net.minecraft.util.Vec3;
|
| 11 | import net.minecraft.world.World;
|
| 12 |
|
| 13 | public class CommandResultStats
|
| 14 | {
|
| 15 | /** The number of result command result types that are possible. */
|
| 16 | private static final int NUM_RESULT_TYPES = CommandResultStats.Type.values().length;
|
| 17 | private static final String[] STRING_RESULT_TYPES = new String[NUM_RESULT_TYPES];
|
| 18 |
|
| 19 | /**
|
| 20 | * List of entityID who set a stat, username for a player, UUID for all entities
|
| 21 | */
|
| 22 | private String[] entitiesID;
|
| 23 |
|
| 24 | /** List of all the Objectives names */
|
| 25 | private String[] objectives;
|
| 26 |
|
| 27 | public CommandResultStats()
|
| 28 | {
|
| 29 | this.entitiesID = STRING_RESULT_TYPES;
|
| 30 | this.objectives = STRING_RESULT_TYPES;
|
| 31 | }
|
| 32 |
|
| 33 | /**
|
| 34 | * Set the score on the ScoreBoard
|
| 35 | *
|
| 36 | * @param scorePoint The score to set to the score board
|
| 37 | */
|
| 38 | public void setCommandStatScore(final ICommandSender sender, CommandResultStats.Type resultTypeIn, int scorePoint)
|
| 39 | {
|
| 40 | String s = this.entitiesID[resultTypeIn.getTypeID()];
|
| 41 |
|
| 42 | if (s != null)
|
| 43 | {
|
| 44 | ICommandSender icommandsender = new ICommandSender()
|
| 45 | {
|
| 46 | public String getName()
|
| 47 | {
|
| 48 | return sender.getName();
|
| 49 | }
|
| 50 | public IChatComponent getDisplayName()
|
| 51 | {
|
| 52 | return sender.getDisplayName();
|
| 53 | }
|
| 54 | public void addChatMessage(IChatComponent component)
|
| 55 | {
|
| 56 | sender.addChatMessage(component);
|
| 57 | }
|
| 58 | public boolean canCommandSenderUseCommand(int permLevel, String commandName)
|
| 59 | {
|
| 60 | return true;
|
| 61 | }
|
| 62 | public BlockPos getPosition()
|
| 63 | {
|
| 64 | return sender.getPosition();
|
| 65 | }
|
| 66 | public Vec3 getPositionVector()
|
| 67 | {
|
| 68 | return sender.getPositionVector();
|
| 69 | }
|
| 70 | public World getEntityWorld()
|
| 71 | {
|
| 72 | return sender.getEntityWorld();
|
| 73 | }
|
| 74 | public Entity getCommandSenderEntity()
|
| 75 | {
|
| 76 | return sender.getCommandSenderEntity();
|
| 77 | }
|
| 78 | public boolean sendCommandFeedback()
|
| 79 | {
|
| 80 | return sender.sendCommandFeedback();
|
| 81 | }
|
| 82 | public void setCommandStat(CommandResultStats.Type type, int amount)
|
| 83 | {
|
| 84 | sender.setCommandStat(type, amount);
|
| 85 | }
|
| 86 | };
|
| 87 | String s1;
|
| 88 |
|
| 89 | try
|
| 90 | {
|
| 91 | s1 = CommandBase.getEntityName(icommandsender, s);
|
| 92 | }
|
| 93 | catch (EntityNotFoundException var11)
|
| 94 | {
|
| 95 | return;
|
| 96 | }
|
| 97 |
|
| 98 | String s2 = this.objectives[resultTypeIn.getTypeID()];
|
| 99 |
|
| 100 | if (s2 != null)
|
| 101 | {
|
| 102 | Scoreboard scoreboard = sender.getEntityWorld().getScoreboard();
|
| 103 | ScoreObjective scoreobjective = scoreboard.getObjective(s2);
|
| 104 |
|
| 105 | if (scoreobjective != null)
|
| 106 | {
|
| 107 | if (scoreboard.entityHasObjective(s1, scoreobjective))
|
| 108 | {
|
| 109 | Score score = scoreboard.getValueFromObjective(s1, scoreobjective);
|
| 110 | score.setScorePoints(scorePoint);
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | public void readStatsFromNBT(NBTTagCompound tagcompound)
|
| 118 | {
|
| 119 | if (tagcompound.hasKey("CommandStats", 10))
|
| 120 | {
|
| 121 | NBTTagCompound nbttagcompound = tagcompound.getCompoundTag("CommandStats");
|
| 122 |
|
| 123 | for (CommandResultStats.Type commandresultstats$type : CommandResultStats.Type.values())
|
| 124 | {
|
| 125 | String s = commandresultstats$type.getTypeName() + "Name";
|
| 126 | String s1 = commandresultstats$type.getTypeName() + "Objective";
|
| 127 |
|
| 128 | if (nbttagcompound.hasKey(s, 8) && nbttagcompound.hasKey(s1, 8))
|
| 129 | {
|
| 130 | String s2 = nbttagcompound.getString(s);
|
| 131 | String s3 = nbttagcompound.getString(s1);
|
| 132 | setScoreBoardStat(this, commandresultstats$type, s2, s3);
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public void writeStatsToNBT(NBTTagCompound tagcompound)
|
| 139 | {
|
| 140 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 141 |
|
| 142 | for (CommandResultStats.Type commandresultstats$type : CommandResultStats.Type.values())
|
| 143 | {
|
| 144 | String s = this.entitiesID[commandresultstats$type.getTypeID()];
|
| 145 | String s1 = this.objectives[commandresultstats$type.getTypeID()];
|
| 146 |
|
| 147 | if (s != null && s1 != null)
|
| 148 | {
|
| 149 | nbttagcompound.setString(commandresultstats$type.getTypeName() + "Name", s);
|
| 150 | nbttagcompound.setString(commandresultstats$type.getTypeName() + "Objective", s1);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | if (!nbttagcompound.hasNoTags())
|
| 155 | {
|
| 156 | tagcompound.setTag("CommandStats", nbttagcompound);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | /**
|
| 161 | * Set a stat in the scoreboard
|
| 162 | *
|
| 163 | * @param entityID The username of the player or the UUID of an Entity
|
| 164 | * @param objectiveName The name of the Objective
|
| 165 | */
|
| 166 | public static void setScoreBoardStat(CommandResultStats stats, CommandResultStats.Type resultType, String entityID, String objectiveName)
|
| 167 | {
|
| 168 | if (entityID != null && entityID.length() != 0 && objectiveName != null && objectiveName.length() != 0)
|
| 169 | {
|
| 170 | if (stats.entitiesID == STRING_RESULT_TYPES || stats.objectives == STRING_RESULT_TYPES)
|
| 171 | {
|
| 172 | stats.entitiesID = new String[NUM_RESULT_TYPES];
|
| 173 | stats.objectives = new String[NUM_RESULT_TYPES];
|
| 174 | }
|
| 175 |
|
| 176 | stats.entitiesID[resultType.getTypeID()] = entityID;
|
| 177 | stats.objectives[resultType.getTypeID()] = objectiveName;
|
| 178 | }
|
| 179 | else
|
| 180 | {
|
| 181 | removeScoreBoardStat(stats, resultType);
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | /**
|
| 186 | * Remove a stat from the scoreboard
|
| 187 | */
|
| 188 | private static void removeScoreBoardStat(CommandResultStats resultStatsIn, CommandResultStats.Type resultTypeIn)
|
| 189 | {
|
| 190 | if (resultStatsIn.entitiesID != STRING_RESULT_TYPES && resultStatsIn.objectives != STRING_RESULT_TYPES)
|
| 191 | {
|
| 192 | resultStatsIn.entitiesID[resultTypeIn.getTypeID()] = null;
|
| 193 | resultStatsIn.objectives[resultTypeIn.getTypeID()] = null;
|
| 194 | boolean flag = true;
|
| 195 |
|
| 196 | for (CommandResultStats.Type commandresultstats$type : CommandResultStats.Type.values())
|
| 197 | {
|
| 198 | if (resultStatsIn.entitiesID[commandresultstats$type.getTypeID()] != null && resultStatsIn.objectives[commandresultstats$type.getTypeID()] != null)
|
| 199 | {
|
| 200 | flag = false;
|
| 201 | break;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | if (flag)
|
| 206 | {
|
| 207 | resultStatsIn.entitiesID = STRING_RESULT_TYPES;
|
| 208 | resultStatsIn.objectives = STRING_RESULT_TYPES;
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | /**
|
| 214 | * Add all stats in the CommandResultStats
|
| 215 | */
|
| 216 | public void addAllStats(CommandResultStats resultStatsIn)
|
| 217 | {
|
| 218 | for (CommandResultStats.Type commandresultstats$type : CommandResultStats.Type.values())
|
| 219 | {
|
| 220 | setScoreBoardStat(this, commandresultstats$type, resultStatsIn.entitiesID[commandresultstats$type.getTypeID()], resultStatsIn.objectives[commandresultstats$type.getTypeID()]);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | public static enum Type
|
| 225 | {
|
| 226 | SUCCESS_COUNT(0, "SuccessCount"),
|
| 227 | AFFECTED_BLOCKS(1, "AffectedBlocks"),
|
| 228 | AFFECTED_ENTITIES(2, "AffectedEntities"),
|
| 229 | AFFECTED_ITEMS(3, "AffectedItems"),
|
| 230 | QUERY_RESULT(4, "QueryResult");
|
| 231 |
|
| 232 | final int typeID;
|
| 233 | final String typeName;
|
| 234 |
|
| 235 | private Type(int id, String name)
|
| 236 | {
|
| 237 | this.typeID = id;
|
| 238 | this.typeName = name;
|
| 239 | }
|
| 240 |
|
| 241 | public int getTypeID()
|
| 242 | {
|
| 243 | return this.typeID;
|
| 244 | }
|
| 245 |
|
| 246 | public String getTypeName()
|
| 247 | {
|
| 248 | return this.typeName;
|
| 249 | }
|
| 250 |
|
| 251 | public static String[] getTypeNames()
|
| 252 | {
|
| 253 | String[] astring = new String[values().length];
|
| 254 | int i = 0;
|
| 255 |
|
| 256 | for (CommandResultStats.Type commandresultstats$type : values())
|
| 257 | {
|
| 258 | astring[i++] = commandresultstats$type.getTypeName();
|
| 259 | }
|
| 260 |
|
| 261 | return astring;
|
| 262 | }
|
| 263 |
|
| 264 | public static CommandResultStats.Type getTypeByName(String name)
|
| 265 | {
|
| 266 | for (CommandResultStats.Type commandresultstats$type : values())
|
| 267 | {
|
| 268 | if (commandresultstats$type.getTypeName().equals(name))
|
| 269 | {
|
| 270 | return commandresultstats$type;
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | return null;
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 |
|