3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Put itemmappings into custom Int2IntBiMap

Dieser Commit ist enthalten in:
KennyTV 2020-06-08 23:54:24 +02:00
Ursprung 5bd1ef882e
Commit 5d8084986f
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
10 geänderte Dateien mit 157 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -346,8 +346,8 @@ public class Protocol1_13To1_12_2 extends Protocol<ClientboundPackets1_12_1, Cli
} }
} else { } else {
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
Integer newItem = MappingData.oldToNewItems.get(item << 4 | i); int newItem = MappingData.oldToNewItems.get(item << 4 | i);
if (newItem != null) { if (newItem != -1) {
PacketWrapper packet = wrapper.create(0x18); PacketWrapper packet = wrapper.create(0x18);
packet.write(Type.VAR_INT, newItem); packet.write(Type.VAR_INT, newItem);
packet.write(Type.VAR_INT, ticks); packet.write(Type.VAR_INT, ticks);

Datei anzeigen

@ -11,6 +11,7 @@ import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.api.data.Mappings;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import us.myles.ViaVersion.util.Int2IntBiMap;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -20,7 +21,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class MappingData { public class MappingData {
public static final BiMap<Integer, Integer> oldToNewItems = HashBiMap.create(); public static final Int2IntBiMap oldToNewItems = new Int2IntBiMap();
public static final Map<String, Integer[]> blockTags = new HashMap<>(); public static final Map<String, Integer[]> blockTags = new HashMap<>();
public static final Map<String, Integer[]> itemTags = new HashMap<>(); public static final Map<String, Integer[]> itemTags = new HashMap<>();
public static final Map<String, Integer[]> fluidTags = new HashMap<>(); public static final Map<String, Integer[]> fluidTags = new HashMap<>();
@ -37,6 +38,7 @@ public class MappingData {
JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json", true); JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json", true);
JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true); JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true);
oldToNewItems.defaultReturnValue(-1);
blockMappings = new BlockMappingsShortArray(mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks")); blockMappings = new BlockMappingsShortArray(mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks"));
MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items")); MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items"));
loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags")); loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags"));

Datei anzeigen

@ -435,7 +435,7 @@ public class InventoryPackets {
} }
} }
item.setIdentifier(MappingData.oldToNewItems.get(rawId).shortValue()); item.setIdentifier(MappingData.oldToNewItems.get(rawId));
item.setData((short) 0); item.setData((short) 0);
} }
@ -487,8 +487,8 @@ public class InventoryPackets {
} }
if (rawId == null) { if (rawId == null) {
Integer oldId = MappingData.oldToNewItems.inverse().get(item.getIdentifier()); int oldId = MappingData.oldToNewItems.inverse().get(item.getIdentifier());
if (oldId != null) { if (oldId != -1) {
// Handle spawn eggs // Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId); Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) { if (eggEntityId.isPresent()) {

Datei anzeigen

@ -1,7 +1,5 @@
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data; package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -10,12 +8,13 @@ import it.unimi.dsi.fastutil.ints.IntSet;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.api.data.Mappings;
import us.myles.ViaVersion.util.Int2IntBiMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class MappingData { public class MappingData {
public static final BiMap<Integer, Integer> oldToNewItems = HashBiMap.create(); public static final Int2IntBiMap oldToNewItems = new Int2IntBiMap();
public static Mappings blockStateMappings; public static Mappings blockStateMappings;
public static Mappings blockMappings; public static Mappings blockMappings;
public static Mappings soundMappings; public static Mappings soundMappings;
@ -27,6 +26,7 @@ public class MappingData {
JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json", true); JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json", true);
JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true); JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true);
oldToNewItems.defaultReturnValue(-1);
blockStateMappings = new Mappings(mapping1_13_2.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates")); blockStateMappings = new Mappings(mapping1_13_2.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates"));
blockMappings = new Mappings(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks")); blockMappings = new Mappings(mapping1_13_2.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks"));
MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_13_2.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items")); MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_13_2.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items"));

