优化所有Ammo相关调用

This commit is contained in:
Light_Quanta 2025-03-03 19:08:23 +08:00
parent 3ca835249f
commit 464ae57e91
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
20 changed files with 120 additions and 176 deletions

View file

@ -366,8 +366,8 @@ public class VehicleHudOverlay {
} }
public static void renderKillIndicator(GuiGraphics guiGraphics, float w, float h) { 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 posX = w / 2f - 7.5f + (float) (2 * (Math.random() - 0.5f));
float posY = h / 2f - 7.5f + (float) (2 * (java.lang.Math.random() - 0.5f)); float posY = h / 2f - 7.5f + (float) (2 * (Math.random() - 0.5f));
float rate = (40 - KILL_INDICATOR * 5) / 5.5f; float rate = (40 - KILL_INDICATOR * 5) / 5.5f;
if (HIT_INDICATOR > 0) { if (HIT_INDICATOR > 0) {

View file

@ -31,15 +31,7 @@ public class AmmoCommand {
var type = context.getArgument("type", AmmoType.class); var type = context.getArgument("type", AmmoType.class);
var value = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> var value = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(type::get).orElse(0);
switch (type) {
case HANDGUN -> c.handgunAmmo;
case RIFLE -> c.rifleAmmo;
case SHOTGUN -> c.shotgunAmmo;
case SNIPER -> c.sniperAmmo;
case HEAVY -> c.heavyAmmo;
}
).orElse(0);
context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translatableKey), value), true); context.getSource().sendSuccess(() -> Component.translatable("commands.ammo.get", Component.translatable(type.translatableKey), value), true);
return 0; return 0;
})))) }))))
@ -50,13 +42,7 @@ public class AmmoCommand {
for (var player : players) { for (var player : players) {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
switch (type) { type.set(capability, value);
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;
}
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
} }
@ -71,20 +57,7 @@ public class AmmoCommand {
for (var player : players) { for (var player : players) {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
switch (type) { type.add(capability, value);
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;
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
} }

View file

@ -9,10 +9,7 @@ import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity;
import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier;
import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.mojang.math.Axis; import com.mojang.math.Axis;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -211,10 +208,10 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity
int ammoCount = this.getItemStacks().stream().filter(stack -> { int ammoCount = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; return AmmoType.HEAVY.get(stack) > 0;
} }
return false; 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(); + 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) { 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) { if (!hasCreativeAmmo) {
ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; return AmmoType.HEAVY.get(stack) > 0;
} }
return false; return false;
}).findFirst().orElse(ItemStack.EMPTY); }).findFirst().orElse(ItemStack.EMPTY);
if (!ammoBox.isEmpty()) { if (!ammoBox.isEmpty()) {
ammoBox.getOrCreateTag().putInt("HeavyAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("HeavyAmmo") - 1)); AmmoType.HEAVY.add(ammoBox, -1);
} else { } else {
this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1));
} }

View file

@ -153,7 +153,7 @@ public class AnnihilatorEntity extends EnergyVehicleEntity implements GeoEntity,
double d0 = pTarget.x - vec3.x; double d0 = pTarget.x - vec3.x;
double d1 = pTarget.y - vec3.y; double d1 = pTarget.y - vec3.y;
double d2 = pTarget.z - vec3.z; 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(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F));
entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875)))); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875))));
} }

View file

@ -9,10 +9,7 @@ import com.atsuishio.superbwarfare.entity.projectile.WgMissileEntity;
import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier;
import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import com.mojang.math.Axis; import com.mojang.math.Axis;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
@ -274,10 +271,10 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit
int ammoCount = this.getItemStacks().stream().filter(stack -> { int ammoCount = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("RifleAmmo") > 0; return AmmoType.RIFLE.get(stack) > 0;
} }
return false; 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(); + 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 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); 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.level().isClientSide) {
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
@ -377,13 +374,13 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit
if (!hasCreativeAmmo) { if (!hasCreativeAmmo) {
ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("RifleAmmo") > 0; return AmmoType.RIFLE.get(stack) > 0;
} }
return false; return false;
}).findFirst().orElse(ItemStack.EMPTY); }).findFirst().orElse(ItemStack.EMPTY);
if (!ammoBox.isEmpty()) { if (!ammoBox.isEmpty()) {
ammoBox.getOrCreateTag().putInt("RifleAmmo", Math.max(0, ammoBox.getOrCreateTag().getInt("RifleAmmo") - 1)); AmmoType.RIFLE.add(ammoBox, -1);
} else { } else {
this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1));
} }

