JsonToNBT .java

net.minecraft.nbt.JsonToNBT (class, 554 lines): members: field_179273_b, getTagFromJson, Primitive, Compound, Any, List, logger. imports: Splitter, Logger.

net.minecraft.nbtclass554 linescommon16.9 KB

API Surface (5)

NBTTagCompound getTagFromJson(String jsonString)
NBTBase parse()
NBTBase parse()
NBTBase parse()
NBTBase parse()

Fields & Constants (2)

logger
field_179273_b

String literals (7)

"Encountered multiple top tags, only one expected"
"Illegal use of \\\": "
"Unbalanced square brackets []: "
"Unbalanced quotation: "
"Unbalanced brackets: "
"Unexpected token \'"
"Name error at: "

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

JsonToNBT.java554 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.nbt;
2
3
import com.google.common.base.Splitter;
4
import com.google.common.collect.Iterables;
5
import com.google.common.collect.Lists;
6
import java.util.Stack;
7
import java.util.regex.Pattern;
8
import org.apache.logging.log4j.LogManager;
9
import org.apache.logging.log4j.Logger;
10
11
public class JsonToNBT
12
{
13
    private static final Logger logger = LogManager.getLogger();
14
    private static final Pattern field_179273_b = Pattern.compile("\\[[-+\\d|,\\s]+\\]");
15
16
    public static NBTTagCompound getTagFromJson(String jsonString) throws NBTException
17
    {
18
        jsonString = jsonString.trim();
19
20
        if (!jsonString.startsWith("{"))
21
        {
22
            throw new NBTException("Invalid tag encountered, expected \'{\' as first char.");
23
        }
24
        else if (func_150310_b(jsonString) != 1)
25
        {
26
            throw new NBTException("Encountered multiple top tags, only one expected");
27
        }
28
        else
29
        {
30
            return (NBTTagCompound)func_150316_a("tag", jsonString).parse();
31
        }
32
    }
33
34
    static int func_150310_b(String p_150310_0_) throws NBTException
35
    {
36
        int i = 0;
37
        boolean flag = false;
38
        Stack<Character> stack = new Stack();
39
40
        for (int j = 0; j < p_150310_0_.length(); ++j)
41
        {
42
            char c0 = p_150310_0_.charAt(j);
43
44
            if (c0 == 34)
45
            {
46
                if (func_179271_b(p_150310_0_, j))
47
                {
48
                    if (!flag)
49
                    {
50
                        throw new NBTException("Illegal use of \\\": " + p_150310_0_);
51
                    }
52
                }
53
                else
54
                {
55
                    flag = !flag;
56
                }
57
            }
58
            else if (!flag)
59
            {
60
                if (c0 != 123 && c0 != 91)
61
                {
62
                    if (c0 == 125 && (stack.isEmpty() || ((Character)stack.pop()).charValue() != 123))
63
                    {
64
                        throw new NBTException("Unbalanced curly brackets {}: " + p_150310_0_);
65
                    }
66
67
                    if (c0 == 93 && (stack.isEmpty() || ((Character)stack.pop()).charValue() != 91))
68
                    {
69
                        throw new NBTException("Unbalanced square brackets []: " + p_150310_0_);
70
                    }
71
                }
72
                else
73
                {
74
                    if (stack.isEmpty())
75
                    {
76
                        ++i;
77
                    }
78
79
                    stack.push(Character.valueOf(c0));
80
                }
81
            }
82
        }
83
84
        if (flag)
85
        {
86
            throw new NBTException("Unbalanced quotation: " + p_150310_0_);
87
        }
88
        else if (!stack.isEmpty())
89
        {
90
            throw new NBTException("Unbalanced brackets: " + p_150310_0_);
91
        }
92
        else
93
        {
94
            if (i == 0 && !p_150310_0_.isEmpty())
95
            {
96
                i = 1;
97
            }
98
99
            return i;
100
        }
101
    }
102
103
    static JsonToNBT.Any func_179272_a(String... p_179272_0_) throws NBTException
104
    {
105
        return func_150316_a(p_179272_0_[0], p_179272_0_[1]);
106
    }
107
108
    static JsonToNBT.Any func_150316_a(String p_150316_0_, String p_150316_1_) throws NBTException
109
    {
110
        p_150316_1_ = p_150316_1_.trim();
111
112
        if (p_150316_1_.startsWith("{"))
113
        {
114
            p_150316_1_ = p_150316_1_.substring(1, p_150316_1_.length() - 1);
115
            JsonToNBT.Compound jsontonbt$compound;
116
            String s1;
117
118
            for (jsontonbt$compound = new JsonToNBT.Compound(p_150316_0_); p_150316_1_.length() > 0; p_150316_1_ = p_150316_1_.substring(s1.length() + 1))
119
            {
120
                s1 = func_150314_a(p_150316_1_, true);
121
122
                if (s1.length() > 0)
123
                {
124
                    boolean flag1 = false;
125
                    jsontonbt$compound.field_150491_b.add(func_179270_a(s1, flag1));
126
                }
127
128
                if (p_150316_1_.length() < s1.length() + 1)
129
                {
130
                    break;
131
                }
132
133
                char c1 = p_150316_1_.charAt(s1.length());
134
135
                if (c1 != 44 && c1 != 123 && c1 != 125 && c1 != 91 && c1 != 93)
136
                {
137
                    throw new NBTException("Unexpected token \'" + c1 + "\' at: " + p_150316_1_.substring(s1.length()));
138
                }
139
            }
140
141
            return jsontonbt$compound;
142
        }
143
        else if (p_150316_1_.startsWith("[") && !field_179273_b.matcher(p_150316_1_).matches())
144
        {
145
            p_150316_1_ = p_150316_1_.substring(1, p_150316_1_.length() - 1);
146
            JsonToNBT.List jsontonbt$list;
147
            String s;
148
149
            for (jsontonbt$list = new JsonToNBT.List(p_150316_0_); p_150316_1_.length() > 0; p_150316_1_ = p_150316_1_.substring(s.length() + 1))
150
            {
151
                s = func_150314_a(p_150316_1_, false);
152
153
                if (s.length() > 0)
154
                {
155
                    boolean flag = true;
156
                    jsontonbt$list.field_150492_b.add(func_179270_a(s, flag));
157
                }
158
159
                if (p_150316_1_.length() < s.length() + 1)
160
                {
161
                    break;
162
                }
163
164
                char c0 = p_150316_1_.charAt(s.length());
165
166
                if (c0 != 44 && c0 != 123 && c0 != 125 && c0 != 91 && c0 != 93)
167
                {
168
                    throw new NBTException("Unexpected token \'" + c0 + "\' at: " + p_150316_1_.substring(s.length()));
169
                }
170
            }
171
172
            return jsontonbt$list;
173
        }
174
        else
175
        {
176
            return new JsonToNBT.Primitive(p_150316_0_, p_150316_1_);
177
        }
178
    }
179
180
    private static JsonToNBT.Any func_179270_a(String p_179270_0_, boolean p_179270_1_) throws NBTException
181
    {
182
        String s = func_150313_b(p_179270_0_, p_179270_1_);
183
        String s1 = func_150311_c(p_179270_0_, p_179270_1_);
184
        return func_179272_a(new String[] {s, s1});
185
    }
186
187
    private static String func_150314_a(String p_150314_0_, boolean p_150314_1_) throws NBTException
188
    {
189
        int i = func_150312_a(p_150314_0_, ':');
190
        int j = func_150312_a(p_150314_0_, ',');
191
192
        if (p_150314_1_)
193
        {
194
            if (i == -1)
195
            {
196
                throw new NBTException("Unable to locate name/value separator for string: " + p_150314_0_);
197
            }
198
199
            if (j != -1 && j < i)
200
            {
201
                throw new NBTException("Name error at: " + p_150314_0_);
202
            }
203
        }
204
        else if (i == -1 || i > j)
205
        {
206
            i = -1;
207
        }
208
209
        return func_179269_a(p_150314_0_, i);
210
    }
211
212
    private static String func_179269_a(String p_179269_0_, int p_179269_1_) throws NBTException
213
    {
214
        Stack<Character> stack = new Stack();
215
        int i = p_179269_1_ + 1;
216
        boolean flag = false;
217
        boolean flag1 = false;
218
        boolean flag2 = false;
219
220
        for (int j = 0; i < p_179269_0_.length(); ++i)
221
        {
222
            char c0 = p_179269_0_.charAt(i);
223
224
            if (c0 == 34)
225
            {
226
                if (func_179271_b(p_179269_0_, i))
227
                {
228
                    if (!flag)
229
                    {
230
                        throw new NBTException("Illegal use of \\\": " + p_179269_0_);
231
                    }
232
                }
233
                else
234
                {
235
                    flag = !flag;
236
237
                    if (flag && !flag2)
238
                    {
239
                        flag1 = true;
240
                    }
241
242
                    if (!flag)
243
                    {
244
                        j = i;
245
                    }
246
                }
247
            }
248
            else if (!flag)
249
            {
250
                if (c0 != 123 && c0 != 91)
251
                {
252
                    if (c0 == 125 && (stack.isEmpty() || ((Character)stack.pop()).charValue() != 123))
253
                    {
254
                        throw new NBTException("Unbalanced curly brackets {}: " + p_179269_0_);
255
                    }
256
257
                    if (c0 == 93 && (stack.isEmpty() || ((Character)stack.pop()).charValue() != 91))
258
                    {
259
                        throw new NBTException("Unbalanced square brackets []: " + p_179269_0_);
260
                    }
261
262
                    if (c0 == 44 && stack.isEmpty())
263
                    {
264
                        return p_179269_0_.substring(0, i);
265
                    }
266
                }
267
                else
268
                {
269
                    stack.push(Character.valueOf(c0));
270
                }
271
            }
272
273
            if (!Character.isWhitespace(c0))
274
            {
275
                if (!flag && flag1 && j != i)
276
                {
277
                    return p_179269_0_.substring(0, j + 1);
278
                }
279
280
                flag2 = true;
281
            }
282
        }
283
284
        return p_179269_0_.substring(0, i);
285
    }
286
287
    private static String func_150313_b(String p_150313_0_, boolean p_150313_1_) throws NBTException
288
    {
289
        if (p_150313_1_)
290
        {
291
            p_150313_0_ = p_150313_0_.trim();
292
293
            if (p_150313_0_.startsWith("{") || p_150313_0_.startsWith("["))
294
            {
295
                return "";
296
            }
297
        }
298
299
        int i = func_150312_a(p_150313_0_, ':');
300
301
        if (i == -1)
302
        {
303
            if (p_150313_1_)
304
            {
305
                return "";
306
            }
307
            else
308
            {
309
                throw new NBTException("Unable to locate name/value separator for string: " + p_150313_0_);
310
            }
311
        }
312
        else
313
        {
314
            return p_150313_0_.substring(0, i).trim();
315
        }
316
    }
317
318
    private static String func_150311_c(String p_150311_0_, boolean p_150311_1_) throws NBTException
319
    {
320
        if (p_150311_1_)
321
        {
322
            p_150311_0_ = p_150311_0_.trim();
323
324
            if (p_150311_0_.startsWith("{") || p_150311_0_.startsWith("["))
325
            {
326
                return p_150311_0_;
327
            }
328
        }
329
330
        int i = func_150312_a(p_150311_0_, ':');
331
332
        if (i == -1)
333
        {
334
            if (p_150311_1_)
335
            {
336
                return p_150311_0_;
337
            }
338
            else
339
            {
340
                throw new NBTException("Unable to locate name/value separator for string: " + p_150311_0_);
341
            }
342
        }
343
        else
344
        {
345
            return p_150311_0_.substring(i + 1).trim();
346
        }
347
    }
348
349
    private static int func_150312_a(String p_150312_0_, char p_150312_1_)
350
    {
351
        int i = 0;
352
353
        for (boolean flag = true; i < p_150312_0_.length(); ++i)
354
        {
355
            char c0 = p_150312_0_.charAt(i);
356
357
            if (c0 == 34)
358
            {
359
                if (!func_179271_b(p_150312_0_, i))
360
                {
361
                    flag = !flag;
362
                }
363
            }
364
            else if (flag)
365
            {
366
                if (c0 == p_150312_1_)
367
                {
368
                    return i;
369
                }
370
371
                if (c0 == 123 || c0 == 91)
372
                {
373
                    return -1;
374
                }
375
            }
376
        }
377
378
        return -1;
379
    }
380
381
    private static boolean func_179271_b(String p_179271_0_, int p_179271_1_)
382
    {
383
        return p_179271_1_ > 0 && p_179271_0_.charAt(p_179271_1_ - 1) == 92 && !func_179271_b(p_179271_0_, p_179271_1_ - 1);
384
    }
385
386
    abstract static class Any
387
    {
388
        protected String json;
389
390
        public abstract NBTBase parse() throws NBTException;
391
    }
392
393
    static class Compound extends JsonToNBT.Any
394
    {
395
        protected java.util.List<JsonToNBT.Any> field_150491_b = Lists.<JsonToNBT.Any>newArrayList();
396
397
        public Compound(String p_i45137_1_)
398
        {
399
            this.json = p_i45137_1_;
400
        }
401
402
        public NBTBase parse() throws NBTException
403
        {
404
            NBTTagCompound nbttagcompound = new NBTTagCompound();
405
406
            for (JsonToNBT.Any jsontonbt$any : this.field_150491_b)
407
            {
408
                nbttagcompound.setTag(jsontonbt$any.json, jsontonbt$any.parse());
409
            }
410
411
            return nbttagcompound;
412
        }
413
    }
414
415
    static class List extends JsonToNBT.Any
416
    {
417
        protected java.util.List<JsonToNBT.Any> field_150492_b = Lists.<JsonToNBT.Any>newArrayList();
418
419
        public List(String json)
420
        {
421
            this.json = json;
422
        }
423
424
        public NBTBase parse() throws NBTException
425
        {
426
            NBTTagList nbttaglist = new NBTTagList();
427
428
            for (JsonToNBT.Any jsontonbt$any : this.field_150492_b)
429
            {
430
                nbttaglist.appendTag(jsontonbt$any.parse());
431
            }
432
433
            return nbttaglist;
434
        }
435
    }
436
437
    static class Primitive extends JsonToNBT.Any
438
    {
439
        private static final Pattern DOUBLE = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+[d|D]");
440
        private static final Pattern FLOAT = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+[f|F]");
441
        private static final Pattern BYTE = Pattern.compile("[-+]?[0-9]+[b|B]");
442
        private static final Pattern LONG = Pattern.compile("[-+]?[0-9]+[l|L]");
443
        private static final Pattern SHORT = Pattern.compile("[-+]?[0-9]+[s|S]");
444
        private static final Pattern INTEGER = Pattern.compile("[-+]?[0-9]+");
445
        private static final Pattern DOUBLE_UNTYPED = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+");
446
        private static final Splitter SPLITTER = Splitter.on(',').omitEmptyStrings();
447
        protected String jsonValue;
448
449
        public Primitive(String p_i45139_1_, String p_i45139_2_)
450
        {
451
            this.json = p_i45139_1_;
452
            this.jsonValue = p_i45139_2_;
453
        }
454
455
        public NBTBase parse() throws NBTException
456
        {
457
            try
458
            {
459
                if (DOUBLE.matcher(this.jsonValue).matches())
460
                {
461
                    return new NBTTagDouble(Double.parseDouble(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
462
                }
463
464
                if (FLOAT.matcher(this.jsonValue).matches())
465
                {
466
                    return new NBTTagFloat(Float.parseFloat(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
467
                }
468
469
                if (BYTE.matcher(this.jsonValue).matches())
470
                {
471
                    return new NBTTagByte(Byte.parseByte(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
472
                }
473
474
                if (LONG.matcher(this.jsonValue).matches())
475
                {
476
                    return new NBTTagLong(Long.parseLong(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
477
                }
478
479
                if (SHORT.matcher(this.jsonValue).matches())
480
                {
481
                    return new NBTTagShort(Short.parseShort(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
482
                }
483
484
                if (INTEGER.matcher(this.jsonValue).matches())
485
                {
486
                    return new NBTTagInt(Integer.parseInt(this.jsonValue));
487
                }
488
489
                if (DOUBLE_UNTYPED.matcher(this.jsonValue).matches())
490
                {
491
                    return new NBTTagDouble(Double.parseDouble(this.jsonValue));
492
                }
493
494
                if (this.jsonValue.equalsIgnoreCase("true") || this.jsonValue.equalsIgnoreCase("false"))
495
                {
496
                    return new NBTTagByte((byte)(Boolean.parseBoolean(this.jsonValue) ? 1 : 0));
497
                }
498
            }
499
            catch (NumberFormatException var6)
500
            {
501
                this.jsonValue = this.jsonValue.replaceAll("\\\\\"", "\"");
502
                return new NBTTagString(this.jsonValue);
503
            }
504
505
            if (this.jsonValue.startsWith("[") && this.jsonValue.endsWith("]"))
506
            {
507
                String s = this.jsonValue.substring(1, this.jsonValue.length() - 1);
508
                String[] astring = (String[])Iterables.toArray(SPLITTER.split(s), String.class);
509
510
                try
511
                {
512
                    int[] aint = new int[astring.length];
513
514
                    for (int j = 0; j < astring.length; ++j)
515
                    {
516
                        aint[j] = Integer.parseInt(astring[j].trim());
517
                    }
518
519
                    return new NBTTagIntArray(aint);
520
                }
521
                catch (NumberFormatException var5)
522
                {
523
                    return new NBTTagString(this.jsonValue);
524
                }
525
            }
526
            else
527
            {
528
                if (this.jsonValue.startsWith("\"") && this.jsonValue.endsWith("\""))
529
                {
530
                    this.jsonValue = this.jsonValue.substring(1, this.jsonValue.length() - 1);
531
                }
532
533
                this.jsonValue = this.jsonValue.replaceAll("\\\\\"", "\"");
534
                StringBuilder stringbuilder = new StringBuilder();
535
536
                for (int i = 0; i < this.jsonValue.length(); ++i)
537
                {
538
                    if (i < this.jsonValue.length() - 1 && this.jsonValue.charAt(i) == 92 && this.jsonValue.charAt(i + 1) == 92)
539
                    {
540
                        stringbuilder.append('\\');
541
                        ++i;
542
                    }
543
                    else
544
                    {
545
                        stringbuilder.append(this.jsonValue.charAt(i));
546
                    }
547
                }
548
549
                return new NBTTagString(stringbuilder.toString());
550
            }
551
        }
552
    }
553
}
554

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