Skip to content

Commit

Permalink
Fix elytra emulation not working on 1.9.0 servers
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Apr 29, 2024
1 parent 4d2505d commit 8db6375
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package com.viaversion.viarewind.legacysupport.feature;

import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viarewind.legacysupport.util.NMSUtil;
import com.viaversion.viarewind.legacysupport.util.ReflectionUtil;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import org.bukkit.entity.Player;
Expand All @@ -27,17 +30,38 @@
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector;

import java.lang.reflect.Method;
import java.util.logging.Level;

@SuppressWarnings("unchecked")
public class ElytraVelocityEmulator implements Listener {

private boolean isGliding(final ProtocolVersion version, final Player player) {
if (version.olderThanOrEqualTo(ProtocolVersion.v1_9_1)) {
final Object nmsPlayer = NMSUtil.getNMSPlayer(player);
if (nmsPlayer == null) return false;

try {
final Method getFlag = ReflectionUtil.getMethod(nmsPlayer.getClass(), "getFlag", int.class);
return (boolean) getFlag.invoke(nmsPlayer, 7);
} catch (Exception e) {
BukkitPlugin.getInstance().getLogger().log(Level.SEVERE, "Failed to get player flag", e);
}
}
return player.isGliding();
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) {
final Player player = e.getPlayer();

if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_9)) {
return; // Only apply for 1.8 and below players
}
if (!player.isGliding()) return; // Only apply if the player is gliding
final ProtocolVersion serverVersion = BukkitPlugin.getInstance().getServerProtocol();
if (!isGliding(serverVersion, player)) {
return; // Only apply if the player is gliding
}

final Vector direction = player.getLocation().getDirection();
final Vector velocity = player.getVelocity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ public static Class<?> getPlayerConnectionClass() {
}
}

public static void sendPacket(final Player player, final Object packet) {
Object nmsPlayer;
public static Object getNMSPlayer(final Player player) {
try {
nmsPlayer = player.getClass().getMethod("getHandle").invoke(player);
return player.getClass().getMethod("getHandle").invoke(player);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
BukkitPlugin.getInstance().getLogger().log(Level.SEVERE, "Failed to get EntityPlayer from player", e);
return null;
}
}

public static void sendPacket(final Player player, final Object packet) {
final Object nmsPlayer = getNMSPlayer(player);
if (nmsPlayer == null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static Method getMethod(final Class<?> clazz, final String methodName, fi
return clazz.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException ex) {
final Class<?> superClass = clazz.getSuperclass();
if (superClass == null) return null;
if (superClass == null) {
return null;
}
return getMethod(superClass, methodName, parameterTypes);
}
}
Expand Down

0 comments on commit 8db6375

Please sign in to comment.