Datei anzeigen

@ -271,8 +271,8 @@ public class InventoryPackets {
} }
public static int getNewItemId(int id) { public static int getNewItemId(int id) {
Integer newId = MappingData.oldToNewItems.get(id); int newId = MappingData.oldToNewItems.get(id);
if (newId == null) { if (newId == -1) {
Via.getPlatform().getLogger().warning("Missing 1.14 item for 1.13.2 item " + id); Via.getPlatform().getLogger().warning("Missing 1.14 item for 1.13.2 item " + id);
return 1; return 1;
} }
@ -313,7 +313,7 @@ public class InventoryPackets {
} }
public static int getOldItemId(int id) { public static int getOldItemId(int id) {
Integer oldId = MappingData.oldToNewItems.inverse().get(id); int oldId = MappingData.oldToNewItems.inverse().get(id);
return oldId != null ? oldId : 1; return oldId != -1 ? oldId : 1;
} }
} }

Datei anzeigen

@ -1,14 +1,13 @@
package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data; package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.api.data.Mappings;
import us.myles.ViaVersion.util.Int2IntBiMap;
public class MappingData { public class MappingData {
public static BiMap<Integer, Integer> oldToNewItems = HashBiMap.create(); public static Int2IntBiMap oldToNewItems = new Int2IntBiMap();
public static Mappings blockMappings; public static Mappings blockMappings;
public static Mappings blockStateMappings; public static Mappings blockStateMappings;
public static Mappings soundMappings; public static Mappings soundMappings;
@ -19,6 +18,7 @@ public class MappingData {
JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true); JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json", true);
JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true); JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true);
oldToNewItems.defaultReturnValue(-1);
blockStateMappings = new Mappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates")); blockStateMappings = new Mappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates"));
blockMappings = new Mappings(mapping1_14.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks")); blockMappings = new Mappings(mapping1_14.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks"));
MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_14.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items")); MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_14.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items"));

Datei anzeigen

@ -143,8 +143,8 @@ public class InventoryPackets {
} }
public static int getNewItemId(int id) { public static int getNewItemId(int id) {
Integer newId = MappingData.oldToNewItems.get(id); int newId = MappingData.oldToNewItems.get(id);
if (newId == null) { if (newId == -1) {
Via.getPlatform().getLogger().warning("Missing 1.15 item for 1.14 item " + id); Via.getPlatform().getLogger().warning("Missing 1.15 item for 1.14 item " + id);
return 1; return 1;
} }
@ -152,7 +152,7 @@ public class InventoryPackets {
} }
public static int getOldItemId(int id) { public static int getOldItemId(int id) {
Integer oldId = MappingData.oldToNewItems.inverse().get(id); int oldId = MappingData.oldToNewItems.inverse().get(id);
return oldId != null ? oldId : 1; return oldId != -1 ? oldId : 1;
} }
} }

Datei anzeigen

@ -1,17 +1,16 @@
package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data; package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.Mappings; import us.myles.ViaVersion.api.data.Mappings;
import us.myles.ViaVersion.util.Int2IntBiMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class MappingData { public class MappingData {
public static BiMap<Integer, Integer> oldToNewItems = HashBiMap.create(); public static Int2IntBiMap oldToNewItems = new Int2IntBiMap();
public static Map<String, String> attributeMappings = new HashMap<>(); public static Map<String, String> attributeMappings = new HashMap<>();
public static Mappings blockMappings; public static Mappings blockMappings;
public static Mappings blockStateMappings; public static Mappings blockStateMappings;
@ -23,6 +22,7 @@ public class MappingData {
JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true); JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json", true);
JsonObject mapping1_16 = MappingDataLoader.loadData("mapping-1.16.json", true); JsonObject mapping1_16 = MappingDataLoader.loadData("mapping-1.16.json", true);
oldToNewItems.defaultReturnValue(-1);
blockStateMappings = new Mappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_16.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates")); blockStateMappings = new Mappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_16.getAsJsonObject("blockstates"), diffmapping.getAsJsonObject("blockstates"));
blockMappings = new Mappings(mapping1_15.getAsJsonObject("blocks"), mapping1_16.getAsJsonObject("blocks")); blockMappings = new Mappings(mapping1_15.getAsJsonObject("blocks"), mapping1_16.getAsJsonObject("blocks"));
MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_15.getAsJsonObject("items"), mapping1_16.getAsJsonObject("items"), diffmapping.getAsJsonObject("items")); MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_15.getAsJsonObject("items"), mapping1_16.getAsJsonObject("items"), diffmapping.getAsJsonObject("items"));

