More renames

This commit is contained in:
Nassim Jahnke 2024-02-29 12:08:29 +01:00
parent 6cdca97d35
commit 01bb8dedda
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
9 changed files with 70 additions and 56 deletions

View File

@ -26,7 +26,6 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.annotations.SerializedName;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -96,8 +95,18 @@ public class DataItem implements Item {
}
@Override
public Int2ObjectMap<Optional<StructuredData<?>>> itemData() {
return new Int2ObjectOpenHashMap<>();
public Int2ObjectMap<Optional<StructuredData<?>>> structuredData() {
throw new UnsupportedOperationException();
}
@Override
public void addData(final StructuredData<?> data) {
throw new UnsupportedOperationException();
}
@Override
public void removeDefaultData(final int id) {
throw new UnsupportedOperationException();
}
@Override

View File

@ -91,7 +91,11 @@ public interface Item {
*/
void setTag(@Nullable CompoundTag tag);
Int2ObjectMap<Optional<StructuredData<?>>> itemData();
Int2ObjectMap<Optional<StructuredData<?>>> structuredData();
void addData(StructuredData<?> data);
void removeDefaultData(int id);
/**
* Returns a copy of the item.

View File

@ -29,16 +29,16 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
public class DynamicItem implements Item {
public class StructuredItem implements Item {
private final Int2ObjectMap<Optional<StructuredData<?>>> data;
private int identifier;
private byte amount;
public DynamicItem() {
public StructuredItem() {
this(0, (byte) 0, new Int2ObjectOpenHashMap<>());
}
public DynamicItem(int identifier, byte amount, Int2ObjectMap<Optional<StructuredData<?>>> data) {
public StructuredItem(final int identifier, final byte amount, final Int2ObjectMap<Optional<StructuredData<?>>> data) {
this.identifier = identifier;
this.amount = amount;
this.data = data;
@ -50,7 +50,7 @@ public class DynamicItem implements Item {
}
@Override
public void setIdentifier(int identifier) {
public void setIdentifier(final int identifier) {
this.identifier = identifier;
}
@ -60,7 +60,7 @@ public class DynamicItem implements Item {
}
@Override
public void setAmount(int amount) {
public void setAmount(final int amount) {
if (amount > Byte.MAX_VALUE || amount < Byte.MIN_VALUE) {
throw new IllegalArgumentException("Invalid item amount: " + amount);
}
@ -73,34 +73,36 @@ public class DynamicItem implements Item {
}
@Override
public void setTag(@Nullable CompoundTag tag) {
public void setTag(@Nullable final CompoundTag tag) {
throw new UnsupportedOperationException();
}
@Override
public Int2ObjectMap<Optional<StructuredData<?>>> itemData() {
public Int2ObjectMap<Optional<StructuredData<?>>> structuredData() {
return data;
}
public void addData(StructuredData<?> data) {
@Override
public void addData(final StructuredData<?> data) {
this.data.put(data.id(), Optional.of(data));
}
public void removeDefault(int id) {
@Override
public void removeDefaultData(final int id) {
// Empty optional to override the Minecraft default
this.data.put(id, Optional.empty());
}
@Override
public Item copy() {
return new DynamicItem(identifier, amount, data);
public StructuredItem copy() {
return new StructuredItem(identifier, amount, new Int2ObjectOpenHashMap<>(data));
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final DynamicItem that = (DynamicItem) o;
final StructuredItem that = (StructuredItem) o;
if (identifier != that.identifier) return false;
if (amount != that.amount) return false;
return data.equals(that.data);

View File

@ -22,9 +22,9 @@
*/
package com.viaversion.viaversion.api.type.types.item;
import com.viaversion.viaversion.api.minecraft.item.DynamicItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.item.StructuredItem;
import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
@ -50,7 +50,7 @@ public class ItemType1_20_5 extends Type<Item> {
final int id = Type.VAR_INT.readPrimitive(buffer);
final Int2ObjectMap<Optional<StructuredData<?>>> data = readData(buffer);
return new DynamicItem(id, amount, data);
return new StructuredItem(id, amount, data);
}
private Int2ObjectMap<Optional<StructuredData<?>>> readData(final ByteBuf buffer) throws Exception {
@ -83,7 +83,7 @@ public class ItemType1_20_5 extends Type<Item> {
buffer.writeByte(object.amount());
Type.VAR_INT.writePrimitive(buffer, object.identifier());
final Int2ObjectMap<Optional<StructuredData<?>>> data = object.itemData();
final Int2ObjectMap<Optional<StructuredData<?>>> data = object.structuredData();
int valuesSize = 0;
int markersSize = 0;
for (final Int2ObjectMap.Entry<Optional<StructuredData<?>>> entry : data.int2ObjectEntrySet()) {

View File

@ -36,12 +36,11 @@ import java.util.List;
public final class Types1_20_5 {
// Only safe to use after protocol loading
// Most of these are only safe to use after protocol loading
public static final ParticleType PARTICLE = new ParticleType();
public static final StructuredDataType ITEM_DATA = new StructuredDataType();
public static final Type<Item> ITEM = new ItemType1_20_5(ITEM_DATA);
public static final Type<Item[]> ITEM_ARRAY = new ArrayType<>(ITEM);
public static final MetaTypes1_20_5 META_TYPES = new MetaTypes1_20_5(PARTICLE);
public static final Type<Metadata> METADATA = new MetadataType(META_TYPES);
public static final Type<List<Metadata>> METADATA_LIST = new MetaListType(METADATA);

View File

@ -62,7 +62,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
map(Type.SHORT); // 1 - Slot ID
map(Type.ITEM1_8, Type.ITEM1_13); // 2 - Slot Value
handler(itemToClientHandler(Type.ITEM1_13));
handler(wrapper -> handleItemToClient(wrapper.get(Type.ITEM1_13, 0)));
}
});
protocol.registerClientbound(ClientboundPackets1_12_1.WINDOW_ITEMS, new PacketHandlers() {
@ -196,7 +196,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
map(Type.VAR_INT); // 1 - Slot ID
map(Type.ITEM1_8, Type.ITEM1_13); // 2 - Item
handler(itemToClientHandler(Type.ITEM1_13));
handler(wrapper -> handleItemToClient(wrapper.get(Type.ITEM1_13, 0)));
}
});
@ -211,7 +211,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
map(Type.VAR_INT); // 4 - Mode
map(Type.ITEM1_13, Type.ITEM1_8); // 5 - Clicked Item
handler(itemToServerHandler(Type.ITEM1_8));
handler(wrapper -> handleItemToServer(wrapper.get(Type.ITEM1_8, 0)));
}
});
@ -253,7 +253,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
map(Type.SHORT); // 0 - Slot
map(Type.ITEM1_13, Type.ITEM1_8); // 1 - Clicked Item
handler(itemToServerHandler(Type.ITEM1_8));
handler(wrapper -> handleItemToServer(wrapper.get(Type.ITEM1_8, 0)));
}
});
}

