diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java index ef87c62b5..13f7745a4 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/GunsTool.java @@ -1,10 +1,10 @@ package com.atsuishio.superbwarfare.tools; -import com.atsuishio.superbwarfare.network.ModVariables; -import com.google.gson.stream.JsonReader; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.network.ModVariables; import com.atsuishio.superbwarfare.network.message.GunsDataMessage; +import com.google.gson.stream.JsonReader; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.packs.resources.ResourceManager; @@ -196,14 +196,27 @@ public class GunsTool { } public static void setGunIntTag(ItemStack stack, String name, int num) { - CompoundTag tag = stack.getOrCreateTag().getCompound("GunData"); - tag.putInt(name, num); - stack.addTagElement("GunData", tag); + if (stack == null) return; + + var tag = stack.getTag(); + if (tag != null && tag.contains("GunData")) { + tag.getCompound("GunData").putInt(name, num); + stack.addTagElement("GunData", tag); + } else { + CompoundTag newTag = new CompoundTag(); + newTag.putInt(name, num); + stack.addTagElement("GunData", newTag); + } } public static int getGunIntTag(ItemStack stack, String name) { - CompoundTag tag = stack.getOrCreateTag().getCompound("GunData"); - return tag.getInt(name); + if (stack == null) return 0; + + var tag = stack.getTag(); + if (tag != null && tag.contains("GunData")) { + return tag.getCompound("GunData").getInt(name); + } + return 0; } }