View file

@ -8,10 +8,7 @@ import com.atsuishio.superbwarfare.entity.projectile.SmallCannonShellEntity;
import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier;
import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import com.mojang.math.Axis; import com.mojang.math.Axis;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
@ -256,10 +253,10 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt
int ammoCount = this.getItemStacks().stream().filter(stack -> { int ammoCount = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("RifleAmmo") > 0; return AmmoType.RIFLE.get(stack) > 0;
} }
return false; 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(); + 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); 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.level().isClientSide) {
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
@ -349,13 +346,13 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt
if (!hasCreativeAmmo) { if (!hasCreativeAmmo) {
ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("RifleAmmo") > 0; return AmmoType.RIFLE.get(stack) > 0;
} }
return false; return false;
}).findFirst().orElse(ItemStack.EMPTY); }).findFirst().orElse(ItemStack.EMPTY);
if (!ammoBox.isEmpty()) { if (!ammoBox.isEmpty()) {
ammoBox.getOrCreateTag().putInt("RifleAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("RifleAmmo") - 1)); AmmoType.RIFLE.add(ammoBox, -1);
} else { } else {
this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1));
} }

View file

@ -124,7 +124,7 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, ICannonEntit
double d0 = pTarget.x - vec3.x; double d0 = pTarget.x - vec3.x;
double d1 = pTarget.y - vec3.y; double d1 = pTarget.y - vec3.y;
double d2 = pTarget.z - vec3.z; 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); double distance = pTarget.distanceTo(vec3);
entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); 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)); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875))) - (float) (distance * 0.008f));

View file

@ -129,7 +129,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, ICannonEn
double d0 = pTarget.x - vec3.x; double d0 = pTarget.x - vec3.x;
double d1 = pTarget.y - vec3.y; double d1 = pTarget.y - vec3.y;
double d2 = pTarget.z - vec3.z; 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); double distance = pTarget.distanceTo(vec3);
entityData.set(YAW, Mth.wrapDegrees((float) (Mth.atan2(d2, d0) * 57.2957763671875) - 90.0F)); 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)); entityData.set(PITCH, Mth.wrapDegrees((float) (-(Mth.atan2(d1, d3) * 57.2957763671875))) - (float) (distance * 0.008f));

View file

