Trying to fix main hand on bungee, handle left handed on 1.8 using 0x80

This commit is contained in:
creeper123123321 2019-03-05 19:47:44 -03:00
parent 05d01e70b7
commit 12b45d95e0
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
16 changed files with 102 additions and 114 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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(

View File

@ -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();
}
}
}

View File

@ -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();
}

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
});
}

View File

@ -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) {
}
}

View File

@ -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) {

View File

@ -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
minimize-cooldown: true
# Left handed handling on 1.8 servers
left-handed-handling: true

View File

@ -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);
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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();