package us.myles.ViaVersion.bukkit.listeners.multiversion;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener;
import us.myles.ViaVersion.bukkit.platform.BukkitViaLoader;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class PlayerSneakListener extends ViaBukkitListener {
    private Map<Player, Boolean> sneaking; // true = 1.14+, else false
    private Method getHandle;
    private Method setSize;
    private boolean is1_9Fix;
    private boolean is1_14Fix;
    private boolean useCache;

    public PlayerSneakListener(ViaVersionPlugin plugin, BukkitViaLoader viaLoader, boolean is1_9Fix, boolean is1_14Fix) {
        super(plugin, null);
        this.is1_9Fix = is1_9Fix;
        this.is1_14Fix = is1_14Fix;
        try {
            getHandle = Class.forName(plugin.getServer().getClass().getPackage().getName() + ".entity.CraftPlayer").getMethod("getHandle");
            setSize = Class.forName(plugin.getServer().getClass().getPackage().getName()
                    .replace("org.bukkit.craftbukkit", "net.minecraft.server") + ".EntityPlayer").getMethod("setSize", Float.TYPE, Float.TYPE);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        // From 1.9 upwards the server hitbox is set in every entity tick, so we have to reset it everytime
        if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_9.getId()) {
            sneaking = new HashMap<>();
            useCache = true;
            viaLoader.storeListener(new ViaBukkitListener(plugin, null) {
                @EventHandler
                public void playerQuit(PlayerQuitEvent event) {
                    sneaking.remove(event.getPlayer());
                }
            }).register();
            plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
                @Override
                public void run() {
                    for (Map.Entry<Player, Boolean> entry : sneaking.entrySet()) {
                        setHight(entry.getKey(), entry.getValue() ? 1.5F : 1.6F);
                    }
                }
            }, 1, 1);
        }
    }

    @EventHandler(ignoreCancelled = true)
    public void playerToggleSneak(PlayerToggleSneakEvent event) {
        Player player = event.getPlayer();
        int protocolVersion = getUserConnection(player).get(ProtocolInfo.class).getProtocolVersion();
        if (is1_14Fix && protocolVersion >= ProtocolVersion.v1_14.getId()) {
            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()) {
            setHight(player, event.isSneaking() ? 1.6F : 1.8F);
            if (!useCache) return;
            if (event.isSneaking())
                sneaking.put(player, false);
            else
                sneaking.remove(player);
        }
    }

    private void setHight(Player player, float hight) {
        try {
            setSize.invoke(getHandle.invoke(player), 0.6F, hight);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

@@ -762,16 +762,226 @@
 tener %7B%0A
+    private static final float STANDING_HEIGHT = 1.8F;%0A    private static final float HEIGHT_1_14 = 1.5F;%0A    private static final float HEIGHT_1_9 = 1.6F;%0A    private static final float DEFAULT_WIDTH = 0.6F;%0A%0A
     priv
@@ -2692,24 +2692,25 @@
         setH
+e
 ight(entry.g
@@ -2741,19 +2741,32 @@
 ) ? 
-1.5F : 1.6F
+HEIGHT_1_14 : HEIGHT_1_9
 );%0A 
@@ -3181,32 +3181,33 @@
             setH
+e
 ight(player, eve
@@ -3228,19 +3228,37 @@
 ) ? 
-1.5F : 1.8F
+HEIGHT_1_14 : STANDING_HEIGHT
 );%0A 
@@ -3523,24 +3523,25 @@
         setH
+e
 ight(player,
@@ -3566,19 +3566,36 @@
 ) ? 
-1.6F : 1.8F
+HEIGHT_1_9 : STANDING_HEIGHT
 );%0A 
@@ -3805,16 +3805,17 @@
 oid setH
+e
 ight(Pla
@@ -3833,16 +3833,17 @@
  float h
+e
 ight) %7B%0A
@@ -3913,15 +3913,25 @@
 r), 
-0.6F
+DEFAULT_WIDTH
 , h
+e
 ight

Use constants for heights