From 73752fd1bed05990de70ca7ed36f5371248491ff Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Tue, 14 May 2024 03:33:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=8E=E5=9D=90=E5=8A=9B?= =?UTF-8?q?=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../target/event/PlayerEventHandler.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/mcreator/target/event/PlayerEventHandler.java b/src/main/java/net/mcreator/target/event/PlayerEventHandler.java index 57479c887..610a143d7 100644 --- a/src/main/java/net/mcreator/target/event/PlayerEventHandler.java +++ b/src/main/java/net/mcreator/target/event/PlayerEventHandler.java @@ -397,15 +397,14 @@ public class PlayerEventHandler { } private static void handleGunRecoil(Player player) { - ItemStack stack = player.getMainHandItem(); + if (!player.getMainHandItem().is(TargetModTags.Items.GUN)) return; - if (!stack.is(TargetModTags.Items.GUN)) { - return; - } + CompoundTag tag = player.getMainHandItem().getOrCreateTag(); + float recoilX = (float) tag.getDouble("recoilx"); + float recoilY = (float) tag.getDouble("recoily"); - float recoilX = (float) stack.getOrCreateTag().getDouble("recoilx"); - float recoilY = (float) stack.getOrCreateTag().getDouble("recoily"); - float recoilYaw = (float) (player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).recoilHorizon; + var capability = player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null); + float recoilYaw = capability.map(c -> c.recoilHorizon).orElse(0d).floatValue(); double[] recoilTimer = {0}; double totalTime = 100; @@ -414,13 +413,12 @@ public class PlayerEventHandler { Runnable recoilRunnable = () -> { while (recoilTimer[0] < recoilDuration) { - float rx; - float ry; + float rx, ry; if (player.isShiftKeyDown() && player.getBbHeight() >= 1 && player.getPersistentData().getDouble("prone") == 0) { rx = 0.7f; ry = 0.8f; } else if (player.getPersistentData().getDouble("prone") > 0) { - if (stack.getOrCreateTag().getDouble("bipod") == 1) { + if (tag.getDouble("bipod") == 1) { rx = 0.05f; ry = 0.1f; } else { @@ -432,20 +430,14 @@ public class PlayerEventHandler { ry = 1f; } - if ((player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).recoil >= 1) { - player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.recoil = 0; - capability.syncPlayerVariables(player); - }); - } + double recoil = capability.map(c -> c.recoil).orElse(0d); - if ((player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).recoil > 0) { - player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.recoil = (player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).recoil + 0.0025; - capability.syncPlayerVariables(player); - }); + if (recoil >= 1) recoil = 0d; - double sinRes = Math.sin(2 * Math.PI * (1.03f * (player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).recoil - 0.032047110911)) + 0.2; + if (recoil > 0) { + recoil += 0.0025; + + double sinRes = Math.sin(2 * Math.PI * (1.03f * recoil - 0.032047110911)) + 0.2; float newPitch = ((float) (player.getXRot() - 1.5f * recoilY * ry * sinRes)); player.setXRot(newPitch); @@ -456,6 +448,12 @@ public class PlayerEventHandler { player.yRotO = player.getYRot(); } + double finalRecoil = recoil; + capability.ifPresent(c -> { + c.recoil = finalRecoil; + c.syncPlayerVariables(player); + }); + recoilTimer[0]++; try { Thread.sleep(sleepTime);