重新实现舰炮发射

This commit is contained in:
Light_Quanta 2025-03-06 02:16:23 +08:00
parent 79e047898d
commit 9ad01a55a3
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
15 changed files with 200 additions and 96 deletions

View file

@ -732,7 +732,7 @@ public class Ah6Entity extends ContainerMobileVehicleEntity implements GeoEntity
public void changeWeapon(int index, int value, boolean isScroll) { public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return; if (index != 0) return;
var type = isScroll ? (value + getWeaponType(0)) % 2 : value; var type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) { var sound = switch (type) {
case 0 -> ModSounds.INTO_MISSILE.get(); case 0 -> ModSounds.INTO_MISSILE.get();

View file

@ -743,7 +743,7 @@ public class Bmp2Entity extends ContainerMobileVehicleEntity implements GeoEntit
public void changeWeapon(int index, int value, boolean isScroll) { public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return; if (index != 0) return;
var type = isScroll ? (value + getWeaponType(0)) % 2 : value; var type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) { var sound = switch (type) {
case 0, 2 -> ModSounds.INTO_MISSILE.get(); case 0, 2 -> ModSounds.INTO_MISSILE.get();

View file

@ -687,7 +687,7 @@ public class Lav150Entity extends ContainerMobileVehicleEntity implements GeoEnt
public void changeWeapon(int index, int value, boolean isScroll) { public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return; if (index != 0) return;
int type = isScroll ? (value + getWeaponType(0)) % 2 : value; int type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) { var sound = switch (type) {
case 0 -> ModSounds.INTO_MISSILE.get(); case 0 -> ModSounds.INTO_MISSILE.get();

View file

@ -11,6 +11,7 @@ import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem;
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.CustomExplosion;
import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool; import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
@ -59,6 +60,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
public static final EntityDataAccessor<Float> PITCH = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> PITCH = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Float> YAW = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor<Float> YAW = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT);
public static final EntityDataAccessor<Integer> WEAPON_TYPE = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT);
public Mk42Entity(PlayMessages.SpawnEntity packet, Level world) { public Mk42Entity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.MK_42.get(), world); this(ModEntities.MK_42.get(), world);
} }
@ -73,6 +76,7 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
this.entityData.define(COOL_DOWN, 0); this.entityData.define(COOL_DOWN, 0);
this.entityData.define(PITCH, 0f); this.entityData.define(PITCH, 0f);
this.entityData.define(YAW, 0f); this.entityData.define(YAW, 0f);
this.entityData.define(WEAPON_TYPE, 0);
} }
@Override @Override
@ -106,6 +110,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
if (stack.getItem() instanceof CannonShellItem) { if (stack.getItem() instanceof CannonShellItem) {
if (this.entityData.get(COOL_DOWN) == 0) { if (this.entityData.get(COOL_DOWN) == 0) {
var weaponType = stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1;
setWeaponType(0, weaponType);
vehicleShoot(player, 0); vehicleShoot(player, 0);
} }
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
@ -232,45 +238,43 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
@Override @Override
public void vehicleShoot(Player player, int type) { public void vehicleShoot(Player player, int type) {
if (this.entityData.get(COOL_DOWN) > 0) { if (this.entityData.get(COOL_DOWN) > 0) return;
return;
}
Level level = player.level(); Level level = player.level();
if (level instanceof ServerLevel server) { if (level instanceof ServerLevel server) {
ItemStack stack = player.getMainHandItem(); var isCreative = player.isCreative() || InventoryTool.countItem(player.getInventory().items, ModItems.CREATIVE_AMMO_BOX.get()) > 0;
if (!(stack.getItem() instanceof CannonShellItem)) { if (!isCreative) {
return; var ammo = getWeaponType(0) == 0 ? ModItems.AP_5_INCHES.get() : ModItems.HE_5_INCHES.get();
var ammoCount = InventoryTool.countItem(player.getInventory().items, ammo);
if (ammoCount <= 0) return;
InventoryTool.consumeItem(player.getInventory().items, ammo, 1);
} }
float hitDamage = 0; float hitDamage;
float explosionRadius = 0; float explosionRadius;
float explosionDamage = 0; float explosionDamage;
float fireProbability = 0; float fireProbability;
int fireTime = 0; int fireTime;
int durability = 0; int durability;
if (stack.is(ModItems.HE_5_INCHES.get())) { if (getWeaponType(0) == 0) {
hitDamage = VehicleConfig.MK42_HE_DAMAGE.get(); // AP
explosionRadius = VehicleConfig.MK42_HE_EXPLOSION_RADIUS.get();
explosionDamage = VehicleConfig.MK42_HE_EXPLOSION_DAMAGE.get();
fireProbability = 0.18F;
fireTime = 2;
durability = 1;
}
if (stack.is(ModItems.AP_5_INCHES.get())) {
hitDamage = VehicleConfig.MK42_AP_DAMAGE.get(); hitDamage = VehicleConfig.MK42_AP_DAMAGE.get();
explosionRadius = VehicleConfig.MK42_AP_EXPLOSION_RADIUS.get(); explosionRadius = VehicleConfig.MK42_AP_EXPLOSION_RADIUS.get();
explosionDamage = VehicleConfig.MK42_AP_EXPLOSION_DAMAGE.get(); explosionDamage = VehicleConfig.MK42_AP_EXPLOSION_DAMAGE.get();
fireProbability = 0; fireProbability = 0;
fireTime = 0; fireTime = 0;
durability = 60; durability = 60;
} } else {
// HE
if (!player.isCreative()) { hitDamage = VehicleConfig.MK42_HE_DAMAGE.get();
stack.shrink(1); explosionRadius = VehicleConfig.MK42_HE_EXPLOSION_RADIUS.get();
explosionDamage = VehicleConfig.MK42_HE_EXPLOSION_DAMAGE.get();
fireProbability = 0.18F;
fireTime = 2;
durability = 1;
} }
CannonShellEntity entityToSpawn = new CannonShellEntity(player, level, hitDamage, explosionRadius, explosionDamage, fireProbability, fireTime) CannonShellEntity entityToSpawn = new CannonShellEntity(player, level, hitDamage, explosionRadius, explosionDamage, fireProbability, fireTime)
@ -282,7 +286,7 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1);
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_RELOAD.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1);
@ -316,7 +320,6 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
final Vec3 center = new Vec3(this.getX(), this.getEyeY(), this.getZ()); final Vec3 center = new Vec3(this.getX(), this.getEyeY(), this.getZ());
for (Entity target : level.getEntitiesOfClass(Entity.class, new AABB(center, center).inflate(20), e -> true).stream().sorted(Comparator.comparingDouble(e -> e.distanceToSqr(center))).toList()) { for (Entity target : level.getEntitiesOfClass(Entity.class, new AABB(center, center).inflate(20), e -> true).stream().sorted(Comparator.comparingDouble(e -> e.distanceToSqr(center))).toList()) {
if (target instanceof ServerPlayer serverPlayer) { if (target instanceof ServerPlayer serverPlayer) {
ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShakeClientMessage(15, 15, 45, this.getX(), this.getEyeY(), this.getZ())); ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new ShakeClientMessage(15, 15, 45, this.getX(), this.getEyeY(), this.getZ()));
} }
@ -347,6 +350,32 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity
entity.setXRot(entity.getXRot() + f1 - f); entity.setXRot(entity.getXRot() + f1 - f);
} }
@Override
public void setWeaponType(int index, int type) {
if (index != 0) return;
entityData.set(WEAPON_TYPE, type);
}
@Override
public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return;
int type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) {
case 0, 1 -> ModSounds.CANNON_RELOAD.get();
default -> null;
};
if (sound == null) return;
setWeaponType(0, type);
this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1);
}
@Override
public int getWeaponType(int index) {
return index == 0 ? entityData.get(WEAPON_TYPE) : -1;
}
@Override @Override
public void onPassengerTurned(Entity entity) { public void onPassengerTurned(Entity entity) {
this.clampRotation(entity); this.clampRotation(entity);

View file

@ -11,6 +11,7 @@ import com.atsuishio.superbwarfare.init.*;
import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem;
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.CustomExplosion;
import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.ParticleTool;
import com.atsuishio.superbwarfare.tools.SoundTool; import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
@ -61,6 +62,8 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public static final float MAX_HEALTH = VehicleConfig.MLE1934_HP.get(); public static final float MAX_HEALTH = VehicleConfig.MLE1934_HP.get();
public static final EntityDataAccessor<Integer> WEAPON_TYPE = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT);
public Mle1934Entity(PlayMessages.SpawnEntity packet, Level world) { public Mle1934Entity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.MLE_1934.get(), world); this(ModEntities.MLE_1934.get(), world);
} }
@ -76,6 +79,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
this.entityData.define(TYPE, 0); this.entityData.define(TYPE, 0);
this.entityData.define(PITCH, 0f); this.entityData.define(PITCH, 0f);
this.entityData.define(YAW, 0f); this.entityData.define(YAW, 0f);
this.entityData.define(WEAPON_TYPE, 0);
} }
@Override @Override
@ -111,6 +115,8 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
if (stack.getItem() instanceof CannonShellItem) { if (stack.getItem() instanceof CannonShellItem) {
if (this.entityData.get(COOL_DOWN) == 0) { if (this.entityData.get(COOL_DOWN) == 0) {
var weaponType = stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1;
setWeaponType(0, weaponType);
vehicleShoot(player, 0); vehicleShoot(player, 0);
} }
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
@ -246,48 +252,46 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
@Override @Override
public void vehicleShoot(Player player, int type) { public void vehicleShoot(Player player, int type) {
if (this.entityData.get(COOL_DOWN) > 0) { if (this.entityData.get(COOL_DOWN) > 0) return;
return;
}
Level level = player.level(); Level level = player.level();
if (level instanceof ServerLevel server) { if (level instanceof ServerLevel server) {
ItemStack stack = player.getMainHandItem(); var isCreative = player.isCreative() || InventoryTool.countItem(player.getInventory().items, ModItems.CREATIVE_AMMO_BOX.get()) > 0;
if (!(stack.getItem() instanceof CannonShellItem)) { int consumed;
return; if (isCreative) {
consumed = 2;
} else {
var ammo = getWeaponType(0) == 0 ? ModItems.AP_5_INCHES.get() : ModItems.HE_5_INCHES.get();
var ammoCount = InventoryTool.countItem(player.getInventory().items, ammo);
// 尝试消耗两发弹药
if (ammoCount <= 0) return;
consumed = InventoryTool.consumeItem(player.getInventory().items, ammo, 2);
} }
float hitDamage = 0; float hitDamage;
float explosionRadius = 0; float explosionRadius;
float explosionDamage = 0; float explosionDamage;
float fireProbability = 0; float fireProbability;
int fireTime = 0; int fireTime;
int durability = 0; int durability;
boolean salvoShoot = false; boolean salvoShoot = consumed == 2;
if (stack.is(ModItems.HE_5_INCHES.get())) { if (getWeaponType(0) == 0) {
hitDamage = VehicleConfig.MLE1934_HE_DAMAGE.get(); hitDamage = VehicleConfig.MLE1934_HE_DAMAGE.get();
explosionRadius = VehicleConfig.MLE1934_HE_EXPLOSION_RADIUS.get(); explosionRadius = VehicleConfig.MLE1934_HE_EXPLOSION_RADIUS.get();
explosionDamage = VehicleConfig.MLE1934_HE_EXPLOSION_DAMAGE.get(); explosionDamage = VehicleConfig.MLE1934_HE_EXPLOSION_DAMAGE.get();
fireProbability = 0.24F; fireProbability = 0.24F;
fireTime = 5; fireTime = 5;
durability = 1; durability = 1;
salvoShoot = stack.getCount() > 1 || player.isCreative(); } else {
}
if (stack.is(ModItems.AP_5_INCHES.get())) {
hitDamage = VehicleConfig.MLE1934_AP_DAMAGE.get(); hitDamage = VehicleConfig.MLE1934_AP_DAMAGE.get();
explosionRadius = VehicleConfig.MLE1934_AP_EXPLOSION_RADIUS.get(); explosionRadius = VehicleConfig.MLE1934_AP_EXPLOSION_RADIUS.get();
explosionDamage = VehicleConfig.MLE1934_AP_EXPLOSION_DAMAGE.get(); explosionDamage = VehicleConfig.MLE1934_AP_EXPLOSION_DAMAGE.get();
fireProbability = 0; fireProbability = 0;
fireTime = 0; fireTime = 0;
durability = 70; durability = 70;
salvoShoot = stack.getCount() > 1 || player.isCreative();
}
if (!player.isCreative()) {
stack.shrink(salvoShoot ? 2 : 1);
} }
float yRot = this.getYRot(); float yRot = this.getYRot();
@ -392,7 +396,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1);
ModUtils.queueServerWork(44, () -> SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_RELOAD.get(), 2, 1)); ModUtils.queueServerWork(44, () -> SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1));
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1);
@ -439,6 +443,32 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt
entity.setXRot(entity.getXRot() + f1 - f); entity.setXRot(entity.getXRot() + f1 - f);
} }
@Override
public void setWeaponType(int index, int type) {
if (index != 0) return;
entityData.set(WEAPON_TYPE, type);
}
@Override
public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return;
int type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) {
case 0, 1 -> ModSounds.CANNON_RELOAD.get();
default -> null;
};
if (sound == null) return;
setWeaponType(0, type);
this.level().playSound(null, this, sound, this.getSoundSource(), 1, 1);
}
@Override
public int getWeaponType(int index) {
return index == 0 ? entityData.get(WEAPON_TYPE) : -1;
}
@Override @Override
public void onPassengerTurned(Entity entity) { public void onPassengerTurned(Entity entity) {
this.clampRotation(entity); this.clampRotation(entity);

View file

@ -915,7 +915,7 @@ public class Yx100Entity extends ContainerMobileVehicleEntity implements GeoEnti
public void changeWeapon(int index, int value, boolean isScroll) { public void changeWeapon(int index, int value, boolean isScroll) {
if (index != 0) return; if (index != 0) return;
var type = isScroll ? (value + getWeaponType(0)) % 2 : value; var type = isScroll ? (value + getWeaponType(0) + 2) % 2 : value;
var sound = switch (type) { var sound = switch (type) {
case 0 -> ModSounds.INTO_MISSILE.get(); case 0 -> ModSounds.INTO_MISSILE.get();

View file

@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.entity.vehicle.base;
import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.menu.VehicleMenu; import com.atsuishio.superbwarfare.menu.VehicleMenu;
import com.atsuishio.superbwarfare.tools.InventoryTool;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -139,10 +140,7 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i
* @return 物品数量 * @return 物品数量
*/ */
public int countItem(@NotNull Item item) { public int countItem(@NotNull Item item) {
return this.getItemStacks().stream() return InventoryTool.countItem(this.getItemStacks(), item);
.filter(stack -> stack.is(item))
.mapToInt(ItemStack::getCount)
.sum();
} }
/** /**
@ -153,15 +151,7 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i
* @return 成功消耗的物品数量 * @return 成功消耗的物品数量
*/ */
public int consumeItem(Item item, int count) { public int consumeItem(Item item, int count) {
int initialCount = count; return InventoryTool.consumeItem(this.getItemStacks(), item, 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;
} }
/** /**
@ -171,27 +161,10 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i
* @param count 要插入的数量 * @param count 要插入的数量
*/ */
public void insertItem(Item item, int count) { public void insertItem(Item item, int count) {
var defaultStack = new ItemStack(item); var rest = InventoryTool.insertItem(this.getItemStacks(), item, count);
var maxStackSize = item.getMaxStackSize(defaultStack);
for (int i = 0; i < this.getItemStacks().size(); i++) { if (rest > 0) {
var stack = this.getItemStacks().get(i); var stackToDrop = new ItemStack(item, rest);
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)); this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), stackToDrop));
} }
} }

View file

@ -342,7 +342,7 @@ public class ModSounds {
public static final RegistryObject<SoundEvent> MK_42_FIRE_3P = REGISTRY.register("mk_42_fire_3p", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_fire_3p"))); public static final RegistryObject<SoundEvent> MK_42_FIRE_3P = REGISTRY.register("mk_42_fire_3p", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_fire_3p")));
public static final RegistryObject<SoundEvent> MK_42_FAR = REGISTRY.register("mk_42_far", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_far"))); public static final RegistryObject<SoundEvent> MK_42_FAR = REGISTRY.register("mk_42_far", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_far")));
public static final RegistryObject<SoundEvent> MK_42_VERYFAR = REGISTRY.register("mk_42_veryfar", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_veryfar"))); public static final RegistryObject<SoundEvent> MK_42_VERYFAR = REGISTRY.register("mk_42_veryfar", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_veryfar")));
public static final RegistryObject<SoundEvent> MK_42_RELOAD = REGISTRY.register("mk_42_reload", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("mk_42_reload"))); public static final RegistryObject<SoundEvent> CANNON_RELOAD = REGISTRY.register("cannon_reload", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("cannon_reload")));
public static final RegistryObject<SoundEvent> CANNON_ZOOM_IN = REGISTRY.register("cannon_zoom_in", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("cannon_zoom_in"))); public static final RegistryObject<SoundEvent> CANNON_ZOOM_IN = REGISTRY.register("cannon_zoom_in", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("cannon_zoom_in")));
public static final RegistryObject<SoundEvent> CANNON_ZOOM_OUT = REGISTRY.register("cannon_zoom_out", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("cannon_zoom_out"))); public static final RegistryObject<SoundEvent> CANNON_ZOOM_OUT = REGISTRY.register("cannon_zoom_out", () -> SoundEvent.createVariableRangeEvent(ModUtils.loc("cannon_zoom_out")));

