From 8d77891f764bfd6fa0e23c64021c64a1afe28cdd Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Wed, 9 Jan 2019 16:50:27 -0200 Subject: [PATCH] not tested villager metadata rewriting --- .../api/minecraft/VillagerData.java | 12 ++++++ .../metadata/types/MetaType1_14.java | 38 +++++++++++++++++++ .../us/myles/ViaVersion/api/type/Type.java | 7 ++-- .../types/minecraft/VillagerDataType.java | 23 +++++++++++ .../type/types/version/Metadata1_14Type.java | 29 ++++++++++++++ .../types/version/MetadataList1_14Type.java | 32 ++++++++++++++++ .../api/type/types/version/Types1_14.java | 18 +++++++++ .../MetadataRewriter.java | 36 ++++++++++++++++++ 8 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/minecraft/VillagerData.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/minecraft/metadata/types/MetaType1_14.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/VillagerDataType.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/version/Types1_14.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/minecraft/VillagerData.java b/common/src/main/java/us/myles/ViaVersion/api/minecraft/VillagerData.java new file mode 100644 index 000000000..656ab9708 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/minecraft/VillagerData.java @@ -0,0 +1,12 @@ +package us.myles.ViaVersion.api.minecraft; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@AllArgsConstructor +@Data +public class VillagerData { + private int type; + private int profession; + private int level; +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/minecraft/metadata/types/MetaType1_14.java b/common/src/main/java/us/myles/ViaVersion/api/minecraft/metadata/types/MetaType1_14.java new file mode 100644 index 000000000..215a5cd76 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/minecraft/metadata/types/MetaType1_14.java @@ -0,0 +1,38 @@ +package us.myles.ViaVersion.api.minecraft.metadata.types; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_13_2to1_13_1.Protocol1_13_2To1_13_1; + +@RequiredArgsConstructor +@Getter +public enum MetaType1_14 implements MetaType { + Byte(0, Type.BYTE), + VarInt(1, Type.VAR_INT), + Float(2, Type.FLOAT), + String(3, Type.STRING), + Chat(4, Type.STRING), + OptChat(5, Type.OPTIONAL_CHAT), + Slot(6, Type.FLAT_VAR_INT_ITEM), + Boolean(7, Type.BOOLEAN), + Vector3F(8, Type.ROTATION), + Position(9, Type.POSITION), + OptPosition(10, Type.OPTIONAL_POSITION), + Direction(11, Type.VAR_INT), + OptUUID(12, Type.OPTIONAL_UUID), + BlockID(13, Type.VAR_INT), + NBTTag(14, Type.NBT), + PARTICLE(15, Protocol1_13_2To1_13_1.PARTICLE_TYPE), + VillagerData(16, Type.VILLAGER_DATA), + Discontinued(99, null); + + private final int typeID; + private final Type type; + + public static MetaType1_14 byId(int id) { + return values()[id]; + } + +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java index ff10237f3..a09e9cf2a 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -3,10 +3,7 @@ package us.myles.ViaVersion.api.type; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.Getter; -import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; -import us.myles.ViaVersion.api.minecraft.EulerAngle; -import us.myles.ViaVersion.api.minecraft.Position; -import us.myles.ViaVersion.api.minecraft.Vector; +import us.myles.ViaVersion.api.minecraft.*; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.type.types.*; import us.myles.ViaVersion.api.type.types.minecraft.*; @@ -75,6 +72,8 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type BLOCK_CHANGE_RECORD = new BlockChangeRecordType(); public static final Type BLOCK_CHANGE_RECORD_ARRAY = new ArrayType<>(Type.BLOCK_CHANGE_RECORD); + public static final Type VILLAGER_DATA = new VillagerDataType(); + /* 1.13 Flat Item (no data) */ public static final Type FLAT_ITEM = new FlatItemType(); public static final Type FLAT_VAR_INT_ITEM = new FlatVarIntItemType(); diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/VillagerDataType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/VillagerDataType.java new file mode 100644 index 000000000..43c65745c --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/VillagerDataType.java @@ -0,0 +1,23 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.VillagerData; +import us.myles.ViaVersion.api.type.Type; + +public class VillagerDataType extends Type { + public VillagerDataType() { + super(VillagerData.class); + } + + @Override + public VillagerData read(ByteBuf buffer) throws Exception { + return new VillagerData(Type.VAR_INT.read(buffer), Type.VAR_INT.read(buffer), Type.VAR_INT.read(buffer)); + } + + @Override + public void write(ByteBuf buffer, VillagerData object) throws Exception { + Type.VAR_INT.write(buffer, object.getType()); + Type.VAR_INT.write(buffer, object.getProfession()); + Type.VAR_INT.write(buffer, object.getLevel()); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java new file mode 100644 index 000000000..f39bec3f2 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Metadata1_14Type.java @@ -0,0 +1,29 @@ +package us.myles.ViaVersion.api.type.types.version; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; +import us.myles.ViaVersion.api.type.types.minecraft.MetaTypeTemplate; + +public class Metadata1_14Type extends MetaTypeTemplate { + @Override + public Metadata read(ByteBuf buffer) throws Exception { + short index = buffer.readUnsignedByte(); + + if (index == 0xff) return null; //End of metadata + MetaType1_14 type = MetaType1_14.byId(buffer.readByte()); + + return new Metadata(index, type, type.getType().read(buffer)); + } + + @Override + public void write(ByteBuf buffer, Metadata object) throws Exception { + if (object == null) { + buffer.writeByte(255); + } else { + buffer.writeByte(object.getId()); + buffer.writeByte(object.getMetaType().getTypeID()); + object.getMetaType().getType().write(buffer, object.getValue()); + } + } +} \ No newline at end of file diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java new file mode 100644 index 000000000..154e07b62 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/MetadataList1_14Type.java @@ -0,0 +1,32 @@ +package us.myles.ViaVersion.api.type.types.version; + +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.types.minecraft.MetaListTypeTemplate; + +import java.util.ArrayList; +import java.util.List; + +public class MetadataList1_14Type extends MetaListTypeTemplate { + @Override + public List read(ByteBuf buffer) throws Exception { + List list = new ArrayList<>(); + Metadata meta; + do { + meta = Types1_14.METADATA.read(buffer); + if (meta != null) + list.add(meta); + } while (meta != null); + + return list; + } + + @Override + public void write(ByteBuf buffer, List object) throws Exception { + for (Metadata m : object) + Types1_14.METADATA.write(buffer, m); + + // Write end of list + Types1_14.METADATA.write(buffer, null); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Types1_14.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Types1_14.java new file mode 100644 index 000000000..161e78c9e --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/Types1_14.java @@ -0,0 +1,18 @@ +package us.myles.ViaVersion.api.type.types.version; + +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.type.Type; + +import java.util.List; + +public class Types1_14 { + /** + * Metadata list type for 1.14 + */ + public static final Type> METADATA_LIST = new MetadataList1_14Type(); + + /** + * Metadata type for 1.14 + */ + public static final Type METADATA = new Metadata1_14Type(); +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java index 3c99f5e16..5d1978906 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java @@ -3,9 +3,11 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_14Types; +import us.myles.ViaVersion.api.minecraft.VillagerData; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_13_2; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; import java.util.ArrayList; @@ -30,6 +32,20 @@ public class MetadataRewriter { int data = (int) metadata.getValue(); metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); } + + if (type.is(Entity1_14Types.EntityType.VILLAGER)) { + if (metadata.getId() == 13) { + // plains + metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); + metadata.setMetaType(MetaType1_14.VillagerData); + } + } else if (type.is(Entity1_14Types.EntityType.ZOMBIE_VILLAGER)) { + if (metadata.getId() == 17) { + // plains + metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); + metadata.setMetaType(MetaType1_14.VillagerData); + } + } } catch (Exception e) { metadatas.remove(metadata); if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { @@ -41,4 +57,24 @@ public class MetadataRewriter { } } + private static int getNewProfessionId(int old) { + // profession -> career + switch (old) { + case 0: // farmer + return 5; + case 1: // librarian + return 9; + case 2: // priest + return 4; // cleric + case 3: // blacksmith + return 1; // armorer + case 4: // butcher + return 2; + case 5: // nitwit + return 11; + default: + return 0; // none + } + } + }