diff --git a/src/main/java/net/mcreator/superbwarfare/ModUtils.java b/src/main/java/net/mcreator/superbwarfare/ModUtils.java index fee14bee7..95ed72ebd 100644 --- a/src/main/java/net/mcreator/superbwarfare/ModUtils.java +++ b/src/main/java/net/mcreator/superbwarfare/ModUtils.java @@ -110,6 +110,7 @@ public class ModUtils { addNetworkMessage(SimulationDistanceMessage.class, SimulationDistanceMessage::encode, SimulationDistanceMessage::decode, SimulationDistanceMessage::handle, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); addNetworkMessage(GunReforgeMessage.class, GunReforgeMessage::encode, GunReforgeMessage::decode, GunReforgeMessage::handler); addNetworkMessage(SetPerkLevelMessage.class, SetPerkLevelMessage::encode, SetPerkLevelMessage::decode, SetPerkLevelMessage::handler); + addNetworkMessage(BreathMessage.class, BreathMessage::buffer, BreathMessage::new, BreathMessage::handler); 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/PlayerEventHandler.java b/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java index 5a349c946..2ea24be90 100644 --- a/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java +++ b/src/main/java/net/mcreator/superbwarfare/event/PlayerEventHandler.java @@ -98,6 +98,10 @@ public class PlayerEventHandler { handleSimulationDistance(player); handleCannonTime(player); handleTacticalSprint(player); + handleBreath(player); + if (player.getPersistentData().getDouble("NoBreath") > 0) { + player.getPersistentData().putDouble("NoBreath", (player.getPersistentData().getDouble("NoBreath") - 1)); + } } } @@ -114,13 +118,51 @@ public class PlayerEventHandler { pose = 1; } - float newPitch = (float) (player.getXRot() - 0.03f * Mth.sin((float) (0.08 * player.tickCount)) * pose * Mth.nextDouble(RandomSource.create(), 0.1, 1)); - player.setXRot(newPitch); - player.xRotO = player.getXRot(); + if (!player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breath && player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zooming) { - float newYaw = (float) (player.getYRot() - 0.015f * Mth.cos((float) (0.07 * (player.tickCount + 2 * Math.PI))) * pose * Mth.nextDouble(RandomSource.create(), 0.05, 1.25)); - player.setYRot(newYaw); - player.yRotO = player.getYRot(); + float newPitch = (float) (player.getXRot() - 0.03f * Mth.sin((float) (0.08 * player.tickCount)) * pose * Mth.nextDouble(RandomSource.create(), 0.1, 1)); + player.setXRot(newPitch); + player.xRotO = player.getXRot(); + + float newYaw = (float) (player.getYRot() - 0.015f * Mth.cos((float) (0.07 * (player.tickCount + 2 * Math.PI))) * pose * Mth.nextDouble(RandomSource.create(), 0.05, 1.25)); + player.setYRot(newYaw); + player.yRotO = player.getYRot(); + } + } + } + + private static void handleBreath(Player player) { + + + if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breath) { + player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + capability.breathTime = Mth.clamp(player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breathTime - 1, 0, 100); + capability.syncPlayerVariables(player); + }); + player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 2, 3, false, false)); + } else { + player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + capability.breathTime = Mth.clamp(player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breathTime + 1, 0, 100); + capability.syncPlayerVariables(player); + }); + } + + if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breathTime == 0) { + player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + capability.breathExhaustion = true; + capability.breath = false; + capability.syncPlayerVariables(player); + }); + if (player instanceof ServerPlayer serverPlayer) { + SoundTool.playLocalSound(serverPlayer, ModSounds.BREATH_EXHAUSTION.get(), 1, 1); + } + } + + if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breathTime == 100) { + player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + capability.breathExhaustion = false; + capability.syncPlayerVariables(player); + }); } } diff --git a/src/main/java/net/mcreator/superbwarfare/init/ModKeyMappings.java b/src/main/java/net/mcreator/superbwarfare/init/ModKeyMappings.java index 68068a40c..bdac76a84 100644 --- a/src/main/java/net/mcreator/superbwarfare/init/ModKeyMappings.java +++ b/src/main/java/net/mcreator/superbwarfare/init/ModKeyMappings.java @@ -195,12 +195,30 @@ public class ModKeyMappings { } }; + public static final KeyMapping BREATH = new KeyMapping("key.superbwarfare.breath", GLFW.GLFW_KEY_LEFT_CONTROL, "key.categories.superbwarfare") { + private boolean isDownOld = false; + + @Override + public void setDown(boolean isDown) { + super.setDown(isDown); + if (isDownOld != isDown && isDown) { + ModUtils.PACKET_HANDLER.sendToServer(new BreathMessage(true)); + BREATH_LASTPRESS = System.currentTimeMillis(); + } else if (isDownOld != isDown) { + int dt = (int) (System.currentTimeMillis() - BREATH_LASTPRESS); + ModUtils.PACKET_HANDLER.sendToServer(new BreathMessage(false)); + } + isDownOld = isDown; + } + }; + private static long FORWARD_LASTPRESS = 0; private static long BACKWARD_LASTPRESS = 0; private static long LEFT_LASTPRESS = 0; private static long RIGHT_LASTPRESS = 0; private static long UP_LASTPRESS = 0; private static long DOWN_LASTPRESS = 0; + private static long BREATH_LASTPRESS = 0; @SubscribeEvent public static void registerKeyMappings(RegisterKeyMappingsEvent event) { @@ -216,6 +234,7 @@ public class ModKeyMappings { event.register(RIGHT); event.register(UP); event.register(DOWN); + event.register(BREATH); } @Mod.EventBusSubscriber({Dist.CLIENT}) @@ -235,6 +254,7 @@ public class ModKeyMappings { RIGHT.consumeClick(); UP.consumeClick(); DOWN.consumeClick(); + BREATH.consumeClick(); } } } diff --git a/src/main/java/net/mcreator/superbwarfare/init/ModSounds.java b/src/main/java/net/mcreator/superbwarfare/init/ModSounds.java index 4383f9427..3d81087c3 100644 --- a/src/main/java/net/mcreator/superbwarfare/init/ModSounds.java +++ b/src/main/java/net/mcreator/superbwarfare/init/ModSounds.java @@ -233,4 +233,6 @@ public class ModSounds { public static final RegistryObject DRONE_SOUND = REGISTRY.register("drone_sound", () -> SoundEvent.createVariableRangeEvent(new ResourceLocation("superbwarfare", "drone_sound"))); public static final RegistryObject GRENADE_PULL = REGISTRY.register("grenade_pull", () -> SoundEvent.createVariableRangeEvent(new ResourceLocation("superbwarfare", "grenade_pull"))); public static final RegistryObject GRENADE_THROW = REGISTRY.register("grenade_throw", () -> SoundEvent.createVariableRangeEvent(new ResourceLocation("superbwarfare", "grenade_throw"))); + public static final RegistryObject BREATH_IN = REGISTRY.register("breath_in", () -> SoundEvent.createVariableRangeEvent(new ResourceLocation("superbwarfare", "breath_in"))); + public static final RegistryObject BREATH_EXHAUSTION = REGISTRY.register("breath_exhaustion", () -> SoundEvent.createVariableRangeEvent(new ResourceLocation("superbwarfare", "breath_exhaustion"))); } diff --git a/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java b/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java index 583149c4a..80543ec21 100644 --- a/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java +++ b/src/main/java/net/mcreator/superbwarfare/network/ModVariables.java @@ -82,9 +82,7 @@ public class ModVariables { clone.recoilHorizon = original.recoilHorizon; clone.firing = original.firing; clone.cannonFiring = original.cannonFiring; - clone.targetAngle = original.targetAngle; clone.rifleAmmo = original.rifleAmmo; - clone.refresh = original.refresh; clone.handgunAmmo = original.handgunAmmo; clone.shotgunAmmo = original.shotgunAmmo; clone.sniperAmmo = original.sniperAmmo; @@ -94,6 +92,9 @@ public class ModVariables { clone.tacticalSprint = original.tacticalSprint; clone.tacticalSprintTime = original.tacticalSprintTime; clone.tacticalSprintExhaustion = original.tacticalSprintExhaustion; + clone.breath = original.breath; + clone.breathTime = original.breathTime; + clone.breathExhaustion = original.breathExhaustion; if (event.getEntity().level().isClientSide()) return; @@ -274,8 +275,7 @@ public class ModVariables { public double firing = 0; public double cannonFiring = 0; public int cannonRecoil = 0; - public double targetAngle = 0; - public boolean refresh = false; + public int rifleAmmo = 0; public int handgunAmmo = 0; public int shotgunAmmo = 0; @@ -287,6 +287,10 @@ public class ModVariables { public int tacticalSprintTime = 600; public boolean tacticalSprintExhaustion = false; + public boolean breath = false; + public int breathTime = 160; + public boolean breathExhaustion = false; + public void syncPlayerVariables(Entity entity) { if (entity instanceof ServerPlayer) ModUtils.PACKET_HANDLER.send(PacketDistributor.DIMENSION.with(entity.level()::dimension), new PlayerVariablesSyncMessage(this, entity.getId())); @@ -301,9 +305,7 @@ public class ModVariables { nbt.putDouble("firing", firing); nbt.putDouble("cannonFiring", cannonFiring); nbt.putInt("cannonRecoil", cannonRecoil); - nbt.putDouble("target_angle", targetAngle); nbt.putInt("rifle_ammo", rifleAmmo); - nbt.putBoolean("refresh", refresh); nbt.putInt("handgun_ammo", handgunAmmo); nbt.putInt("shotgun_ammo", shotgunAmmo); nbt.putInt("sniper_ammo", sniperAmmo); @@ -313,6 +315,9 @@ public class ModVariables { nbt.putBoolean("tacticalSprint", tacticalSprint); nbt.putInt("tacticalSprintTime", tacticalSprintTime); nbt.putBoolean("tacticalSprintExhaustion", tacticalSprintExhaustion); + nbt.putBoolean("breath", breath); + nbt.putInt("breathTime", breathTime); + nbt.putBoolean("breathExhaustion", breathExhaustion); return nbt; } @@ -326,9 +331,7 @@ public class ModVariables { firing = nbt.getDouble("firing"); cannonFiring = nbt.getDouble("cannonFiring"); cannonRecoil = nbt.getInt("cannonRecoil"); - targetAngle = nbt.getDouble("target_angle"); rifleAmmo = nbt.getInt("rifle_ammo"); - refresh = nbt.getBoolean("refresh"); handgunAmmo = nbt.getInt("handgun_ammo"); shotgunAmmo = nbt.getInt("shotgun_ammo"); sniperAmmo = nbt.getInt("sniper_ammo"); @@ -338,6 +341,9 @@ public class ModVariables { tacticalSprint = nbt.getBoolean("tacticalSprint"); tacticalSprintTime = nbt.getInt("tacticalSprintTime"); tacticalSprintExhaustion = nbt.getBoolean("tacticalSprintExhaustion"); + breath = nbt.getBoolean("breath"); + breathTime = nbt.getInt("breathTime"); + breathExhaustion = nbt.getBoolean("breathExhaustion"); } } @@ -387,9 +393,7 @@ public class ModVariables { variables.firing = message.data.firing; variables.cannonFiring = message.data.cannonFiring; variables.cannonRecoil = message.data.cannonRecoil; - variables.targetAngle = message.data.targetAngle; variables.rifleAmmo = message.data.rifleAmmo; - variables.refresh = message.data.refresh; variables.handgunAmmo = message.data.handgunAmmo; variables.shotgunAmmo = message.data.shotgunAmmo; variables.sniperAmmo = message.data.sniperAmmo; @@ -399,6 +403,9 @@ public class ModVariables { variables.tacticalSprint = message.data.tacticalSprint; variables.tacticalSprintTime = message.data.tacticalSprintTime; variables.tacticalSprintExhaustion = message.data.tacticalSprintExhaustion; + variables.breath = message.data.breath; + variables.breathTime = message.data.breathTime; + variables.breathExhaustion = message.data.breathExhaustion; }); } } diff --git a/src/main/java/net/mcreator/superbwarfare/network/message/BreathMessage.java b/src/main/java/net/mcreator/superbwarfare/network/message/BreathMessage.java new file mode 100644 index 000000000..b9872a88d --- /dev/null +++ b/src/main/java/net/mcreator/superbwarfare/network/message/BreathMessage.java @@ -0,0 +1,66 @@ +package net.mcreator.superbwarfare.network.message; + +import net.mcreator.superbwarfare.init.ModSounds; +import net.mcreator.superbwarfare.network.ModVariables; +import net.mcreator.superbwarfare.tools.SoundTool; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraftforge.network.NetworkEvent; + +import java.util.function.Supplier; + +public class BreathMessage { + private final boolean type; + + public BreathMessage(boolean type) { + this.type = type; + } + + public BreathMessage(FriendlyByteBuf buffer) { + this.type = buffer.readBoolean(); + } + + public static void buffer(BreathMessage message, FriendlyByteBuf buffer) { + buffer.writeBoolean(message.type); + } + + public static void handler(BreathMessage message, Supplier contextSupplier) { + NetworkEvent.Context context = contextSupplier.get(); + context.enqueueWork(() -> { + if (context.getSender() != null) { + pressAction(context.getSender(), message.type); + } + }); + context.setPacketHandled(true); + } + + public static void pressAction(Player entity, boolean type) { + Level world = entity.level(); + + if (!world.isLoaded(entity.blockPosition())) { + return; + } + + var cap = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null); + + if (type && !cap.orElse(new ModVariables.PlayerVariables()).breathExhaustion && cap.orElse(new ModVariables.PlayerVariables()).zooming && entity.getPersistentData().getDouble("NoBreath") == 0) { + cap.ifPresent(capability -> { + capability.breath = true; + capability.syncPlayerVariables(entity); + }); + if (entity instanceof ServerPlayer serverPlayer) { + SoundTool.playLocalSound(serverPlayer, ModSounds.BREATH_IN.get(), 1, 1); + } + entity.getPersistentData().putDouble("NoBreath", 20); + } + + if (!type) { + cap.ifPresent(capability -> { + capability.breath = false; + capability.syncPlayerVariables(entity); + }); + } + } +} diff --git a/src/main/resources/assets/superbwarfare/lang/en_us.json b/src/main/resources/assets/superbwarfare/lang/en_us.json index 1e63d0f56..f3a64384d 100644 --- a/src/main/resources/assets/superbwarfare/lang/en_us.json +++ b/src/main/resources/assets/superbwarfare/lang/en_us.json @@ -279,6 +279,7 @@ "key.superbwarfare.right": "Right", "key.superbwarfare.up": "Up", "key.superbwarfare.down": "Down", + "key.superbwarfare.breath": "Breathe", "effect.superbwarfare.shock": "Shock", "item.minecraft.potion.effect.superbwarfare_shock": "Potion of Shock", diff --git a/src/main/resources/assets/superbwarfare/lang/zh_cn.json b/src/main/resources/assets/superbwarfare/lang/zh_cn.json index 0de553174..42776690b 100644 --- a/src/main/resources/assets/superbwarfare/lang/zh_cn.json +++ b/src/main/resources/assets/superbwarfare/lang/zh_cn.json @@ -279,6 +279,7 @@ "key.superbwarfare.right": "向右移动", "key.superbwarfare.up": "上升", "key.superbwarfare.down": "下降", + "key.superbwarfare.breath": "屏息", "effect.superbwarfare.shock": "电击", "item.minecraft.potion.effect.superbwarfare_shock": "电击药水", diff --git a/src/main/resources/assets/superbwarfare/sounds.json b/src/main/resources/assets/superbwarfare/sounds.json index d86f6c2ab..8093df4cc 100644 --- a/src/main/resources/assets/superbwarfare/sounds.json +++ b/src/main/resources/assets/superbwarfare/sounds.json @@ -1874,5 +1874,21 @@ "stream": false } ] + }, + "breath_in": { + "sounds": [ + { + "name": "superbwarfare:breath_in", + "stream": false + } + ] + }, + "breath_exhaustion": { + "sounds": [ + { + "name": "superbwarfare:breath_exhaustion", + "stream": false + } + ] } } \ No newline at end of file diff --git a/src/main/resources/assets/superbwarfare/sounds/breath_exhaustion.ogg b/src/main/resources/assets/superbwarfare/sounds/breath_exhaustion.ogg new file mode 100644 index 000000000..900d260df Binary files /dev/null and b/src/main/resources/assets/superbwarfare/sounds/breath_exhaustion.ogg differ diff --git a/src/main/resources/assets/superbwarfare/sounds/breath_in.ogg b/src/main/resources/assets/superbwarfare/sounds/breath_in.ogg new file mode 100644 index 000000000..6591958af Binary files /dev/null and b/src/main/resources/assets/superbwarfare/sounds/breath_in.ogg differ