|
| 1 | +package io.github.kurrycat.mpkmod.compatibility.fabric_1_21_11; |
| 2 | + |
| 3 | +import com.mojang.blaze3d.platform.InputConstants; |
| 4 | +import com.mojang.blaze3d.vertex.PoseStack; |
| 5 | +import io.github.kurrycat.mpkmod.compatibility.API; |
| 6 | +import io.github.kurrycat.mpkmod.compatibility.MCClasses.Player; |
| 7 | +import io.github.kurrycat.mpkmod.compatibility.fabric_1_21_11.mixin.KeyMappingAccessor; |
| 8 | +import io.github.kurrycat.mpkmod.ticks.ButtonMS; |
| 9 | +import io.github.kurrycat.mpkmod.ticks.ButtonMSList; |
| 10 | +import io.github.kurrycat.mpkmod.util.BoundingBox3D; |
| 11 | +import io.github.kurrycat.mpkmod.util.Vector3D; |
| 12 | +import net.fabricmc.fabric.api.networking.v1.PacketSender; |
| 13 | +import net.minecraft.client.DeltaTracker; |
| 14 | +import net.minecraft.client.Minecraft; |
| 15 | +import net.minecraft.client.Options; |
| 16 | +import net.minecraft.client.gui.GuiGraphics; |
| 17 | +import net.minecraft.client.input.KeyEvent; |
| 18 | +import net.minecraft.client.multiplayer.ClientPacketListener; |
| 19 | +import net.minecraft.client.player.LocalPlayer; |
| 20 | +import net.minecraft.util.Util; |
| 21 | +import net.minecraft.world.phys.AABB; |
| 22 | +import net.minecraft.world.phys.Vec3; |
| 23 | + |
| 24 | +public class EventHandler { |
| 25 | + private static final ButtonMSList timeQueue = new ButtonMSList(); |
| 26 | + |
| 27 | + /** |
| 28 | + * @param keyInput The Minecraft {@link KeyEvent} object. |
| 29 | + * @param action The action, where 0 = unpressed, 1 = pressed, 2 = held. |
| 30 | + */ |
| 31 | + public void onKey(KeyEvent keyInput, int action) { |
| 32 | + Options options = Minecraft.getInstance().options; |
| 33 | + long eventNanos = Util.getNanos(); |
| 34 | + |
| 35 | + InputConstants.Key inputKey = InputConstants.getKey(new KeyEvent(keyInput.key(), keyInput.scancode(), keyInput.modifiers())); |
| 36 | + |
| 37 | + int[] keys = { |
| 38 | + ((KeyMappingAccessor) options.keyUp).getKey().getValue(), |
| 39 | + ((KeyMappingAccessor) options.keyLeft).getKey().getValue(), |
| 40 | + ((KeyMappingAccessor) options.keyDown).getKey().getValue(), |
| 41 | + ((KeyMappingAccessor) options.keyRight).getKey().getValue(), |
| 42 | + ((KeyMappingAccessor) options.keySprint).getKey().getValue(), |
| 43 | + ((KeyMappingAccessor) options.keyShift).getKey().getValue(), |
| 44 | + ((KeyMappingAccessor) options.keyJump).getKey().getValue() |
| 45 | + }; |
| 46 | + |
| 47 | + for (int i = 0; i < keys.length; i++) { |
| 48 | + if (keyInput.key() == keys[i]) { |
| 49 | + timeQueue.add(ButtonMS.of(ButtonMS.Button.values()[i], eventNanos, action == 1)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (action == 1) { |
| 54 | + FunctionCompatibility.pressedButtons.add(inputKey.getValue()); |
| 55 | + } else if (action == 0) { |
| 56 | + FunctionCompatibility.pressedButtons.remove(inputKey.getValue()); |
| 57 | + } |
| 58 | + |
| 59 | + API.Events.onKeyInput(keyInput.key(), inputKey.getDisplayName().getString(), action == 1); |
| 60 | + |
| 61 | + MPKMod.keyBindingMap.forEach((id, keyBinding) -> { |
| 62 | + if (keyBinding.isDown()) { |
| 63 | + API.Events.onKeybind(id); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public void onInGameOverlayRender(GuiGraphics drawContext, DeltaTracker renderTickCounter) { |
| 69 | + drawContext.pose().pushMatrix(); |
| 70 | + API.<FunctionCompatibility>getFunctionHolder().drawContext = drawContext; |
| 71 | + API.Events.onRenderOverlay(); |
| 72 | + drawContext.pose().popMatrix(); |
| 73 | + } |
| 74 | + |
| 75 | + public void onRenderWorldOverlay(PoseStack matrixStack, float tickDelta) { |
| 76 | + MPKMod.INSTANCE.matrixStack = matrixStack; |
| 77 | + matrixStack.pushPose(); |
| 78 | + Vec3 pos = Minecraft.getInstance().gameRenderer.getMainCamera().position().reverse(); |
| 79 | + MPKMod.INSTANCE.matrixStack.translate(pos); |
| 80 | + API.Events.onRenderWorldOverlay(tickDelta); |
| 81 | + matrixStack.popPose(); |
| 82 | + } |
| 83 | + |
| 84 | + public void onClientTickStart(Minecraft mc) { |
| 85 | + if (mc.isPaused() || mc.level == null) return; |
| 86 | + API.Events.onTickStart(); |
| 87 | + } |
| 88 | + |
| 89 | + public void onClientTickEnd(Minecraft mc) { |
| 90 | + if (mc.isPaused() || mc.level == null) return; |
| 91 | + LocalPlayer mcPlayer = mc.player; |
| 92 | + |
| 93 | + if (mcPlayer != null) { |
| 94 | + AABB playerBB = mcPlayer.getBoundingBox(); |
| 95 | + new Player() |
| 96 | + .setPos(new Vector3D(mcPlayer.getX(), mcPlayer.getY(), mcPlayer.getZ())) |
| 97 | + .setLastPos(new Vector3D(mcPlayer.xo, mcPlayer.yo, mcPlayer.zo)) |
| 98 | + .setMotion(new Vector3D(mcPlayer.getDeltaMovement().x, mcPlayer.getDeltaMovement().y, mcPlayer.getDeltaMovement().z)) |
| 99 | + .setRotation(mcPlayer.getRotationVector().y, mcPlayer.getRotationVector().x) |
| 100 | + .setOnGround(mcPlayer.onGround()) |
| 101 | + .setSprinting(mcPlayer.isSprinting()) |
| 102 | + .setBoundingBox(new BoundingBox3D( |
| 103 | + new Vector3D(playerBB.minX, playerBB.minY, playerBB.minZ), |
| 104 | + new Vector3D(playerBB.maxX, playerBB.maxY, playerBB.maxZ) |
| 105 | + )) |
| 106 | + .setFlying(mcPlayer.getAbilities().flying) |
| 107 | + .constructKeyInput() |
| 108 | + .setKeyMSList(timeQueue) |
| 109 | + .buildAndSave(); |
| 110 | + timeQueue.clear(); |
| 111 | + } |
| 112 | + |
| 113 | + API.Events.onTickEnd(); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + public void onServerConnect(ClientPacketListener clientPlayNetworkHandler, PacketSender packetSender, Minecraft minecraftClient) { |
| 118 | + API.Events.onServerConnect(clientPlayNetworkHandler.getConnection().isMemoryConnection()); |
| 119 | + } |
| 120 | + |
| 121 | + public void onServerDisconnect(ClientPacketListener clientPlayNetworkHandler, Minecraft minecraftClient) { |
| 122 | + API.Events.onServerDisconnect(); |
| 123 | + } |
| 124 | +} |
0 commit comments