StatisticsFile .java

net.minecraft.stats.StatisticsFile (class, 251 lines): members: sendAchievements, field_150885_f, field_150886_g, field_150888_e, func_150876_a, parseJson.

net.minecraft.statsclass251 linescommonextends StatFileWriter9.2 KB

API Surface (10)

void readStatFile()
void saveStatFile()
void unlockAchievement(EntityPlayer playerIn, StatBase statIn, int p_150873_3_)
Set<StatBase> func_150878_c()
Map<StatBase, TupleIntJsonSerializable> parseJson(String p_150881_1_)
String dumpJson(Map<StatBase, TupleIntJsonSerializable> p_150880_0_)
void func_150877_d()
void func_150876_a(EntityPlayerMP p_150876_1_)
void sendAchievements(EntityPlayerMP player)
boolean func_150879_e()

Fields & Constants (6)

logger
mcServer
statsFile
field_150888_e
field_150885_f
field_150886_g

String literals (11)

"Couldn\'t read statistics file "
"Couldn\'t parse statistics file "
"Couldn\'t save stats"
"chat.type.achievement"
"chat.type.achievement.taken"
"progress"
"Invalid statistic progress in "
"Invalid statistic in "
": Don\'t know what "
"Couldn\'t save statistic "
": error serializing progress"

Full Source

