From 203ed22b8db50bf197bcca0aeee2530dc9baf108 Mon Sep 17 00:00:00 2001 From: Atsuihsio <842960157@qq.com> Date: Mon, 30 Sep 2024 03:28:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=80=E7=81=AB=E5=8A=A8?= =?UTF-8?q?=E7=94=BB=E7=9A=84=E8=A7=A6=E5=8F=91=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/mcreator/superbwarfare/ModUtils.java | 1 + .../event/ClientEventHandler.java | 14 ++++++- .../event/PlayerEventHandler.java | 17 -------- .../superbwarfare/network/ModVariables.java | 5 --- .../network/message/FireMessage.java | 42 ++++++++++++------- .../message/ShootAnimationMessage.java | 31 ++++++++++++++ .../network/message/ShootMessage.java | 15 +++++-- 7 files changed, 83 insertions(+), 42 deletions(-) create mode 100644 src/main/java/net/mcreator/superbwarfare/network/message/ShootAnimationMessage.java diff --git a/src/main/java/net/mcreator/superbwarfare/ModUtils.java b/src/main/java/net/mcreator/superbwarfare/ModUtils.java index dc2ab13d2..ff7c8c628 100644 --- a/src/main/java/net/mcreator/superbwarfare/ModUtils.java +++ b/src/main/java/net/mcreator/superbwarfare/ModUtils.java @@ -121,6 +121,7 @@ public class ModUtils { addNetworkMessage(ModVariables.PlayerVariablesSyncMessage.class, ModVariables.PlayerVariablesSyncMessage::buffer, ModVariables.PlayerVariablesSyncMessage::new, ModVariables.PlayerVariablesSyncMessage::handler); addNetworkMessage(ModVariables.PlayerVariablesSyncMessage.class, ModVariables.PlayerVariablesSyncMessage::buffer, ModVariables.PlayerVariablesSyncMessage::new, ModVariables.PlayerVariablesSyncMessage::handler); addNetworkMessage(ShootMessage.class, ShootMessage::encode, ShootMessage::decode, ShootMessage::handler); + addNetworkMessage(ShootAnimationMessage.class, ShootAnimationMessage::encode, ShootAnimationMessage::decode, ShootAnimationMessage::handle, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); event.enqueueWork(() -> BrewingRecipeRegistry.addRecipe(Ingredient.of(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER)), Ingredient.of(Items.LIGHTNING_ROD), PotionUtils.setPotion(new ItemStack(Items.POTION), ModPotion.SHOCK.get()))); diff --git a/src/main/java/net/mcreator/superbwarfare/event/ClientEventHandler.java b/src/main/java/net/mcreator/superbwarfare/event/ClientEventHandler.java index 1645e33b8..a71f9d372 100644 --- a/src/main/java/net/mcreator/superbwarfare/event/ClientEventHandler.java +++ b/src/main/java/net/mcreator/superbwarfare/event/ClientEventHandler.java @@ -36,9 +36,13 @@ import net.minecraftforge.client.event.ViewportEvent; import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.network.NetworkEvent; import org.lwjgl.glfw.GLFW; +import java.util.function.Supplier; + import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_X; import static net.mcreator.superbwarfare.entity.DroneEntity.ROT_Z; import static net.mcreator.superbwarfare.event.PlayerEventHandler.isProne; @@ -62,6 +66,7 @@ public class ClientEventHandler { public static double[] turnRot = {0, 0, 0}; public static double[] cameraRot = {0, 0, 0}; + public static double fireRecoilTime = 0; public static double firePosTimer = 0; public static double fireRotTimer = 0; public static double firePos = 0; @@ -423,6 +428,12 @@ public class ClientEventHandler { zoomPosZ = -Math.pow(2 * zoomTime - 1, 2) + 1; } + public static void handleFireRecoilTimeMessage(double time, Supplier ctx) { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + fireRecoilTime = time; + } + } + private static void handleWeaponFire(ViewportEvent.ComputeCameraAngles event, LivingEntity entity) { float times = 1.5f * Minecraft.getInstance().getDeltaFrameTime(); double yaw = event.getYaw(); @@ -432,11 +443,12 @@ public class ClientEventHandler { double amplitude = 15000 * stack.getOrCreateTag().getDouble("recoil_y") * stack.getOrCreateTag().getDouble("recoil_x"); var capability = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null); - if (capability.orElse(new ModVariables.PlayerVariables()).firing > 0) { + if (fireRecoilTime > 0) { firePosTimer = 0.001; fireRotTimer = 0.001; firePosZ = 0.1; fireSpread += 0.2; + fireRecoilTime -= 7 * times; } fireSpread = Mth.clamp(fireSpread - 0.6 * (Math.pow(fireSpread, 2) * times), 0, 100); diff --git a/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java b/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java index 2919b40e3..174b15226 100644 --- a/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java +++ b/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java @@ -409,7 +409,6 @@ public class PlayerEventHandler { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { capability.recoilHorizon = 2 * Math.random() - 1; capability.recoil = 0.1; - capability.firing = 1; capability.syncPlayerVariables(player); }); tag.putBoolean("shoot", false); @@ -427,27 +426,11 @@ public class PlayerEventHandler { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { capability.recoilHorizon = 2 * Math.random() - 1; capability.recoil = 0.1; - capability.firing = 1; capability.syncPlayerVariables(player); }); tag.putBoolean("shoot", false); } - /* - 开火动画计时器 - */ - if ((player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).firing > 0) { - player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.firing = (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).firing - 0.1; - capability.syncPlayerVariables(player); - }); - } else { - player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.firing = 0; - capability.syncPlayerVariables(player); - }); - } - /* 计算后坐力 */ diff --git a/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java b/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java index 40aabe42c..d42c26615 100644 --- a/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java +++ b/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java @@ -73,7 +73,6 @@ public class ModVariables { clone.holdFire = original.holdFire; clone.recoil = original.recoil; clone.recoilHorizon = original.recoilHorizon; - clone.firing = original.firing; clone.rifleAmmo = original.rifleAmmo; clone.handgunAmmo = original.handgunAmmo; clone.shotgunAmmo = original.shotgunAmmo; @@ -264,7 +263,6 @@ public class ModVariables { public boolean holdFire = false; public double recoil = 0; public double recoilHorizon = 0; - public double firing = 0; public int cannonRecoil = 0; public int rifleAmmo = 0; @@ -292,7 +290,6 @@ public class ModVariables { nbt.putBoolean("holdFire", holdFire); nbt.putDouble("recoil", recoil); nbt.putDouble("recoil_horizon", recoilHorizon); - nbt.putDouble("firing", firing); nbt.putInt("cannonRecoil", cannonRecoil); nbt.putInt("rifle_ammo", rifleAmmo); nbt.putInt("handgun_ammo", handgunAmmo); @@ -317,7 +314,6 @@ public class ModVariables { holdFire = nbt.getBoolean("holdFire"); recoil = nbt.getDouble("recoil"); recoilHorizon = nbt.getDouble("recoil_horizon"); - firing = nbt.getDouble("firing"); cannonRecoil = nbt.getInt("cannonRecoil"); rifleAmmo = nbt.getInt("rifle_ammo"); handgunAmmo = nbt.getInt("handgun_ammo"); @@ -373,7 +369,6 @@ public class ModVariables { variables.holdFire = message.data.holdFire; variables.recoil = message.data.recoil; variables.recoilHorizon = message.data.recoilHorizon; - variables.firing = message.data.firing; variables.cannonRecoil = message.data.cannonRecoil; variables.rifleAmmo = message.data.rifleAmmo; variables.handgunAmmo = message.data.handgunAmmo; diff --git a/src/main/java/net/mcreator/superbwarfare/network/message/FireMessage.java b/src/main/java/net/mcreator/superbwarfare/network/message/FireMessage.java index 2c966f040..2f097bef8 100644 --- a/src/main/java/net/mcreator/superbwarfare/network/message/FireMessage.java +++ b/src/main/java/net/mcreator/superbwarfare/network/message/FireMessage.java @@ -32,6 +32,7 @@ import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.network.NetworkEvent; +import net.minecraftforge.network.PacketDistributor; import org.joml.Vector3d; import java.util.concurrent.atomic.AtomicBoolean; @@ -263,8 +264,6 @@ public class FireMessage { player.playSound(ModSounds.BOCEK_ZOOM_FIRE_3P.get(), 2, 1); } } else { - stack.getOrCreateTag().putBoolean("shoot", true); - var perk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO); for (int index0 = 0; index0 < (perk == ModPerks.HE_BULLET.get() ? 1 : 10); index0++) { @@ -287,12 +286,6 @@ public class FireMessage { } } - player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.recoil = 0.1; - capability.firing = 1; - capability.syncPlayerVariables(player); - }); - player.getCooldowns().addCooldown(player.getMainHandItem().getItem(), 7); player.getMainHandItem().getOrCreateTag().putInt("arrow_empty", 7); player.getMainHandItem().getOrCreateTag().putDouble("power", 0); @@ -308,6 +301,11 @@ public class FireMessage { if (count == 0 && !player.isCreative()) { player.getInventory().clearOrCountMatchingItems(p -> Items.ARROW == p.getItem(), 1, player.inventoryMenu.getCraftSlots()); } + + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } } } @@ -409,14 +407,17 @@ public class FireMessage { level.addFreshEntity(taserBulletProjectile); } - stack.getOrCreateTag().putBoolean("shoot", true); - stack.getOrCreateTag().putInt("fire_animation", 4); stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1)); stack.getCapability(ForgeCapabilities.ENERGY).ifPresent( energy -> energy.extractEnergy(2000 + 200 * perkLevel, false) ); + + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } } } } @@ -463,10 +464,13 @@ public class FireMessage { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.M_79_VERYFAR.get(), SoundSource.PLAYERS, 10, 1); } - stack.getOrCreateTag().putBoolean("shoot", true); - stack.getOrCreateTag().putInt("fire_animation", 2); stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1)); + + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } } } } @@ -520,10 +524,13 @@ public class FireMessage { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.RPG_VERYFAR.get(), SoundSource.PLAYERS, 10, 1); } - tag.putBoolean("shoot", true); - tag.putInt("fire_animation", 2); tag.putInt("ammo", tag.getInt("ammo") - 1); + + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } } } @@ -578,9 +585,12 @@ public class FireMessage { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.JAVELIN_FAR.get(), SoundSource.PLAYERS, 10, 1); } - tag.putBoolean("shoot", true); - tag.putInt("fire_animation", 2); tag.putInt("ammo", tag.getInt("ammo") - 1); + + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } } } diff --git a/src/main/java/net/mcreator/superbwarfare/network/message/ShootAnimationMessage.java b/src/main/java/net/mcreator/superbwarfare/network/message/ShootAnimationMessage.java new file mode 100644 index 000000000..255f66af4 --- /dev/null +++ b/src/main/java/net/mcreator/superbwarfare/network/message/ShootAnimationMessage.java @@ -0,0 +1,31 @@ +package net.mcreator.superbwarfare.network.message; + +import net.mcreator.superbwarfare.event.ClientEventHandler; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +public class ShootAnimationMessage { + public double time; + + public ShootAnimationMessage(double time) { + this.time = time; + } + + public static void encode(ShootAnimationMessage message, FriendlyByteBuf buffer) { + buffer.writeDouble(message.time); + } + + public static ShootAnimationMessage decode(FriendlyByteBuf buffer) { + return new ShootAnimationMessage(buffer.readDouble()); + } + + public static void handle(ShootAnimationMessage message, Supplier context) { + context.get().enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, + () -> () -> ClientEventHandler.handleFireRecoilTimeMessage(message.time, context))); + context.get().setPacketHandled(true); + } +} diff --git a/src/main/java/net/mcreator/superbwarfare/network/message/ShootMessage.java b/src/main/java/net/mcreator/superbwarfare/network/message/ShootMessage.java index 9ec38ad31..a0f385135 100644 --- a/src/main/java/net/mcreator/superbwarfare/network/message/ShootMessage.java +++ b/src/main/java/net/mcreator/superbwarfare/network/message/ShootMessage.java @@ -1,5 +1,6 @@ package net.mcreator.superbwarfare.network.message; +import net.mcreator.superbwarfare.ModUtils; import net.mcreator.superbwarfare.init.ModItems; import net.mcreator.superbwarfare.init.ModPerks; import net.mcreator.superbwarfare.init.ModSounds; @@ -20,6 +21,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.network.NetworkEvent; +import net.minecraftforge.network.PacketDistributor; import java.util.function.Supplier; @@ -159,6 +161,11 @@ public class ShootMessage { playGunSounds(player); stack.getOrCreateTag().putBoolean("shoot", true); + + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } + } } else if (stack.is(ModItems.MINIGUN.get())) { var tag = stack.getOrCreateTag(); @@ -194,8 +201,6 @@ public class ShootMessage { } } - stack.getOrCreateTag().putBoolean("shoot", true); - for (int index0 = 0; index0 < (perk == ModPerks.HE_BULLET.get() ? 1 : (int) stack.getOrCreateTag().getDouble("projectile_amount")); index0++) { gunShoot(player, spared); } @@ -208,8 +213,12 @@ public class ShootMessage { } tag.putInt("fire_animation", 2); - } + stack.getOrCreateTag().putBoolean("shoot", true); + if (player.level() instanceof ServerLevel && player instanceof ServerPlayer serverPlayer) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShootAnimationMessage(10)); + } + } } } }