View File

@ -22,10 +22,10 @@ import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.data.ParticleMappings;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.DynamicItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.item.StructuredItem;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_20_3;
@ -175,27 +175,45 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
if (item == null) return null;
super.handleItemToClient(item);
return toStructuredItem(item);
}
final CompoundTag tag = item.tag();
final DynamicItem dynamicItem = new DynamicItem(item.identifier(), (byte) item.amount(), new Int2ObjectOpenHashMap<>());
@Override
public @Nullable Item handleItemToServer(@Nullable final Item item) {
if (item == null) return null;
super.handleItemToServer(item);
return toOldItem(item);
}
public static Item toOldItem(final Item item) {
final CompoundTag tag = new CompoundTag();
// TODO
return new DataItem(item.identifier(), (byte) item.amount(), (short) 0, tag);
}
public static Item toStructuredItem(final Item old) {
final CompoundTag tag = old.tag();
final StructuredItem item = new StructuredItem(old.identifier(), (byte) old.amount(), new Int2ObjectOpenHashMap<>());
if (tag == null) {
return dynamicItem;
return item;
}
// Rewrite nbt to new data structures
final NumberTag damage = tag.getNumberTag("Damage");
if (damage != null) {
addData(dynamicItem, "damage", Type.VAR_INT, damage.asInt());
addData(item, "damage", Type.VAR_INT, damage.asInt());
}
final NumberTag repairCost = tag.getNumberTag("RepairCost");
if (repairCost != null) {
addData(dynamicItem, "repair_cost", Type.VAR_INT, repairCost.asInt());
addData(item, "repair_cost", Type.VAR_INT, repairCost.asInt());
}
return dynamicItem;
// TODO
return item;
}
private <T> void addData(final DynamicItem item, final String serializer, final Type<T> type, final T value) {
private static <T> void addData(final StructuredItem item, final String serializer, final Type<T> type, final T value) {
final int id = serializerId(serializer);
if (id == -1) {
Via.getPlatform().getLogger().severe("Could not find item data serializer for type " + type);
@ -205,17 +223,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
item.addData(new StructuredData<>(type, value, id));
}
private int serializerId(final String type) {
return protocol.getMappingData().getDataComponentSerializerMappings().mappedId(type);
}
@Override
public @Nullable Item handleItemToServer(@Nullable final Item item) {
if (item == null) return null;
super.handleItemToServer(item);
final CompoundTag tag = new CompoundTag();
return new DataItem(item.identifier(), (byte) item.amount(), (short) 0, tag);
private static int serializerId(final String type) {
return Protocol1_20_5To1_20_3.MAPPINGS.getDataComponentSerializerMappings().mappedId(type);
}
}

View File

@ -528,14 +528,6 @@ public class ItemRewriter<C extends ClientboundPacketType, S extends Serverbound
};
}
public PacketHandler itemToClientHandler(Type<Item> type) {
return wrapper -> handleItemToClient(wrapper.get(type, 0));
}
public PacketHandler itemToServerHandler(Type<Item> type) {
return wrapper -> handleItemToServer(wrapper.get(type, 0));
}
private void handleClientboundItem(final PacketWrapper wrapper) throws Exception {
final Item item = handleItemToClient(wrapper.read(itemType));
wrapper.write(mappedItemType, item);

View File

@ -35,7 +35,7 @@ import com.viaversion.viaversion.template.protocols.Protocol1_99To_98;
public final class BlockItemPacketRewriter1_99 extends ItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_99To_98> {
public BlockItemPacketRewriter1_99(final Protocol1_99To_98 protocol) {
super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY);
super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_ARRAY); // Add two more types if they changed
}
@Override