From 12b45d95e0364e740d96aa57e4f4b6c8faf06fbf Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Tue, 5 Mar 2019 19:47:44 -0300 Subject: [PATCH] Trying to fix main hand on bungee, handle left handed on 1.8 using 0x80 --- .../bukkit/platform/BukkitViaConfig.java | 5 ++ .../bungee/listeners/MainHandPatch.java | 51 ------------------- .../bungee/platform/BungeeViaConfig.java | 5 ++ .../bungee/platform/BungeeViaLoader.java | 9 ++-- .../providers/BungeeMainHandProvider.java | 42 +++++++++++++++ .../ViaVersion/api/ViaVersionConfig.java | 5 ++ .../protocol1_9to1_8/Protocol1_9TO1_8.java | 1 + .../packets/EntityPackets.java | 2 +- .../packets/PlayerPackets.java | 12 ++++- .../providers/MainHandProvider.java | 9 ++++ .../storage/EntityTracker.java | 9 +++- .../resources/assets/viaversion/config.yml | 4 +- .../sponge/platform/SpongeViaConfig.java | 5 ++ .../velocity/listeners/MainHandPatch.java | 49 ------------------ .../velocity/platform/VelocityViaConfig.java | 5 ++ .../velocity/platform/VelocityViaLoader.java | 3 +- 16 files changed, 102 insertions(+), 114 deletions(-) delete mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/MainHandPatch.java create mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeMainHandProvider.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/MainHandProvider.java delete mode 100644 velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/MainHandPatch.java diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaConfig.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaConfig.java index 8a66a7c41..cd2f7b690 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaConfig.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaConfig.java @@ -249,4 +249,9 @@ public class BukkitViaConfig extends Config implements ViaVersionConfig { public int get1_13TabCompleteDelay() { return getInt("1_13-tab-complete-delay", 0); } + + @Override + public boolean isLeftHandedHandling() { + return getBoolean("left-handed-handling", true); + } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/MainHandPatch.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/MainHandPatch.java deleted file mode 100644 index 709df56f4..000000000 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/MainHandPatch.java +++ /dev/null @@ -1,51 +0,0 @@ -package us.myles.ViaVersion.bungee.listeners; - -import net.md_5.bungee.api.event.ServerConnectEvent; -import net.md_5.bungee.api.plugin.Listener; -import net.md_5.bungee.event.EventHandler; -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -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.storage.EntityTracker; - -import java.lang.reflect.Method; - -/* - This solves the wrong mainhand issue when you join with BungeeCord on a 1.8 server, and switch to a 1.9 or higher. - */ -public class MainHandPatch implements Listener { - private static Method getSettings = null; - private static Method setMainHand = null; - - static { - try { - getSettings = Class.forName("net.md_5.bungee.UserConnection").getDeclaredMethod("getSettings"); - setMainHand = Class.forName("net.md_5.bungee.protocol.packet.ClientSettings").getDeclaredMethod("setMainHand", int.class); - } catch (Exception ignored) { - } - } - - @EventHandler - public void onServerConnect(ServerConnectEvent event) { - // Ignore if it doesn't exist (Like BungeeCord 1.8) - if (setMainHand == null || getSettings == null) - return; - - UserConnection user = Via.getManager().getConnection(event.getPlayer().getUniqueId()); - if (user == null) return; - - try { - if (user.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class)) { - Object settings = getSettings.invoke(event.getPlayer()); - if (settings != null) { - if (user.has(EntityTracker.class)) { - setMainHand.invoke(settings, user.get(EntityTracker.class).getMainHand()); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaConfig.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaConfig.java index e58044525..e3cdb6050 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaConfig.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaConfig.java @@ -302,4 +302,9 @@ public class BungeeViaConfig extends Config implements ViaVersionConfig { public int get1_13TabCompleteDelay() { return getInt("1_13-tab-complete-delay", 0); } + + @Override + public boolean isLeftHandedHandling() { + return getBoolean("left-handed-handling", true); + } } 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 428643bfe..1e0ffa437 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 @@ -8,16 +8,13 @@ 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.ElytraPatch; -import us.myles.ViaVersion.bungee.listeners.MainHandPatch; import us.myles.ViaVersion.bungee.listeners.UpdateListener; -import us.myles.ViaVersion.bungee.providers.BungeeBossBarProvider; -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.providers.*; import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MainHandProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import java.util.HashSet; @@ -45,7 +42,6 @@ public class BungeeViaLoader implements ViaPlatformLoader { registerListener(plugin); registerListener(new UpdateListener()); registerListener(new BungeeServerHandler()); - registerListener(new MainHandPatch()); registerListener(new ElytraPatch()); // Providers @@ -53,6 +49,7 @@ public class BungeeViaLoader implements ViaPlatformLoader { Via.getManager().getProviders().use(VersionProvider.class, new BungeeVersionProvider()); Via.getManager().getProviders().use(EntityIdProvider.class, new BungeeEntityIdProvider()); Via.getManager().getProviders().use(BossBarProvider.class, new BungeeBossBarProvider()); + Via.getManager().getProviders().use(MainHandProvider.class, new BungeeMainHandProvider()); if (plugin.getConf().getBungeePingInterval() > 0) { tasks.add(plugin.getProxy().getScheduler().schedule( diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeMainHandProvider.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeMainHandProvider.java new file mode 100644 index 000000000..c2bbf36be --- /dev/null +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/providers/BungeeMainHandProvider.java @@ -0,0 +1,42 @@ +package us.myles.ViaVersion.bungee.providers; + +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MainHandProvider; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/* + This solves the wrong mainhand issue when you join with BungeeCord on a 1.8 server, and switch to a 1.9 or higher. + */ +public class BungeeMainHandProvider extends MainHandProvider { + private static Method getSettings = null; + private static Method setMainHand = null; + + static { + try { + getSettings = Class.forName("net.md_5.bungee.UserConnection").getDeclaredMethod("getSettings"); + setMainHand = Class.forName("net.md_5.bungee.protocol.packet.ClientSettings").getDeclaredMethod("setMainHand", int.class); + } catch (Exception ignored) { + } + } + + @Override + public void setMainHand(UserConnection user, int hand) { + ProtocolInfo info = user.get(ProtocolInfo.class); + if (info == null || info.getUuid() == null) return; + ProxiedPlayer player = ProxyServer.getInstance().getPlayer(info.getUuid()); + if (player == null) return; + try { + Object settings = getSettings.invoke(player); + if (settings != null) { + setMainHand.invoke(settings, hand); + } + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java b/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java index 1cc3ad3e2..15bee2245 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java +++ b/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java @@ -308,4 +308,9 @@ public interface ViaVersionConfig { * @return the delay in ticks */ int get1_13TabCompleteDelay(); + + /** + * Handles left handed info by using unused bit 7 on Client Settings packet + */ + boolean isLeftHandedHandling(); } 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 d15ac6577..3ae52ed92 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 @@ -95,6 +95,7 @@ public class Protocol1_9TO1_8 extends Protocol { providers.register(CommandBlockProvider.class, new CommandBlockProvider()); providers.register(EntityIdProvider.class, new EntityIdProvider()); providers.register(BossBarProvider.class, new BossBarProvider()); + providers.register(MainHandProvider.class, new MainHandProvider()); 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/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 464cf7833..1686ae2e4 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 @@ -283,7 +283,7 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { if (!Via.getConfig().isMinimizeCooldown()) return; - if (wrapper.get(Type.VAR_INT, 0) != wrapper.user().get(EntityTracker.class).getEntityID()) { + if (wrapper.get(Type.VAR_INT, 0) != wrapper.user().get(EntityTracker.class).getProvidedEntityId()) { return; } int propertiesToRead = wrapper.read(Type.INT); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java index 84cabf12d..b568f645e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java @@ -19,6 +19,7 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.chat.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.chat.GameMode; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.CommandBlockProvider; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MainHandProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; @@ -458,8 +459,15 @@ public class PlayerPackets { public void handle(PacketWrapper wrapper) throws Exception { int hand = wrapper.read(Type.VAR_INT); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.setMainHand(hand); + if (Via.getConfig().isLeftHandedHandling()) { + // Add 0x80 if left handed + if (hand == 0) wrapper.set(Type.UNSIGNED_BYTE, 0, + (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80) + ); + } + wrapper.sendToServer(Protocol1_9TO1_8.class, true, true); + wrapper.cancel(); + Via.getManager().getProviders().get(MainHandProvider.class).setMainHand(wrapper.user(), hand); } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/MainHandProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/MainHandProvider.java new file mode 100644 index 000000000..4735eadb8 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/MainHandProvider.java @@ -0,0 +1,9 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.providers; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.platform.providers.Provider; + +public class MainHandProvider implements Provider { + public void setMainHand(UserConnection user, int hand) { + } +} 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 212bae1e2..5b9908a43 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 @@ -52,8 +52,6 @@ public class EntityTracker extends StoredObject { @Setter private GameMode gameMode; @Setter - private int mainHand; - @Setter private String currentTeam; public EntityTracker(UserConnection user) { @@ -170,6 +168,13 @@ public class EntityTracker extends StoredObject { } } } + if (metadata.getId() == 12 && Via.getConfig().isLeftHandedHandling()) { // Player model + metadataList.add(new Metadata( + 13, // Main hand + MetaType1_9.Byte, + (byte) (((((byte) metadata.getValue()) & 0x80) != 0) ? 0 : 1) + )); + } } if (type == Entity1_10Types.EntityType.ARMOR_STAND && Via.getConfig().isHologramPatch()) { if (metadata.getId() == 0 && getMetaByIndex(metadataList, 10) != null) { diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index 62cb0dc6f..658d9f61e 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -165,4 +165,6 @@ replacement-piston-id: 0 # Force the string -> json transform force-json-transform: false # Minimize the cooldown animation in 1.8 servers -minimize-cooldown: true \ No newline at end of file +minimize-cooldown: true +# Left handed handling on 1.8 servers +left-handed-handling: true \ No newline at end of file diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaConfig.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaConfig.java index 8944f60ec..59fcbffcc 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaConfig.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeViaConfig.java @@ -255,4 +255,9 @@ public class SpongeViaConfig extends Config implements ViaVersionConfig { public int get1_13TabCompleteDelay() { return getInt("1_13-tab-complete-delay", 0); } + + @Override + public boolean isLeftHandedHandling() { + return getBoolean("left-handed-handling", true); + } } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/MainHandPatch.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/MainHandPatch.java deleted file mode 100644 index eae7f08c3..000000000 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/MainHandPatch.java +++ /dev/null @@ -1,49 +0,0 @@ -package us.myles.ViaVersion.velocity.listeners; - -import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.event.player.ServerConnectedEvent; -import com.velocitypowered.api.proxy.player.PlayerSettings; -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -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.storage.EntityTracker; -import us.myles.ViaVersion.util.ReflectionUtil; - -import java.lang.reflect.Method; - -/* - This solves the wrong mainhand issue when you join with BungeeCord on a 1.8 server, and switch to a 1.9 or higher. - */ -public class MainHandPatch { - private static Method setSettings; - - static { - try { - Class clientSettings = Class.forName("com.velocitypowered.proxy.protocol.packet.ClientSettings"); - setSettings = Class.forName("com.velocitypowered.proxy.connection.client.ConnectedPlayer").getDeclaredMethod("setPlayerSettings", clientSettings); - setSettings.setAccessible(true); - } catch (ClassNotFoundException | NoSuchMethodException e) { - e.printStackTrace(); - } - } - - @Subscribe - public void onServerConnect(ServerConnectedEvent event) { - UserConnection user = Via.getManager().getConnection(event.getPlayer().getUniqueId()); - if (user == null || setSettings == null) return; - - try { - if (user.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9TO1_8.class)) { - PlayerSettings settings = event.getPlayer().getPlayerSettings(); - if (user.has(EntityTracker.class)) { - Object clientSettings = ReflectionUtil.get(settings, "settings", Object.class); - ReflectionUtil.set(clientSettings, "mainHand", user.get(EntityTracker.class).getMainHand()); - setSettings.invoke(event.getPlayer(), clientSettings); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaConfig.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaConfig.java index 1a5144fb9..009ac5388 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaConfig.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaConfig.java @@ -307,4 +307,9 @@ public class VelocityViaConfig extends Config implements ViaVersionConfig { public int get1_13TabCompleteDelay() { return getInt("1_13-tab-complete-delay", 0); } + + @Override + public boolean isLeftHandedHandling() { + return getBoolean("left-handed-handling", true); + } } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java index f81447437..dcf4dc96f 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaLoader.java @@ -9,7 +9,6 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BossBarProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.velocity.handlers.VelocityServerHandler; import us.myles.ViaVersion.velocity.listeners.ElytraPatch; -import us.myles.ViaVersion.velocity.listeners.MainHandPatch; import us.myles.ViaVersion.velocity.listeners.UpdateListener; import us.myles.ViaVersion.velocity.providers.VelocityBossBarProvider; import us.myles.ViaVersion.velocity.providers.VelocityMovementTransmitter; @@ -26,10 +25,10 @@ public class VelocityViaLoader implements ViaPlatformLoader { Via.getManager().getProviders().use(BossBarProvider.class, new VelocityBossBarProvider()); Via.getManager().getProviders().use(VersionProvider.class, new VelocityVersionProvider()); // We probably don't need a EntityIdProvider because velocity sends a Join packet on server change + // We don't need main hand patch because Join Game packet makes client send hand data again VelocityPlugin.PROXY.getEventManager().register(plugin, new UpdateListener()); VelocityPlugin.PROXY.getEventManager().register(plugin, new VelocityServerHandler()); - VelocityPlugin.PROXY.getEventManager().register(plugin, new MainHandPatch()); VelocityPlugin.PROXY.getEventManager().register(plugin, new ElytraPatch()); int pingInterval = ((VelocityViaConfig) Via.getPlatform().getConf()).getVelocityPingInterval();