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:
Ursprung
817febe605
Commit
7a96498f6d
@ -18,14 +18,18 @@
|
||||
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.ListTag;
|
||||
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
||||
import com.viaversion.viaversion.api.data.MappingDataBase;
|
||||
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.List;
|
||||
|
||||
public class MappingData extends MappingDataBase {
|
||||
|
||||
private final Object2IntMap<String> byId = new Object2IntOpenHashMap<>();
|
||||
private final List<String> itemIds = new ArrayList<>();
|
||||
|
||||
public MappingData() {
|
||||
@ -36,14 +40,17 @@ public class MappingData extends MappingDataBase {
|
||||
protected void loadExtras(final CompoundTag data) {
|
||||
super.loadExtras(data);
|
||||
|
||||
final CompoundTag items = MappingDataLoader.loadNBT("itemIds-1.20.3.nbt");
|
||||
for (final StringTag tag : items.getListTag("items", StringTag.class)) {
|
||||
final ListTag<StringTag> items = MappingDataLoader.loadNBT("itemIds-1.20.3.nbt").getListTag("items", StringTag.class);
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
final StringTag tag = items.get(i);
|
||||
itemIds.add(tag.getValue());
|
||||
byId.put(tag.getValue(), i);
|
||||
}
|
||||
byId.defaultReturnValue(-1);
|
||||
}
|
||||
|
||||
public int itemId(final String name) {
|
||||
return itemIds.indexOf(name);
|
||||
return byId.getInt(name);
|
||||
}
|
||||
|
||||
public String itemName(final int id) {
|
||||
|
@ -671,20 +671,29 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
|
||||
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);
|
||||
}
|
||||
|
||||
private int toItemId(final String 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) {
|
||||
final StringTag id = item.getStringTag("id");
|
||||
final NumberTag count = item.getNumberTag("Count");
|
||||
private @Nullable Item itemFromTag(final CompoundTag item) {
|
||||
final String id = item.getString("id");
|
||||
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");
|
||||
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,
|
||||
@ -746,11 +755,15 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
|
||||
private void updateBees(final StructuredDataContainer data, final ListTag<CompoundTag> beesTag) {
|
||||
final Bee[] bees = beesTag.stream().map(bee -> {
|
||||
final CompoundTag entityData = bee.getCompoundTag("EntityData");
|
||||
if (entityData == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final int ticksInHive = bee.getInt("TicksInHive");
|
||||
final int minOccupationTicks = bee.getInt("MinOccupationTicks");
|
||||
|
||||
return new Bee(entityData, ticksInHive, minOccupationTicks);
|
||||
}).toArray(Bee[]::new);
|
||||
}).filter(Objects::nonNull).toArray(Bee[]::new);
|
||||
|
||||
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);
|
||||
if (sherdsTag != null) {
|
||||
if (sherdsTag != null && sherdsTag.size() == 4) {
|
||||
final String sherd1 = sherdsTag.get(0).getValue();
|
||||
final String sherd2 = sherdsTag.get(1).getValue();
|
||||
final String sherd3 = sherdsTag.get(2).getValue();
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren