| 1 | package net.minecraft.scoreboard;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import net.minecraft.entity.Entity;
|
| 10 | import net.minecraft.entity.player.EntityPlayer;
|
| 11 | import net.minecraft.util.EnumChatFormatting;
|
| 12 |
|
| 13 | public class Scoreboard
|
| 14 | {
|
| 15 | private final Map<String, ScoreObjective> scoreObjectives = Maps.<String, ScoreObjective>newHashMap();
|
| 16 | private final Map<IScoreObjectiveCriteria, List<ScoreObjective>> scoreObjectiveCriterias = Maps.<IScoreObjectiveCriteria, List<ScoreObjective>>newHashMap();
|
| 17 | private final Map<String, Map<ScoreObjective, Score>> entitiesScoreObjectives = Maps.<String, Map<ScoreObjective, Score>>newHashMap();
|
| 18 |
|
| 19 | /** Index 0 is tab menu, 1 is sidebar, and 2 is below name */
|
| 20 | private final ScoreObjective[] objectiveDisplaySlots = new ScoreObjective[19];
|
| 21 | private final Map<String, ScorePlayerTeam> teams = Maps.<String, ScorePlayerTeam>newHashMap();
|
| 22 | private final Map<String, ScorePlayerTeam> teamMemberships = Maps.<String, ScorePlayerTeam>newHashMap();
|
| 23 | private static String[] field_178823_g = null;
|
| 24 |
|
| 25 | /**
|
| 26 | * Returns a ScoreObjective for the objective name
|
| 27 | */
|
| 28 | public ScoreObjective getObjective(String name)
|
| 29 | {
|
| 30 | return (ScoreObjective)this.scoreObjectives.get(name);
|
| 31 | }
|
| 32 |
|
| 33 | /**
|
| 34 | * Create and returns the score objective for the given name and ScoreCriteria
|
| 35 | */
|
| 36 | public ScoreObjective addScoreObjective(String name, IScoreObjectiveCriteria criteria)
|
| 37 | {
|
| 38 | if (name.length() > 16)
|
| 39 | {
|
| 40 | throw new IllegalArgumentException("The objective name \'" + name + "\' is too long!");
|
| 41 | }
|
| 42 | else
|
| 43 | {
|
| 44 | ScoreObjective scoreobjective = this.getObjective(name);
|
| 45 |
|
| 46 | if (scoreobjective != null)
|
| 47 | {
|
| 48 | throw new IllegalArgumentException("An objective with the name \'" + name + "\' already exists!");
|
| 49 | }
|
| 50 | else
|
| 51 | {
|
| 52 | scoreobjective = new ScoreObjective(this, name, criteria);
|
| 53 | List<ScoreObjective> list = (List)this.scoreObjectiveCriterias.get(criteria);
|
| 54 |
|
| 55 | if (list == null)
|
| 56 | {
|
| 57 | list = Lists.<ScoreObjective>newArrayList();
|
| 58 | this.scoreObjectiveCriterias.put(criteria, list);
|
| 59 | }
|
| 60 |
|
| 61 | list.add(scoreobjective);
|
| 62 | this.scoreObjectives.put(name, scoreobjective);
|
| 63 | this.onScoreObjectiveAdded(scoreobjective);
|
| 64 | return scoreobjective;
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public Collection<ScoreObjective> getObjectivesFromCriteria(IScoreObjectiveCriteria criteria)
|
| 70 | {
|
| 71 | Collection<ScoreObjective> collection = (Collection)this.scoreObjectiveCriterias.get(criteria);
|
| 72 | return collection == null ? Lists.<ScoreObjective>newArrayList() : Lists.newArrayList(collection);
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Returns if the entity has the given ScoreObjective
|
| 77 | */
|
| 78 | public boolean entityHasObjective(String name, ScoreObjective p_178819_2_)
|
| 79 | {
|
| 80 | Map<ScoreObjective, Score> map = (Map)this.entitiesScoreObjectives.get(name);
|
| 81 |
|
| 82 | if (map == null)
|
| 83 | {
|
| 84 | return false;
|
| 85 | }
|
| 86 | else
|
| 87 | {
|
| 88 | Score score = (Score)map.get(p_178819_2_);
|
| 89 | return score != null;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Returns the value of the given objective for the given entity name
|
| 95 | */
|
| 96 | public Score getValueFromObjective(String name, ScoreObjective objective)
|
| 97 | {
|
| 98 | if (name.length() > 40)
|
| 99 | {
|
| 100 | throw new IllegalArgumentException("The player name \'" + name + "\' is too long!");
|
| 101 | }
|
| 102 | else
|
| 103 | {
|
| 104 | Map<ScoreObjective, Score> map = (Map)this.entitiesScoreObjectives.get(name);
|
| 105 |
|
| 106 | if (map == null)
|
| 107 | {
|
| 108 | map = Maps.<ScoreObjective, Score>newHashMap();
|
| 109 | this.entitiesScoreObjectives.put(name, map);
|
| 110 | }
|
| 111 |
|
| 112 | Score score = (Score)map.get(objective);
|
| 113 |
|
| 114 | if (score == null)
|
| 115 | {
|
| 116 | score = new Score(this, objective, name);
|
| 117 | map.put(objective, score);
|
| 118 | }
|
| 119 |
|
| 120 | return score;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public Collection<Score> getSortedScores(ScoreObjective objective)
|
| 125 | {
|
| 126 | List<Score> list = Lists.<Score>newArrayList();
|
| 127 |
|
| 128 | for (Map<ScoreObjective, Score> map : this.entitiesScoreObjectives.values())
|
| 129 | {
|
| 130 | Score score = (Score)map.get(objective);
|
| 131 |
|
| 132 | if (score != null)
|
| 133 | {
|
| 134 | list.add(score);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | Collections.sort(list, Score.scoreComparator);
|
| 139 | return list;
|
| 140 | }
|
| 141 |
|
| 142 | public Collection<ScoreObjective> getScoreObjectives()
|
| 143 | {
|
| 144 | return this.scoreObjectives.values();
|
| 145 | }
|
| 146 |
|
| 147 | public Collection<String> getObjectiveNames()
|
| 148 | {
|
| 149 | return this.entitiesScoreObjectives.keySet();
|
| 150 | }
|
| 151 |
|
| 152 | /**
|
| 153 | * Remove the given ScoreObjective for the given Entity name.
|
| 154 | */
|
| 155 | public void removeObjectiveFromEntity(String name, ScoreObjective objective)
|
| 156 | {
|
| 157 | if (objective == null)
|
| 158 | {
|
| 159 | Map<ScoreObjective, Score> map = (Map)this.entitiesScoreObjectives.remove(name);
|
| 160 |
|
| 161 | if (map != null)
|
| 162 | {
|
| 163 | this.func_96516_a(name);
|
| 164 | }
|
| 165 | }
|
| 166 | else
|
| 167 | {
|
| 168 | Map<ScoreObjective, Score> map2 = (Map)this.entitiesScoreObjectives.get(name);
|
| 169 |
|
| 170 | if (map2 != null)
|
| 171 | {
|
| 172 | Score score = (Score)map2.remove(objective);
|
| 173 |
|
| 174 | if (map2.size() < 1)
|
| 175 | {
|
| 176 | Map<ScoreObjective, Score> map1 = (Map)this.entitiesScoreObjectives.remove(name);
|
| 177 |
|
| 178 | if (map1 != null)
|
| 179 | {
|
| 180 | this.func_96516_a(name);
|
| 181 | }
|
| 182 | }
|
| 183 | else if (score != null)
|
| 184 | {
|
| 185 | this.func_178820_a(name, objective);
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | public Collection<Score> getScores()
|
| 192 | {
|
| 193 | Collection<Map<ScoreObjective, Score>> collection = this.entitiesScoreObjectives.values();
|
| 194 | List<Score> list = Lists.<Score>newArrayList();
|
| 195 |
|
| 196 | for (Map<ScoreObjective, Score> map : collection)
|
| 197 | {
|
| 198 | list.addAll(map.values());
|
| 199 | }
|
| 200 |
|
| 201 | return list;
|
| 202 | }
|
| 203 |
|
| 204 | public Map<ScoreObjective, Score> getObjectivesForEntity(String name)
|
| 205 | {
|
| 206 | Map<ScoreObjective, Score> map = (Map)this.entitiesScoreObjectives.get(name);
|
| 207 |
|
| 208 | if (map == null)
|
| 209 | {
|
| 210 | map = Maps.<ScoreObjective, Score>newHashMap();
|
| 211 | }
|
| 212 |
|
| 213 | return map;
|
| 214 | }
|
| 215 |
|
| 216 | public void removeObjective(ScoreObjective p_96519_1_)
|
| 217 | {
|
| 218 | this.scoreObjectives.remove(p_96519_1_.getName());
|
| 219 |
|
| 220 | for (int i = 0; i < 19; ++i)
|
| 221 | {
|
| 222 | if (this.getObjectiveInDisplaySlot(i) == p_96519_1_)
|
| 223 | {
|
| 224 | this.setObjectiveInDisplaySlot(i, (ScoreObjective)null);
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | List<ScoreObjective> list = (List)this.scoreObjectiveCriterias.get(p_96519_1_.getCriteria());
|
| 229 |
|
| 230 | if (list != null)
|
| 231 | {
|
| 232 | list.remove(p_96519_1_);
|
| 233 | }
|
| 234 |
|
| 235 | for (Map<ScoreObjective, Score> map : this.entitiesScoreObjectives.values())
|
| 236 | {
|
| 237 | map.remove(p_96519_1_);
|
| 238 | }
|
| 239 |
|
| 240 | this.onScoreObjectiveRemoved(p_96519_1_);
|
| 241 | }
|
| 242 |
|
| 243 | /**
|
| 244 | * 0 is tab menu, 1 is sidebar, 2 is below name
|
| 245 | */
|
| 246 | public void setObjectiveInDisplaySlot(int p_96530_1_, ScoreObjective p_96530_2_)
|
| 247 | {
|
| 248 | this.objectiveDisplaySlots[p_96530_1_] = p_96530_2_;
|
| 249 | }
|
| 250 |
|
| 251 | /**
|
| 252 | * 0 is tab menu, 1 is sidebar, 2 is below name
|
| 253 | */
|
| 254 | public ScoreObjective getObjectiveInDisplaySlot(int p_96539_1_)
|
| 255 | {
|
| 256 | return this.objectiveDisplaySlots[p_96539_1_];
|
| 257 | }
|
| 258 |
|
| 259 | /**
|
| 260 | * Retrieve the ScorePlayerTeam instance identified by the passed team name
|
| 261 | */
|
| 262 | public ScorePlayerTeam getTeam(String p_96508_1_)
|
| 263 | {
|
| 264 | return (ScorePlayerTeam)this.teams.get(p_96508_1_);
|
| 265 | }
|
| 266 |
|
| 267 | public ScorePlayerTeam createTeam(String name)
|
| 268 | {
|
| 269 | if (name.length() > 16)
|
| 270 | {
|
| 271 | throw new IllegalArgumentException("The team name \'" + name + "\' is too long!");
|
| 272 | }
|
| 273 | else
|
| 274 | {
|
| 275 | ScorePlayerTeam scoreplayerteam = this.getTeam(name);
|
| 276 |
|
| 277 | if (scoreplayerteam != null)
|
| 278 | {
|
| 279 | throw new IllegalArgumentException("A team with the name \'" + name + "\' already exists!");
|
| 280 | }
|
| 281 | else
|
| 282 | {
|
| 283 | scoreplayerteam = new ScorePlayerTeam(this, name);
|
| 284 | this.teams.put(name, scoreplayerteam);
|
| 285 | this.broadcastTeamCreated(scoreplayerteam);
|
| 286 | return scoreplayerteam;
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 |
|
| 291 | /**
|
| 292 | * Removes the team from the scoreboard, updates all player memberships and broadcasts the deletion to all players
|
| 293 | */
|
| 294 | public void removeTeam(ScorePlayerTeam p_96511_1_)
|
| 295 | {
|
| 296 | this.teams.remove(p_96511_1_.getRegisteredName());
|
| 297 |
|
| 298 | for (String s : p_96511_1_.getMembershipCollection())
|
| 299 | {
|
| 300 | this.teamMemberships.remove(s);
|
| 301 | }
|
| 302 |
|
| 303 | this.func_96513_c(p_96511_1_);
|
| 304 | }
|
| 305 |
|
| 306 | /**
|
| 307 | * Adds a player to the given team
|
| 308 | */
|
| 309 | public boolean addPlayerToTeam(String player, String newTeam)
|
| 310 | {
|
| 311 | if (player.length() > 40)
|
| 312 | {
|
| 313 | throw new IllegalArgumentException("The player name \'" + player + "\' is too long!");
|
| 314 | }
|
| 315 | else if (!this.teams.containsKey(newTeam))
|
| 316 | {
|
| 317 | return false;
|
| 318 | }
|
| 319 | else
|
| 320 | {
|
| 321 | ScorePlayerTeam scoreplayerteam = this.getTeam(newTeam);
|
| 322 |
|
| 323 | if (this.getPlayersTeam(player) != null)
|
| 324 | {
|
| 325 | this.removePlayerFromTeams(player);
|
| 326 | }
|
| 327 |
|
| 328 | this.teamMemberships.put(player, scoreplayerteam);
|
| 329 | scoreplayerteam.getMembershipCollection().add(player);
|
| 330 | return true;
|
| 331 | }
|
| 332 | }
|
| 333 |
|
| 334 | public boolean removePlayerFromTeams(String p_96524_1_)
|
| 335 | {
|
| 336 | ScorePlayerTeam scoreplayerteam = this.getPlayersTeam(p_96524_1_);
|
| 337 |
|
| 338 | if (scoreplayerteam != null)
|
| 339 | {
|
| 340 | this.removePlayerFromTeam(p_96524_1_, scoreplayerteam);
|
| 341 | return true;
|
| 342 | }
|
| 343 | else
|
| 344 | {
|
| 345 | return false;
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | /**
|
| 350 | * Removes the given username from the given ScorePlayerTeam. If the player is not on the team then an
|
| 351 | * IllegalStateException is thrown.
|
| 352 | */
|
| 353 | public void removePlayerFromTeam(String p_96512_1_, ScorePlayerTeam p_96512_2_)
|
| 354 | {
|
| 355 | if (this.getPlayersTeam(p_96512_1_) != p_96512_2_)
|
| 356 | {
|
| 357 | throw new IllegalStateException("Player is either on another team or not on any team. Cannot remove from team \'" + p_96512_2_.getRegisteredName() + "\'.");
|
| 358 | }
|
| 359 | else
|
| 360 | {
|
| 361 | this.teamMemberships.remove(p_96512_1_);
|
| 362 | p_96512_2_.getMembershipCollection().remove(p_96512_1_);
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | public Collection<String> getTeamNames()
|
| 367 | {
|
| 368 | return this.teams.keySet();
|
| 369 | }
|
| 370 |
|
| 371 | public Collection<ScorePlayerTeam> getTeams()
|
| 372 | {
|
| 373 | return this.teams.values();
|
| 374 | }
|
| 375 |
|
| 376 | /**
|
| 377 | * Gets the ScorePlayerTeam object for the given username.
|
| 378 | */
|
| 379 | public ScorePlayerTeam getPlayersTeam(String p_96509_1_)
|
| 380 | {
|
| 381 | return (ScorePlayerTeam)this.teamMemberships.get(p_96509_1_);
|
| 382 | }
|
| 383 |
|
| 384 | /**
|
| 385 | * Called when a score objective is added
|
| 386 | */
|
| 387 | public void onScoreObjectiveAdded(ScoreObjective scoreObjectiveIn)
|
| 388 | {
|
| 389 | }
|
| 390 |
|
| 391 | public void onObjectiveDisplayNameChanged(ScoreObjective p_96532_1_)
|
| 392 | {
|
| 393 | }
|
| 394 |
|
| 395 | public void onScoreObjectiveRemoved(ScoreObjective p_96533_1_)
|
| 396 | {
|
| 397 | }
|
| 398 |
|
| 399 | public void func_96536_a(Score p_96536_1_)
|
| 400 | {
|
| 401 | }
|
| 402 |
|
| 403 | public void func_96516_a(String p_96516_1_)
|
| 404 | {
|
| 405 | }
|
| 406 |
|
| 407 | public void func_178820_a(String p_178820_1_, ScoreObjective p_178820_2_)
|
| 408 | {
|
| 409 | }
|
| 410 |
|
| 411 | /**
|
| 412 | * This packet will notify the players that this team is created, and that will register it on the client
|
| 413 | */
|
| 414 | public void broadcastTeamCreated(ScorePlayerTeam playerTeam)
|
| 415 | {
|
| 416 | }
|
| 417 |
|
| 418 | /**
|
| 419 | * This packet will notify the players that this team is updated
|
| 420 | */
|
| 421 | public void sendTeamUpdate(ScorePlayerTeam playerTeam)
|
| 422 | {
|
| 423 | }
|
| 424 |
|
| 425 | public void func_96513_c(ScorePlayerTeam playerTeam)
|
| 426 | {
|
| 427 | }
|
| 428 |
|
| 429 | /**
|
| 430 | * Returns 'list' for 0, 'sidebar' for 1, 'belowName for 2, otherwise null.
|
| 431 | */
|
| 432 | public static String getObjectiveDisplaySlot(int p_96517_0_)
|
| 433 | {
|
| 434 | switch (p_96517_0_)
|
| 435 | {
|
| 436 | case 0:
|
| 437 | return "list";
|
| 438 |
|
| 439 | case 1:
|
| 440 | return "sidebar";
|
| 441 |
|
| 442 | case 2:
|
| 443 | return "belowName";
|
| 444 |
|
| 445 | default:
|
| 446 | if (p_96517_0_ >= 3 && p_96517_0_ <= 18)
|
| 447 | {
|
| 448 | EnumChatFormatting enumchatformatting = EnumChatFormatting.func_175744_a(p_96517_0_ - 3);
|
| 449 |
|
| 450 | if (enumchatformatting != null && enumchatformatting != EnumChatFormatting.RESET)
|
| 451 | {
|
| 452 | return "sidebar.team." + enumchatformatting.getFriendlyName();
|
| 453 | }
|
| 454 | }
|
| 455 |
|
| 456 | return null;
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | /**
|
| 461 | * Returns 0 for (case-insensitive) 'list', 1 for 'sidebar', 2 for 'belowName', otherwise -1.
|
| 462 | */
|
| 463 | public static int getObjectiveDisplaySlotNumber(String p_96537_0_)
|
| 464 | {
|
| 465 | if (p_96537_0_.equalsIgnoreCase("list"))
|
| 466 | {
|
| 467 | return 0;
|
| 468 | }
|
| 469 | else if (p_96537_0_.equalsIgnoreCase("sidebar"))
|
| 470 | {
|
| 471 | return 1;
|
| 472 | }
|
| 473 | else if (p_96537_0_.equalsIgnoreCase("belowName"))
|
| 474 | {
|
| 475 | return 2;
|
| 476 | }
|
| 477 | else
|
| 478 | {
|
| 479 | if (p_96537_0_.startsWith("sidebar.team."))
|
| 480 | {
|
| 481 | String s = p_96537_0_.substring("sidebar.team.".length());
|
| 482 | EnumChatFormatting enumchatformatting = EnumChatFormatting.getValueByName(s);
|
| 483 |
|
| 484 | if (enumchatformatting != null && enumchatformatting.getColorIndex() >= 0)
|
| 485 | {
|
| 486 | return enumchatformatting.getColorIndex() + 3;
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | return -1;
|
| 491 | }
|
| 492 | }
|
| 493 |
|
| 494 | public static String[] getDisplaySlotStrings()
|
| 495 | {
|
| 496 | if (field_178823_g == null)
|
| 497 | {
|
| 498 | field_178823_g = new String[19];
|
| 499 |
|
| 500 | for (int i = 0; i < 19; ++i)
|
| 501 | {
|
| 502 | field_178823_g[i] = getObjectiveDisplaySlot(i);
|
| 503 | }
|
| 504 | }
|
| 505 |
|
| 506 | return field_178823_g;
|
| 507 | }
|
| 508 |
|
| 509 | public void func_181140_a(Entity p_181140_1_)
|
| 510 | {
|
| 511 | if (p_181140_1_ != null && !(p_181140_1_ instanceof EntityPlayer) && !p_181140_1_.isEntityAlive())
|
| 512 | {
|
| 513 | String s = p_181140_1_.getUniqueID().toString();
|
| 514 | this.removeObjectiveFromEntity(s, (ScoreObjective)null);
|
| 515 | this.removePlayerFromTeams(s);
|
| 516 | }
|
| 517 | }
|
| 518 | }
|
| 519 |
|