package com.matt.forgehax.mods;

import static com.matt.forgehax.Helper.getLocalPlayer;
import static com.matt.forgehax.Helper.getNetworkManager;

import com.google.common.collect.Lists;
import com.matt.forgehax.asm.events.PacketEvent;
import com.matt.forgehax.events.LocalPlayerUpdateEvent;
import com.matt.forgehax.util.PacketHelper;
import com.matt.forgehax.util.mod.Category;
import com.matt.forgehax.util.mod.ToggleMod;
import com.matt.forgehax.util.mod.loader.RegisterMod;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.CPacketPlayer;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@RegisterMod
public class StepMod extends ToggleMod {
  public static final float DEFAULT_STEP_HEIGHT = 0.6f;

  public StepMod() {
    super(Category.PLAYER, "Step", false, "Step up blocks");
  }

  @Override
  public void onDisabled() {
    if (getLocalPlayer() != null) {
      getLocalPlayer().stepHeight = DEFAULT_STEP_HEIGHT;
    }
  }

  @SubscribeEvent
  public void onLocalPlayerUpdate(LocalPlayerUpdateEvent event) {
    EntityPlayer localPlayer = (EntityPlayer) event.getEntityLiving();
    if (localPlayer.onGround) {
      localPlayer.stepHeight = 1.f;
    } else {
      localPlayer.stepHeight = DEFAULT_STEP_HEIGHT;
    }
  }

  private CPacketPlayer previousPositionPacket = null;

  @SubscribeEvent
  public void onPacketSending(PacketEvent.Outgoing.Pre event) {
    if (event.getPacket() instanceof CPacketPlayer.Position
        || event.getPacket() instanceof CPacketPlayer.PositionRotation) {
      CPacketPlayer packetPlayer = (CPacketPlayer) event.getPacket();
      if (previousPositionPacket != null && !PacketHelper.isIgnored(event.getPacket())) {
        double diffY = packetPlayer.getY(0.f) - previousPositionPacket.getY(0.f);
        // y difference must be positive
        // greater than 1, but less than 1.5
        if (diffY > DEFAULT_STEP_HEIGHT && diffY <= 1.2491870787) {
          List<Packet> sendList = Lists.newArrayList();
          // if this is true, this must be a step
          // now to send additional packets to get around NCP
          double x = previousPositionPacket.getX(0.D);
          double y = previousPositionPacket.getY(0.D);
          double z = previousPositionPacket.getZ(0.D);
          sendList.add(new CPacketPlayer.Position(x, y + 0.4199999869D, z, true));
          sendList.add(new CPacketPlayer.Position(x, y + 0.7531999805D, z, true));
          sendList.add(
              new CPacketPlayer.Position(
                  packetPlayer.getX(0.f),
                  packetPlayer.getY(0.f),
                  packetPlayer.getZ(0.f),
                  packetPlayer.isOnGround()));
          for (Packet toSend : sendList) {
            PacketHelper.ignore(toSend);
            getNetworkManager().sendPacket(toSend);
          }
          event.setCanceled(true);
        }
      }
      previousPositionPacket = (CPacketPlayer) event.getPacket();
    }
  }
}

@@ -325,24 +325,71 @@
 cketHelper;%0A
+import com.matt.forgehax.util.command.Setting;%0A
 import com.m
@@ -420,16 +420,16 @@
 tegory;%0A
-
 import c
@@ -860,16 +860,285 @@
  = 0.6f;
+%0A  %0A  public final Setting%3CFloat%3E stepHeight =%0A      getCommandStub()%0A          .builders()%0A          .%3CFloat%3EnewSettingBuilder()%0A          .name(%22height%22)%0A          .description(%22how high you can step%22)%0A          .defaultTo(1.2f)%0A          .min(0f)%0A          .build();
 %0A%0A  publ
@@ -1585,11 +1585,31 @@
 t = 
-1.f
+stepHeight.getAsFloat()
 ;%0A  
@@ -3364,11 +3364,154 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public String getDisplayText() %7B%0A      return String.format(%22%25s%5B%25s%5D%22, super.getDisplayText(), stepHeight.get().toString());%0A  %7D
 %0A%7D%0A

Allow custom step height for StepMod