From 464ae57e91bbf439782415ffa139277bc651f219 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Mon, 3 Mar 2025 19:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=80=E6=9C=89Ammo?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/overlay/VehicleHudOverlay.java | 4 +- .../superbwarfare/command/AmmoCommand.java | 33 ++----------- .../entity/vehicle/Ah6Entity.java | 13 ++---- .../entity/vehicle/AnnihilatorEntity.java | 2 +- .../entity/vehicle/Bmp2Entity.java | 15 +++--- .../entity/vehicle/Lav150Entity.java | 15 +++--- .../entity/vehicle/Mk42Entity.java | 2 +- .../entity/vehicle/Mle1934Entity.java | 2 +- .../entity/vehicle/MobileVehicleEntity.java | 8 ++-- .../entity/vehicle/SpeedboatEntity.java | 17 +++---- .../entity/vehicle/VehicleEntity.java | 6 +-- .../entity/vehicle/Yx100Entity.java | 9 ++-- .../entity/vehicle/Yx100GunEntity.java | 5 +- .../event/ClientEventHandler.java | 12 ++--- .../event/LivingEventHandler.java | 21 +++------ .../item/common/ammo/AmmoBox.java | 18 ++++---- .../item/common/ammo/AmmoSupplierItem.java | 30 +----------- .../superbwarfare/network/ModVariables.java | 21 +++++---- .../superbwarfare/tools/AmmoType.java | 46 +++++++++++++++---- .../superbwarfare/tools/GunsTool.java | 17 +------ 20 files changed, 120 insertions(+), 176 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java index e0473d9f4..da0c91b39 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java @@ -366,8 +366,8 @@ public class VehicleHudOverlay { } public static void renderKillIndicator(GuiGraphics guiGraphics, float w, float h) { - float posX = w / 2f - 7.5f + (float) (2 * (java.lang.Math.random() - 0.5f)); - float posY = h / 2f - 7.5f + (float) (2 * (java.lang.Math.random() - 0.5f)); + float posX = w / 2f - 7.5f + (float) (2 * (Math.random() - 0.5f)); + float posY = h / 2f - 7.5f + (float) (2 * (Math.random() - 0.5f)); float rate = (40 - KILL_INDICATOR * 5) / 5.5f; if (HIT_INDICATOR > 0) { diff --git a/src/main/java/com/atsuishio/superbwarfare/command/AmmoCommand.java b/src/main/java/com/atsuishio/superbwarfare/command/AmmoCommand.java index 603054578..179ac781e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/command/AmmoCommand.java +++ b/src/main/java/com/atsuishio/superbwarfare/command/AmmoCommand.java @@ -31,15 +31,7 @@ public class AmmoCommand { var type = context.getArgument("type", AmmoType.class); - var value = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> - switch (type) { - case HANDGUN -> c.handgunAmmo; - case RIFLE -> c.rifleAmmo; - case SHOTGUN -> c.shotgunAmmo; - case SNIPER -> c.sniperAmmo; - case HEAVY -> c.heavyAmmo; - } - ).orElse(0); + var value = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(type::get).orElse(0); context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translatableKey), value), true); return 0; })))) @@ -50,13 +42,7 @@ public class AmmoCommand { for (var player : players) { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - switch (type) { - case HANDGUN -> capability.handgunAmmo = value; - case RIFLE -> capability.rifleAmmo = value; - case SHOTGUN -> capability.shotgunAmmo = value; - case SNIPER -> capability.sniperAmmo = value; - case HEAVY -> capability.heavyAmmo = value; - } + type.set(capability, value); capability.syncPlayerVariables(player); }); } @@ -71,20 +57,7 @@ public class AmmoCommand { for (var player : players) { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - switch (type) { - case HANDGUN -> capability.handgunAmmo += value; - case RIFLE -> capability.rifleAmmo += value; - case SHOTGUN -> capability.shotgunAmmo += value; - case SNIPER -> capability.sniperAmmo += value; - case HEAVY -> capability.heavyAmmo += value; - } - // 迫真溢出检测 - if (capability.handgunAmmo < 0) capability.handgunAmmo = Integer.MAX_VALUE; - if (capability.rifleAmmo < 0) capability.rifleAmmo = Integer.MAX_VALUE; - if (capability.shotgunAmmo < 0) capability.shotgunAmmo = Integer.MAX_VALUE; - if (capability.sniperAmmo < 0) capability.sniperAmmo = Integer.MAX_VALUE; - if (capability.heavyAmmo < 0) capability.heavyAmmo = Integer.MAX_VALUE; - + type.add(capability, value); capability.syncPlayerVariables(player); }); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java index b957405dd..9aae6fdb4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java @@ -9,10 +9,7 @@ import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; -import com.atsuishio.superbwarfare.tools.CustomExplosion; -import com.atsuishio.superbwarfare.tools.EntityFindUtil; -import com.atsuishio.superbwarfare.tools.ParticleTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import com.google.common.collect.Lists; import com.mojang.math.Axis; import net.minecraft.core.BlockPos; @@ -211,10 +208,10 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; + return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("HeavyAmmo")).sum() + }).mapToInt(AmmoType.HEAVY::get).sum() + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); if ((this.getItemStacks().stream().filter(stack -> stack.is(ModItems.ROCKET_70.get())).mapToInt(ItemStack::getCount).sum() > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && reloadCoolDown == 0 && this.getEntityData().get(LOADED_ROCKET) < 14) { @@ -589,13 +586,13 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity if (!hasCreativeAmmo) { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; + return AmmoType.HEAVY.get(stack) > 0; } return false; }).findFirst().orElse(ItemStack.EMPTY); if (!ammoBox.isEmpty()) { - ammoBox.getOrCreateTag().putInt("HeavyAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("HeavyAmmo") - 1)); + AmmoType.HEAVY.add(ammoBox, -1); } else { this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/AnnihilatorEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/AnnihilatorEntity.java index c835c53c1..9e637d143 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/AnnihilatorEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/AnnihilatorEntity.java @@ -153,7 +153,7 @@ public class AnnihilatorEntity extends EnergyVehicleEntity implements GeoEntity, double d0 = pTarget.x - vec3.x; double d1 = pTarget.y - vec3.y; double d2 = pTarget.z - vec3.z; - double d3 = java.lang.Math.sqrt(d0 * d0 + d2 * d2); + double d3 = Math.sqrt(d0 * d0 + d2 * d2); entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875)))); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java index a76deb8c7..8c36fd673 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java @@ -9,10 +9,7 @@ import com.atsuishio.superbwarfare.entity.projectile.WgMissileEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; -import com.atsuishio.superbwarfare.tools.CustomExplosion; -import com.atsuishio.superbwarfare.tools.EntityFindUtil; -import com.atsuishio.superbwarfare.tools.ParticleTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import com.mojang.math.Axis; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -274,10 +271,10 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("RifleAmmo") > 0; + return AmmoType.RIFLE.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("RifleAmmo")).sum() + }).mapToInt(AmmoType.RIFLE::get).sum() + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).mapToInt(ItemStack::getCount).sum(); if ((this.getItemStacks().stream().filter(stack -> stack.is(ModItems.WIRE_GUIDE_MISSILE.get())).mapToInt(ItemStack::getCount).sum() > 0 @@ -330,7 +327,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit sendParticle((ServerLevel) this.level(), ParticleTypes.LARGE_SMOKE, worldPosition.x - 1.1 * this.getDeltaMovement().x, worldPosition.y, worldPosition.z - 1.1 * this.getDeltaMovement().z, 1, 0.02, 0.02, 0.02, 0, false); - float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - this.entityData.get(HEAT))); + float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - this.entityData.get(HEAT))); if (!player.level().isClientSide) { if (player instanceof ServerPlayer serverPlayer) { @@ -377,13 +374,13 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit if (!hasCreativeAmmo) { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("RifleAmmo") > 0; + return AmmoType.RIFLE.get(stack) > 0; } return false; }).findFirst().orElse(ItemStack.EMPTY); if (!ammoBox.isEmpty()) { - ammoBox.getOrCreateTag().putInt("RifleAmmo", Math.max(0, ammoBox.getOrCreateTag().getInt("RifleAmmo") - 1)); + AmmoType.RIFLE.add(ammoBox, -1); } else { this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java index 4331e6a33..b03274f73 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java @@ -8,10 +8,7 @@ import com.atsuishio.superbwarfare.entity.projectile.SmallCannonShellEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; -import com.atsuishio.superbwarfare.tools.CustomExplosion; -import com.atsuishio.superbwarfare.tools.EntityFindUtil; -import com.atsuishio.superbwarfare.tools.ParticleTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import com.mojang.math.Axis; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -256,10 +253,10 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("RifleAmmo") > 0; + return AmmoType.RIFLE.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("RifleAmmo")).sum() + }).mapToInt(AmmoType.RIFLE::get).sum() + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).mapToInt(ItemStack::getCount).sum(); @@ -302,7 +299,7 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt sendParticle((ServerLevel) this.level(), ParticleTypes.LARGE_SMOKE, worldPosition.x - 1.1 * this.getDeltaMovement().x, worldPosition.y, worldPosition.z - 1.1 * this.getDeltaMovement().z, 1, 0.02, 0.02, 0.02, 0, false); - float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - this.entityData.get(HEAT))); + float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - this.entityData.get(HEAT))); if (!player.level().isClientSide) { if (player instanceof ServerPlayer serverPlayer) { @@ -349,13 +346,13 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt if (!hasCreativeAmmo) { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("RifleAmmo") > 0; + return AmmoType.RIFLE.get(stack) > 0; } return false; }).findFirst().orElse(ItemStack.EMPTY); if (!ammoBox.isEmpty()) { - ammoBox.getOrCreateTag().putInt("RifleAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("RifleAmmo") - 1)); + AmmoType.RIFLE.add(ammoBox, -1); } else { this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java index fedf64710..92e9aa5da 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java @@ -124,7 +124,7 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit double d0 = pTarget.x - vec3.x; double d1 = pTarget.y - vec3.y; double d2 = pTarget.z - vec3.z; - double d3 = java.lang.Math.sqrt(d0 * d0 + d2 * d2); + double d3 = Math.sqrt(d0 * d0 + d2 * d2); double distance = pTarget.distanceTo(vec3); entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875))) - (float) (distance * 0.008f)); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java index 72d7a844a..f8f9f383c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java @@ -129,7 +129,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, ICannonEn double d0 = pTarget.x - vec3.x; double d1 = pTarget.y - vec3.y; double d2 = pTarget.z - vec3.z; - double d3 = java.lang.Math.sqrt(d0 * d0 + d2 * d2); + double d3 = Math.sqrt(d0 * d0 + d2 * d2); double distance = pTarget.distanceTo(vec3); entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875))) - (float) (distance * 0.008f)); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MobileVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MobileVehicleEntity.java index d51141065..8cf894da0 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MobileVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MobileVehicleEntity.java @@ -266,17 +266,17 @@ public class MobileVehicleEntity extends EnergyVehicleEntity { public Vector3f getForwardDirection() { return new Vector3f( - Mth.sin(-getYRot() * ((float) java.lang.Math.PI / 180)), + Mth.sin(-getYRot() * ((float) Math.PI / 180)), 0.0f, - Mth.cos(getYRot() * ((float) java.lang.Math.PI / 180)) + Mth.cos(getYRot() * ((float) Math.PI / 180)) ).normalize(); } public Vector3f getRightDirection() { return new Vector3f( - Mth.cos(-getYRot() * ((float) java.lang.Math.PI / 180)), + Mth.cos(-getYRot() * ((float) Math.PI / 180)), 0.0f, - Mth.sin(getYRot() * ((float) java.lang.Math.PI / 180)) + Mth.sin(getYRot() * ((float) Math.PI / 180)) ).normalize(); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java index 98254ab8f..dbe981d72 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java @@ -8,10 +8,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.ModVariables; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; -import com.atsuishio.superbwarfare.tools.CustomExplosion; -import com.atsuishio.superbwarfare.tools.EntityFindUtil; -import com.atsuishio.superbwarfare.tools.ParticleTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.Packet; @@ -203,10 +200,10 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; + return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("HeavyAmmo")).sum() + }).mapToInt(AmmoType.HEAVY::get).sum() + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); @@ -241,7 +238,7 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo (float) 0.4); this.level().addFreshEntity(projectile); - float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - this.entityData.get(HEAT))); + float pitch = this.entityData.get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - this.entityData.get(HEAT))); if (!player.level().isClientSide) { if (player instanceof ServerPlayer serverPlayer) { @@ -268,13 +265,13 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo if (!hasCreativeAmmo) { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; + return AmmoType.HEAVY.get(stack) > 0; } return false; }).findFirst().orElse(ItemStack.EMPTY); if (!ammoBox.isEmpty()) { - ammoBox.getOrCreateTag().putInt("HeavyAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("HeavyAmmo") - 1)); + AmmoType.HEAVY.add(ammoBox, -1); } else { this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); } @@ -403,7 +400,7 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo } double xOffset = (int) -((i - 1) / 2.0 + 1) * 0.95; - Vec3 vec3 = (new Vec3(xOffset, 0.0D, zOffset)).yRot(-this.getYRot() * ((float) java.lang.Math.PI / 180F) - ((float) java.lang.Math.PI / 2F)); + Vec3 vec3 = (new Vec3(xOffset, 0.0D, zOffset)).yRot(-this.getYRot() * ((float) Math.PI / 180F) - ((float) Math.PI / 2F)); pCallback.accept(pPassenger, this.getX() + vec3.x, posY, this.getZ() + vec3.z); } else { pCallback.accept(pPassenger, this.getX(), posY, this.getZ()); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java index a556b7eca..925bc413c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java @@ -470,9 +470,9 @@ public class VehicleEntity extends Entity { protected Vec3 getDismountOffset(double vehicleWidth, double passengerWidth) { double offset = (vehicleWidth + passengerWidth + (double) 1.0E-5f) / 1.75; float yaw = getYRot() + 90.0f; - float x = -Mth.sin(yaw * ((float) java.lang.Math.PI / 180)); - float z = Mth.cos(yaw * ((float) java.lang.Math.PI / 180)); - float n = java.lang.Math.max(java.lang.Math.abs(x), java.lang.Math.abs(z)); + float x = -Mth.sin(yaw * ((float) Math.PI / 180)); + float z = Mth.cos(yaw * ((float) Math.PI / 180)); + float n = Math.max(Math.abs(x), Math.abs(z)); return new Vec3((double) x * offset / (double) n, 0.0, (double) z * offset / (double) n); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java index 7e6545ca6..eeb9547f7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java @@ -7,10 +7,7 @@ import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; -import com.atsuishio.superbwarfare.tools.CustomExplosion; -import com.atsuishio.superbwarfare.tools.EntityFindUtil; -import com.atsuishio.superbwarfare.tools.ParticleTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import com.mojang.math.Axis; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -230,10 +227,10 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("RifleAmmo") > 0; + return AmmoType.RIFLE.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("RifleAmmo")).sum() + }).mapToInt(AmmoType.RIFLE::get).sum() + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).mapToInt(ItemStack::getCount).sum(); boolean hasCreativeAmmo = player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get())); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java index 6c0b95e08..bb524115e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java @@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.entity.vehicle; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.init.ModEntities; import com.atsuishio.superbwarfare.init.ModItems; +import com.atsuishio.superbwarfare.tools.AmmoType; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; @@ -97,10 +98,10 @@ public class Yx100GunEntity extends VehicleEntity implements GeoEntity, ICannonE if (this.getVehicle() instanceof Yx100Entity yx100) { int ammoCount = yx100.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { - return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; + return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(stack -> stack.getOrCreateTag().getInt("HeavyAmmo")).sum() + }).mapToInt(AmmoType.HEAVY::get).sum() + yx100.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java index 562d8450d..42047b346 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/ClientEventHandler.java @@ -724,14 +724,14 @@ public class ClientEventHandler { public static void playVehicleClientSounds(Player player, IArmedVehicleEntity iVehicle) { if (iVehicle instanceof SpeedboatEntity speedboat) { - float pitch = speedboat.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - speedboat.getEntityData().get(HEAT))); + float pitch = speedboat.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - speedboat.getEntityData().get(HEAT))); player.playSound(ModSounds.M_2_FIRE_1P.get(), 1f, pitch); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); } if (iVehicle instanceof MultiWeaponVehicleEntity multiWeaponVehicle) { if (iVehicle instanceof Ah6Entity ah6Entity) { - float pitch = ah6Entity.heat <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - ah6Entity.heat)); + float pitch = ah6Entity.heat <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - ah6Entity.heat)); if (multiWeaponVehicle.getWeaponType() == 0) { ah6Entity.heat += 5; player.playSound(ModSounds.HELICOPTER_CANNON_FIRE_1P.get(), 1f, pitch); @@ -741,22 +741,22 @@ public class ClientEventHandler { } if (iVehicle instanceof Lav150Entity lav150) { if (multiWeaponVehicle.getWeaponType() == 0) { - float pitch = lav150.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - lav150.getEntityData().get(HEAT))); + float pitch = lav150.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - lav150.getEntityData().get(HEAT))); player.playSound(ModSounds.LAV_CANNON_FIRE_1P.get(), 1f, pitch); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); } else if (multiWeaponVehicle.getWeaponType() == 1) { - float pitch = lav150.getEntityData().get(COAX_HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - lav150.getEntityData().get(COAX_HEAT))); + float pitch = lav150.getEntityData().get(COAX_HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - lav150.getEntityData().get(COAX_HEAT))); player.playSound(ModSounds.COAX_FIRE_1P.get(), 1f, pitch); } } if (iVehicle instanceof Bmp2Entity bmp2) { if (multiWeaponVehicle.getWeaponType() == 0) { - float pitch = bmp2.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - bmp2.getEntityData().get(HEAT))); + float pitch = bmp2.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - bmp2.getEntityData().get(HEAT))); player.playSound(ModSounds.BMP_CANNON_FIRE_1P.get(), 1f, pitch); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); } else if (multiWeaponVehicle.getWeaponType() == 1) { - float pitch = bmp2.getEntityData().get(COAX_HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - bmp2.getEntityData().get(COAX_HEAT))); + float pitch = bmp2.getEntityData().get(COAX_HEAT) <= 60 ? 1 : (float) (1 - 0.011 * Math.abs(60 - bmp2.getEntityData().get(COAX_HEAT))); player.playSound(ModSounds.COAX_FIRE_1P.get(), 1f, pitch); } else if (multiWeaponVehicle.getWeaponType() == 2) { player.playSound(ModSounds.BMP_MISSILE_FIRE_1P.get(), 1f, 1); diff --git a/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java index 83589ad8e..819fda8ee 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/LivingEventHandler.java @@ -19,10 +19,7 @@ import com.atsuishio.superbwarfare.network.message.PlayerGunKillMessage; import com.atsuishio.superbwarfare.perk.AmmoPerk; import com.atsuishio.superbwarfare.perk.Perk; import com.atsuishio.superbwarfare.perk.PerkHelper; -import com.atsuishio.superbwarfare.tools.DamageTypeTool; -import com.atsuishio.superbwarfare.tools.FormatTool; -import com.atsuishio.superbwarfare.tools.GunsTool; -import com.atsuishio.superbwarfare.tools.SoundTool; +import com.atsuishio.superbwarfare.tools.*; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.game.ClientboundStopSoundPacket; @@ -715,17 +712,13 @@ public class LivingEventHandler { CompoundTag tag = stack.getOrCreateTag(); player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - tag.putInt("RifleAmmo", cap.rifleAmmo); - capability.rifleAmmo = 0; - tag.putInt("HandgunAmmo", cap.handgunAmmo); - capability.handgunAmmo = 0; - tag.putInt("ShotgunAmmo", cap.shotgunAmmo); - capability.shotgunAmmo = 0; - tag.putInt("SniperAmmo", cap.sniperAmmo); - capability.sniperAmmo = 0; - tag.putInt("HeavyAmmo", cap.heavyAmmo); - capability.heavyAmmo = 0; + + for (var type : AmmoType.values()) { + type.set(tag, type.get(cap)); + type.set(capability, 0); + } tag.putBoolean("IsDrop", true); + capability.syncPlayerVariables(player); }); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoBox.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoBox.java index 196db1324..e365663ac 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoBox.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoBox.java @@ -48,12 +48,12 @@ public class AmmoBox extends Item { if (player.isCrouching()) { // 存入弹药 - ammoType.setCount(tag, ammoType.getCount(cap) + ammoType.getCount(tag)); - ammoType.setCount(cap, 0); + ammoType.add(tag, ammoType.get(cap)); + ammoType.set(cap, 0); } else { // 取出弹药 - ammoType.setCount(cap, ammoType.getCount(cap) + ammoType.getCount(tag)); - ammoType.setCount(tag, 0); + ammoType.add(cap, ammoType.get(tag)); + ammoType.set(tag, 0); } } capability.syncPlayerVariables(player); @@ -126,22 +126,22 @@ public class AmmoBox extends Item { tooltip.add(Component.translatable("des.superbwarfare.ammo_box.handgun").withStyle(ChatFormatting.AQUA) .append(Component.literal("").withStyle(ChatFormatting.RESET)) - .append(Component.literal(FormatTool.format0D(AmmoType.HANDGUN.getCount(tag)) + ((type != AmmoType.HANDGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); + .append(Component.literal(FormatTool.format0D(AmmoType.HANDGUN.get(tag)) + ((type != AmmoType.HANDGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); tooltip.add(Component.translatable("des.superbwarfare.ammo_box.rifle").withStyle(ChatFormatting.GREEN) .append(Component.literal("").withStyle(ChatFormatting.RESET)) - .append(Component.literal(FormatTool.format0D(AmmoType.RIFLE.getCount(tag)) + ((type != AmmoType.RIFLE) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); + .append(Component.literal(FormatTool.format0D(AmmoType.RIFLE.get(tag)) + ((type != AmmoType.RIFLE) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); tooltip.add(Component.translatable("des.superbwarfare.ammo_box.shotgun").withStyle(ChatFormatting.RED) .append(Component.literal("").withStyle(ChatFormatting.RESET)) - .append(Component.literal(FormatTool.format0D(AmmoType.SHOTGUN.getCount(tag)) + ((type != AmmoType.SHOTGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); + .append(Component.literal(FormatTool.format0D(AmmoType.SHOTGUN.get(tag)) + ((type != AmmoType.SHOTGUN) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); tooltip.add(Component.translatable("des.superbwarfare.ammo_box.sniper").withStyle(ChatFormatting.GOLD) .append(Component.literal("").withStyle(ChatFormatting.RESET)) - .append(Component.literal(FormatTool.format0D(AmmoType.SNIPER.getCount(tag)) + ((type != AmmoType.SNIPER) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); + .append(Component.literal(FormatTool.format0D(AmmoType.SNIPER.get(tag)) + ((type != AmmoType.SNIPER) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); tooltip.add(Component.translatable("des.superbwarfare.ammo_box.heavy").withStyle(ChatFormatting.LIGHT_PURPLE) .append(Component.literal("").withStyle(ChatFormatting.RESET)) - .append(Component.literal(FormatTool.format0D(AmmoType.HEAVY.getCount(tag)) + ((type != AmmoType.HEAVY) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); + .append(Component.literal(FormatTool.format0D(AmmoType.HEAVY.get(tag)) + ((type != AmmoType.HEAVY) ? " " : " ←-")).withStyle(ChatFormatting.BOLD))); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java index 830ac5255..7834b1027 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/AmmoSupplierItem.java @@ -34,36 +34,10 @@ public class AmmoSupplierItem extends Item { ItemStack offhandItem = player.getOffhandItem(); if (offhandItem.is(ModItems.AMMO_BOX.get())) { - int newAmmoCount = switch (this.type) { - case HANDGUN -> offhandItem.getOrCreateTag().getInt("HandgunAmmo"); - case RIFLE -> offhandItem.getOrCreateTag().getInt("RifleAmmo"); - case SHOTGUN -> offhandItem.getOrCreateTag().getInt("ShotgunAmmo"); - case SNIPER -> offhandItem.getOrCreateTag().getInt("SniperAmmo"); - case HEAVY -> offhandItem.getOrCreateTag().getInt("HeavyAmmo"); - } + ammoToAdd * count; - switch (this.type) { - case HANDGUN -> offhandItem.getOrCreateTag().putInt("HandgunAmmo", newAmmoCount); - case RIFLE -> offhandItem.getOrCreateTag().putInt("RifleAmmo", newAmmoCount); - case SHOTGUN -> offhandItem.getOrCreateTag().putInt("ShotgunAmmo", newAmmoCount); - case SNIPER -> offhandItem.getOrCreateTag().putInt("SniperAmmo", newAmmoCount); - case HEAVY -> offhandItem.getOrCreateTag().putInt("HeavyAmmo", newAmmoCount); - } + this.type.add(offhandItem, ammoToAdd * count); } else { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - int newAmmoCount = switch (this.type) { - case HANDGUN -> capability.handgunAmmo; - case RIFLE -> capability.rifleAmmo; - case SHOTGUN -> capability.shotgunAmmo; - case SNIPER -> capability.sniperAmmo; - case HEAVY -> capability.heavyAmmo; - } + ammoToAdd * count; - switch (this.type) { - case HANDGUN -> capability.handgunAmmo = newAmmoCount; - case RIFLE -> capability.rifleAmmo = newAmmoCount; - case SHOTGUN -> capability.shotgunAmmo = newAmmoCount; - case SNIPER -> capability.sniperAmmo = newAmmoCount; - case HEAVY -> capability.heavyAmmo = newAmmoCount; - } + this.type.add(capability, ammoToAdd * count); capability.syncPlayerVariables(player); }); } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/ModVariables.java b/src/main/java/com/atsuishio/superbwarfare/network/ModVariables.java index b8750c264..01d38c253 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/ModVariables.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/ModVariables.java @@ -1,6 +1,7 @@ package com.atsuishio.superbwarfare.network; import com.atsuishio.superbwarfare.ModUtils; +import com.atsuishio.superbwarfare.tools.AmmoType; import net.minecraft.client.Minecraft; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -235,11 +236,11 @@ public class ModVariables { CompoundTag nbt = new CompoundTag(); nbt.putBoolean("Zoom", zoom); nbt.putBoolean("HoldFire", holdFire); - nbt.putInt("RifleAmmo", rifleAmmo); - nbt.putInt("HandgunAmmo", handgunAmmo); - nbt.putInt("ShotgunAmmo", shotgunAmmo); - nbt.putInt("SniperAmmo", sniperAmmo); - nbt.putInt("HeavyAmmo", heavyAmmo); + + for (var type : AmmoType.values()) { + type.set(nbt, type.get(this)); + } + nbt.putBoolean("BowPullHold", bowPullHold); nbt.putBoolean("BowPull", bowPull); nbt.putBoolean("DoubleJump", playerDoubleJump); @@ -259,11 +260,11 @@ public class ModVariables { zoom = nbt.getBoolean("Zoom"); holdFire = nbt.getBoolean("HoldFire"); - rifleAmmo = nbt.getInt("RifleAmmo"); - handgunAmmo = nbt.getInt("HandgunAmmo"); - shotgunAmmo = nbt.getInt("ShotgunAmmo"); - sniperAmmo = nbt.getInt("SniperAmmo"); - heavyAmmo = nbt.getInt("HeavyAmmo"); + + for (var type : AmmoType.values()) { + type.set(this, type.get(nbt)); + } + bowPullHold = nbt.getBoolean("BowPullHold"); bowPull = nbt.getBoolean("BowPull"); playerDoubleJump = nbt.getBoolean("DoubleJump"); diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java b/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java index 5b5e5c952..6f1a0c0d8 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/AmmoType.java @@ -27,23 +27,35 @@ public enum AmmoType { return null; } - public int getCount(ItemStack stack) { - return getCount(stack.getOrCreateTag()); + // ItemStack + public int get(ItemStack stack) { + return get(stack.getOrCreateTag()); } - public void setCount(ItemStack stack, int count) { - setCount(stack.getOrCreateTag(), count); + public void set(ItemStack stack, int count) { + set(stack.getOrCreateTag(), count); } - public int getCount(CompoundTag tag) { + public void add(ItemStack stack, int count) { + add(stack.getOrCreateTag(), count); + } + + // NBTTag + public int get(CompoundTag tag) { return tag.getInt(this.name); } - public void setCount(CompoundTag tag, int count) { + public void set(CompoundTag tag, int count) { + if (count < 0) count = 0; tag.putInt(this.name, count); } - public int getCount(ModVariables.PlayerVariables variable) { + public void add(CompoundTag tag, int count) { + set(tag, safeAdd(get(tag), count)); + } + + // PlayerVariables + public int get(ModVariables.PlayerVariables variable) { return switch (this) { case HANDGUN -> variable.handgunAmmo; case RIFLE -> variable.rifleAmmo; @@ -53,7 +65,9 @@ public enum AmmoType { }; } - public void setCount(ModVariables.PlayerVariables variable, int count) { + public void set(ModVariables.PlayerVariables variable, int count) { + if (count < 0) count = 0; + switch (this) { case HANDGUN -> variable.handgunAmmo = count; case RIFLE -> variable.rifleAmmo = count; @@ -63,6 +77,22 @@ public enum AmmoType { } } + public void add(ModVariables.PlayerVariables variable, int count) { + set(variable, safeAdd(get(variable), count)); + } + + private int safeAdd(int a, int b) { + var newCount = (long) a + (long) b; + + if (newCount > Integer.MAX_VALUE) { + newCount = Integer.MAX_VALUE; + } else if (newCount < 0) { + newCount = 0; + } + + return (int) newCount; + } + @Override public String toString() { return this.name; diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java index c96023da0..f2171763c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java @@ -114,24 +114,11 @@ public class GunsTool { GunsTool.setGunBooleanTag(stack, "NeedBoltAction", false); } - int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> switch (type) { - case RIFLE -> c.rifleAmmo; - case HANDGUN -> c.handgunAmmo; - case SHOTGUN -> c.shotgunAmmo; - case SNIPER -> c.sniperAmmo; - case HEAVY -> c.heavyAmmo; - }).orElse(0); + int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(type::get).orElse(0); player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); - switch (type) { - case RIFLE -> capability.rifleAmmo = newAmmoCount; - case HANDGUN -> capability.handgunAmmo = newAmmoCount; - case SHOTGUN -> capability.shotgunAmmo = newAmmoCount; - case SNIPER -> capability.sniperAmmo = newAmmoCount; - case HEAVY -> capability.heavyAmmo = newAmmoCount; - } - + type.set(capability, newAmmoCount); capability.syncPlayerVariables(player); });