优化弹药盒的使用

This commit is contained in:
Atsuihsio 2024-07-04 20:33:34 +08:00
parent 34aef27803
commit bc4d7539c9

View file

@ -26,8 +26,9 @@ public abstract class AmmoSupplierItem extends Item {
@Override @Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand); ItemStack stack = player.getItemInHand(hand);
int count = stack.getCount();
player.getCooldowns().addCooldown(this, 10); player.getCooldowns().addCooldown(this, 10);
stack.shrink(1); stack.shrink(count);
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
var newAmmoCount = switch (this.type) { var newAmmoCount = switch (this.type) {
@ -35,7 +36,7 @@ public abstract class AmmoSupplierItem extends Item {
case RIFLE -> capability.rifleAmmo; case RIFLE -> capability.rifleAmmo;
case SHOTGUN -> capability.shotgunAmmo; case SHOTGUN -> capability.shotgunAmmo;
case SNIPER -> capability.sniperAmmo; case SNIPER -> capability.sniperAmmo;
} + ammoToAdd; } + ammoToAdd * count;
switch (this.type) { switch (this.type) {
case HANDGUN -> capability.handgunAmmo = newAmmoCount; case HANDGUN -> capability.handgunAmmo = newAmmoCount;
case RIFLE -> capability.rifleAmmo = newAmmoCount; case RIFLE -> capability.rifleAmmo = newAmmoCount;
@ -46,7 +47,7 @@ public abstract class AmmoSupplierItem extends Item {
}); });
if (!level.isClientSide()) { if (!level.isClientSide()) {
player.displayClientMessage(Component.translatable("item.target.ammo_supplier.supply", Component.translatable(this.type.translatableKey).getString(), ammoToAdd), false); player.displayClientMessage(Component.translatable("item.target.ammo_supplier.supply", Component.translatable(this.type.translatableKey).getString(), ammoToAdd * count), false);
level.playSound(null, player.blockPosition(), TargetModSounds.BULLET_SUPPLY.get(), SoundSource.PLAYERS, 1, 1); level.playSound(null, player.blockPosition(), TargetModSounds.BULLET_SUPPLY.get(), SoundSource.PLAYERS, 1, 1);
} }
return InteractionResultHolder.consume(stack); return InteractionResultHolder.consume(stack);