Skip to content

Commit 098f7ff

Browse files
Allow platforms to use the mapping system (#3754)
1 parent 18f04bf commit 098f7ff

File tree

9 files changed

+63
-47
lines changed

9 files changed

+63
-47
lines changed

api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void load() {
7070
getLogger().info("Loading " + unmappedVersion + " -> " + mappedVersion + " mappings...");
7171
}
7272

73-
final CompoundTag data = readNBTFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
73+
final CompoundTag data = readMappingsFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
7474
blockMappings = loadMappings(data, "blocks");
7575
blockStateMappings = loadMappings(data, "blockstates");
7676
blockEntityMappings = loadMappings(data, "blockentities");
@@ -82,8 +82,8 @@ public void load() {
8282
attributeMappings = loadMappings(data, "attributes");
8383
itemMappings = loadBiMappings(data, "items");
8484

85-
final CompoundTag unmappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + unmappedVersion + ".nbt", true);
86-
final CompoundTag mappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + mappedVersion + ".nbt", true);
85+
final CompoundTag unmappedIdentifierData = readIdentifiersFile("identifiers-" + unmappedVersion + ".nbt");
86+
final CompoundTag mappedIdentifierData = readIdentifiersFile("identifiers-" + mappedVersion + ".nbt");
8787
if (unmappedIdentifierData != null && mappedIdentifierData != null) {
8888
entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities");
8989
argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes");
@@ -114,16 +114,20 @@ public void load() {
114114
loadExtras(data);
115115
}
116116

117-
protected @Nullable CompoundTag readNBTFile(final String name) {
118-
return MappingDataLoader.loadNBT(name);
117+
protected @Nullable CompoundTag readMappingsFile(final String name) {
118+
return MappingDataLoader.INSTANCE.loadNBT(name);
119+
}
120+
121+
protected @Nullable CompoundTag readIdentifiersFile(final String name) {
122+
return MappingDataLoader.INSTANCE.loadNBT(name, true);
119123
}
120124

121125
protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) {
122-
return MappingDataLoader.loadMappings(data, key);
126+
return MappingDataLoader.INSTANCE.loadMappings(data, key);
123127
}
124128

125129
protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
126-
return MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
130+
return MappingDataLoader.INSTANCE.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
127131
}
128132

129133
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {

api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,28 @@
5151
import java.util.stream.Collectors;
5252
import org.checkerframework.checker.nullness.qual.Nullable;
5353

54-
public final class MappingDataLoader {
54+
public class MappingDataLoader {
5555

56-
private static final Map<String, CompoundTag> MAPPINGS_CACHE = new HashMap<>();
57-
private static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
56+
public static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
5857
private static final byte DIRECT_ID = 0;
5958
private static final byte SHIFTS_ID = 1;
6059
private static final byte CHANGES_ID = 2;
6160
private static final byte IDENTITY_ID = 3;
62-
private static boolean cacheValid = true;
6361

64-
public static void clearCache() {
65-
MAPPINGS_CACHE.clear();
62+
public static final MappingDataLoader INSTANCE = new MappingDataLoader(MappingDataLoader.class, "assets/viaversion/data/");
63+
64+
private final Map<String, CompoundTag> mappingsCache = new HashMap<>();
65+
private final Class<?> dataLoaderClass;
66+
private final String dataPath;
67+
private boolean cacheValid = true;
68+
69+
public MappingDataLoader(final Class<?> dataLoaderClass, final String dataPath) {
70+
this.dataLoaderClass = dataLoaderClass;
71+
this.dataPath = dataPath;
72+
}
73+
74+
public void clearCache() {
75+
mappingsCache.clear();
6676
cacheValid = false;
6777
}
6878

@@ -71,8 +81,8 @@ public static void clearCache() {
7181
*
7282
* @return loaded json object, or null if not found or invalid
7383
*/
74-
public static @Nullable JsonObject loadFromDataDir(final String name) {
75-
final File file = new File(Via.getPlatform().getDataFolder(), name);
84+
public @Nullable JsonObject loadFromDataDir(final String name) {
85+
final File file = getFile(name);
7686
if (!file.exists()) {
7787
return loadData(name);
7888
}
@@ -94,7 +104,7 @@ public static void clearCache() {
94104
*
95105
* @return loaded json object from bundled resources if present
96106
*/
97-
public static @Nullable JsonObject loadData(final String name) {
107+
public @Nullable JsonObject loadData(final String name) {
98108
final InputStream stream = getResource(name);
99109
if (stream == null) {
100110
return null;
@@ -107,29 +117,29 @@ public static void clearCache() {
107117
}
108118
}
109119

110-
public static @Nullable CompoundTag loadNBT(final String name, final boolean cache) {
120+
public @Nullable CompoundTag loadNBT(final String name, final boolean cache) {
111121
if (!cacheValid) {
112122
return loadNBTFromFile(name);
113123
}
114124

115-
CompoundTag data = MAPPINGS_CACHE.get(name);
125+
CompoundTag data = mappingsCache.get(name);
116126
if (data != null) {
117127
return data;
118128
}
119129

120130
data = loadNBTFromFile(name);
121131

122132
if (cache && data != null) {
123-
MAPPINGS_CACHE.put(name, data);
133+
mappingsCache.put(name, data);
124134
}
125135
return data;
126136
}
127137

128-
public static @Nullable CompoundTag loadNBT(final String name) {
138+
public @Nullable CompoundTag loadNBT(final String name) {
129139
return loadNBT(name, false);
130140
}
131141

132-
public static @Nullable CompoundTag loadNBTFromFile(final String name) {
142+
public @Nullable CompoundTag loadNBTFromFile(final String name) {
133143
final InputStream resource = getResource(name);
134144
if (resource == null) {
135145
return null;
@@ -142,7 +152,7 @@ public static void clearCache() {
142152
}
143153
}
144154

145-
public static @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) {
155+
public @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) {
146156
return loadMappings(mappingsTag, key, size -> {
147157
final int[] array = new int[size];
148158
Arrays.fill(array, -1);
@@ -151,12 +161,12 @@ public static void clearCache() {
151161
}
152162

153163
@Beta
154-
public static <M extends Mappings, V> @Nullable Mappings loadMappings(
155-
final CompoundTag mappingsTag,
156-
final String key,
157-
final MappingHolderSupplier<V> holderSupplier,
158-
final AddConsumer<V> addConsumer,
159-
final MappingsSupplier<M, V> mappingsSupplier
164+
public <M extends Mappings, V> @Nullable Mappings loadMappings(
165+
final CompoundTag mappingsTag,
166+
final String key,
167+
final MappingHolderSupplier<V> holderSupplier,
168+
final AddConsumer<V> addConsumer,
169+
final MappingsSupplier<M, V> mappingsSupplier
160170
) {
161171
final CompoundTag tag = mappingsTag.getCompoundTag(key);
162172
if (tag == null) {
@@ -227,7 +237,7 @@ public static void clearCache() {
227237
return mappingsSupplier.create(mappings, mappedSizeTag.asInt());
228238
}
229239

230-
public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
240+
public FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
231241
final ListTag<StringTag> unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class);
232242
final ListTag<StringTag> mappedElements = mappedIdentifiers.getListTag(key, StringTag.class);
233243
if (unmappedElements == null || mappedElements == null) {
@@ -240,9 +250,9 @@ public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final
240250
}
241251

242252
return new FullMappingsBase(
243-
unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
244-
mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
245-
mappings
253+
unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
254+
mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
255+
mappings
246256
);
247257
}
248258

@@ -252,7 +262,7 @@ public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final
252262
* @param object json object
253263
* @return map with indexes hashed by their id value
254264
*/
255-
public static Object2IntMap<String> indexedObjectToMap(final JsonObject object) {
265+
public Object2IntMap<String> indexedObjectToMap(final JsonObject object) {
256266
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(object.size(), .99F);
257267
map.defaultReturnValue(-1);
258268
for (final Map.Entry<String, JsonElement> entry : object.entrySet()) {
@@ -267,7 +277,7 @@ public static Object2IntMap<String> indexedObjectToMap(final JsonObject object)
267277
* @param array json array
268278
* @return map with indexes hashed by their id value
269279
*/
270-
public static Object2IntMap<String> arrayToMap(final JsonArray array) {
280+
public Object2IntMap<String> arrayToMap(final JsonArray array) {
271281
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(array.size(), .99F);
272282
map.defaultReturnValue(-1);
273283
for (int i = 0; i < array.size(); i++) {
@@ -276,8 +286,12 @@ public static Object2IntMap<String> arrayToMap(final JsonArray array) {
276286
return map;
277287
}
278288

279-
public static @Nullable InputStream getResource(final String name) {
280-
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
289+
public @Nullable InputStream getResource(final String name) {
290+
return dataLoaderClass.getClassLoader().getResourceAsStream(dataPath + name);
291+
}
292+
293+
public File getFile(final String name) {
294+
return new File(Via.getPlatform().getDataFolder(), name);
281295
}
282296

283297
@FunctionalInterface

common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public void shutdownLoaderExecutor() {
526526
mappingLoaderFutures = null;
527527

528528
// Clear cached mapping files
529-
MappingDataLoader.clearCache();
529+
MappingDataLoader.INSTANCE.clearCache();
530530
}
531531

532532
private Function<Throwable, Void> mappingLoaderThrowable(Class<? extends Protocol> protocolClass) {

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public static void init() {
167167
}
168168

169169
Via.getPlatform().getLogger().info("Loading block connection mappings ...");
170-
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
170+
ListTag<StringTag> blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
171171
for (int id = 0; id < blockStates.size(); id++) {
172172
String key = blockStates.get(id).getValue();
173173
KEY_TO_ID.put(key, id);
@@ -178,7 +178,7 @@ public static void init() {
178178
if (!Via.getConfig().isReduceBlockStorageMemory()) {
179179
blockConnectionData = new Int2ObjectOpenHashMap<>(2048);
180180

181-
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
181+
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.INSTANCE.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
182182
for (CompoundTag blockTag : blockConnectionMappings) {
183183
BlockData blockData = new BlockData();
184184
for (Entry<String, Tag> entry : blockTag.entrySet()) {

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected void loadExtras(final CompoundTag data) {
8181
blockMappings.setNewId(1557, 3986); // chiseled stone bricks
8282
}
8383

84-
JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json");
84+
JsonObject object = MappingDataLoader.INSTANCE.loadFromDataDir("channelmappings-1.13.json");
8585
if (object != null) {
8686
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
8787
String oldChannel = entry.getKey();
@@ -145,7 +145,7 @@ protected void loadExtras(final CompoundTag data) {
145145
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
146146
// Special cursed case
147147
if (key.equals("items")) {
148-
return (BiMappings) MappingDataLoader.loadMappings(data, "items", size -> {
148+
return (BiMappings) MappingDataLoader.INSTANCE.loadMappings(data, "items", size -> {
149149
final Int2IntBiHashMap map = new Int2IntBiHashMap(size);
150150
map.defaultReturnValue(-1);
151151
return map;

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MappingData() {
3535

3636
@Override
3737
public void loadExtras(final CompoundTag data) {
38-
final CompoundTag heightmap = MappingDataLoader.loadNBT("heightmap-1.14.nbt");
38+
final CompoundTag heightmap = MappingDataLoader.INSTANCE.loadNBT("heightmap-1.14.nbt");
3939
final IntArrayTag motionBlocking = heightmap.getIntArrayTag("motionBlocking");
4040
this.motionBlocking = new IntOpenHashSet(motionBlocking.getValue());
4141

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
2121
import com.github.steveice10.opennbt.tag.builtin.ListTag;
22-
import com.github.steveice10.opennbt.tag.builtin.Tag;
2322
import com.viaversion.viaversion.api.data.MappingDataBase;
2423
import com.viaversion.viaversion.api.data.MappingDataLoader;
2524
import java.util.HashMap;
@@ -35,7 +34,7 @@ public MappingData() {
3534

3635
@Override
3736
public void loadExtras(final CompoundTag data) {
38-
dimensionRegistry = MappingDataLoader.loadNBTFromFile("dimension-registry-1.16.2.nbt");
37+
dimensionRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("dimension-registry-1.16.2.nbt");
3938

4039
// Data of each dimension
4140
final ListTag<CompoundTag> dimensions = dimensionRegistry.getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class);

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/MappingData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MappingData() {
3131

3232
@Override
3333
protected void loadExtras(final CompoundTag data) {
34-
damageTypesRegistry = MappingDataLoader.loadNBTFromFile("damage-types-1.19.4.nbt");
34+
damageTypesRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("damage-types-1.19.4.nbt");
3535
}
3636

3737
public CompoundTag damageTypesRegistry() {

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/data/MappingData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
2121
import com.github.steveice10.opennbt.tag.builtin.ListTag;
2222
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
23-
import com.github.steveice10.opennbt.tag.builtin.Tag;
2423
import com.viaversion.viaversion.api.data.MappingDataBase;
2524
import com.viaversion.viaversion.api.data.MappingDataLoader;
2625
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
@@ -37,7 +36,7 @@ public MappingData() {
3736

3837
@Override
3938
protected void loadExtras(final CompoundTag daata) {
40-
final ListTag<CompoundTag> chatTypes = MappingDataLoader.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class);
39+
final ListTag<CompoundTag> chatTypes = MappingDataLoader.INSTANCE.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class);
4140
for (final CompoundTag chatType : chatTypes) {
4241
final NumberTag idTag = chatType.getNumberTag("id");
4342
defaultChatTypes.put(idTag.asInt(), chatType);

0 commit comments

Comments
 (0)