|
1 | 1 | package org.embeddedt.modernfix.dfu; |
2 | 2 |
|
3 | 3 | import com.mojang.datafixers.DSL; |
4 | | -import com.mojang.datafixers.DataFixUtils; |
5 | 4 | import com.mojang.datafixers.DataFixer; |
6 | 5 | import com.mojang.datafixers.schemas.Schema; |
7 | | -import com.mojang.datafixers.types.Type; |
8 | | -import com.mojang.datafixers.types.constant.EmptyPart; |
9 | | -import com.mojang.datafixers.types.templates.TypeTemplate; |
10 | 6 | import com.mojang.serialization.Dynamic; |
11 | | -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
12 | | -import net.minecraft.SharedConstants; |
13 | 7 | import org.apache.logging.log4j.LogManager; |
14 | 8 | import org.apache.logging.log4j.Logger; |
15 | 9 |
|
16 | | -import java.util.Collections; |
17 | | -import java.util.Map; |
18 | 10 | import java.util.function.Supplier; |
19 | 11 |
|
20 | 12 | public class LazyDataFixer implements DataFixer { |
21 | 13 | private static final Logger LOGGER = LogManager.getLogger("ModernFix"); |
22 | 14 | private DataFixer backingDataFixer; |
23 | 15 | private final Supplier<DataFixer> dfuSupplier; |
24 | | - private static final Schema FAKE_SCHEMA = new EmptySchema(); |
25 | 16 |
|
26 | 17 | public LazyDataFixer(Supplier<DataFixer> dfuSupplier) { |
27 | 18 | LOGGER.info("Bypassed Mojang DFU"); |
28 | 19 | this.backingDataFixer = null; |
29 | 20 | this.dfuSupplier = dfuSupplier; |
30 | 21 | } |
31 | | - @Override |
32 | | - public <T> Dynamic<T> update(DSL.TypeReference type, Dynamic<T> input, int version, int newVersion) { |
33 | | - if(version >= newVersion) |
34 | | - return input; |
| 22 | + |
| 23 | + private DataFixer getDataFixer() { |
35 | 24 | synchronized (this) { |
36 | 25 | if(backingDataFixer == null) { |
37 | 26 | LOGGER.info("Instantiating Mojang DFU"); |
38 | 27 | DFUBlaster.blastMaps(); |
39 | 28 | backingDataFixer = dfuSupplier.get(); |
40 | 29 | } |
41 | 30 | } |
42 | | - return backingDataFixer.update(type, input, version, newVersion); |
| 31 | + return backingDataFixer; |
43 | 32 | } |
44 | 33 |
|
45 | | - /** |
46 | | - * "getSchema is only there for checks that are not important" - fry, 2021 |
47 | | - */ |
48 | 34 | @Override |
49 | | - public Schema getSchema(int key) { |
50 | | - return FAKE_SCHEMA; |
| 35 | + public <T> Dynamic<T> update(DSL.TypeReference type, Dynamic<T> input, int version, int newVersion) { |
| 36 | + if(version >= newVersion) |
| 37 | + return input; |
| 38 | + return getDataFixer().update(type, input, version, newVersion); |
51 | 39 | } |
52 | 40 |
|
53 | | - /** |
54 | | - * Empty schema that also returns empty Type<?> instances to prevent crashes. |
55 | | - */ |
56 | | - static class EmptySchema extends Schema { |
57 | | - public EmptySchema() { |
58 | | - super(DataFixUtils.makeKey(SharedConstants.getCurrentVersion().getWorldVersion()), null); |
59 | | - } |
60 | | - |
61 | | - private static final Type<?> EMPTY_TYPE = new EmptyPart(); |
62 | | - private static final TypeTemplate FAKE_TEMPLATE = EMPTY_TYPE.template(); |
63 | | - |
64 | | - |
65 | | - @Override |
66 | | - protected Map<String, Type<?>> buildTypes() { |
67 | | - Object2ObjectOpenHashMap<String, Type<?>> map = new Object2ObjectOpenHashMap<>(); |
68 | | - map.defaultReturnValue(new EmptyPart()); |
69 | | - return map; |
70 | | - } |
71 | | - |
72 | | - @Override |
73 | | - public TypeTemplate resolveTemplate(String name) { |
74 | | - return FAKE_TEMPLATE; |
75 | | - } |
76 | | - |
77 | | - @Override |
78 | | - public Type<?> getChoiceType(DSL.TypeReference type, String choiceName) { |
79 | | - return EMPTY_TYPE; |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - public void registerTypes(Schema schema, Map<String, Supplier<TypeTemplate>> entityTypes, Map<String, Supplier<TypeTemplate>> blockEntityTypes) { |
84 | | - } |
85 | | - |
86 | | - @Override |
87 | | - public Map<String, Supplier<TypeTemplate>> registerEntities(Schema schema) { |
88 | | - return Collections.emptyMap(); |
89 | | - } |
90 | | - |
91 | | - @Override |
92 | | - public Map<String, Supplier<TypeTemplate>> registerBlockEntities(Schema schema) { |
93 | | - return Collections.emptyMap(); |
94 | | - } |
| 41 | + @Override |
| 42 | + public Schema getSchema(int key) { |
| 43 | + return getDataFixer().getSchema(key); |
95 | 44 | } |
96 | 45 | } |
0 commit comments