Datei anzeigen

@ -206,8 +206,8 @@ public class InventoryPackets {
} }
public static int getNewItemId(int id) { public static int getNewItemId(int id) {
Integer newId = MappingData.oldToNewItems.get(id); int newId = MappingData.oldToNewItems.get(id);
if (newId == null) { if (newId == -1) {
Via.getPlatform().getLogger().warning("Missing 1.16 item for 1.15.2 item " + id); Via.getPlatform().getLogger().warning("Missing 1.16 item for 1.15.2 item " + id);
return 1; return 1;
} }
@ -215,7 +215,7 @@ public class InventoryPackets {
} }
public static int getOldItemId(int id) { public static int getOldItemId(int id) {
Integer oldId = MappingData.oldToNewItems.inverse().get(id); int oldId = MappingData.oldToNewItems.inverse().get(id);
return oldId != null ? oldId : 1; return oldId != -1 ? oldId : 1;
} }
} }

Datei anzeigen

@ -0,0 +1,128 @@
package us.myles.ViaVersion.util;
import com.google.common.base.Preconditions;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* Simple wrapper class for two {@link Int2IntMap}s.
*
* @see #inverse() to get the inversed map
*/
public class Int2IntBiMap implements Int2IntMap {
private final Int2IntMap map;
private final Int2IntBiMap inverse;
public Int2IntBiMap() {
this.map = new Int2IntOpenHashMap();
this.inverse = new Int2IntBiMap(this);
}
private Int2IntBiMap(Int2IntBiMap inverse) {
this.map = new Int2IntOpenHashMap();
this.inverse = inverse;
}
/**
* @return the inverse of this bimap
*/
public Int2IntBiMap inverse() {
return inverse;
}
/**
* Puts the key and value into the maps.
*
* @param key key
* @param value value
* @return old value if present
* @throws IllegalArgumentException if the value already exists in the map
*/
@Override
public int put(int key, int value) {
if (containsKey(key) && value == get(key)) return value;
Preconditions.checkArgument(!containsValue(value), "value already present: %s", value);
map.put(key, value);
inverse.map.put(value, key);
return defaultReturnValue();
}
@Override
public boolean remove(int key, int value) {
map.remove(key, value);
return inverse.map.remove(key, value);
}
@Override
public int get(int key) {
return map.get(key);
}
@Override
public void clear() {
map.clear();
inverse.map.clear();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
@Deprecated
public void putAll(@NotNull Map<? extends Integer, ? extends Integer> m) {
throw new UnsupportedOperationException();
}
@Override
public void defaultReturnValue(int rv) {
map.defaultReturnValue(rv);
inverse.map.defaultReturnValue(rv);
}
@Override
public int defaultReturnValue() {
return map.defaultReturnValue();
}
@Override
public ObjectSet<Entry> int2IntEntrySet() {
return map.int2IntEntrySet();
}
@Override
@NotNull
public IntSet keySet() {
return map.keySet();
}
@Override
@NotNull
public IntSet values() {
return inverse.map.keySet();
}
@Override
public boolean containsKey(int key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(int value) {
return inverse.map.containsKey(value);
}
}