|
| 1 | +package net.earthcomputer.clientcommands; |
| 2 | + |
| 3 | +import org.lwjgl.input.Keyboard; |
| 4 | + |
| 5 | +import net.minecraft.client.Minecraft; |
| 6 | +import net.minecraft.client.entity.EntityPlayerSP; |
| 7 | +import net.minecraft.client.settings.KeyBinding; |
| 8 | +import net.minecraft.network.Packet; |
| 9 | +import net.minecraft.network.play.client.CPacketPlayer; |
| 10 | +import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock; |
| 11 | +import net.minecraft.util.math.MathHelper; |
| 12 | +import net.minecraftforge.client.settings.IKeyConflictContext; |
| 13 | +import net.minecraftforge.client.settings.KeyConflictContext; |
| 14 | +import net.minecraftforge.fml.client.registry.ClientRegistry; |
| 15 | + |
| 16 | +public class SpecialActionKey { |
| 17 | + |
| 18 | + private SpecialActionKey() { |
| 19 | + } |
| 20 | + |
| 21 | + private static final KeyBinding KEY_BINDING = new KeyBinding("key.specialAction", new IKeyConflictContext() { |
| 22 | + @Override |
| 23 | + public boolean isActive() { |
| 24 | + return KeyConflictContext.IN_GAME.isActive(); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public boolean conflicts(IKeyConflictContext other) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + }, Keyboard.KEY_LMENU, "key.categories.misc"); |
| 32 | + |
| 33 | + private static boolean isSpecialActionKeyPressed() { |
| 34 | + return Keyboard.isKeyDown(KEY_BINDING.getKeyCode()); |
| 35 | + } |
| 36 | + |
| 37 | + public static void registerKeyBinding() { |
| 38 | + ClientRegistry.registerKeyBinding(KEY_BINDING); |
| 39 | + } |
| 40 | + |
| 41 | + public static void registerEvents() { |
| 42 | + EventManager.addOutboundPacketPreListener(e -> { |
| 43 | + Packet<?> packet = e.getPacket(); |
| 44 | + if (packet instanceof CPacketPlayerTryUseItemOnBlock) { |
| 45 | + if (isSpecialActionKeyPressed()) { |
| 46 | + EntityPlayerSP player = Minecraft.getMinecraft().player; |
| 47 | + Minecraft.getMinecraft().getConnection().sendPacket(new CPacketPlayer.Rotation( |
| 48 | + MathHelper.wrapDegrees(player.rotationYaw + 180), -player.rotationPitch, player.onGround)); |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + EventManager.addOutboundPacketPostListener(e -> { |
| 53 | + Packet<?> packet = e.getPacket(); |
| 54 | + if (packet instanceof CPacketPlayerTryUseItemOnBlock) { |
| 55 | + if (isSpecialActionKeyPressed()) { |
| 56 | + EntityPlayerSP player = Minecraft.getMinecraft().player; |
| 57 | + Minecraft.getMinecraft().getConnection().sendPacket( |
| 58 | + new CPacketPlayer.Rotation(player.rotationYaw, player.rotationPitch, player.onGround)); |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments