| 1 | package net.minecraft.stats;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.util.IJsonSerializable;
|
| 7 | import net.minecraft.util.TupleIntJsonSerializable;
|
| 8 |
|
| 9 | public class StatFileWriter
|
| 10 | {
|
| 11 | protected final Map<StatBase, TupleIntJsonSerializable> statsData = Maps.<StatBase, TupleIntJsonSerializable>newConcurrentMap();
|
| 12 |
|
| 13 | /**
|
| 14 | * Returns true if the achievement has been unlocked.
|
| 15 | */
|
| 16 | public boolean hasAchievementUnlocked(Achievement achievementIn)
|
| 17 | {
|
| 18 | return this.readStat(achievementIn) > 0;
|
| 19 | }
|
| 20 |
|
| 21 | /**
|
| 22 | * Returns true if the parent has been unlocked, or there is no parent
|
| 23 | */
|
| 24 | public boolean canUnlockAchievement(Achievement achievementIn)
|
| 25 | {
|
| 26 | return achievementIn.parentAchievement == null || this.hasAchievementUnlocked(achievementIn.parentAchievement);
|
| 27 | }
|
| 28 |
|
| 29 | public int func_150874_c(Achievement p_150874_1_)
|
| 30 | {
|
| 31 | if (this.hasAchievementUnlocked(p_150874_1_))
|
| 32 | {
|
| 33 | return 0;
|
| 34 | }
|
| 35 | else
|
| 36 | {
|
| 37 | int i = 0;
|
| 38 |
|
| 39 | for (Achievement achievement = p_150874_1_.parentAchievement; achievement != null && !this.hasAchievementUnlocked(achievement); ++i)
|
| 40 | {
|
| 41 | achievement = achievement.parentAchievement;
|
| 42 | }
|
| 43 |
|
| 44 | return i;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public void increaseStat(EntityPlayer player, StatBase stat, int amount)
|
| 49 | {
|
| 50 | if (!stat.isAchievement() || this.canUnlockAchievement((Achievement)stat))
|
| 51 | {
|
| 52 | this.unlockAchievement(player, stat, this.readStat(stat) + amount);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * Triggers the logging of an achievement and attempts to announce to server
|
| 58 | */
|
| 59 | public void unlockAchievement(EntityPlayer playerIn, StatBase statIn, int p_150873_3_)
|
| 60 | {
|
| 61 | TupleIntJsonSerializable tupleintjsonserializable = (TupleIntJsonSerializable)this.statsData.get(statIn);
|
| 62 |
|
| 63 | if (tupleintjsonserializable == null)
|
| 64 | {
|
| 65 | tupleintjsonserializable = new TupleIntJsonSerializable();
|
| 66 | this.statsData.put(statIn, tupleintjsonserializable);
|
| 67 | }
|
| 68 |
|
| 69 | tupleintjsonserializable.setIntegerValue(p_150873_3_);
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Reads the given stat and returns its value as an int.
|
| 74 | */
|
| 75 | public int readStat(StatBase stat)
|
| 76 | {
|
| 77 | TupleIntJsonSerializable tupleintjsonserializable = (TupleIntJsonSerializable)this.statsData.get(stat);
|
| 78 | return tupleintjsonserializable == null ? 0 : tupleintjsonserializable.getIntegerValue();
|
| 79 | }
|
| 80 |
|
| 81 | public <T extends IJsonSerializable> T func_150870_b(StatBase p_150870_1_)
|
| 82 | {
|
| 83 | TupleIntJsonSerializable tupleintjsonserializable = (TupleIntJsonSerializable)this.statsData.get(p_150870_1_);
|
| 84 | return (T)(tupleintjsonserializable != null ? tupleintjsonserializable.getJsonSerializableValue() : null);
|
| 85 | }
|
| 86 |
|
| 87 | public <T extends IJsonSerializable> T func_150872_a(StatBase p_150872_1_, T p_150872_2_)
|
| 88 | {
|
| 89 | TupleIntJsonSerializable tupleintjsonserializable = (TupleIntJsonSerializable)this.statsData.get(p_150872_1_);
|
| 90 |
|
| 91 | if (tupleintjsonserializable == null)
|
| 92 | {
|
| 93 | tupleintjsonserializable = new TupleIntJsonSerializable();
|
| 94 | this.statsData.put(p_150872_1_, tupleintjsonserializable);
|
| 95 | }
|
| 96 |
|
| 97 | tupleintjsonserializable.setJsonSerializableValue(p_150872_2_);
|
| 98 | return (T)p_150872_2_;
|
| 99 | }
|
| 100 | }
|
| 101 |
|