修改弹药盒的逻辑

This commit is contained in:
17146 2025-03-03 00:48:08 +08:00
parent c0528f10ba
commit 6513d65f22
2 changed files with 17 additions and 16 deletions

View file

@ -30,8 +30,10 @@ public class AmmoBox extends Item {
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
CompoundTag tag = stack.getOrCreateTag();
if (hand == InteractionHand.OFF_HAND) return InteractionResultHolder.fail(stack);
CompoundTag tag = stack.getOrCreateTag();
player.getCooldowns().addCooldown(this, 10);
int type = stack.getOrCreateTag().getInt("Type");
@ -162,5 +164,4 @@ public class AmmoBox extends Item {
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(FormatTool.format0D(ItemNBTTool.getInt(stack, "HeavyAmmo", 0)) + ((type == 0 || type == 5) ? " ←-" : " ")).withStyle(ChatFormatting.BOLD)));
}
}

View file

@ -31,26 +31,26 @@ public class AmmoSupplierItem extends Item {
player.getCooldowns().addCooldown(this, 10);
stack.shrink(count);
ItemStack ammobox = player.getOffhandItem();
ItemStack offhandItem = player.getOffhandItem();
if (ammobox.is(ModItems.AMMO_BOX.get())) {
var newAmmoCount = switch (this.type) {
case HANDGUN -> ammobox.getOrCreateTag().getInt("HandgunAmmo");
case RIFLE -> ammobox.getOrCreateTag().getInt("RifleAmmo");
case SHOTGUN -> ammobox.getOrCreateTag().getInt("ShotgunAmmo");
case SNIPER -> ammobox.getOrCreateTag().getInt("SniperAmmo");
case HEAVY -> ammobox.getOrCreateTag().getInt("HeavyAmmo");
if (offhandItem.is(ModItems.AMMO_BOX.get())) {
int newAmmoCount = switch (this.type) {
case HANDGUN -> offhandItem.getOrCreateTag().getInt("HandgunAmmo");
case RIFLE -> offhandItem.getOrCreateTag().getInt("RifleAmmo");
case SHOTGUN -> offhandItem.getOrCreateTag().getInt("ShotgunAmmo");
case SNIPER -> offhandItem.getOrCreateTag().getInt("SniperAmmo");
case HEAVY -> offhandItem.getOrCreateTag().getInt("HeavyAmmo");
} + ammoToAdd * count;
switch (this.type) {
case HANDGUN -> ammobox.getOrCreateTag().putInt("HandgunAmmo", newAmmoCount);
case RIFLE -> ammobox.getOrCreateTag().putInt("RifleAmmo", newAmmoCount);
case SHOTGUN -> ammobox.getOrCreateTag().putInt("ShotgunAmmo", newAmmoCount);
case SNIPER -> ammobox.getOrCreateTag().putInt("SniperAmmo", newAmmoCount);
case HEAVY -> ammobox.getOrCreateTag().putInt("HeavyAmmo", newAmmoCount);
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 {
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
var newAmmoCount = switch (this.type) {
int newAmmoCount = switch (this.type) {
case HANDGUN -> capability.handgunAmmo;
case RIFLE -> capability.rifleAmmo;
case SHOTGUN -> capability.shotgunAmmo;