View file

@ -0,0 +1,72 @@
package com.atsuishio.superbwarfare.tools;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.joml.Math;
public class InventoryTool {
/**
* 计算物品列表内指定物品的数量
*
* @param item 物品类型
* @return 物品数量
*/
public static int countItem(NonNullList<ItemStack> itemList, @NotNull Item item) {
return itemList.stream()
.filter(stack -> stack.is(item))
.mapToInt(ItemStack::getCount)
.sum();
}
/**
* 消耗物品列表内指定物品
*
* @param item 物品类型
* @param count 要消耗的数量
* @return 成功消耗的物品数量
*/
public static int consumeItem(NonNullList<ItemStack> itemList, Item item, int count) {
int initialCount = count;
var items = itemList.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 要插入的数量
* @return 未能成功插入的物品数量
*/
public static int insertItem(NonNullList<ItemStack> itemList, Item item, int count) {
var defaultStack = new ItemStack(item);
var maxStackSize = item.getMaxStackSize(defaultStack);
for (int i = 0; i < itemList.size(); i++) {
var stack = itemList.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);
itemList.set(i, new ItemStack(item, countToAdd));
count -= countToAdd;
}
if (count <= 0) break;
}
return count;
}
}

View file

@ -2135,18 +2135,18 @@
} }
] ]
}, },
"mk_42_reload": { "cannon_reload": {
"sounds": [ "sounds": [
{ {
"name": "superbwarfare:mk_42/mk_42_reload_01", "name": "superbwarfare:cannon/cannon_reload_01",
"stream": false "stream": false
}, },
{ {
"name": "superbwarfare:mk_42/mk_42_reload_02", "name": "superbwarfare:cannon/cannon_reload_02",
"stream": false "stream": false
}, },
{ {
"name": "superbwarfare:mk_42/mk_42_reload_03", "name": "superbwarfare:cannon/cannon_reload_03",
"stream": false "stream": false
} }
] ]
@ -2154,7 +2154,7 @@
"cannon_zoom_in": { "cannon_zoom_in": {
"sounds": [ "sounds": [
{ {
"name": "superbwarfare:mk_42/cannon_zoom_in", "name": "superbwarfare:cannon/cannon_zoom_in",
"stream": false "stream": false
} }
] ]
@ -2162,7 +2162,7 @@
"cannon_zoom_out": { "cannon_zoom_out": {
"sounds": [ "sounds": [
{ {
"name": "superbwarfare:mk_42/cannon_zoom_out", "name": "superbwarfare:cannon/cannon_zoom_out",
"stream": false "stream": false
} }
] ]