@ -266,17 +266,17 @@ public class MobileVehicleEntity extends EnergyVehicleEntity {
public Vector3f getForwardDirection() { public Vector3f getForwardDirection() {
return new Vector3f( return new Vector3f(
Mth.sin(-getYRot() * ((float) java.lang.Math.PI / 180)), Mth.sin(-getYRot() * ((float) Math.PI / 180)),
0.0f, 0.0f,
Mth.cos(getYRot() * ((float) java.lang.Math.PI / 180)) Mth.cos(getYRot() * ((float) Math.PI / 180))
).normalize(); ).normalize();
} }
public Vector3f getRightDirection() { public Vector3f getRightDirection() {
return new Vector3f( return new Vector3f(
Mth.cos(-getYRot() * ((float) java.lang.Math.PI / 180)), Mth.cos(-getYRot() * ((float) Math.PI / 180)),
0.0f, 0.0f,
Mth.sin(getYRot() * ((float) java.lang.Math.PI / 180)) Mth.sin(getYRot() * ((float) Math.PI / 180))
).normalize(); ).normalize();
} }

View file

@ -8,10 +8,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier;
import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.ModVariables; import com.atsuishio.superbwarfare.network.ModVariables;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
@ -203,10 +200,10 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo
int ammoCount = this.getItemStacks().stream().filter(stack -> { int ammoCount = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; return AmmoType.HEAVY.get(stack) > 0;
} }
return false; 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(); + 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); (float) 0.4);
this.level().addFreshEntity(projectile); 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.level().isClientSide) {
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
@ -268,13 +265,13 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo
if (!hasCreativeAmmo) { if (!hasCreativeAmmo) {
ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> { ItemStack ammoBox = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; return AmmoType.HEAVY.get(stack) > 0;
} }
return false; return false;
}).findFirst().orElse(ItemStack.EMPTY); }).findFirst().orElse(ItemStack.EMPTY);
if (!ammoBox.isEmpty()) { if (!ammoBox.isEmpty()) {
ammoBox.getOrCreateTag().putInt("HeavyAmmo", java.lang.Math.max(0, ammoBox.getOrCreateTag().getInt("HeavyAmmo") - 1)); AmmoType.HEAVY.add(ammoBox, -1);
} else { } else {
this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).findFirst().ifPresent(stack -> stack.shrink(1)); 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; 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); pCallback.accept(pPassenger, this.getX() + vec3.x, posY, this.getZ() + vec3.z);
} else { } else {
pCallback.accept(pPassenger, this.getX(), posY, this.getZ()); pCallback.accept(pPassenger, this.getX(), posY, this.getZ());

View file

@ -470,9 +470,9 @@ public class VehicleEntity extends Entity {
protected Vec3 getDismountOffset(double vehicleWidth, double passengerWidth) { protected Vec3 getDismountOffset(double vehicleWidth, double passengerWidth) {
double offset = (vehicleWidth + passengerWidth + (double) 1.0E-5f) / 1.75; double offset = (vehicleWidth + passengerWidth + (double) 1.0E-5f) / 1.75;
float yaw = getYRot() + 90.0f; float yaw = getYRot() + 90.0f;
float x = -Mth.sin(yaw * ((float) java.lang.Math.PI / 180)); float x = -Mth.sin(yaw * ((float) Math.PI / 180));
float z = Mth.cos(yaw * ((float) java.lang.Math.PI / 180)); float z = Mth.cos(yaw * ((float) Math.PI / 180));
float n = java.lang.Math.max(java.lang.Math.abs(x), java.lang.Math.abs(z)); 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); return new Vec3((double) x * offset / (double) n, 0.0, (double) z * offset / (double) n);
} }

View file

@ -7,10 +7,7 @@ import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity;
import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier;
import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.network.message.ShakeClientMessage; import com.atsuishio.superbwarfare.network.message.ShakeClientMessage;
import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.EntityFindUtil;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import com.mojang.math.Axis; import com.mojang.math.Axis;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
@ -230,10 +227,10 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti
int ammoCount = this.getItemStacks().stream().filter(stack -> { int ammoCount = this.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("RifleAmmo") > 0; return AmmoType.RIFLE.get(stack) > 0;
} }
return false; 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(); + 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())); boolean hasCreativeAmmo = player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()));

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.entity.vehicle;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.init.ModEntities; import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.tools.AmmoType;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; 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) { if (this.getVehicle() instanceof Yx100Entity yx100) {
int ammoCount = yx100.getItemStacks().stream().filter(stack -> { int ammoCount = yx100.getItemStacks().stream().filter(stack -> {
if (stack.is(ModItems.AMMO_BOX.get())) { if (stack.is(ModItems.AMMO_BOX.get())) {
return stack.getOrCreateTag().getInt("HeavyAmmo") > 0; return AmmoType.HEAVY.get(stack) > 0;
} }
return false; 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(); + yx100.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum();

View file

@ -724,14 +724,14 @@ public class ClientEventHandler {
public static void playVehicleClientSounds(Player player, IArmedVehicleEntity iVehicle) { public static void playVehicleClientSounds(Player player, IArmedVehicleEntity iVehicle) {
if (iVehicle instanceof SpeedboatEntity speedboat) { 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.M_2_FIRE_1P.get(), 1f, pitch);
player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1);
} }
if (iVehicle instanceof MultiWeaponVehicleEntity multiWeaponVehicle) { if (iVehicle instanceof MultiWeaponVehicleEntity multiWeaponVehicle) {
if (iVehicle instanceof Ah6Entity ah6Entity) { 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) { if (multiWeaponVehicle.getWeaponType() == 0) {
ah6Entity.heat += 5; ah6Entity.heat += 5;
player.playSound(ModSounds.HELICOPTER_CANNON_FIRE_1P.get(), 1f, pitch); player.playSound(ModSounds.HELICOPTER_CANNON_FIRE_1P.get(), 1f, pitch);
@ -741,22 +741,22 @@ public class ClientEventHandler {
} }
if (iVehicle instanceof Lav150Entity lav150) { if (iVehicle instanceof Lav150Entity lav150) {
if (multiWeaponVehicle.getWeaponType() == 0) { 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.LAV_CANNON_FIRE_1P.get(), 1f, pitch);
player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1);
} else if (multiWeaponVehicle.getWeaponType() == 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); player.playSound(ModSounds.COAX_FIRE_1P.get(), 1f, pitch);
} }
} }
if (iVehicle instanceof Bmp2Entity bmp2) { if (iVehicle instanceof Bmp2Entity bmp2) {
if (multiWeaponVehicle.getWeaponType() == 0) { 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.BMP_CANNON_FIRE_1P.get(), 1f, pitch);
player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1); player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1);
} else if (multiWeaponVehicle.getWeaponType() == 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); player.playSound(ModSounds.COAX_FIRE_1P.get(), 1f, pitch);
} else if (multiWeaponVehicle.getWeaponType() == 2) { } else if (multiWeaponVehicle.getWeaponType() == 2) {
player.playSound(ModSounds.BMP_MISSILE_FIRE_1P.get(), 1f, 1); player.playSound(ModSounds.BMP_MISSILE_FIRE_1P.get(), 1f, 1);

View file

@ -19,10 +19,7 @@ import com.atsuishio.superbwarfare.network.message.PlayerGunKillMessage;
import com.atsuishio.superbwarfare.perk.AmmoPerk; import com.atsuishio.superbwarfare.perk.AmmoPerk;
import com.atsuishio.superbwarfare.perk.Perk; import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.perk.PerkHelper; import com.atsuishio.superbwarfare.perk.PerkHelper;
import com.atsuishio.superbwarfare.tools.DamageTypeTool; import com.atsuishio.superbwarfare.tools.*;
import com.atsuishio.superbwarfare.tools.FormatTool;
import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket; import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
@ -715,17 +712,13 @@ public class LivingEventHandler {
CompoundTag tag = stack.getOrCreateTag(); CompoundTag tag = stack.getOrCreateTag();
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
tag.putInt("RifleAmmo", cap.rifleAmmo);
capability.rifleAmmo = 0; for (var type : AmmoType.values()) {
tag.putInt("HandgunAmmo", cap.handgunAmmo); type.set(tag, type.get(cap));
capability.handgunAmmo = 0; type.set(capability, 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;
tag.putBoolean("IsDrop", true); tag.putBoolean("IsDrop", true);
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });

View file

@ -48,12 +48,12 @@ public class AmmoBox extends Item {
if (player.isCrouching()) { if (player.isCrouching()) {
// 存入弹药 // 存入弹药
ammoType.setCount(tag, ammoType.getCount(cap) + ammoType.getCount(tag)); ammoType.add(tag, ammoType.get(cap));
ammoType.setCount(cap, 0); ammoType.set(cap, 0);
} else { } else {
// 取出弹药 // 取出弹药
ammoType.setCount(cap, ammoType.getCount(cap) + ammoType.getCount(tag)); ammoType.add(cap, ammoType.get(tag));
ammoType.setCount(tag, 0); ammoType.set(tag, 0);
} }
} }
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
@ -126,22 +126,22 @@ public class AmmoBox extends Item {
tooltip.add(Component.translatable("des.superbwarfare.ammo_box.handgun").withStyle(ChatFormatting.AQUA) tooltip.add(Component.translatable("des.superbwarfare.ammo_box.handgun").withStyle(ChatFormatting.AQUA)
.append(Component.literal("").withStyle(ChatFormatting.RESET)) .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) tooltip.add(Component.translatable("des.superbwarfare.ammo_box.rifle").withStyle(ChatFormatting.GREEN)
.append(Component.literal("").withStyle(ChatFormatting.RESET)) .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) tooltip.add(Component.translatable("des.superbwarfare.ammo_box.shotgun").withStyle(ChatFormatting.RED)
.append(Component.literal("").withStyle(ChatFormatting.RESET)) .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) tooltip.add(Component.translatable("des.superbwarfare.ammo_box.sniper").withStyle(ChatFormatting.GOLD)
.append(Component.literal("").withStyle(ChatFormatting.RESET)) .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) tooltip.add(Component.translatable("des.superbwarfare.ammo_box.heavy").withStyle(ChatFormatting.LIGHT_PURPLE)
.append(Component.literal("").withStyle(ChatFormatting.RESET)) .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)));
} }
} }

