From 23b68a867c4b1054e4810a78788759ea9a33ae7f Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Thu, 9 May 2019 14:43:48 +0200 Subject: [PATCH] abstract MetadataRewriter --- .../api/entities/Entity1_10Types.java | 2 +- .../api/entities/Entity1_11Types.java | 2 +- .../api/entities/Entity1_12Types.java | 2 +- .../api/entities/Entity1_13Types.java | 2 +- .../api/entities/Entity1_14Types.java | 2 +- .../ViaVersion/api/entities/IEntityType.java | 9 + .../ViaVersion/api/protocol/Protocol.java | 11 + .../api/protocol/ProtocolPipeline.java | 7 + .../api/rewriters/MetadataRewriter.java | 50 ++++ .../ViaVersion/api/storage/EntityTracker.java | 3 +- .../protocol1_11to1_10/MetadataRewriter.java | 218 ---------------- .../Protocol1_11To1_10.java | 9 +- .../metadata/MetadataRewriter1_11To1_10.java | 216 ++++++++++++++++ .../storage/EntityTracker1_11.java | 6 +- .../MetadataRewriter.java | 37 --- .../Protocol1_12To1_11_1.java | 7 +- .../MetadataRewriter1_12To1_11_1.java | 27 ++ .../storage/EntityTracker1_12.java | 6 +- .../MetadataRewriter.java | 48 ---- .../Protocol1_13_1To1_13.java | 3 + .../MetadataRewriter1_13_1To1_13.java | 40 +++ .../packets/EntityPackets.java | 10 +- .../MetadataRewriter.java | 109 -------- .../Protocol1_13To1_12_2.java | 3 + .../MetadataRewriter1_13To1_12_2.java | 94 +++++++ .../packets/EntityPackets.java | 10 +- .../storage/EntityTracker1_13.java | 6 +- .../MetadataRewriter.java | 235 ------------------ .../Protocol1_14To1_13_2.java | 3 + .../MetadataRewriter1_14To1_13_2.java | 227 +++++++++++++++++ .../packets/EntityPackets.java | 14 +- .../packets/WorldPackets.java | 4 +- .../storage/EntityTracker1_14.java | 6 +- .../protocol1_9to1_8/Protocol1_9To1_8.java | 3 + .../metadata/MetadataRewriter.java | 156 ------------ .../metadata/MetadataRewriter1_9To1_8.java | 127 ++++++++++ .../packets/EntityPackets.java | 12 +- .../packets/SpawnPackets.java | 20 +- .../storage/EntityTracker1_9.java | 32 +-- 39 files changed, 901 insertions(+), 877 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/entities/IEntityType.java create mode 100644 common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_10Types.java b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_10Types.java index 4010cc358..e04410b0c 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_10Types.java +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_10Types.java @@ -29,7 +29,7 @@ public class Entity1_10Types { @AllArgsConstructor @Getter - public enum EntityType { + public enum EntityType implements IEntityType { ENTITY(-1), DROPPED_ITEM(1, EntityType.ENTITY), EXPERIENCE_ORB(2, EntityType.ENTITY), diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_11Types.java b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_11Types.java index ca8d91b22..6a3b39d36 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_11Types.java +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_11Types.java @@ -28,7 +28,7 @@ public class Entity1_11Types { @AllArgsConstructor @Getter - public enum EntityType { + public enum EntityType implements IEntityType { ENTITY(-1), DROPPED_ITEM(1, ENTITY), EXPERIENCE_ORB(2, ENTITY), diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_12Types.java b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_12Types.java index 4b7cffb18..a9061af4e 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_12Types.java +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_12Types.java @@ -38,7 +38,7 @@ public class Entity1_12Types { @AllArgsConstructor @Getter - public enum EntityType { + public enum EntityType implements IEntityType { ENTITY(-1), DROPPED_ITEM(1, ENTITY), EXPERIENCE_ORB(2, ENTITY), diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_13Types.java b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_13Types.java index 6389f0f09..8227ca4be 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_13Types.java +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_13Types.java @@ -29,7 +29,7 @@ public class Entity1_13Types { @AllArgsConstructor @Getter - public enum EntityType { + public enum EntityType implements IEntityType { // Auto generated ENTITY(-1), // abm diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_14Types.java b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_14Types.java index 6a77337a1..88e0b6490 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_14Types.java +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/Entity1_14Types.java @@ -23,7 +23,7 @@ public class Entity1_14Types { @AllArgsConstructor @Getter - public enum EntityType { + public enum EntityType implements IEntityType { // Auto generated ENTITY(-1), diff --git a/common/src/main/java/us/myles/ViaVersion/api/entities/IEntityType.java b/common/src/main/java/us/myles/ViaVersion/api/entities/IEntityType.java new file mode 100644 index 000000000..b8c6ee35b --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/entities/IEntityType.java @@ -0,0 +1,9 @@ +package us.myles.ViaVersion.api.entities; + +public interface IEntityType { + + int getId(); + + IEntityType getParent(); + +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index ad248bf26..d6621b1de 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -15,12 +15,15 @@ import us.myles.ViaVersion.packets.State; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; public abstract class Protocol { private final Map, ProtocolPacket> incoming = new HashMap<>(); private final Map, ProtocolPacket> outgoing = new HashMap<>(); + private final Map storedObjects = new ConcurrentHashMap<>(); + public Protocol() { registerPackets(); } @@ -173,6 +176,14 @@ public abstract class Protocol { } } + public T get(Class objectClass) { + return (T) storedObjects.get(objectClass); + } + + public void put(Object object) { + storedObjects.put(object.getClass(), object); + } + @Override public String toString() { return "Protocol:" + getClass().getSimpleName(); diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index 6b6d37534..e3e3d762d 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -151,6 +151,13 @@ public class ProtocolPipeline extends Protocol { return false; } + public

P getProtocol(Class

pipeClass) { + for (Protocol protocol : protocolList) { + if (protocol.getClass() == pipeClass) return (P) protocol; + } + return null; + } + /** * Use the pipeline to filter a NMS packet * diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java new file mode 100644 index 000000000..cfe89827d --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java @@ -0,0 +1,50 @@ +package us.myles.ViaVersion.api.rewriters; + +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.IEntityType; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +public abstract class MetadataRewriter { + + public final void handleMetadata(int entityId, T type, List metadatas, UserConnection connection) { + Map metadataMap = new HashMap<>(metadatas.size()); + for (Metadata metadata : metadatas) { + metadataMap.put(metadata.getId(), metadata); + } + + metadataMap = Collections.unmodifiableMap(metadataMap); + + for (Metadata metadata : new ArrayList<>(metadatas)) { + int oldId = metadata.getId(); + try { + handleMetadata(entityId, type, metadata, metadatas, metadataMap, connection); + } catch (Exception e) { + metadatas.remove(metadata); + if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { + Logger logger = Via.getPlatform().getLogger(); + + logger.warning("An error occurred with entity metadata handler"); + logger.warning("This is most likely down to one of your plugins sending bad datawatchers. Please test if this occurs without any plugins except ViaVersion before reporting it on GitHub"); + logger.warning("Also make sure that all your plugins are compatible with your server version."); + logger.warning("Entity type: " + type); + logger.warning("Metadata: " + metadata); + e.printStackTrace(); + } + } + } + } + + protected void handleMetadata(int entityId, T type, Metadata metadata, List metadatas, UserConnection connection) throws Exception {} + + protected void handleMetadata(int entityId, T type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { + handleMetadata(entityId, type, metadata, metadatas, connection); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java index 247d57d27..e9791cd3a 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java @@ -6,11 +6,12 @@ import lombok.Setter; import us.myles.ViaVersion.api.data.ExternalJoinGameListener; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.IEntityType; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { +public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { private final Map clientEntityTypes = new ConcurrentHashMap<>(); @Getter @Setter diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java deleted file mode 100644 index 54824713f..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java +++ /dev/null @@ -1,218 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_11to1_10; - -import com.google.common.base.Optional; -import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; -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_9; -import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_11; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - public static EntityType rewriteEntityType(int numType, List metadata) { - Optional optType = EntityType.findById(numType); - if (!optType.isPresent()) { - Via.getManager().getPlatform().getLogger().severe("Error: could not find Entity type " + numType + " with metadata: " + metadata); - return null; - } - - EntityType type = optType.get(); - - try { - if (type.is(EntityType.GUARDIAN)) { - // ElderGuardian - 4 - Optional options = getById(metadata, 12); - if (options.isPresent()) { - if ((((byte) options.get().getValue()) & 0x04) == 0x04) { - return EntityType.ELDER_GUARDIAN; - } - } - } - if (type.is(EntityType.SKELETON)) { - // WitherSkeleton - 5 - // Stray - 6 - Optional options = getById(metadata, 12); - if (options.isPresent()) { - if (((int) options.get().getValue()) == 1) { - return EntityType.WITHER_SKELETON; - } - if (((int) options.get().getValue()) == 2) { - return EntityType.STRAY; - } - } - } - if (type.is(EntityType.ZOMBIE)) { - // ZombieVillager - 27 - // Husk - 23 - Optional options = getById(metadata, 13); - if (options.isPresent()) { - int value = (int) options.get().getValue(); - if (value > 0 && value < 6) { - metadata.add(new Metadata(16, MetaType1_9.VarInt, value - 1)); // Add profession type to new metadata - return EntityType.ZOMBIE_VILLAGER; - } - if (value == 6) { - return EntityType.HUSK; - } - } - } - if (type.is(EntityType.HORSE)) { - // SkeletonHorse - 28 - // ZombieHorse - 29 - // Donkey - 31 - // Mule - 32 - Optional options = getById(metadata, 14); - if (options.isPresent()) { - if (((int) options.get().getValue()) == 0) { - return EntityType.HORSE; - } - if (((int) options.get().getValue()) == 1) { - return EntityType.DONKEY; - } - if (((int) options.get().getValue()) == 2) { - return EntityType.MULE; - } - if (((int) options.get().getValue()) == 3) { - return EntityType.ZOMBIE_HORSE; - } - if (((int) options.get().getValue()) == 4) { - return EntityType.SKELETON_HORSE; - } - } - } - } catch (Exception e) { - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity type rewriter"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - - return type; - } - - public static void handleMetadata(int entityId, EntityType type, List metadatas, UserConnection connection) { - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - if (metadata.getValue() instanceof Item) { - // Apply rewrite - EntityIdRewriter.toClientItem((Item) metadata.getValue()); - } - if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) { // Guardians - int oldid = metadata.getId(); - if (oldid == 12) { - metadata.setMetaType(MetaType1_9.Boolean); - boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02; - metadata.setValue(val); - } - } - if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) { // Skeletons - int oldid = metadata.getId(); - if (oldid == 12) { - metadatas.remove(metadata); - } - if (oldid == 13) { - metadata.setId(12); - } - } - if (type.isOrHasParent(EntityType.ZOMBIE)) { // Zombie | Zombie Villager | Husk - if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.getId() == 14) { - metadatas.remove(metadata); - } else { - if (metadata.getId() == 15) { - metadata.setId(14); - } else { - if (metadata.getId() == 14) { - metadata.setId(15); - } - } - } - } - if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) { // Horses - // Remap metadata id - int oldid = metadata.getId(); - if (oldid == 14) { // Type - metadatas.remove(metadata); - } - if (oldid == 16) { // Owner - metadata.setId(14); - } - if (oldid == 17) { // Armor - metadata.setId(16); - } - - // Process per type - if (type.is(EntityType.HORSE)) { - // Normal Horse - } else { - // Remove 15, 16 - if (metadata.getId() == 15 || metadata.getId() == 16) { - metadatas.remove(metadata); - } - } - if (type.is(EntityType.DONKEY, EntityType.MULE)) { - // Chested Horse - if (metadata.getId() == 13) { - if ((((byte) metadata.getValue()) & 0x08) == 0x08) { - metadatas.add(new Metadata(15, MetaType1_9.Boolean, true)); - } else { - metadatas.add(new Metadata(15, MetaType1_9.Boolean, false)); - } - } - } - } - if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) { - Optional flags = getById(metadatas, 11); - Optional customName = getById(metadatas, 2); - Optional customNameVisible = getById(metadatas, 3); - if (metadata.getId() == 0 && flags.isPresent() && customName.isPresent() && customNameVisible.isPresent()) { - Metadata meta = flags.get(); - byte data = (byte) metadata.getValue(); - // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true - if ((data & 0x20) == 0x20 && ((byte) meta.getValue() & 0x01) == 0x01 - && ((String) customName.get().getValue()).length() != 0 && (boolean) customNameVisible.get().getValue()) { - EntityTracker1_11 tracker = connection.get(EntityTracker1_11.class); - if (!tracker.isHologram(entityId)) { - tracker.addHologram(entityId); - try { - // Send movement - PacketWrapper wrapper = new PacketWrapper(0x25, null, connection); - wrapper.write(Type.VAR_INT, entityId); - wrapper.write(Type.SHORT, (short) 0); - wrapper.write(Type.SHORT, (short) (128D * (-Via.getConfig().getHologramYOffset() * 32D))); - wrapper.write(Type.SHORT, (short) 0); - wrapper.write(Type.BOOLEAN, true); - - wrapper.send(Protocol1_11To1_10.class); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - } - } - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - - } - - public static Optional getById(List metadatas, int id) { - for (Metadata metadata : metadatas) { - if (metadata.getId() == id) return Optional.of(metadata); - } - return Optional.absent(); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index 30562d7e6..953557b7b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -16,6 +16,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10; import us.myles.ViaVersion.protocols.protocol1_11to1_10.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_11; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; @@ -31,6 +32,8 @@ public class Protocol1_11To1_10 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_11To1_10()); + InventoryPackets.register(this); // Spawn Object @@ -83,13 +86,13 @@ public class Protocol1_11To1_10 extends Protocol { // Change Type :) int type = wrapper.get(Type.VAR_INT, 1); - Entity1_11Types.EntityType entType = MetadataRewriter.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0)); + Entity1_11Types.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0)); if (entType != null) wrapper.set(Type.VAR_INT, 1, entType.getId()); // Register Type ID wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); + get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); } }); } @@ -152,7 +155,7 @@ public class Protocol1_11To1_10 extends Protocol { if (!type.isPresent()) return; - MetadataRewriter.handleMetadata(entityId, type.get(), wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); + get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, type.get(), wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java new file mode 100644 index 000000000..e12421252 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java @@ -0,0 +1,216 @@ +package us.myles.ViaVersion.protocols.protocol1_11to1_10.metadata; + +import com.google.common.base.Optional; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_11Types; +import us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; +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_9; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.EntityIdRewriter; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.Protocol1_11To1_10; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_11; + +import java.util.List; +import java.util.Map; + +public class MetadataRewriter1_11To1_10 extends MetadataRewriter { + + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) { + if (metadata.getValue() instanceof Item) { + // Apply rewrite + EntityIdRewriter.toClientItem((Item) metadata.getValue()); + } + + if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) { // Guardians + int oldid = metadata.getId(); + if (oldid == 12) { + metadata.setMetaType(MetaType1_9.Boolean); + boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02; + metadata.setValue(val); + } + } + + if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) { // Skeletons + int oldid = metadata.getId(); + if (oldid == 12) { + metadatas.remove(metadata); + } + if (oldid == 13) { + metadata.setId(12); + } + } + + if (type.isOrHasParent(EntityType.ZOMBIE)) { // Zombie | Zombie Villager | Husk + if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.getId() == 14) { + metadatas.remove(metadata); + } else { + if (metadata.getId() == 15) { + metadata.setId(14); + } else { + if (metadata.getId() == 14) { + metadata.setId(15); + } + } + } + } + + if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) { // Horses + // Remap metadata id + int oldid = metadata.getId(); + if (oldid == 14) { // Type + metadatas.remove(metadata); + } + if (oldid == 16) { // Owner + metadata.setId(14); + } + if (oldid == 17) { // Armor + metadata.setId(16); + } + + // Process per type + if (type.is(EntityType.HORSE)) { + // Normal Horse + } else { + // Remove 15, 16 + if (metadata.getId() == 15 || metadata.getId() == 16) { + metadatas.remove(metadata); + } + } + if (type.is(EntityType.DONKEY, EntityType.MULE)) { + // Chested Horse + if (metadata.getId() == 13) { + if ((((byte) metadata.getValue()) & 0x08) == 0x08) { + metadatas.add(new Metadata(15, MetaType1_9.Boolean, true)); + } else { + metadatas.add(new Metadata(15, MetaType1_9.Boolean, false)); + } + } + } + } + + if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) { + Optional flags = Optional.fromNullable(metadataMap.get(11)); + Optional customName = Optional.fromNullable(metadataMap.get(2)); + Optional customNameVisible = Optional.fromNullable(metadataMap.get(3)); + if (metadata.getId() == 0 && flags.isPresent() && customName.isPresent() && customNameVisible.isPresent()) { + Metadata meta = flags.get(); + byte data = (byte) metadata.getValue(); + // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true + if ((data & 0x20) == 0x20 && ((byte) meta.getValue() & 0x01) == 0x01 + && !((String) customName.get().getValue()).isEmpty() && (boolean) customNameVisible.get().getValue()) { + EntityTracker1_11 tracker = connection.get(EntityTracker1_11.class); + if (!tracker.isHologram(entityId)) { + tracker.addHologram(entityId); + try { + // Send movement + PacketWrapper wrapper = new PacketWrapper(0x25, null, connection); + wrapper.write(Type.VAR_INT, entityId); + wrapper.write(Type.SHORT, (short) 0); + wrapper.write(Type.SHORT, (short) (128D * (-Via.getConfig().getHologramYOffset() * 32D))); + wrapper.write(Type.SHORT, (short) 0); + wrapper.write(Type.BOOLEAN, true); + + wrapper.send(Protocol1_11To1_10.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + } + } + + public static EntityType rewriteEntityType(int numType, List metadata) { + Optional optType = EntityType.findById(numType); + if (!optType.isPresent()) { + Via.getManager().getPlatform().getLogger().severe("Error: could not find Entity type " + numType + " with metadata: " + metadata); + return null; + } + + EntityType type = optType.get(); + + try { + if (type.is(EntityType.GUARDIAN)) { + // ElderGuardian - 4 + Optional options = getById(metadata, 12); + if (options.isPresent()) { + if ((((byte) options.get().getValue()) & 0x04) == 0x04) { + return EntityType.ELDER_GUARDIAN; + } + } + } + if (type.is(EntityType.SKELETON)) { + // WitherSkeleton - 5 + // Stray - 6 + Optional options = getById(metadata, 12); + if (options.isPresent()) { + if (((int) options.get().getValue()) == 1) { + return EntityType.WITHER_SKELETON; + } + if (((int) options.get().getValue()) == 2) { + return EntityType.STRAY; + } + } + } + if (type.is(EntityType.ZOMBIE)) { + // ZombieVillager - 27 + // Husk - 23 + Optional options = getById(metadata, 13); + if (options.isPresent()) { + int value = (int) options.get().getValue(); + if (value > 0 && value < 6) { + metadata.add(new Metadata(16, MetaType1_9.VarInt, value - 1)); // Add profession type to new metadata + return EntityType.ZOMBIE_VILLAGER; + } + if (value == 6) { + return EntityType.HUSK; + } + } + } + if (type.is(EntityType.HORSE)) { + // SkeletonHorse - 28 + // ZombieHorse - 29 + // Donkey - 31 + // Mule - 32 + Optional options = getById(metadata, 14); + if (options.isPresent()) { + if (((int) options.get().getValue()) == 0) { + return EntityType.HORSE; + } + if (((int) options.get().getValue()) == 1) { + return EntityType.DONKEY; + } + if (((int) options.get().getValue()) == 2) { + return EntityType.MULE; + } + if (((int) options.get().getValue()) == 3) { + return EntityType.ZOMBIE_HORSE; + } + if (((int) options.get().getValue()) == 4) { + return EntityType.SKELETON_HORSE; + } + } + } + } catch (Exception e) { + if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().warning("An error occurred with entity type rewriter"); + Via.getPlatform().getLogger().warning("Metadata: " + metadata); + e.printStackTrace(); + } + } + + return type; + } + + public static Optional getById(List metadatas, int id) { + for (Metadata metadata : metadatas) { + if (metadata.getId() == id) return Optional.of(metadata); + } + return Optional.absent(); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java index 64cf63717..0148b89b7 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java @@ -2,13 +2,13 @@ package us.myles.ViaVersion.protocols.protocol1_11to1_10.storage; import com.google.common.collect.Sets; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_11Types; +import us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; import java.util.Set; -import static us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; - -public class EntityTracker1_11 extends EntityTracker { +public class EntityTracker1_11 extends EntityTracker { private final Set holograms = Sets.newConcurrentHashSet(); public EntityTracker1_11(UserConnection user) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/MetadataRewriter.java deleted file mode 100644 index a4e0c4865..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/MetadataRewriter.java +++ /dev/null @@ -1,37 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_12to1_11_1; - -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_12Types; -import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - public static void handleMetadata(int entityId, Entity1_12Types.EntityType type, List metadatas, UserConnection connection) { - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - if (metadata.getValue() instanceof Item) { - // Apply rewrite - BedRewriter.toClientItem((Item) metadata.getValue()); - } - // Evocation Illager aggressive property became 13 - if (type.is(Entity1_12Types.EntityType.EVOCATION_ILLAGER)) { - if (metadata.getId() == 12) { - metadata.setId(13); - } - } - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java index 4b7142890..0b6b59b79 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java @@ -19,6 +19,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_12; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker1_12; @@ -30,6 +31,8 @@ public class Protocol1_12To1_11_1 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_12To1_11_1()); + InventoryPackets.register(this); // Outgoing // Spawn Object @@ -85,7 +88,7 @@ public class Protocol1_12To1_11_1 extends Protocol { Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, false); // Register Type ID wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); + get(MetadataRewriter1_12To1_11_1.class).handleMetadata(entityId, entType, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); } }); } @@ -244,7 +247,7 @@ public class Protocol1_12To1_11_1 extends Protocol { if (!type.isPresent()) return; - MetadataRewriter.handleMetadata(entityId, type.get(), wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); + get(MetadataRewriter1_12To1_11_1.class).handleMetadata(entityId, type.get(), wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java new file mode 100644 index 000000000..f3a9a13cb --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java @@ -0,0 +1,27 @@ +package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.metadata; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_12Types; +import us.myles.ViaVersion.api.entities.Entity1_12Types.EntityType; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter; + +import java.util.List; + +public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter { + + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { + if (metadata.getValue() instanceof Item) { + // Apply rewrite + BedRewriter.toClientItem((Item) metadata.getValue()); + } + // Evocation Illager aggressive property became 13 + if (type.is(EntityType.EVOCATION_ILLAGER)) { + if (metadata.getId() == 12) { + metadata.setId(13); + } + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java index c49d701db..ab0ea3461 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java @@ -1,11 +1,11 @@ package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_12Types; +import us.myles.ViaVersion.api.entities.Entity1_12Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; -import static us.myles.ViaVersion.api.entities.Entity1_12Types.EntityType; - -public class EntityTracker1_12 extends EntityTracker { +public class EntityTracker1_12 extends EntityTracker { public EntityTracker1_12(UserConnection user) { super(user, EntityType.PLAYER); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/MetadataRewriter.java deleted file mode 100644 index 95ec608ea..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/MetadataRewriter.java +++ /dev/null @@ -1,48 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_13_1to1_13; - -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_13Types; -import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; -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; -import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - - public static void handleMetadata(int entityId, Entity1_13Types.EntityType type, List metadatas, UserConnection connection) { - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - // 1.13 changed item to flat item (no data) - if (metadata.getMetaType() == MetaType1_13.Slot) { - InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_13.BlockID) { - // Convert to new block id - int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); - } - if (type == null) continue; - if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { - // New block format - int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); - } - if (type.isOrHasParent(EntityType.ABSTRACT_ARROW) && metadata.getId() >= 7) { - metadata.setId(metadata.getId() + 1); // New shooter UUID - } - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - } - -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index f5ca05f4c..4b92c31f0 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.WorldPackets; @@ -19,6 +20,8 @@ public class Protocol1_13_1To1_13 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_13_1To1_13()); + EntityPackets.register(this); InventoryPackets.register(this); WorldPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java new file mode 100644 index 000000000..9199dd26c --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java @@ -0,0 +1,40 @@ +package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.metadata; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_13Types; +import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; +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; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; +import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; + +import java.util.List; + +public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter { + + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { + // 1.13 changed item to flat item (no data) + if (metadata.getMetaType() == MetaType1_13.Slot) { + InventoryPackets.toClient((Item) metadata.getValue()); + } else if (metadata.getMetaType() == MetaType1_13.BlockID) { + // Convert to new block id + int data = (int) metadata.getValue(); + metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); + } + + if (type == null) return; + + if (type.isOrHasParent(EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { + // New block format + int data = (int) metadata.getValue(); + metadata.setValue(Protocol1_13_1To1_13.getNewBlockStateId(data)); + } + + if (type.isOrHasParent(EntityType.ABSTRACT_ARROW) && metadata.getId() >= 7) { + metadata.setId(metadata.getId() + 1); // New shooter UUID + } + } + +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java index 1f0c11234..ea39de1f8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java @@ -10,13 +10,13 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { - public static void register(Protocol protocol) { + public static void register(final Protocol protocol) { //spawn entity protocol.registerOutgoing(State.PLAY, 0x0, 0x0, new PacketRemapper() { @@ -81,7 +81,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } @@ -108,7 +108,7 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } @@ -142,7 +142,7 @@ public class EntityPackets { int entityId = wrapper.get(Type.VAR_INT, 0); Optional type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); - MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/MetadataRewriter.java deleted file mode 100644 index 6c8f8494f..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/MetadataRewriter.java +++ /dev/null @@ -1,109 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_13to1_12_2; - -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_13Types; -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; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - public static void handleMetadata(int entityId, Entity1_13Types.EntityType type, List metadatas, UserConnection connection) { - int particleId = -1, parameter1 = 0, parameter2 = 0; - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - // Handle new MetaTypes - if (metadata.getMetaType().getTypeID() > 4) { - metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID() + 1)); - } else { - metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID())); - } - - // Handle String -> Chat DisplayName - if (metadata.getId() == 2) { - metadata.setMetaType(MetaType1_13.OptChat); - if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) { - metadata.setValue(ChatRewriter.legacyTextToJson((String) metadata.getValue())); - } else { - metadata.setValue(null); - } - } - - // 1.13 changed item to flat item (no data) - if (metadata.getMetaType() == MetaType1_13.Slot) { - metadata.setMetaType(MetaType1_13.Slot); - InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_13.BlockID) { - // Convert to new block id - metadata.setValue(WorldPackets.toNewId((int) metadata.getValue())); - } - - // Skip type related changes when the type is null - if (type == null) - continue; - - // Handle new colors - if (type.is(Entity1_13Types.EntityType.WOLF) && metadata.getId() == 17) { - metadata.setValue(15 - (int) metadata.getValue()); - } - - // Handle new zombie meta (INDEX 15 - Boolean - Zombie is shaking while enabled) - if (type.isOrHasParent(Entity1_13Types.EntityType.ZOMBIE)) { - if (metadata.getId() > 14) - metadata.setId(metadata.getId() + 1); - } - - // Handle Minecart inner block - if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { - // New block format - int oldId = (int) metadata.getValue(); - int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15)); - int newId = WorldPackets.toNewId(combined); - metadata.setValue(newId); - } - - // Handle other changes - if (type.is(Entity1_13Types.EntityType.AREA_EFFECT_CLOUD)) { - if (metadata.getId() == 9) { - particleId = (int) metadata.getValue(); - } else if (metadata.getId() == 10) { - parameter1 = (int) metadata.getValue(); - } else if (metadata.getId() == 11) { - parameter2 = (int) metadata.getValue(); - } - - if (metadata.getId() >= 9) - metadatas.remove(metadata); // Remove - } - - if (metadata.getId() == 0) { - metadata.setValue((byte) ((byte) metadata.getValue() & ~0x10)); // Previously unused, now swimming - } - - // TODO: Boat has changed - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - - // Handle AreaEffectCloud outside the loop - if (type != null && type.is(Entity1_13Types.EntityType.AREA_EFFECT_CLOUD) && particleId != -1) { - Particle particle = ParticleRewriter.rewriteParticle(particleId, new Integer[]{parameter1, parameter2}); - if (particle != null && particle.getId() != -1) { - metadatas.add(new Metadata(9, MetaType1_13.PARTICLE, particle)); - } - } - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index a22651d40..3fdcbcbf4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -23,6 +23,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.provi import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.BlockIdData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; @@ -133,6 +134,8 @@ public class Protocol1_13To1_12_2 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_13To1_12_2()); + // Register grouped packet changes EntityPackets.register(this); WorldPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java new file mode 100644 index 000000000..2353a8a75 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java @@ -0,0 +1,94 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_13Types; +import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; +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; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; + +import java.util.List; +import java.util.Map; + +public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { + + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { + // Handle new MetaTypes + if (metadata.getMetaType().getTypeID() > 4) { + metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID() + 1)); + } else { + metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID())); + } + + // Handle String -> Chat DisplayName + if (metadata.getId() == 2) { + metadata.setMetaType(MetaType1_13.OptChat); + if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) { + metadata.setValue(ChatRewriter.legacyTextToJson((String) metadata.getValue())); + } else { + metadata.setValue(null); + } + } + + // 1.13 changed item to flat item (no data) + if (metadata.getMetaType() == MetaType1_13.Slot) { + metadata.setMetaType(MetaType1_13.Slot); + InventoryPackets.toClient((Item) metadata.getValue()); + } else if (metadata.getMetaType() == MetaType1_13.BlockID) { + // Convert to new block id + metadata.setValue(WorldPackets.toNewId((int) metadata.getValue())); + } + + // Skip type related changes when the type is null + if (type == null) return; + + // Handle new colors + if (type.is(EntityType.WOLF) && metadata.getId() == 17) { + metadata.setValue(15 - (int) metadata.getValue()); + } + + // Handle new zombie meta (INDEX 15 - Boolean - Zombie is shaking while enabled) + if (type.isOrHasParent(EntityType.ZOMBIE)) { + if (metadata.getId() > 14) + metadata.setId(metadata.getId() + 1); + } + + // Handle Minecart inner block + if (type.isOrHasParent(EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { + // New block format + int oldId = (int) metadata.getValue(); + int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15)); + int newId = WorldPackets.toNewId(combined); + metadata.setValue(newId); + } + + // Handle other changes + if (type.is(EntityType.AREA_EFFECT_CLOUD)) { + if (metadata.getId() == 9) { + int particleId = (int) metadata.getValue(); + int parameter1 = metadataMap.containsKey(10) ? (int) metadataMap.get(10).getValue() : 0; + int parameter2 = metadataMap.containsKey(11) ? (int) metadataMap.get(11).getValue() : 0; + + Particle particle = ParticleRewriter.rewriteParticle(particleId, new Integer[]{parameter1, parameter2}); + if (particle != null && particle.getId() != -1) { + metadatas.add(new Metadata(9, MetaType1_13.PARTICLE, particle)); + } + } + + if (metadata.getId() >= 9) + metadatas.remove(metadata); // Remove + } + + if (metadata.getId() == 0) { + metadata.setValue((byte) ((byte) metadata.getValue() & ~0x10)); // Previously unused, now swimming + } + + // TODO: Boat has changed + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java index 2b848ac14..304f623c6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java @@ -10,12 +10,12 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_12; import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { - public static void register(Protocol protocol) { + public static void register(final Protocol protocol) { // Outgoing packets // Spawn Object @@ -112,7 +112,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13To1_12_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } @@ -139,7 +139,7 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13To1_12_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } @@ -172,7 +172,7 @@ public class EntityPackets { int entityId = wrapper.get(Type.VAR_INT, 0); Optional type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); - MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_13To1_12_2.class).handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java index 454bcd18e..f3556f24f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java @@ -1,11 +1,11 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_13Types; +import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; -import static us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; - -public class EntityTracker1_13 extends EntityTracker { +public class EntityTracker1_13 extends EntityTracker { public EntityTracker1_13(UserConnection user) { super(user, EntityType.PLAYER); 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 deleted file mode 100644 index f2015af0e..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ /dev/null @@ -1,235 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_14to1_13_2; - -import us.myles.ViaVersion.api.PacketWrapper; -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_14; -import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - - public static void handleMetadata(int entityId, Entity1_14Types.EntityType type, List metadatas, UserConnection connection) { - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - metadata.setMetaType(MetaType1_14.byId(metadata.getMetaType().getTypeID())); - - EntityTracker1_14 tracker = connection.get(EntityTracker1_14.class); - - if (metadata.getMetaType() == MetaType1_14.Slot) { - InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { - // Convert to new block id - int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); - } - - if (type == null) continue; - - //Metadata 6 added to abstract_entity - if (metadata.getId() > 5) { - metadata.setId(metadata.getId() + 1); - } - - //Metadata 12 added to living_entity - if (metadata.getId() > 11 && type.isOrHasParent(Entity1_14Types.EntityType.LIVINGENTITY)) { - metadata.setId(metadata.getId() + 1); - } - - if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_INSENTIENT)) { - if (metadata.getId() == 13) { - tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) - | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata - metadata.setValue(tracker.getInsentientData(entityId)); - } - } - - if (type.isOrHasParent(Entity1_14Types.EntityType.PLAYER)) { - if (entityId != tracker.getClientEntityId()) { - if (metadata.getId() == 0) { - byte flags = ((Number) metadata.getValue()).byteValue(); - // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize - tracker.setEntityFlags(entityId, flags); - } else if (metadata.getId() == 7) { - tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); - } - if (metadata.getId() == 0 || metadata.getId() == 7) { - metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); - } - } - } else if (type.isOrHasParent(Entity1_14Types.EntityType.ZOMBIE)) { - if (metadata.getId() == 16) { - tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) - | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking - metadatas.remove(metadata); // "Are hands held up" - metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); - } else if (metadata.getId() > 16) { - metadata.setId(metadata.getId() - 1); - } - } - if (type.isOrHasParent(Entity1_14Types.EntityType.MINECART_ABSTRACT)) { - if (metadata.getId() == 10) { - // New block format - int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); - } - } else if (type.is(Entity1_14Types.EntityType.HORSE)) { - if (metadata.getId() == 18) { - metadatas.remove(metadata); - - int armorType = (int) metadata.getValue(); - Item armorItem = null; - if (armorType == 1) { //iron armor - armorItem = new Item(InventoryPackets.getNewItemId(727), (byte) 1, (short) 0, null); - } else if (armorType == 2) { //gold armor - armorItem = new Item(InventoryPackets.getNewItemId(728), (byte) 1, (short) 0, null); - } else if (armorType == 3) { //diamond armor - armorItem = new Item(InventoryPackets.getNewItemId(729), (byte) 1, (short) 0, null); - } - - PacketWrapper equipmentPacket = new PacketWrapper(0x46, null, connection); - equipmentPacket.write(Type.VAR_INT, entityId); - equipmentPacket.write(Type.VAR_INT, 4); - equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, armorItem); - equipmentPacket.send(Protocol1_14To1_13_2.class); - } - } else if (type.is(Entity1_14Types.EntityType.VILLAGER)) { - if (metadata.getId() == 15) { - // 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() == 18) { - // plains - metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); - metadata.setMetaType(MetaType1_14.VillagerData); - } - } else if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_ARROW)) { - if (metadata.getId() >= 9) { // New piercing - metadata.setId(metadata.getId() + 1); - } - } else if (type.is(Entity1_14Types.EntityType.FIREWORKS_ROCKET)) { - if (metadata.getId() == 8) { - if (metadata.getValue().equals(0)) - metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 - metadata.setMetaType(MetaType1_14.OptVarInt); - } - } else if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_SKELETON)) { - if (metadata.getId() == 14) { - tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) - | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking - metadatas.remove(metadata); // "Is swinging arms" - metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); - } - } else if (type.is(Entity1_14Types.EntityType.AREA_EFFECT_CLOUD)) { - if (metadata.getId() == 10) { - Particle particle = (Particle) metadata.getValue(); - particle.setId(getNewParticleId(particle.getId())); - } - } - - if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_ILLAGER_BASE)) { - if (metadata.getId() == 14) { - tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) - | (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0))); // New attacking - metadatas.remove(metadata); // "Has target (aggressive state)" - metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); - } - } - - // TODO Are witch and ravager also abstract illagers? They all inherit the new metadata 14 added in 19w13a - if (type.is(Entity1_14Types.EntityType.WITCH) || type.is(Entity1_14Types.EntityType.RAVAGER) || type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_ILLAGER_BASE)) { - if (metadata.getId() >= 14) { // TODO 19w13 added a new boolean (raid participant - is celebrating) with id 14 - metadata.setId(metadata.getId() + 1); - } - } - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - } - - private static boolean isSneaking(byte flags) { - return (flags & 0x2) != 0; - } - - private static boolean isSwimming(byte flags) { - return (flags & 0x10) != 0; - } - - 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 - } - } - - private static boolean isFallFlying(int entityFlags) { - return (entityFlags & 0x80) != 0; - } - - public static int recalculatePlayerPose(int entityId, EntityTracker1_14 tracker) { - byte flags = tracker.getEntityFlags(entityId); - // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize - int pose = 0; // standing - if (isFallFlying(flags)) { - pose = 1; - } else if (tracker.isSleeping(entityId)) { - pose = 2; - } else if (isSwimming(flags)) { - pose = 3; - } else if (tracker.isRiptide(entityId)) { - pose = 4; - } else if (isSneaking(flags)) { - pose = 5; - } - return pose; - } - - public static int getNewParticleId(int id) { - if (id >= 10) { - id += 2; // new lava drips 10, 11 - } - if (id >= 13) { - id += 1; // new water drip 11 -> 13 - } - if (id >= 27) { - id += 1; // new 24 -> 27 - } - if (id >= 29) { - id += 1; // skip new short happy villager - } - if (id >= 44) { - id += 1; // new 39 -> 44 - } - return id; - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index 9b2a480f3..f99f45c6e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.PlayerPackets; @@ -24,6 +25,8 @@ public class Protocol1_14To1_13_2 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_14To1_13_2()); + InventoryPackets.register(this); EntityPackets.register(this); WorldPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java new file mode 100644 index 000000000..93fab0b04 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java @@ -0,0 +1,227 @@ +package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata; + +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_14Types; +import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; +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_14; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; + +import java.util.List; + +public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { + + @Override + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { + metadata.setMetaType(MetaType1_14.byId(metadata.getMetaType().getTypeID())); + + EntityTracker1_14 tracker = connection.get(EntityTracker1_14.class); + + if (metadata.getMetaType() == MetaType1_14.Slot) { + InventoryPackets.toClient((Item) metadata.getValue()); + } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + // Convert to new block id + int data = (int) metadata.getValue(); + metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); + } + + if (type == null) return; + + //Metadata 6 added to abstract_entity + if (metadata.getId() > 5) { + metadata.setId(metadata.getId() + 1); + } + + //Metadata 12 added to living_entity + if (metadata.getId() > 11 && type.isOrHasParent(EntityType.LIVINGENTITY)) { + metadata.setId(metadata.getId() + 1); + } + + if (type.isOrHasParent(EntityType.ABSTRACT_INSENTIENT)) { + if (metadata.getId() == 13) { + tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) + | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata + metadata.setValue(tracker.getInsentientData(entityId)); + } + } + + if (type.isOrHasParent(EntityType.PLAYER)) { + if (entityId != tracker.getClientEntityId()) { + if (metadata.getId() == 0) { + byte flags = ((Number) metadata.getValue()).byteValue(); + // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize + tracker.setEntityFlags(entityId, flags); + } else if (metadata.getId() == 7) { + tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); + } + if (metadata.getId() == 0 || metadata.getId() == 7) { + metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); + } + } + } else if (type.isOrHasParent(EntityType.ZOMBIE)) { + if (metadata.getId() == 16) { + tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) + | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking + metadatas.remove(metadata); // "Are hands held up" + metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); + } else if (metadata.getId() > 16) { + metadata.setId(metadata.getId() - 1); + } + } + + if (type.isOrHasParent(EntityType.MINECART_ABSTRACT)) { + if (metadata.getId() == 10) { + // New block format + int data = (int) metadata.getValue(); + metadata.setValue(Protocol1_14To1_13_2.getNewBlockStateId(data)); + } + } else if (type.is(EntityType.HORSE)) { + if (metadata.getId() == 18) { + metadatas.remove(metadata); + + int armorType = (int) metadata.getValue(); + Item armorItem = null; + if (armorType == 1) { //iron armor + armorItem = new Item(InventoryPackets.getNewItemId(727), (byte) 1, (short) 0, null); + } else if (armorType == 2) { //gold armor + armorItem = new Item(InventoryPackets.getNewItemId(728), (byte) 1, (short) 0, null); + } else if (armorType == 3) { //diamond armor + armorItem = new Item(InventoryPackets.getNewItemId(729), (byte) 1, (short) 0, null); + } + + PacketWrapper equipmentPacket = new PacketWrapper(0x46, null, connection); + equipmentPacket.write(Type.VAR_INT, entityId); + equipmentPacket.write(Type.VAR_INT, 4); + equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, armorItem); + equipmentPacket.send(Protocol1_14To1_13_2.class); + } + } else if (type.is(EntityType.VILLAGER)) { + if (metadata.getId() == 15) { + // plains + metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); + metadata.setMetaType(MetaType1_14.VillagerData); + } + } else if (type.is(EntityType.ZOMBIE_VILLAGER)) { + if (metadata.getId() == 18) { + // plains + metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); + metadata.setMetaType(MetaType1_14.VillagerData); + } + } else if (type.isOrHasParent(EntityType.ABSTRACT_ARROW)) { + if (metadata.getId() >= 9) { // New piercing + metadata.setId(metadata.getId() + 1); + } + } else if (type.is(EntityType.FIREWORKS_ROCKET)) { + if (metadata.getId() == 8) { + if (metadata.getValue().equals(0)) + metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 + metadata.setMetaType(MetaType1_14.OptVarInt); + } + } else if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) { + if (metadata.getId() == 14) { + tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) + | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking + metadatas.remove(metadata); // "Is swinging arms" + metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); + } + } else if (type.is(EntityType.AREA_EFFECT_CLOUD)) { + if (metadata.getId() == 10) { + Particle particle = (Particle) metadata.getValue(); + particle.setId(getNewParticleId(particle.getId())); + } + } + + if (type.isOrHasParent(EntityType.ABSTRACT_ILLAGER_BASE)) { + if (metadata.getId() == 14) { + tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) + | (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0))); // New attacking + metadatas.remove(metadata); // "Has target (aggressive state)" + metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); + } + } + + // TODO Are witch and ravager also abstract illagers? They all inherit the new metadata 14 added in 19w13a + if (type.is(EntityType.WITCH) || type.is(EntityType.RAVAGER) || type.isOrHasParent(EntityType.ABSTRACT_ILLAGER_BASE)) { + if (metadata.getId() >= 14) { // TODO 19w13 added a new boolean (raid participant - is celebrating) with id 14 + metadata.setId(metadata.getId() + 1); + } + } + } + + private static boolean isSneaking(byte flags) { + return (flags & 0x2) != 0; + } + + private static boolean isSwimming(byte flags) { + return (flags & 0x10) != 0; + } + + 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 + } + } + + private static boolean isFallFlying(int entityFlags) { + return (entityFlags & 0x80) != 0; + } + + public static int recalculatePlayerPose(int entityId, EntityTracker1_14 tracker) { + byte flags = tracker.getEntityFlags(entityId); + // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize + int pose = 0; // standing + if (isFallFlying(flags)) { + pose = 1; + } else if (tracker.isSleeping(entityId)) { + pose = 2; + } else if (isSwimming(flags)) { + pose = 3; + } else if (tracker.isRiptide(entityId)) { + pose = 4; + } else if (isSneaking(flags)) { + pose = 5; + } + return pose; + } + + public static int getNewParticleId(int id) { + if (id >= 10) { + id += 2; // new lava drips 10, 11 + } + if (id >= 13) { + id += 1; // new water drip 11 -> 13 + } + if (id >= 27) { + id += 1; // new 24 -> 27 + } + if (id >= 29) { + id += 1; // skip new short happy villager + } + if (id >= 44) { + id += 1; // new 39 -> 44 + } + return id; + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index 876480c02..4195cc4fd 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -14,7 +14,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_13_2; import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.EntityTypeRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; @@ -25,7 +25,7 @@ import java.util.UUID; public class EntityPackets { - public static void register(Protocol protocol) { + public static void register(final Protocol protocol) { // Spawn entity protocol.registerOutgoing(State.PLAY, 0x00, 0x00, new PacketRemapper() { @@ -139,7 +139,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } @@ -179,7 +179,7 @@ public class EntityPackets { Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER; // Register Type ID wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); - MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } @@ -203,7 +203,7 @@ public class EntityPackets { metadataPacket.write(Type.VAR_INT, entityId); List metadataList = new LinkedList<>(); if (tracker.getClientEntityId() != entityId) { - metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker))); } metadataList.add(new Metadata(12, MetaType1_14.OptPosition, null)); metadataPacket.write(Types1_14.METADATA_LIST, metadataList); @@ -230,7 +230,7 @@ public class EntityPackets { List metadataList = new LinkedList<>(); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, position)); if (tracker.getClientEntityId() != entityId) { - metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter.recalculatePlayerPose(entityId, tracker))); + metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker))); } wrapper.write(Types1_14.METADATA_LIST, metadataList); } @@ -266,7 +266,7 @@ public class EntityPackets { int entityId = wrapper.get(Type.VAR_INT, 0); Optional type = wrapper.user().get(EntityTracker1_14.class).getEntity(entityId); - MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, type.orNull(), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 6806b55b3..f4fd5c29d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -15,7 +15,7 @@ import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; @@ -262,7 +262,7 @@ public class WorldPackets { InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); } - int newId = MetadataRewriter.getNewParticleId(id); + int newId = MetadataRewriter1_14To1_13_2.getNewParticleId(id); if (newId != id) { wrapper.set(Type.INT, 0, newId); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index 5b8a74ee1..7a53c384e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -4,6 +4,8 @@ import lombok.Getter; import lombok.Setter; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_14Types; +import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; @@ -12,9 +14,7 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.WorldPackets; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import static us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; - -public class EntityTracker1_14 extends EntityTracker { +public class EntityTracker1_14 extends EntityTracker { private final Map insentientData = new ConcurrentHashMap<>(); // 0x1 = sleeping, 0x2 = riptide private final Map sleepingAndRiptideData = new ConcurrentHashMap<>(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java index 15411563d..23412139c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java @@ -11,6 +11,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.*; @@ -72,6 +73,8 @@ public class Protocol1_9To1_8 extends Protocol { @Override protected void registerPackets() { + put(new MetadataRewriter1_9To1_8()); + // Disconnect workaround (JSON!) registerOutgoing(State.LOGIN, 0x00, 0x00, new PacketRemapper() { @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java deleted file mode 100644 index 80560530c..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java +++ /dev/null @@ -1,156 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; - -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.entities.Entity1_10Types; -import us.myles.ViaVersion.api.minecraft.EulerAngle; -import us.myles.ViaVersion.api.minecraft.Vector; -import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.minecraft.metadata.MetaType; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_8; -import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import java.util.logging.Logger; - -public class MetadataRewriter { - public static void transform(Entity1_10Types.EntityType type, List list) { - short id = -1; - int data = -1; - for (Metadata entry : new ArrayList<>(list)) { - MetaIndex metaIndex = MetaIndex.searchIndex(type, entry.getId()); - try { - if (metaIndex != null) { - if (metaIndex.getNewType() != MetaType1_9.Discontinued) { - if (metaIndex.getNewType() != MetaType1_9.BlockID || id != -1 && data == -1 || id == -1 && data != -1) { // block ID is only written if we have both parts - entry.setId(metaIndex.getNewIndex()); - entry.setMetaType(metaIndex.getNewType()); - } - Object value = entry.getValue(); - switch (metaIndex.getNewType()) { - case Byte: - // convert from int, byte - if (metaIndex.getOldType() == MetaType1_8.Byte) { - entry.setValue(value); - } - if (metaIndex.getOldType() == MetaType1_8.Int) { - entry.setValue(((Integer) value).byteValue()); - } - // After writing the last one - if (metaIndex == MetaIndex.ENTITY_STATUS && type == Entity1_10Types.EntityType.PLAYER) { - Byte val = 0; - if ((((Byte) value) & 0x10) == 0x10) { // Player eating/aiming/drinking - val = 1; - } - int newIndex = MetaIndex.PLAYER_HAND.getNewIndex(); - MetaType metaType = MetaIndex.PLAYER_HAND.getNewType(); - Metadata metadata = new Metadata(newIndex, metaType, val); - list.add(metadata); - } - break; - case OptUUID: - String owner = (String) value; - UUID toWrite = null; - if (owner.length() != 0) { - try { - toWrite = UUID.fromString(owner); - } catch (Exception ignored) { - } - } - entry.setValue(toWrite); - break; - case BlockID: - // if we have both sources :)) - if (metaIndex.getOldType() == MetaType1_8.Byte) { - data = (Byte) value; - } - if (metaIndex.getOldType() == MetaType1_8.Short) { - id = (Short) value; - } - if (id != -1 && data != -1) { - int combined = id | (data & 0xF); - data = -1; - id = -1; - entry.setValue(combined); - } else { - list.remove(entry); - } - break; - case VarInt: - // convert from int, short, byte - if (metaIndex.getOldType() == MetaType1_8.Byte) { - entry.setValue(((Byte) value).intValue()); - } - if (metaIndex.getOldType() == MetaType1_8.Short) { - entry.setValue(((Short) value).intValue()); - } - if (metaIndex.getOldType() == MetaType1_8.Int) { - entry.setValue(value); - } - break; - case Float: - entry.setValue(value); - break; - case String: - entry.setValue(value); - break; - case Boolean: - if (metaIndex == MetaIndex.AGEABLE_AGE) - entry.setValue((Byte) value < 0); - else - entry.setValue((Byte) value != 0); - break; - case Slot: - entry.setValue(value); - ItemRewriter.toClient((Item) entry.getValue()); - break; - case Position: - Vector vector = (Vector) value; - entry.setValue(vector); - break; - case Vector3F: - EulerAngle angle = (EulerAngle) value; - entry.setValue(angle); - break; - case Chat: - value = Protocol1_9To1_8.fixJson((String) value); - entry.setValue(value); - break; - default: - Via.getPlatform().getLogger().warning("[Out] Unhandled MetaDataType: " + metaIndex.getNewType()); - list.remove(entry); - break; - } - } else { - list.remove(entry); - } - } else { - throw new Exception("Could not find valid metadata"); - } - } catch (Exception e) { - list.remove(entry); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Logger log = Via.getPlatform().getLogger(); - - log.warning("This is most likely down to one of your plugins sending bad datawatchers. Please test if this occurs without any plugins except ViaVersion before reporting it on GitHub"); - log.warning("Also make sure that all your plugins are compatible with your server version."); - if (type != null) - log.severe("An error occurred with entity meta data for " + type + " OldID: " + entry.getId()); - else - log.severe("An error occurred with entity meta data for UNKNOWN_ENTITY OldID: " + entry.getId()); - if (metaIndex != null) { - log.severe("Value: " + entry.getValue()); - log.severe("Old ID: " + metaIndex.getIndex() + " New ID: " + metaIndex.getNewIndex()); - log.severe("Old Type: " + metaIndex.getOldType() + " New Type: " + metaIndex.getNewType()); - } - e.printStackTrace(); - } - } - } - } - -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java new file mode 100644 index 000000000..f5a73f341 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java @@ -0,0 +1,127 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_10Types; +import us.myles.ViaVersion.api.entities.Entity1_10Types.EntityType; +import us.myles.ViaVersion.api.minecraft.EulerAngle; +import us.myles.ViaVersion.api.minecraft.Vector; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_8; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class MetadataRewriter1_9To1_8 extends MetadataRewriter { + + @Override + protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { + MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.getId()); + if (metaIndex == null) { + throw new Exception("Could not find valid metadata"); + } + + if (metaIndex.getNewType() == MetaType1_9.Discontinued) { + metadatas.remove(metadata); + return; + } + + metadata.setId(metaIndex.getNewIndex()); + metadata.setMetaType(metaIndex.getNewType()); + + if (type == EntityType.ENDERMAN && metaIndex.getNewType() == MetaType1_9.BlockID) { + if (metaIndex.getOldType() == MetaType1_8.Short) { + int id = (Short) metadata.getValue(); + int data = metadataMap.containsKey(17) ? (Byte) metadataMap.get(17).getValue() : 0; + int combined = (id << 4) | (data & 0xF); + metadata.setValue(combined); + } else { + metadatas.remove(metadata); + } + return; + } + + Object value = metadata.getValue(); + switch (metaIndex.getNewType()) { + case Byte: + // convert from int, byte + if (metaIndex.getOldType() == MetaType1_8.Byte) { + metadata.setValue(value); + } + if (metaIndex.getOldType() == MetaType1_8.Int) { + metadata.setValue(((Integer) value).byteValue()); + } + // After writing the last one + if (metaIndex == MetaIndex.ENTITY_STATUS && type == EntityType.PLAYER) { + Byte val = 0; + if ((((Byte) value) & 0x10) == 0x10) { // Player eating/aiming/drinking + val = 1; + } + int newIndex = MetaIndex.PLAYER_HAND.getNewIndex(); + MetaType metaType = MetaIndex.PLAYER_HAND.getNewType(); + metadatas.add(new Metadata(newIndex, metaType, val)); + } + break; + case OptUUID: + String owner = (String) value; + UUID toWrite = null; + if (!owner.isEmpty()) { + try { + toWrite = UUID.fromString(owner); + } catch (Exception ignored) {} + } + metadata.setValue(toWrite); + break; + case VarInt: + // convert from int, short, byte + if (metaIndex.getOldType() == MetaType1_8.Byte) { + metadata.setValue(((Byte) value).intValue()); + } + if (metaIndex.getOldType() == MetaType1_8.Short) { + metadata.setValue(((Short) value).intValue()); + } + if (metaIndex.getOldType() == MetaType1_8.Int) { + metadata.setValue(value); + } + break; + case Float: + metadata.setValue(value); + break; + case String: + metadata.setValue(value); + break; + case Boolean: + if (metaIndex == MetaIndex.AGEABLE_AGE) + metadata.setValue((Byte) value < 0); + else + metadata.setValue((Byte) value != 0); + break; + case Slot: + metadata.setValue(value); + ItemRewriter.toClient((Item) metadata.getValue()); + break; + case Position: + Vector vector = (Vector) value; + metadata.setValue(vector); + break; + case Vector3F: + EulerAngle angle = (EulerAngle) value; + metadata.setValue(angle); + break; + case Chat: + value = Protocol1_9To1_8.fixJson((String) value); + metadata.setValue(value); + break; + default: + metadatas.remove(metadata); + throw new Exception("Unhandled MetaDataType: " + metaIndex.getNewType()); + } + } + +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java index c244fcca5..1526d74b2 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -19,7 +19,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.*; @@ -32,7 +32,7 @@ public class EntityPackets { } }; - public static void register(Protocol protocol) { + public static void register(final Protocol protocol) { // Attach Entity Packet protocol.registerOutgoing(State.PLAY, 0x1B, 0x3A, new PacketRemapper() { @@ -184,14 +184,14 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); - int entityID = wrapper.get(Type.VAR_INT, 0); + int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityID); + Optional type = tracker.getEntity(entityId); if (type.isPresent()) { - MetadataRewriter.transform(type.get(), metadataList); + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); } else { // Buffer - tracker.addMetadataToBuffer(entityID, metadataList); + tracker.addMetadataToBuffer(entityId, metadataList); wrapper.cancel(); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 41c503c18..f24f16835 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -18,7 +18,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.ArrayList; @@ -32,7 +32,7 @@ public class SpawnPackets { } }; - public static void register(Protocol protocol) { + public static void register(final Protocol protocol) { // Spawn Object Packet protocol.registerOutgoing(State.PLAY, 0x0E, 0x00, new PacketRemapper() { @Override @@ -213,13 +213,13 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); - int entityID = wrapper.get(Type.VAR_INT, 0); + int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityID); + Optional type = tracker.getEntity(entityId); if (type.isPresent()) { - MetadataRewriter.transform(type.get(), metadataList); + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); } else { - Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); + Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); } } @@ -319,13 +319,13 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); - int entityID = wrapper.get(Type.VAR_INT, 0); + int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityID); + Optional type = tracker.getEntity(entityId); if (type.isPresent()) { - MetadataRewriter.transform(type.get(), metadataList); + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); } else { - Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); + Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java index 4e5405cc3..6b4dc6285 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java @@ -12,6 +12,7 @@ import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_10Types; +import us.myles.ViaVersion.api.entities.Entity1_10Types.EntityType; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -22,7 +23,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.chat.GameMode; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; @@ -30,10 +31,8 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -import static us.myles.ViaVersion.api.entities.Entity1_10Types.EntityType; - @Getter -public class EntityTracker1_9 extends EntityTracker { +public class EntityTracker1_9 extends EntityTracker { private final Map uuidMap = new ConcurrentHashMap<>(); private final Map> metadataBuffer = new ConcurrentHashMap<>(); private final Map vehicleMap = new ConcurrentHashMap<>(); @@ -118,41 +117,41 @@ public class EntityTracker1_9 extends EntityTracker { } public void handleMetadata(int entityId, List metadataList) { - Entity1_10Types.EntityType type = getEntity(entityId).orNull(); + EntityType type = getEntity(entityId).orNull(); if (type == null) { return; } for (Metadata metadata : new ArrayList<>(metadataList)) { // Fix: wither (crash fix) - if (type == Entity1_10Types.EntityType.WITHER) { + if (type == EntityType.WITHER) { if (metadata.getId() == 10) { metadataList.remove(metadata); //metadataList.add(new Metadata(10, NewType.Byte.getTypeID(), Type.BYTE, 0)); } } // Fix: enderdragon (crash fix) - if (type == Entity1_10Types.EntityType.ENDER_DRAGON) { + if (type == EntityType.ENDER_DRAGON) { if (metadata.getId() == 11) { metadataList.remove(metadata); // metadataList.add(new Metadata(11, NewType.Byte.getTypeID(), Type.VAR_INT, 0)); } } - if (type == Entity1_10Types.EntityType.SKELETON) { + if (type == EntityType.SKELETON) { if ((getMetaByIndex(metadataList, 12)) == null) { metadataList.add(new Metadata(12, MetaType1_9.Boolean, true)); } } //ECHOPET Patch - if (type == Entity1_10Types.EntityType.HORSE) { + if (type == EntityType.HORSE) { // Wrong metadata value from EchoPet, patch since it's discontinued. (https://github.com/DSH105/EchoPet/blob/06947a8b08ce40be9a518c2982af494b3b99d140/modules/API/src/main/java/com/dsh105/echopet/compat/api/entity/HorseArmour.java#L22) if (metadata.getId() == 16 && (int) metadata.getValue() == Integer.MIN_VALUE) metadata.setValue(0); } - if (type == Entity1_10Types.EntityType.PLAYER) { + if (type == EntityType.PLAYER) { if (metadata.getId() == 0) { // Byte byte data = (byte) metadata.getValue(); @@ -177,7 +176,7 @@ public class EntityTracker1_9 extends EntityTracker { )); } } - if (type == Entity1_10Types.EntityType.ARMOR_STAND && Via.getConfig().isHologramPatch()) { + if (type == EntityType.ARMOR_STAND && Via.getConfig().isHologramPatch()) { if (metadata.getId() == 0 && getMetaByIndex(metadataList, 10) != null) { Metadata meta = getMetaByIndex(metadataList, 10); //Only happens if the armorstand is small byte data = (byte) metadata.getValue(); @@ -204,11 +203,11 @@ public class EntityTracker1_9 extends EntityTracker { UUID uuid = getUser().get(ProtocolInfo.class).getUuid(); // Boss bar if (Via.getConfig().isBossbarPatch()) { - if (type == Entity1_10Types.EntityType.ENDER_DRAGON || type == Entity1_10Types.EntityType.WITHER) { + if (type == EntityType.ENDER_DRAGON || type == EntityType.WITHER) { if (metadata.getId() == 2) { BossBar bar = bossBarMap.get(entityId); String title = (String) metadata.getValue(); - title = title.isEmpty() ? (type == Entity1_10Types.EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither") : title; + title = title.isEmpty() ? (type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither") : title; if (bar == null) { bar = Via.getAPI().createBossBar(title, BossColor.PINK, BossStyle.SOLID); bossBarMap.put(entityId, bar); @@ -223,10 +222,10 @@ public class EntityTracker1_9 extends EntityTracker { } else if (metadata.getId() == 6 && !Via.getConfig().isBossbarAntiflicker()) { // If anti flicker is enabled, don't update health BossBar bar = bossBarMap.get(entityId); // Make health range between 0 and 1 - float maxHealth = type == Entity1_10Types.EntityType.ENDER_DRAGON ? 200.0f : 300.0f; + float maxHealth = type == EntityType.ENDER_DRAGON ? 200.0f : 300.0f; float health = Math.max(0.0f, Math.min(((float) metadata.getValue()) / maxHealth, 1.0f)); if (bar == null) { - String title = type == Entity1_10Types.EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither"; + String title = type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither"; bar = Via.getAPI().createBossBar(title, health, BossColor.PINK, BossStyle.SOLID); bossBarMap.put(entityId, bar); bar.addPlayer(uuid); @@ -293,7 +292,8 @@ public class EntityTracker1_9 extends EntityTracker { PacketWrapper wrapper = new PacketWrapper(0x39, null, getUser()); wrapper.write(Type.VAR_INT, entityId); wrapper.write(Types1_9.METADATA_LIST, metadataList); - MetadataRewriter.transform(getEntity(entityId).orNull(), metadataList); + getUser().get(ProtocolInfo.class).getPipeline().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class) + .handleMetadata(entityId, getEntity(entityId).orNull(), metadataList, getUser()); handleMetadata(entityId, metadataList); if (!metadataList.isEmpty()) { try {