From 373427792eccf67557054a06c713fb084814c8ff Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Fri, 20 Dec 2024 21:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=AF=BC=E8=87=B4NPE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/tools/GunsTool.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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; } }