View file

@ -34,36 +34,10 @@ public class AmmoSupplierItem extends Item {
ItemStack offhandItem = player.getOffhandItem(); ItemStack offhandItem = player.getOffhandItem();
if (offhandItem.is(ModItems.AMMO_BOX.get())) { if (offhandItem.is(ModItems.AMMO_BOX.get())) {
int newAmmoCount = switch (this.type) { this.type.add(offhandItem, ammoToAdd * count);
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);
}
} else { } else {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
int newAmmoCount = switch (this.type) { this.type.add(capability, ammoToAdd * count);
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;
}
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
} }

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.network; package com.atsuishio.superbwarfare.network;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.tools.AmmoType;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -235,11 +236,11 @@ public class ModVariables {
CompoundTag nbt = new CompoundTag(); CompoundTag nbt = new CompoundTag();
nbt.putBoolean("Zoom", zoom); nbt.putBoolean("Zoom", zoom);
nbt.putBoolean("HoldFire", holdFire); nbt.putBoolean("HoldFire", holdFire);
nbt.putInt("RifleAmmo", rifleAmmo);
nbt.putInt("HandgunAmmo", handgunAmmo); for (var type : AmmoType.values()) {
nbt.putInt("ShotgunAmmo", shotgunAmmo); type.set(nbt, type.get(this));
nbt.putInt("SniperAmmo", sniperAmmo); }
nbt.putInt("HeavyAmmo", heavyAmmo);
nbt.putBoolean("BowPullHold", bowPullHold); nbt.putBoolean("BowPullHold", bowPullHold);
nbt.putBoolean("BowPull", bowPull); nbt.putBoolean("BowPull", bowPull);
nbt.putBoolean("DoubleJump", playerDoubleJump); nbt.putBoolean("DoubleJump", playerDoubleJump);
@ -259,11 +260,11 @@ public class ModVariables {
zoom = nbt.getBoolean("Zoom"); zoom = nbt.getBoolean("Zoom");
holdFire = nbt.getBoolean("HoldFire"); holdFire = nbt.getBoolean("HoldFire");
rifleAmmo = nbt.getInt("RifleAmmo");
handgunAmmo = nbt.getInt("HandgunAmmo"); for (var type : AmmoType.values()) {
shotgunAmmo = nbt.getInt("ShotgunAmmo"); type.set(this, type.get(nbt));
sniperAmmo = nbt.getInt("SniperAmmo"); }
heavyAmmo = nbt.getInt("HeavyAmmo");
bowPullHold = nbt.getBoolean("BowPullHold"); bowPullHold = nbt.getBoolean("BowPullHold");
bowPull = nbt.getBoolean("BowPull"); bowPull = nbt.getBoolean("BowPull");
playerDoubleJump = nbt.getBoolean("DoubleJump"); playerDoubleJump = nbt.getBoolean("DoubleJump");

View file

@ -27,23 +27,35 @@ public enum AmmoType {
return null; return null;
} }
public int getCount(ItemStack stack) { // ItemStack
return getCount(stack.getOrCreateTag()); public int get(ItemStack stack) {
return get(stack.getOrCreateTag());
} }
public void setCount(ItemStack stack, int count) { public void set(ItemStack stack, int count) {
setCount(stack.getOrCreateTag(), 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); 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); 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) { return switch (this) {
case HANDGUN -> variable.handgunAmmo; case HANDGUN -> variable.handgunAmmo;
case RIFLE -> variable.rifleAmmo; 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) { switch (this) {
case HANDGUN -> variable.handgunAmmo = count; case HANDGUN -> variable.handgunAmmo = count;
case RIFLE -> variable.rifleAmmo = 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 @Override
public String toString() { public String toString() {
return this.name; return this.name;

View file

@ -114,24 +114,11 @@ public class GunsTool {
GunsTool.setGunBooleanTag(stack, "NeedBoltAction", false); GunsTool.setGunBooleanTag(stack, "NeedBoltAction", false);
} }
int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> switch (type) { int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(type::get).orElse(0);
case RIFLE -> c.rifleAmmo;
case HANDGUN -> c.handgunAmmo;
case SHOTGUN -> c.shotgunAmmo;
case SNIPER -> c.sniperAmmo;
case HEAVY -> c.heavyAmmo;
}).orElse(0);
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd);
switch (type) { type.set(capability, newAmmoCount);
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;
}
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });