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

Some sanity checks, get item id by name fast

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-03-14 15:04:35 +01:00
Ursprung 817febe605
Commit 7a96498f6d
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
2 geänderte Dateien mit 31 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -18,14 +18,18 @@
package com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data; package com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.MappingDataLoader;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class MappingData extends MappingDataBase { public class MappingData extends MappingDataBase {
private final Object2IntMap<String> byId = new Object2IntOpenHashMap<>();
private final List<String> itemIds = new ArrayList<>(); private final List<String> itemIds = new ArrayList<>();
public MappingData() { public MappingData() {
@ -36,14 +40,17 @@ public class MappingData extends MappingDataBase {
protected void loadExtras(final CompoundTag data) { protected void loadExtras(final CompoundTag data) {
super.loadExtras(data); super.loadExtras(data);
final CompoundTag items = MappingDataLoader.loadNBT("itemIds-1.20.3.nbt"); final ListTag<StringTag> items = MappingDataLoader.loadNBT("itemIds-1.20.3.nbt").getListTag("items", StringTag.class);
for (final StringTag tag : items.getListTag("items", StringTag.class)) { for (int i = 0; i < items.size(); i++) {
final StringTag tag = items.get(i);
itemIds.add(tag.getValue()); itemIds.add(tag.getValue());
byId.put(tag.getValue(), i);
} }
byId.defaultReturnValue(-1);
} }
public int itemId(final String name) { public int itemId(final String name) {
return itemIds.indexOf(name); return byId.getInt(name);
} }
public String itemName(final int id) { public String itemName(final int id) {

Datei anzeigen

@ -671,20 +671,29 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
return; return;
} }
final Item[] items = chargedProjectiles.stream().map(this::itemFromTag).toArray(Item[]::new); final Item[] items = chargedProjectiles.stream().map(this::itemFromTag).filter(Objects::nonNull).toArray(Item[]::new);
data.set(dataKey, items); data.set(dataKey, items);
} }
private int toItemId(final String id) { private int toItemId(final String id) {
final int unmappedId = protocol.getMappingData().itemId(Key.stripMinecraftNamespace(id)); final int unmappedId = protocol.getMappingData().itemId(Key.stripMinecraftNamespace(id));
return protocol.getMappingData().getNewItemId(unmappedId); return unmappedId != -1 ? protocol.getMappingData().getNewItemId(unmappedId) : -1;
} }
private Item itemFromTag(final CompoundTag item) { private @Nullable Item itemFromTag(final CompoundTag item) {
final StringTag id = item.getStringTag("id"); final String id = item.getString("id");
final NumberTag count = item.getNumberTag("Count"); if (id == null) {
return null;
}
final int itemId = toItemId(id);
if (itemId == -1) {
return null;
}
final byte count = item.getByte("Count", (byte) 1);
final CompoundTag tag = item.getCompoundTag("tag"); final CompoundTag tag = item.getCompoundTag("tag");
return handleItemToClient(new DataItem(toItemId(id.getValue()), count.asByte(), (short) 0, tag)); return handleItemToClient(new DataItem(itemId, count, (short) 0, tag));
} }
private void updateEnchantments(final StructuredDataContainer data, final CompoundTag tag, final String key, private void updateEnchantments(final StructuredDataContainer data, final CompoundTag tag, final String key,
@ -746,11 +755,15 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
private void updateBees(final StructuredDataContainer data, final ListTag<CompoundTag> beesTag) { private void updateBees(final StructuredDataContainer data, final ListTag<CompoundTag> beesTag) {
final Bee[] bees = beesTag.stream().map(bee -> { final Bee[] bees = beesTag.stream().map(bee -> {
final CompoundTag entityData = bee.getCompoundTag("EntityData"); final CompoundTag entityData = bee.getCompoundTag("EntityData");
if (entityData == null) {
return null;
}
final int ticksInHive = bee.getInt("TicksInHive"); final int ticksInHive = bee.getInt("TicksInHive");
final int minOccupationTicks = bee.getInt("MinOccupationTicks"); final int minOccupationTicks = bee.getInt("MinOccupationTicks");
return new Bee(entityData, ticksInHive, minOccupationTicks); return new Bee(entityData, ticksInHive, minOccupationTicks);
}).toArray(Bee[]::new); }).filter(Objects::nonNull).toArray(Bee[]::new);
data.set(StructuredDataKey.BEES, bees); data.set(StructuredDataKey.BEES, bees);
} }
@ -837,7 +850,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
} }
final ListTag<StringTag> sherdsTag = tag.getListTag("sherds", StringTag.class); final ListTag<StringTag> sherdsTag = tag.getListTag("sherds", StringTag.class);
if (sherdsTag != null) { if (sherdsTag != null && sherdsTag.size() == 4) {
final String sherd1 = sherdsTag.get(0).getValue(); final String sherd1 = sherdsTag.get(0).getValue();
final String sherd2 = sherdsTag.get(1).getValue(); final String sherd2 = sherdsTag.get(1).getValue();
final String sherd3 = sherdsTag.get(2).getValue(); final String sherd3 = sherdsTag.get(2).getValue();