3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 00:00:28 +01:00

Hitbox fix options

Dieser Commit ist enthalten in:
KennyTV 2019-04-22 15:55:38 +02:00
Ursprung e38bdffc84
Commit b58cbf85e5
2 geänderte Dateien mit 39 neuen und 25 gelöschten Zeilen

Datei anzeigen

@ -2,22 +2,29 @@ package us.myles.ViaVersion.bukkit.listeners.multiversion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.event.player.PlayerToggleSneakEvent;
import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener; import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener;
import us.myles.ViaVersion.bukkit.platform.BukkitViaLoader;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class PlayerSneakListener extends ViaBukkitListener { public class PlayerSneakListener extends ViaBukkitListener {
private Map<Player, Boolean> sneaking; // true = 1.14+, else false
private Method getHandle; private Method getHandle;
private Method setSize; private Method setSize;
private boolean is1_9Fix; private boolean is1_9Fix;
private boolean is1_14Fix; private boolean is1_14Fix;
private boolean useCache;
public PlayerSneakListener(ViaVersionPlugin plugin, boolean is1_9Fix, boolean is1_14Fix) { public PlayerSneakListener(ViaVersionPlugin plugin, BukkitViaLoader viaLoader, boolean is1_9Fix, boolean is1_14Fix) {
super(plugin, null); super(plugin, null);
this.is1_9Fix = is1_9Fix; this.is1_9Fix = is1_9Fix;
this.is1_14Fix = is1_14Fix; this.is1_14Fix = is1_14Fix;
@ -28,38 +35,45 @@ public class PlayerSneakListener extends ViaBukkitListener {
} catch (ClassNotFoundException | NoSuchMethodException e) { } catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
} }
// From 1.9 upwards the server hitbox is set in every entity tick, so we have to reset it everytime
getPlugin().getServer().getScheduler().runTaskTimer(getPlugin(), new Runnable() { if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_9.getId()) {
@Override sneaking = new HashMap<>();
public void run() { useCache = true;
for (Player onlinePlayer : getPlugin().getServer().getOnlinePlayers()) { viaLoader.storeListener(new ViaBukkitListener(plugin, null) {
try { @EventHandler
final Object handle = getHandle.invoke(onlinePlayer); public void playerQuit(PlayerQuitEvent event) {
final Class<?> aClass = Class.forName(getPlugin().getServer().getClass().getPackage().getName() sneaking.remove(event.getPlayer());
.replace("org.bukkit.craftbukkit", "net.minecraft.server") + ".EntityPlayer"); }
System.out.println(aClass.getField("length").get(handle)); }).register();
} catch (IllegalAccessException e) { plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
e.printStackTrace(); @Override
} catch (InvocationTargetException e) { public void run() {
e.printStackTrace(); for (Map.Entry<Player, Boolean> entry : sneaking.entrySet()) {
} catch (ClassNotFoundException e) { setHight(entry.getKey(), entry.getValue() ? 1.5F : 1.6F);
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} }
} }
} }, 1, 1);
}, 1, 1); }
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void playerToggleSneak(final PlayerToggleSneakEvent event) { public void playerToggleSneak(PlayerToggleSneakEvent event) {
final Player player = event.getPlayer(); Player player = event.getPlayer();
final int protocolVersion = getUserConnection(player).get(ProtocolInfo.class).getProtocolVersion(); int protocolVersion = getUserConnection(player).get(ProtocolInfo.class).getProtocolVersion();
if (is1_14Fix && protocolVersion >= ProtocolVersion.v1_14.getId()) { if (is1_14Fix && protocolVersion >= ProtocolVersion.v1_14.getId()) {
setHight(player, event.isSneaking() ? 1.5F : 1.8F); setHight(player, event.isSneaking() ? 1.5F : 1.8F);
if (!useCache) return;
if (event.isSneaking())
sneaking.put(player, true);
else
sneaking.remove(player);
} else if (is1_9Fix && protocolVersion >= ProtocolVersion.v1_9.getId()) { } else if (is1_9Fix && protocolVersion >= ProtocolVersion.v1_9.getId()) {
setHight(player, event.isSneaking() ? 1.6F : 1.8F); setHight(player, event.isSneaking() ? 1.6F : 1.8F);
if (!useCache) return;
if (event.isSneaking())
sneaking.put(player, false);
else
sneaking.remove(player);
} }
} }

Datei anzeigen

@ -80,7 +80,7 @@ public class BukkitViaLoader implements ViaPlatformLoader {
if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_14.getId()) { if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_14.getId()) {
boolean use1_9Fix = plugin.getConf().is1_9HitboxFix() && ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId(); boolean use1_9Fix = plugin.getConf().is1_9HitboxFix() && ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId();
if (use1_9Fix || plugin.getConf().is1_14HitboxFix()) { if (use1_9Fix || plugin.getConf().is1_14HitboxFix()) {
storeListener(new PlayerSneakListener(plugin, use1_9Fix, plugin.getConf().is1_14HitboxFix())).register(); storeListener(new PlayerSneakListener(plugin, this, use1_9Fix, plugin.getConf().is1_14HitboxFix())).register();
} }
} }