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 76ef18a97..dc2b92bf5 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/VehicleHudOverlay.java @@ -35,7 +35,6 @@ import org.joml.Math; import static com.atsuishio.superbwarfare.client.RenderHelper.preciseBlit; import static com.atsuishio.superbwarfare.client.overlay.CrossHairOverlay.*; -import static com.atsuishio.superbwarfare.entity.vehicle.Ah6Entity.WEAPON_TYPE; import static com.atsuishio.superbwarfare.entity.vehicle.Lav150Entity.COAX_HEAT; import static com.atsuishio.superbwarfare.entity.vehicle.Lav150Entity.HEAT; import static com.atsuishio.superbwarfare.entity.vehicle.Yx100Entity.AMMO; @@ -105,7 +104,7 @@ public class VehicleHudOverlay { if (player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get())) && !(iVehicle instanceof ICannonEntity - || (iVehicle instanceof Ah6Entity ah6Entity && ah6Entity.getEntityData().get(WEAPON_TYPE) == 1)) + || (iVehicle instanceof Ah6Entity ah6Entity && ah6Entity.getWeaponType() == 1)) ) { event.getGuiGraphics().drawString( Minecraft.getInstance().font, @@ -157,7 +156,7 @@ public class VehicleHudOverlay { return Component.translatable("des.superbwarfare.tips.ammo_type.cal50").getString(); } if (iVehicle instanceof Ah6Entity ah6Entity) { - if (ah6Entity.getEntityData().get(WEAPON_TYPE) == 0) { + if (ah6Entity.getWeaponType() == 0) { return Component.translatable("des.superbwarfare.tips.ammo_type.20mm_cannon").getString(); } else { return Component.translatable("des.superbwarfare.tips.ammo_type.rocket").getString(); 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 9aae6fdb4..05b4234a7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Ah6Entity.java @@ -211,10 +211,9 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(AmmoType.HEAVY::get).sum() - + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); + }).mapToInt(AmmoType.HEAVY::get).sum() + countItem(ModItems.HEAVY_AMMO.get()); - 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 ((countItem(ModItems.ROCKET_70.get()) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && reloadCoolDown == 0 && this.getEntityData().get(LOADED_ROCKET) < 14) { this.entityData.set(LOADED_ROCKET, this.getEntityData().get(LOADED_ROCKET) + 1); reloadCoolDown = 25; if (!player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) { @@ -223,7 +222,7 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity this.level().playSound(null, this, ModSounds.MISSILE_RELOAD.get(), this.getSoundSource(), 1, 1); } - if (this.getEntityData().get(WEAPON_TYPE) == 0) { + if (this.getWeaponType() == 0) { this.entityData.set(AMMO, ammoCount); } else { this.entityData.set(AMMO, this.getEntityData().get(LOADED_ROCKET)); @@ -539,7 +538,7 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity Vector4f worldPositionRight; Vector4f worldPositionLeft; - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { if (this.cannotFire) return; x = 1.15f; @@ -616,7 +615,7 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShakeClientMessage(6, 5, 7, this.getX(), this.getEyeY(), this.getZ())); } } - } else if (entityData.get(WEAPON_TYPE) == 1 && this.getEntityData().get(LOADED_ROCKET) > 0) { + } else if (getWeaponType() == 1 && this.getEntityData().get(LOADED_ROCKET) > 0) { x = 1.7f; y = 0.62f - 1.45f; z = 0.8f; @@ -712,9 +711,9 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity @Override public boolean canShoot(Player player) { - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { return (this.entityData.get(AMMO) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && !cannotFire; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { return this.entityData.get(AMMO) > 0; } return false; @@ -767,8 +766,8 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity @Override public void changeWeapon(int scroll) { - var type = (entityData.get(WEAPON_TYPE) + scroll + 2) % 2; - entityData.set(WEAPON_TYPE, type); + var type = (getWeaponType() + scroll + 2) % 2; + setWeaponType(type); var sound = switch (type) { case 0 -> ModSounds.INTO_MISSILE.get(); @@ -784,6 +783,11 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity return entityData.get(WEAPON_TYPE); } + @Override + public void setWeaponType(int type) { + entityData.set(WEAPON_TYPE, type); + } + @Override public ResourceLocation getVehicleIcon() { return ModUtils.loc("textures/vehicle_icon/ah_6_icon.png"); 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 8c36fd673..92df5707a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Bmp2Entity.java @@ -274,10 +274,9 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit return AmmoType.RIFLE.get(stack) > 0; } return false; - }).mapToInt(AmmoType.RIFLE::get).sum() - + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).mapToInt(ItemStack::getCount).sum(); + }).mapToInt(AmmoType.RIFLE::get).sum() + countItem(ModItems.RIFLE_AMMO.get()); - if ((this.getItemStacks().stream().filter(stack -> stack.is(ModItems.WIRE_GUIDE_MISSILE.get())).mapToInt(ItemStack::getCount).sum() > 0 + if ((countItem(ModItems.WIRE_GUIDE_MISSILE.get()) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && this.reloadCoolDown <= 0 && this.getEntityData().get(LOADED_MISSILE) < 1) { this.entityData.set(LOADED_MISSILE, this.getEntityData().get(LOADED_MISSILE) + 1); @@ -288,9 +287,9 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit this.level().playSound(null, this, ModSounds.BMP_MISSILE_RELOAD.get(), this.getSoundSource(), 1, 1); } - if (this.getEntityData().get(WEAPON_TYPE) == 0) { - this.entityData.set(AMMO, this.getItemStacks().stream().filter(stack -> stack.is(ModItems.SMALL_SHELL.get())).mapToInt(ItemStack::getCount).sum()); - } else if (this.getEntityData().get(WEAPON_TYPE) == 1) { + if (getWeaponType() == 0) { + this.entityData.set(AMMO, countItem(ModItems.SMALL_SHELL.get())); + } else if (getWeaponType() == 1) { this.entityData.set(AMMO, ammoCount); } else { this.entityData.set(AMMO, this.getEntityData().get(LOADED_MISSILE)); @@ -308,7 +307,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit @Override public void vehicleShoot(Player player) { Matrix4f transform = getBarrelTransform(); - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { if (this.cannotFire) return; float x = -0.1125f; float y = 0.174025f; @@ -349,7 +348,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit this.entityData.set(HEAT, this.entityData.get(HEAT) + 7); this.entityData.set(FIRE_ANIM, 3); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.SMALL_SHELL.get())).findFirst().ifPresent(stack -> stack.shrink(1)); - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { if (this.cannotFireCoax) return; float x = 0.1125f; float y = 0.174025f; @@ -397,7 +396,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit serverPlayer.playSound(ModSounds.M_60_VERYFAR.get(), 12, 1); } } - } else if (entityData.get(WEAPON_TYPE) == 2 && this.getEntityData().get(LOADED_MISSILE) > 0) { + } else if (getWeaponType() == 2 && this.getEntityData().get(LOADED_MISSILE) > 0) { Matrix4f transformT = getBarrelTransform(); Vector4f worldPosition = transformPosition(transformT, 0, 1, 0); @@ -682,11 +681,11 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit } private PlayState firePredicate(AnimationState event) { - if (this.entityData.get(FIRE_ANIM) > 1 && entityData.get(WEAPON_TYPE) == 0) { + if (this.entityData.get(FIRE_ANIM) > 1 && getWeaponType() == 0) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lav.fire")); } - if (this.entityData.get(FIRE_ANIM) > 0 && entityData.get(WEAPON_TYPE) == 1) { + if (this.entityData.get(FIRE_ANIM) > 0 && getWeaponType() == 1) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lav.fire2")); } @@ -720,9 +719,9 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit @Override public int mainGunRpm() { - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { return 250; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { return 750; } return 250; @@ -730,11 +729,11 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit @Override public boolean canShoot(Player player) { - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { return (this.entityData.get(AMMO) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && !cannotFire; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { return (this.entityData.get(AMMO) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && !cannotFireCoax; - } else if (entityData.get(WEAPON_TYPE) == 2) { + } else if (getWeaponType() == 2) { return (this.entityData.get(LOADED_MISSILE) > 0); } return false; @@ -762,8 +761,8 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit @Override public void changeWeapon(int scroll) { - var type = (entityData.get(WEAPON_TYPE) + scroll + 3) % 3; - entityData.set(WEAPON_TYPE, type); + var type = (getWeaponType() + scroll + 3) % 3; + setWeaponType(type); var sound = switch (type) { case 0, 2 -> ModSounds.INTO_MISSILE.get(); @@ -779,6 +778,11 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit return entityData.get(WEAPON_TYPE); } + @Override + public void setWeaponType(int type) { + entityData.set(WEAPON_TYPE, type); + } + @Override public ResourceLocation getVehicleIcon() { return ModUtils.loc("textures/vehicle_icon/bmp2_icon.png"); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/ContainerMobileVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/ContainerMobileVehicleEntity.java index 1f2dab7a5..ea01e6793 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/ContainerMobileVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/ContainerMobileVehicleEntity.java @@ -12,10 +12,12 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.HasCustomInventoryScreen; +import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.vehicle.ContainerEntity; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.gameevent.GameEvent; @@ -23,6 +25,7 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.wrapper.InvWrapper; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.joml.Math; @@ -129,6 +132,70 @@ public class ContainerMobileVehicleEntity extends MobileVehicleEntity implements return this.items; } + /** + * 计算当前载具内指定物品的数量 + * + * @param item 物品类型 + * @return 物品数量 + */ + public int countItem(@NotNull Item item) { + return this.getItemStacks().stream() + .filter(stack -> stack.is(item)) + .mapToInt(ItemStack::getCount) + .sum(); + } + + /** + * 消耗载具内指定物品 + * + * @param item 物品类型 + * @param count 要消耗的数量 + * @return 成功消耗的物品数量 + */ + public int consumeItem(Item item, int count) { + int initialCount = count; + var items = this.getItemStacks().stream().filter(stack -> stack.is(item)).toList(); + for (var stack : items) { + var countToShrink = Math.min(stack.getCount(), count); + stack.shrink(countToShrink); + count -= countToShrink; + if (count <= 0) break; + } + return initialCount - count; + } + + /** + * 尝试插入指定物品指定数量,如果载具内已满则生成掉落物 + * + * @param item 物品类型 + * @param count 要插入的数量 + */ + public void insertItem(Item item, int count) { + var defaultStack = new ItemStack(item); + var maxStackSize = item.getMaxStackSize(defaultStack); + + for (int i = 0; i < this.getItemStacks().size(); i++) { + var stack = this.getItemStacks().get(i); + + if (stack.is(item) && stack.getCount() < maxStackSize) { + var countToAdd = Math.min(maxStackSize - stack.getCount(), count); + stack.grow(countToAdd); + count -= countToAdd; + } else if (stack.isEmpty()) { + var countToAdd = Math.min(maxStackSize, count); + this.getItemStacks().set(i, new ItemStack(item, countToAdd)); + count -= countToAdd; + } + + if (count <= 0) break; + } + + if (count > 0) { + var stackToDrop = new ItemStack(item, count); + this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), stackToDrop)); + } + } + @Override public void clearItemStacks() { this.items.clear(); 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 b03274f73..41e373093 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Lav150Entity.java @@ -249,20 +249,18 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt } private void handleAmmo() { - if (!(this.getFirstPassenger() instanceof Player player)) return; + if (!(this.getFirstPassenger() instanceof Player)) return; int ammoCount = this.getItemStacks().stream().filter(stack -> { if (stack.is(ModItems.AMMO_BOX.get())) { return AmmoType.RIFLE.get(stack) > 0; } return false; - }).mapToInt(AmmoType.RIFLE::get).sum() - + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.RIFLE_AMMO.get())).mapToInt(ItemStack::getCount).sum(); + }).mapToInt(AmmoType.RIFLE::get).sum() + countItem(ModItems.RIFLE_AMMO.get()); - - if (this.getEntityData().get(WEAPON_TYPE) == 0) { - this.entityData.set(AMMO, this.getItemStacks().stream().filter(stack -> stack.is(ModItems.SMALL_SHELL.get())).mapToInt(ItemStack::getCount).sum()); - } else if (this.getEntityData().get(WEAPON_TYPE) == 1) { + if (getWeaponType() == 0) { + this.entityData.set(AMMO, countItem(ModItems.SMALL_SHELL.get())); + } else if (getWeaponType() == 1) { this.entityData.set(AMMO, ammoCount); } } @@ -280,7 +278,7 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt boolean hasCreativeAmmo = player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get())); Matrix4f transform = getBarrelTransform(); - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { if (this.cannotFire) return; float x = -0.0234375f; float y = 0f; @@ -322,7 +320,7 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt this.entityData.set(FIRE_ANIM, 3); this.getItemStacks().stream().filter(stack -> stack.is(ModItems.SMALL_SHELL.get())).findFirst().ifPresent(stack -> stack.shrink(1)); - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { if (this.cannotFireCoax) return; float x = 0.3f; float y = 0.08f; @@ -629,11 +627,11 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt } private PlayState firePredicate(AnimationState event) { - if (this.entityData.get(FIRE_ANIM) > 1 && entityData.get(WEAPON_TYPE) == 0) { + if (this.entityData.get(FIRE_ANIM) > 1 && getWeaponType() == 0) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lav.fire")); } - if (this.entityData.get(FIRE_ANIM) > 0 && entityData.get(WEAPON_TYPE) == 1) { + if (this.entityData.get(FIRE_ANIM) > 0 && getWeaponType() == 1) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lav.fire2")); } @@ -667,9 +665,9 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt @Override public int mainGunRpm() { - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { return 300; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { return 600; } return 300; @@ -677,9 +675,9 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt @Override public boolean canShoot(Player player) { - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { return (this.entityData.get(AMMO) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && !cannotFire; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { return (this.entityData.get(AMMO) > 0 || player.getInventory().hasAnyMatching(s -> s.is(ModItems.CREATIVE_AMMO_BOX.get()))) && !cannotFireCoax; } return false; @@ -707,8 +705,8 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt @Override public void changeWeapon(int scroll) { - var type = (entityData.get(WEAPON_TYPE) + scroll + 2) % 2; - entityData.set(WEAPON_TYPE, type); + var type = (getWeaponType() + scroll + 2) % 2; + setWeaponType(type); var sound = switch (type) { case 0 -> ModSounds.INTO_MISSILE.get(); @@ -724,6 +722,11 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt return entityData.get(WEAPON_TYPE); } + @Override + public void setWeaponType(int type) { + entityData.set(WEAPON_TYPE, type); + } + @Override public ResourceLocation getVehicleIcon() { return ModUtils.loc("textures/vehicle_icon/lav150_icon.png"); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MultiWeaponVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MultiWeaponVehicleEntity.java index de957cb62..f3d12d984 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MultiWeaponVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MultiWeaponVehicleEntity.java @@ -5,4 +5,6 @@ public interface MultiWeaponVehicleEntity extends IArmedVehicleEntity { void changeWeapon(int scroll); int getWeaponType(); + + void setWeaponType(int type); } 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 dbe981d72..c36eefbf0 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/SpeedboatEntity.java @@ -203,8 +203,7 @@ public class SpeedboatEntity extends ContainerMobileVehicleEntity implements Geo return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(AmmoType.HEAVY::get).sum() - + this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); + }).mapToInt(AmmoType.HEAVY::get).sum() + countItem(ModItems.HEAVY_AMMO.get()); this.entityData.set(AMMO, ammoCount); 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 925bc413c..3087349b0 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/VehicleEntity.java @@ -376,17 +376,12 @@ public class VehicleEntity extends Entity { } } - if (this.getHealth() <= 0.25 * this.getMaxHealth()) { - if (this.level() instanceof ServerLevel serverLevel) { - ParticleTool.sendParticle(serverLevel, ParticleTypes.LARGE_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); - ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); + if (this.level() instanceof ServerLevel serverLevel) { + if (this.getHealth() <= 0.25 * this.getMaxHealth()) { + playLowHealthParticle(serverLevel); } - } - - if (this.getHealth() <= 0.15 * this.getMaxHealth()) { - if (this.level() instanceof ServerLevel serverLevel) { - ParticleTool.sendParticle(serverLevel, ParticleTypes.LARGE_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); - ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); + if (this.getHealth() <= 0.15 * this.getMaxHealth()) { + playLowHealthParticle(serverLevel); } } @@ -409,6 +404,11 @@ public class VehicleEntity extends Entity { } } + private void playLowHealthParticle(ServerLevel serverLevel) { + ParticleTool.sendParticle(serverLevel, ParticleTypes.LARGE_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); + ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY() + 0.7f * getBbHeight(), this.getZ(), 1, 0.35 * this.getBbWidth(), 0.15 * this.getBbHeight(), 0.35 * this.getBbWidth(), 0.01, true); + } + public void destroy() { } 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 7cf5bccbd..2cdf69a65 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100Entity.java @@ -7,7 +7,10 @@ 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.*; +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.mojang.math.Axis; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; @@ -30,12 +33,13 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.MoverType; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Item; import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; +import net.minecraftforge.event.ForgeEventFactory; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.PacketDistributor; import net.minecraftforge.network.PlayMessages; @@ -110,14 +114,14 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti public void addAdditionalSaveData(CompoundTag compound) { super.addAdditionalSaveData(compound); compound.putInt("LoadedAmmo", this.entityData.get(LOADED_AMMO)); - compound.putInt("WeaponType", this.entityData.get(WEAPON_TYPE)); + compound.putInt("WeaponType", getWeaponType()); } @Override public void readAdditionalSaveData(CompoundTag compound) { super.readAdditionalSaveData(compound); this.entityData.set(LOADED_AMMO, compound.getInt("LoadedAmmo")); - this.entityData.set(WEAPON_TYPE, compound.getInt("WeaponType")); + setWeaponType(compound.getInt("WeaponType")); } @Override @@ -240,38 +244,32 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti this.refreshDimensions(); } + private Item getCurrentAmmoItem() { + return switch (getWeaponType()) { + case 0 -> ModItems.AP_5_INCHES.get(); + case 1 -> ModItems.HE_5_INCHES.get(); + default -> throw new IllegalStateException("Unexpected value: " + getWeaponType()); + }; + } + private void handleAmmo() { if (!(this.getFirstPassenger() instanceof Player player)) return; - int ammoCount = this.getItemStacks().stream().filter(stack -> { - if (stack.is(ModItems.AMMO_BOX.get())) { - return AmmoType.RIFLE.get(stack) > 0; - } - return false; - }).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())); if (hasCreativeAmmo) { this.entityData.set(AMMO, 9999); - } else if (this.getEntityData().get(WEAPON_TYPE) == 0) { - this.entityData.set(AMMO, this.getItemStacks().stream().filter(stack -> stack.is(ModItems.AP_5_INCHES.get())).mapToInt(ItemStack::getCount).sum()); - } else if (this.getEntityData().get(WEAPON_TYPE) == 1) { - this.entityData.set(AMMO, this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HE_5_INCHES.get())).mapToInt(ItemStack::getCount).sum()); + } else { + this.entityData.set(AMMO, countItem(getCurrentAmmoItem())); } - if (this.getEntityData().get(LOADED_AMMO) == 0 && reloadCoolDown <= 0) { - if (this.getEntityData().get(WEAPON_TYPE) == 0 && (this.getItemStacks().stream().filter(stack -> stack.is(ModItems.AP_5_INCHES.get())).mapToInt(ItemStack::getCount).sum() > 0 || hasCreativeAmmo)) { - this.entityData.set(LOADED_AMMO, 1); - if (!hasCreativeAmmo) { - this.getItemStacks().stream().filter(stack -> stack.is(ModItems.AP_5_INCHES.get())).findFirst().ifPresent(stack -> stack.shrink(1)); - } - } else if (this.getEntityData().get(WEAPON_TYPE) == 1 && (this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HE_5_INCHES.get())).mapToInt(ItemStack::getCount).sum() > 0 || hasCreativeAmmo)) { - this.entityData.set(LOADED_AMMO, 1); - if (!hasCreativeAmmo) { - this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HE_5_INCHES.get())).findFirst().ifPresent(stack -> stack.shrink(1)); - } + if (this.getEntityData().get(LOADED_AMMO) == 0 + && reloadCoolDown <= 0 + && (hasCreativeAmmo || countItem(getCurrentAmmoItem()) > 0) + ) { + this.entityData.set(LOADED_AMMO, 1); + if (!hasCreativeAmmo) { + consumeItem(getCurrentAmmoItem(), 1); } } } @@ -286,27 +284,22 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti @Override public void vehicleShoot(Player player) { - if (reloadCoolDown > 0) { - return; - } + if (reloadCoolDown > 0) return; Matrix4f transform = getBarrelTransform(); - float hitDamage = 0; - float explosionRadius = 0; - float explosionDamage = 0; - float fireProbability = 0; - int fireTime = 0; - int durability = 0; - float v = 0; + float hitDamage, explosionRadius, explosionDamage, fireProbability; + int fireTime, durability; + float v; - if (entityData.get(WEAPON_TYPE) == 0) { + if (getWeaponType() == 0) { hitDamage = 500; explosionRadius = 4; explosionDamage = 100; fireProbability = 0; + fireTime = 0; durability = 60; v = 40; - } else if (entityData.get(WEAPON_TYPE) == 1) { + } else if (getWeaponType() == 1) { hitDamage = 100; explosionRadius = 10; explosionDamage = 150; @@ -314,6 +307,8 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti fireTime = 2; durability = 1; v = 25; + } else { + throw new IllegalStateException("Unexpected value: " + getWeaponType()); } Vector4f worldPosition = transformPosition(transform, 0, 0, 0); @@ -350,7 +345,7 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti } } - public final Vec3 getBarrelVector(float pPartialTicks) { + public Vec3 getBarrelVector(float pPartialTicks) { return this.calculateViewVector(this.getBarrelXRot(pPartialTicks), this.getBarrelYRot(pPartialTicks)); } @@ -435,7 +430,6 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti diffY = Mth.wrapDegrees(gunAngle - getTurretYRot() + 0.05f); diffX = Mth.wrapDegrees(driver.getXRot() - this.getTurretXRot()); - this.setTurretXRot(Mth.clamp(this.getTurretXRot() + Mth.clamp(0.95f * diffX, -5, 5), -30f, 4f)); this.setTurretYRot(this.getTurretYRot() + Mth.clamp(0.95f * diffY, -8, 8)); } @@ -579,7 +573,7 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti ModDamageTypes.causeProjectileBoomDamage(this.level().registryAccess(), attacker, attacker), 80f, this.getX(), this.getY(), this.getZ(), 5f, ExplosionConfig.EXPLOSION_DESTROY.get() ? Explosion.BlockInteraction.DESTROY : Explosion.BlockInteraction.KEEP).setDamageMultiplier(1); explosion.explode(); - net.minecraftforge.event.ForgeEventFactory.onExplosionStart(this.level(), explosion); + ForgeEventFactory.onExplosionStart(this.level(), explosion); explosion.finalizeExplosion(false); ParticleTool.spawnMediumExplosionParticles(this.level(), this.position()); } @@ -682,12 +676,7 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti @Override public void changeWeapon(int scroll) { if (entityData.get(LOADED_AMMO) > 0) { - if (entityData.get(WEAPON_TYPE) == 0) { - this.getItemStacks().stream().filter(stack -> stack.is(ModItems.AP_5_INCHES.get())).findFirst().ifPresent(stack -> stack.grow(1)); - } else if (entityData.get(WEAPON_TYPE) == 1) { - this.getItemStacks().stream().filter(stack -> stack.is(ModItems.HE_5_INCHES.get())).findFirst().ifPresent(stack -> stack.grow(1)); - } - + this.insertItem(getCurrentAmmoItem(), 1); entityData.set(LOADED_AMMO, 0); } @@ -698,8 +687,8 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti player.connection.send(clientboundstopsoundpacket); } - var type = (entityData.get(WEAPON_TYPE) + scroll + 2) % 2; - entityData.set(WEAPON_TYPE, type); + var type = (getWeaponType() + scroll + 2) % 2; + setWeaponType(type); var sound = switch (type) { case 0 -> ModSounds.INTO_MISSILE.get(); @@ -714,6 +703,11 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti return entityData.get(WEAPON_TYPE); } + @Override + public void setWeaponType(int type) { + entityData.set(WEAPON_TYPE, type); + } + @Override public ResourceLocation getVehicleIcon() { return ModUtils.loc("textures/vehicle_icon/bmp2_icon.png"); 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 bb524115e..361d2e874 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Yx100GunEntity.java @@ -18,7 +18,6 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.PlayMessages; @@ -101,8 +100,7 @@ public class Yx100GunEntity extends VehicleEntity implements GeoEntity, ICannonE return AmmoType.HEAVY.get(stack) > 0; } return false; - }).mapToInt(AmmoType.HEAVY::get).sum() - + yx100.getItemStacks().stream().filter(stack -> stack.is(ModItems.HEAVY_AMMO.get())).mapToInt(ItemStack::getCount).sum(); + }).mapToInt(AmmoType.HEAVY::get).sum() + yx100.countItem(ModItems.HEAVY_AMMO.get()); this.entityData.set(AMMO, ammoCount);