mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-22 10:05:12 +01:00
Allow platforms to use the mapping system (#3754)
This commit is contained in:
parent
18f04bf8ea
commit
098f7ff3c2
@ -70,7 +70,7 @@ public class MappingDataBase implements MappingData {
|
||||
getLogger().info("Loading " + unmappedVersion + " -> " + mappedVersion + " mappings...");
|
||||
}
|
||||
|
||||
final CompoundTag data = readNBTFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
|
||||
final CompoundTag data = readMappingsFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
|
||||
blockMappings = loadMappings(data, "blocks");
|
||||
blockStateMappings = loadMappings(data, "blockstates");
|
||||
blockEntityMappings = loadMappings(data, "blockentities");
|
||||
@ -82,8 +82,8 @@ public class MappingDataBase implements MappingData {
|
||||
attributeMappings = loadMappings(data, "attributes");
|
||||
itemMappings = loadBiMappings(data, "items");
|
||||
|
||||
final CompoundTag unmappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + unmappedVersion + ".nbt", true);
|
||||
final CompoundTag mappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + mappedVersion + ".nbt", true);
|
||||
final CompoundTag unmappedIdentifierData = readIdentifiersFile("identifiers-" + unmappedVersion + ".nbt");
|
||||
final CompoundTag mappedIdentifierData = readIdentifiersFile("identifiers-" + mappedVersion + ".nbt");
|
||||
if (unmappedIdentifierData != null && mappedIdentifierData != null) {
|
||||
entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities");
|
||||
argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes");
|
||||
@ -114,16 +114,20 @@ public class MappingDataBase implements MappingData {
|
||||
loadExtras(data);
|
||||
}
|
||||
|
||||
protected @Nullable CompoundTag readNBTFile(final String name) {
|
||||
return MappingDataLoader.loadNBT(name);
|
||||
protected @Nullable CompoundTag readMappingsFile(final String name) {
|
||||
return MappingDataLoader.INSTANCE.loadNBT(name);
|
||||
}
|
||||
|
||||
protected @Nullable CompoundTag readIdentifiersFile(final String name) {
|
||||
return MappingDataLoader.INSTANCE.loadNBT(name, true);
|
||||
}
|
||||
|
||||
protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) {
|
||||
return MappingDataLoader.loadMappings(data, key);
|
||||
return MappingDataLoader.INSTANCE.loadMappings(data, key);
|
||||
}
|
||||
|
||||
protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
|
||||
return MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
|
||||
return MappingDataLoader.INSTANCE.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
|
||||
}
|
||||
|
||||
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
|
||||
|
@ -51,18 +51,28 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public final class MappingDataLoader {
|
||||
public class MappingDataLoader {
|
||||
|
||||
private static final Map<String, CompoundTag> MAPPINGS_CACHE = new HashMap<>();
|
||||
private static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
|
||||
public static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
|
||||
private static final byte DIRECT_ID = 0;
|
||||
private static final byte SHIFTS_ID = 1;
|
||||
private static final byte CHANGES_ID = 2;
|
||||
private static final byte IDENTITY_ID = 3;
|
||||
private static boolean cacheValid = true;
|
||||
|
||||
public static void clearCache() {
|
||||
MAPPINGS_CACHE.clear();
|
||||
public static final MappingDataLoader INSTANCE = new MappingDataLoader(MappingDataLoader.class, "assets/viaversion/data/");
|
||||
|
||||
private final Map<String, CompoundTag> mappingsCache = new HashMap<>();
|
||||
private final Class<?> dataLoaderClass;
|
||||
private final String dataPath;
|
||||
private boolean cacheValid = true;
|
||||
|
||||
public MappingDataLoader(final Class<?> dataLoaderClass, final String dataPath) {
|
||||
this.dataLoaderClass = dataLoaderClass;
|
||||
this.dataPath = dataPath;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
mappingsCache.clear();
|
||||
cacheValid = false;
|
||||
}
|
||||
|
||||
@ -71,8 +81,8 @@ public final class MappingDataLoader {
|
||||
*
|
||||
* @return loaded json object, or null if not found or invalid
|
||||
*/
|
||||
public static @Nullable JsonObject loadFromDataDir(final String name) {
|
||||
final File file = new File(Via.getPlatform().getDataFolder(), name);
|
||||
public @Nullable JsonObject loadFromDataDir(final String name) {
|
||||
final File file = getFile(name);
|
||||
if (!file.exists()) {
|
||||
return loadData(name);
|
||||
}
|
||||
@ -94,7 +104,7 @@ public final class MappingDataLoader {
|
||||
*
|
||||
* @return loaded json object from bundled resources if present
|
||||
*/
|
||||
public static @Nullable JsonObject loadData(final String name) {
|
||||
public @Nullable JsonObject loadData(final String name) {
|
||||
final InputStream stream = getResource(name);
|
||||
if (stream == null) {
|
||||
return null;
|
||||
@ -107,12 +117,12 @@ public final class MappingDataLoader {
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable CompoundTag loadNBT(final String name, final boolean cache) {
|
||||
public @Nullable CompoundTag loadNBT(final String name, final boolean cache) {
|
||||
if (!cacheValid) {
|
||||
return loadNBTFromFile(name);
|
||||
}
|
||||
|
||||
CompoundTag data = MAPPINGS_CACHE.get(name);
|
||||
CompoundTag data = mappingsCache.get(name);
|
||||
if (data != null) {
|
||||
return data;
|
||||
}
|
||||
@ -120,16 +130,16 @@ public final class MappingDataLoader {
|
||||
data = loadNBTFromFile(name);
|
||||
|
||||
if (cache && data != null) {
|
||||
MAPPINGS_CACHE.put(name, data);
|
||||
mappingsCache.put(name, data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public static @Nullable CompoundTag loadNBT(final String name) {
|
||||
public @Nullable CompoundTag loadNBT(final String name) {
|
||||
return loadNBT(name, false);
|
||||
}
|
||||
|
||||
public static @Nullable CompoundTag loadNBTFromFile(final String name) {
|
||||
public @Nullable CompoundTag loadNBTFromFile(final String name) {
|
||||
final InputStream resource = getResource(name);
|
||||
if (resource == null) {
|
||||
return null;
|
||||
@ -142,7 +152,7 @@ public final class MappingDataLoader {
|
||||
}
|
||||
}
|
||||
|
||||
public static @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) {
|
||||
public @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) {
|
||||
return loadMappings(mappingsTag, key, size -> {
|
||||
final int[] array = new int[size];
|
||||
Arrays.fill(array, -1);
|
||||
@ -151,12 +161,12 @@ public final class MappingDataLoader {
|
||||
}
|
||||
|
||||
@Beta
|
||||
public static <M extends Mappings, V> @Nullable Mappings loadMappings(
|
||||
final CompoundTag mappingsTag,
|
||||
final String key,
|
||||
final MappingHolderSupplier<V> holderSupplier,
|
||||
final AddConsumer<V> addConsumer,
|
||||
final MappingsSupplier<M, V> mappingsSupplier
|
||||
public <M extends Mappings, V> @Nullable Mappings loadMappings(
|
||||
final CompoundTag mappingsTag,
|
||||
final String key,
|
||||
final MappingHolderSupplier<V> holderSupplier,
|
||||
final AddConsumer<V> addConsumer,
|
||||
final MappingsSupplier<M, V> mappingsSupplier
|
||||
) {
|
||||
final CompoundTag tag = mappingsTag.getCompoundTag(key);
|
||||
if (tag == null) {
|
||||
@ -227,7 +237,7 @@ public final class MappingDataLoader {
|
||||
return mappingsSupplier.create(mappings, mappedSizeTag.asInt());
|
||||
}
|
||||
|
||||
public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
|
||||
public FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
|
||||
final ListTag<StringTag> unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class);
|
||||
final ListTag<StringTag> mappedElements = mappedIdentifiers.getListTag(key, StringTag.class);
|
||||
if (unmappedElements == null || mappedElements == null) {
|
||||
@ -240,9 +250,9 @@ public final class MappingDataLoader {
|
||||
}
|
||||
|
||||
return new FullMappingsBase(
|
||||
unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
|
||||
mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
|
||||
mappings
|
||||
unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
|
||||
mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
|
||||
mappings
|
||||
);
|
||||
}
|
||||
|
||||
@ -252,7 +262,7 @@ public final class MappingDataLoader {
|
||||
* @param object json object
|
||||
* @return map with indexes hashed by their id value
|
||||
*/
|
||||
public static Object2IntMap<String> indexedObjectToMap(final JsonObject object) {
|
||||
public Object2IntMap<String> indexedObjectToMap(final JsonObject object) {
|
||||
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(object.size(), .99F);
|
||||
map.defaultReturnValue(-1);
|
||||
for (final Map.Entry<String, JsonElement> entry : object.entrySet()) {
|
||||
@ -267,7 +277,7 @@ public final class MappingDataLoader {
|
||||
* @param array json array
|
||||
* @return map with indexes hashed by their id value
|
||||
*/
|
||||
public static Object2IntMap<String> arrayToMap(final JsonArray array) {
|
||||
public Object2IntMap<String> arrayToMap(final JsonArray array) {
|
||||
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(array.size(), .99F);
|
||||
map.defaultReturnValue(-1);
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
@ -276,8 +286,12 @@ public final class MappingDataLoader {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @Nullable InputStream getResource(final String name) {
|
||||
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name);
|
||||
public @Nullable InputStream getResource(final String name) {
|
||||
return dataLoaderClass.getClassLoader().getResourceAsStream(dataPath + name);
|
||||
}
|
||||
|
||||
public File getFile(final String name) {
|
||||
return new File(Via.getPlatform().getDataFolder(), name);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
|
@ -526,7 +526,7 @@ public class ProtocolManagerImpl implements ProtocolManager {
|
||||
mappingLoaderFutures = null;
|
||||
|
||||
// Clear cached mapping files
|
||||
MappingDataLoader.clearCache();
|
||||
MappingDataLoader.INSTANCE.clearCache();
|
||||
}
|
||||
|
||||
private Function<Throwable, Void> mappingLoaderThrowable(Class<? extends Protocol> protocolClass) {
|
||||
|
@ -167,7 +167,7 @@ public final class ConnectionData {
|
||||
}
|
||||
|
||||
Via.getPlatform().getLogger().info("Loading block connection mappings ...");
|
||||
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
|
||||
ListTag<StringTag> blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
|
||||
for (int id = 0; id < blockStates.size(); id++) {
|
||||
String key = blockStates.get(id).getValue();
|
||||
KEY_TO_ID.put(key, id);
|
||||
@ -178,7 +178,7 @@ public final class ConnectionData {
|
||||
if (!Via.getConfig().isReduceBlockStorageMemory()) {
|
||||
blockConnectionData = new Int2ObjectOpenHashMap<>(2048);
|
||||
|
||||
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
|
||||
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.INSTANCE.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
|
||||
for (CompoundTag blockTag : blockConnectionMappings) {
|
||||
BlockData blockData = new BlockData();
|
||||
for (Entry<String, Tag> entry : blockTag.entrySet()) {
|
||||
|
@ -81,7 +81,7 @@ public class MappingData extends MappingDataBase {
|
||||
blockMappings.setNewId(1557, 3986); // chiseled stone bricks
|
||||
}
|
||||
|
||||
JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json");
|
||||
JsonObject object = MappingDataLoader.INSTANCE.loadFromDataDir("channelmappings-1.13.json");
|
||||
if (object != null) {
|
||||
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
|
||||
String oldChannel = entry.getKey();
|
||||
@ -145,7 +145,7 @@ public class MappingData extends MappingDataBase {
|
||||
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
|
||||
// Special cursed case
|
||||
if (key.equals("items")) {
|
||||
return (BiMappings) MappingDataLoader.loadMappings(data, "items", size -> {
|
||||
return (BiMappings) MappingDataLoader.INSTANCE.loadMappings(data, "items", size -> {
|
||||
final Int2IntBiHashMap map = new Int2IntBiHashMap(size);
|
||||
map.defaultReturnValue(-1);
|
||||
return map;
|
||||
|
@ -35,7 +35,7 @@ public class MappingData extends MappingDataBase {
|
||||
|
||||
@Override
|
||||
public void loadExtras(final CompoundTag data) {
|
||||
final CompoundTag heightmap = MappingDataLoader.loadNBT("heightmap-1.14.nbt");
|
||||
final CompoundTag heightmap = MappingDataLoader.INSTANCE.loadNBT("heightmap-1.14.nbt");
|
||||
final IntArrayTag motionBlocking = heightmap.getIntArrayTag("motionBlocking");
|
||||
this.motionBlocking = new IntOpenHashSet(motionBlocking.getValue());
|
||||
|
||||
|
@ -19,7 +19,6 @@ package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data;
|
||||
|
||||
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
||||
import com.viaversion.viaversion.api.data.MappingDataBase;
|
||||
import com.viaversion.viaversion.api.data.MappingDataLoader;
|
||||
import java.util.HashMap;
|
||||
@ -35,7 +34,7 @@ public class MappingData extends MappingDataBase {
|
||||
|
||||
@Override
|
||||
public void loadExtras(final CompoundTag data) {
|
||||
dimensionRegistry = MappingDataLoader.loadNBTFromFile("dimension-registry-1.16.2.nbt");
|
||||
dimensionRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("dimension-registry-1.16.2.nbt");
|
||||
|
||||
// Data of each dimension
|
||||
final ListTag<CompoundTag> dimensions = dimensionRegistry.getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class);
|
||||
|
@ -31,7 +31,7 @@ public final class MappingData extends MappingDataBase {
|
||||
|
||||
@Override
|
||||
protected void loadExtras(final CompoundTag data) {
|
||||
damageTypesRegistry = MappingDataLoader.loadNBTFromFile("damage-types-1.19.4.nbt");
|
||||
damageTypesRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("damage-types-1.19.4.nbt");
|
||||
}
|
||||
|
||||
public CompoundTag damageTypesRegistry() {
|
||||
|
@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data;
|
||||
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.ListTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
||||
import com.viaversion.viaversion.api.data.MappingDataBase;
|
||||
import com.viaversion.viaversion.api.data.MappingDataLoader;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
@ -37,7 +36,7 @@ public final class MappingData extends MappingDataBase {
|
||||
|
||||
@Override
|
||||
protected void loadExtras(final CompoundTag daata) {
|
||||
final ListTag<CompoundTag> chatTypes = MappingDataLoader.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class);
|
||||
final ListTag<CompoundTag> chatTypes = MappingDataLoader.INSTANCE.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class);
|
||||
for (final CompoundTag chatType : chatTypes) {
|
||||
final NumberTag idTag = chatType.getNumberTag("id");
|
||||
defaultChatTypes.put(idTag.asInt(), chatType);
|
||||
|
Loading…
Reference in New Issue
Block a user