StatisticsFile.java251 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.stats;
2
3
import com.google.common.collect.Maps;
4
import com.google.common.collect.Sets;
5
import com.google.gson.JsonElement;
6
import com.google.gson.JsonObject;
7
import com.google.gson.JsonParseException;
8
import com.google.gson.JsonParser;
9
import java.io.File;
10
import java.io.IOException;
11
import java.lang.reflect.Constructor;
12
import java.util.Map;
13
import java.util.Set;
14
import java.util.Map.Entry;
15
import net.minecraft.entity.player.EntityPlayer;
16
import net.minecraft.entity.player.EntityPlayerMP;
17
import net.minecraft.network.play.server.S37PacketStatistics;
18
import net.minecraft.server.MinecraftServer;
19
import net.minecraft.util.ChatComponentTranslation;
20
import net.minecraft.util.IJsonSerializable;
21
import net.minecraft.util.TupleIntJsonSerializable;
22
import org.apache.commons.io.FileUtils;
23
import org.apache.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
25
26
public class StatisticsFile extends StatFileWriter
27
{
28
    private static final Logger logger = LogManager.getLogger();
29
    private final MinecraftServer mcServer;
30
    private final File statsFile;
31
    private final Set<StatBase> field_150888_e = Sets.<StatBase>newHashSet();
32
    private int field_150885_f = -300;
33
    private boolean field_150886_g = false;
34
35
    public StatisticsFile(MinecraftServer serverIn, File statsFileIn)
36
    {
37
        this.mcServer = serverIn;
38
        this.statsFile = statsFileIn;
39
    }
40
41
    public void readStatFile()
42
    {
43
        if (this.statsFile.isFile())
44
        {
45
            try
46
            {
47
                this.statsData.clear();
48
                this.statsData.putAll(this.parseJson(FileUtils.readFileToString(this.statsFile)));
49
            }
50
            catch (IOException ioexception)
51
            {
52
                logger.error((String)("Couldn\'t read statistics file " + this.statsFile), (Throwable)ioexception);
53
            }
54
            catch (JsonParseException jsonparseexception)
55
            {
56
                logger.error((String)("Couldn\'t parse statistics file " + this.statsFile), (Throwable)jsonparseexception);
57
            }
58
        }
59
    }
60
61
    public void saveStatFile()
62
    {
63
        try
64
        {
65
            FileUtils.writeStringToFile(this.statsFile, dumpJson(this.statsData));
66
        }
67
        catch (IOException ioexception)
68
        {
69
            logger.error((String)"Couldn\'t save stats", (Throwable)ioexception);
70
        }
71
    }
72
73
    /**
74
     * Triggers the logging of an achievement and attempts to announce to server
75
     */
76
    public void unlockAchievement(EntityPlayer playerIn, StatBase statIn, int p_150873_3_)
77
    {
78
        int i = statIn.isAchievement() ? this.readStat(statIn) : 0;
79
        super.unlockAchievement(playerIn, statIn, p_150873_3_);
80
        this.field_150888_e.add(statIn);
81
82
        if (statIn.isAchievement() && i == 0 && p_150873_3_ > 0)
83
        {
84
            this.field_150886_g = true;
85
86
            if (this.mcServer.isAnnouncingPlayerAchievements())
87
            {
88
                this.mcServer.getConfigurationManager().sendChatMsg(new ChatComponentTranslation("chat.type.achievement", new Object[] {playerIn.getDisplayName(), statIn.createChatComponent()}));
89
            }
90
        }
91
92
        if (statIn.isAchievement() && i > 0 && p_150873_3_ == 0)
93
        {
94
            this.field_150886_g = true;
95
96
            if (this.mcServer.isAnnouncingPlayerAchievements())
97
            {
98
                this.mcServer.getConfigurationManager().sendChatMsg(new ChatComponentTranslation("chat.type.achievement.taken", new Object[] {playerIn.getDisplayName(), statIn.createChatComponent()}));
99
            }
100
        }
101
    }
102
103
    public Set<StatBase> func_150878_c()
104
    {
105
        Set<StatBase> set = Sets.newHashSet(this.field_150888_e);
106
        this.field_150888_e.clear();
107
        this.field_150886_g = false;
108
        return set;
109
    }
110
111
    public Map<StatBase, TupleIntJsonSerializable> parseJson(String p_150881_1_)
112
    {
113
        JsonElement jsonelement = (new JsonParser()).parse(p_150881_1_);
114
115
        if (!jsonelement.isJsonObject())
116
        {
117
            return Maps.<StatBase, TupleIntJsonSerializable>newHashMap();
118
        }
119
        else
120
        {
121
            JsonObject jsonobject = jsonelement.getAsJsonObject();
122
            Map<StatBase, TupleIntJsonSerializable> map = Maps.<StatBase, TupleIntJsonSerializable>newHashMap();
123
124
            for (Entry<String, JsonElement> entry : jsonobject.entrySet())
125
            {
126
                StatBase statbase = StatList.getOneShotStat((String)entry.getKey());
127
128
                if (statbase != null)
129
                {
130
                    TupleIntJsonSerializable tupleintjsonserializable = new TupleIntJsonSerializable();
131
132
                    if (((JsonElement)entry.getValue()).isJsonPrimitive() && ((JsonElement)entry.getValue()).getAsJsonPrimitive().isNumber())
133
                    {
134
                        tupleintjsonserializable.setIntegerValue(((JsonElement)entry.getValue()).getAsInt());
135
                    }
136
                    else if (((JsonElement)entry.getValue()).isJsonObject())
137
                    {
138
                        JsonObject jsonobject1 = ((JsonElement)entry.getValue()).getAsJsonObject();
139
140
                        if (jsonobject1.has("value") && jsonobject1.get("value").isJsonPrimitive() && jsonobject1.get("value").getAsJsonPrimitive().isNumber())
141
                        {
142
                            tupleintjsonserializable.setIntegerValue(jsonobject1.getAsJsonPrimitive("value").getAsInt());
143
                        }
144
145
                        if (jsonobject1.has("progress") && statbase.func_150954_l() != null)
146
                        {
147
                            try
148
                            {
149
                                Constructor <? extends IJsonSerializable > constructor = statbase.func_150954_l().getConstructor(new Class[0]);
150
                                IJsonSerializable ijsonserializable = (IJsonSerializable)constructor.newInstance(new Object[0]);
151
                                ijsonserializable.fromJson(jsonobject1.get("progress"));
152
                                tupleintjsonserializable.setJsonSerializableValue(ijsonserializable);
153
                            }
154
                            catch (Throwable throwable)
155
                            {
156
                                logger.warn("Invalid statistic progress in " + this.statsFile, throwable);
157
                            }
158
                        }
159
                    }
160
161
                    map.put(statbase, tupleintjsonserializable);
162
                }
163
                else
164
                {
165
                    logger.warn("Invalid statistic in " + this.statsFile + ": Don\'t know what " + (String)entry.getKey() + " is");
166
                }
167
            }
168
169
            return map;
170
        }
171
    }
172
173
    public static String dumpJson(Map<StatBase, TupleIntJsonSerializable> p_150880_0_)
174
    {
175
        JsonObject jsonobject = new JsonObject();
176
177
        for (Entry<StatBase, TupleIntJsonSerializable> entry : p_150880_0_.entrySet())
178
        {
179
            if (((TupleIntJsonSerializable)entry.getValue()).getJsonSerializableValue() != null)
180
            {
181
                JsonObject jsonobject1 = new JsonObject();
182
                jsonobject1.addProperty("value", (Number)Integer.valueOf(((TupleIntJsonSerializable)entry.getValue()).getIntegerValue()));
183
184
                try
185
                {
186
                    jsonobject1.add("progress", ((TupleIntJsonSerializable)entry.getValue()).getJsonSerializableValue().getSerializableElement());
187
                }
188
                catch (Throwable throwable)
189
                {
190
                    logger.warn("Couldn\'t save statistic " + ((StatBase)entry.getKey()).getStatName() + ": error serializing progress", throwable);
191
                }
192
193
                jsonobject.add(((StatBase)entry.getKey()).statId, jsonobject1);
194
            }
195
            else
196
            {
197
                jsonobject.addProperty(((StatBase)entry.getKey()).statId, (Number)Integer.valueOf(((TupleIntJsonSerializable)entry.getValue()).getIntegerValue()));
198
            }
199
        }
200
201
        return jsonobject.toString();
202
    }
203
204
    public void func_150877_d()
205
    {
206
        for (StatBase statbase : this.statsData.keySet())
207
        {
208
            this.field_150888_e.add(statbase);
209
        }
210
    }
211
212
    public void func_150876_a(EntityPlayerMP p_150876_1_)
213
    {
214
        int i = this.mcServer.getTickCounter();
215
        Map<StatBase, Integer> map = Maps.<StatBase, Integer>newHashMap();
216
217
        if (this.field_150886_g || i - this.field_150885_f > 300)
218
        {
219
            this.field_150885_f = i;
220
221
            for (StatBase statbase : this.func_150878_c())
222
            {
223
                map.put(statbase, Integer.valueOf(this.readStat(statbase)));
224
            }
225
        }
226
227
        p_150876_1_.playerNetServerHandler.sendPacket(new S37PacketStatistics(map));
228
    }
229
230
    public void sendAchievements(EntityPlayerMP player)
231
    {
232
        Map<StatBase, Integer> map = Maps.<StatBase, Integer>newHashMap();
233
234
        for (Achievement achievement : AchievementList.achievementList)
235
        {
236
            if (this.hasAchievementUnlocked(achievement))
237
            {
238
                map.put(achievement, Integer.valueOf(this.readStat(achievement)));
239
                this.field_150888_e.remove(achievement);
240
            }
241
        }
242
243
        player.playerNetServerHandler.sendPacket(new S37PacketStatistics(map));
244
    }
245
246
    public boolean func_150879_e()
247
    {
248
        return this.field_150886_g;
249
    }
250
}
251

Deobfuscated MCP 1.8.9 source of StatisticsFile from Marcelektro/MavenMCP-1.8.9 (net/minecraft/stats/StatisticsFile.java), stored verbatim.