From 69e444f83d30dd7c3fbb8264fae97199aa38a5dd Mon Sep 17 00:00:00 2001 From: Matsv Date: Wed, 2 Nov 2016 18:07:21 +0100 Subject: [PATCH] Fix Bungee constant entity ids --- .../bungee/platform/BungeeViaLoader.java | 3 ++ .../providers/BungeeEntityIdProvider.java | 28 +++++++++++++++++++ .../protocol1_9to1_8/Protocol1_9TO1_8.java | 9 ++---- .../providers/EntityIdProvider.java | 12 ++++++++ .../storage/EntityTracker.java | 13 +++++++-- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeEntityIdProvider.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java index eb34076cb..29d552868 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java @@ -7,10 +7,12 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.bungee.handlers.BungeeServerHandler; import us.myles.ViaVersion.bungee.listeners.UpdateListener; +import us.myles.ViaVersion.bungee.providers.BungeeEntityIdProvider; import us.myles.ViaVersion.bungee.providers.BungeeMovementTransmitter; import us.myles.ViaVersion.bungee.providers.BungeeVersionProvider; import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; import us.myles.ViaVersion.protocols.base.VersionProvider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import java.util.concurrent.TimeUnit; @@ -29,6 +31,7 @@ public class BungeeViaLoader implements ViaPlatformLoader { // Providers Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter()); Via.getManager().getProviders().use(VersionProvider.class, new BungeeVersionProvider()); + Via.getManager().getProviders().use(EntityIdProvider.class, new BungeeEntityIdProvider()); if (plugin.getConf().getBungeePingInterval() > 0) { plugin.getProxy().getScheduler().schedule(plugin, new ProtocolDetectorService(plugin), 0, plugin.getConf().getBungeePingInterval(), TimeUnit.SECONDS); } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeEntityIdProvider.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeEntityIdProvider.java new file mode 100644 index 000000000..3d223ae2f --- /dev/null +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeEntityIdProvider.java @@ -0,0 +1,28 @@ +package us.myles.ViaVersion.bungee.providers; + +import net.md_5.bungee.api.connection.ProxiedPlayer; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.bungee.storage.BungeeStorage; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; + +import java.lang.reflect.Method; + +public class BungeeEntityIdProvider extends EntityIdProvider { + private static Method getClientEntityId; + + static { + try { + getClientEntityId = Class.forName("net.md_5.bungee.UserConnection").getDeclaredMethod("getClientEntityId"); + } catch (NoSuchMethodException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + public int getEntityId(UserConnection user) throws Exception { + BungeeStorage storage = user.get(BungeeStorage.class); + ProxiedPlayer player = storage.getPlayer(); + + return (int) getClientEntityId.invoke(player); + } +} 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 67c2dce6a..88057bdb2 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 @@ -5,17 +5,15 @@ import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.Protocol; 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.api.type.types.version.Metadata1_8Type; -import us.myles.ViaVersion.api.type.types.version.MetadataList1_8Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.*; @@ -30,10 +28,6 @@ public class Protocol1_9TO1_8 extends Protocol { return fixJson(line); } }; - @Deprecated - public static Type> METADATA_LIST = new MetadataList1_8Type(); - @Deprecated - public static Type METADATA = new Metadata1_8Type(); public static String fixJson(String line) { if (line == null || line.equalsIgnoreCase("null")) { @@ -101,6 +95,7 @@ public class Protocol1_9TO1_8 extends Protocol { protected void register(ViaProviders providers) { providers.register(HandItemProvider.class, new HandItemProvider()); providers.register(BulkChunkTranslatorProvider.class, new BulkChunkTranslatorProvider()); + providers.register(EntityIdProvider.class, new EntityIdProvider()); providers.require(MovementTransmitterProvider.class); if (Via.getConfig().isStimulatePlayerTick()) { Via.getPlatform().runRepeatingSync(new ViaIdleThread(), 1L); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java new file mode 100644 index 000000000..b1a47ff72 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java @@ -0,0 +1,12 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.providers; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.platform.providers.Provider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; + +public class EntityIdProvider implements Provider { + + public int getEntityId(UserConnection user) throws Exception { + return user.get(EntityTracker.class).getEntityID(); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java index b1521fc18..af67d38df 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java @@ -24,6 +24,7 @@ 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.providers.EntityIdProvider; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -44,7 +45,7 @@ public class EntityTracker extends StoredObject { @Setter private boolean autoTeam = false; @Setter - private int entityID; + private int entityID = -1; @Setter private Position currentlyDigging = null; private boolean teamExists = false; @@ -150,7 +151,7 @@ public class EntityTracker extends StoredObject { if (metadata.getId() == 0) { // Byte byte data = (byte) metadata.getValue(); - if (entityID != getEntityID() && Via.getConfig().isShieldBlocking()) { + if (entityID != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { if ((data & 0x10) == 0x10) { if (validBlocking.contains(entityID)) { Item shield = new Item((short) 442, (byte) 1, (short) 0, null); @@ -285,4 +286,12 @@ public class EntityTracker extends StoredObject { metadataBuffer.remove(entityID); } } + + public int getProvidedEntityId() { + try { + return Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(getUser()); + } catch (Exception e) { + return entityID; + } + } }