修复一个可能导致NPE的问题

This commit is contained in:
17146 2024-12-20 21:35:31 +08:00
parent 5abcc7175f
commit 373427792e

View file

@ -1,10 +1,10 @@
package com.atsuishio.superbwarfare.tools; 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.ModUtils;
import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.network.ModVariables;
import com.atsuishio.superbwarfare.network.message.GunsDataMessage; import com.atsuishio.superbwarfare.network.message.GunsDataMessage;
import com.google.gson.stream.JsonReader;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
@ -196,14 +196,27 @@ public class GunsTool {
} }
public static void setGunIntTag(ItemStack stack, String name, int num) { public static void setGunIntTag(ItemStack stack, String name, int num) {
CompoundTag tag = stack.getOrCreateTag().getCompound("GunData"); if (stack == null) return;
tag.putInt(name, num);
var tag = stack.getTag();
if (tag != null && tag.contains("GunData")) {
tag.getCompound("GunData").putInt(name, num);
stack.addTagElement("GunData", tag); 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) { public static int getGunIntTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag().getCompound("GunData"); if (stack == null) return 0;
return tag.getInt(name);
var tag = stack.getTag();
if (tag != null && tag.contains("GunData")) {
return tag.getCompound("GunData").getInt(name);
}
return 0;
} }
} }