Skip to content

Commit

Permalink
fix nbt issue in get and set methods for nms
Browse files Browse the repository at this point in the history
  • Loading branch information
hakan-krgn committed Nov 2, 2023
1 parent 5cc2cfa commit 04046ca
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public ItemStack set(@Nonnull ItemStack itemStack, @Nonnull String key, @Nonnull
if (itemStack.getType().equals(Material.AIR))
return itemStack;

net.minecraft.world.item.ItemStack nmsCopy = CraftItemStack.asNMSCopy(itemStack);
nmsCopy.w().a(key, value);
return CraftItemStack.asBukkitCopy(nmsCopy);
try {
net.minecraft.world.item.ItemStack nmsCopy = CraftItemStack.asNMSCopy(itemStack);
nmsCopy.w().a(key, value);
return CraftItemStack.asBukkitCopy(nmsCopy);
} catch (Exception e) {
return itemStack;
}
}

/**
Expand All @@ -45,9 +49,13 @@ public ItemStack set(@Nonnull ItemStack itemStack, @Nonnull String nbt) {
if (itemStack.getType().equals(Material.AIR))
return itemStack;

net.minecraft.world.item.ItemStack nmsCopy = CraftItemStack.asNMSCopy(itemStack);
nmsCopy.w().a(this.parse(nbt));
return CraftItemStack.asBukkitCopy(nmsCopy);
try {
net.minecraft.world.item.ItemStack nmsCopy = CraftItemStack.asNMSCopy(itemStack);
nmsCopy.w().a(this.parse(nbt));
return CraftItemStack.asBukkitCopy(nmsCopy);
} catch (Exception e) {
return itemStack;
}
}

/**
Expand All @@ -63,8 +71,12 @@ public String get(@Nonnull ItemStack itemStack, @Nonnull String key) {
if (nmsCopy == null)
return "{}";

NBTTagCompound nbtTagCompound = nmsCopy.w();
return nbtTagCompound.e(key) ? nbtTagCompound.l(key) : "{}";
try {
NBTTagCompound nbtTagCompound = nmsCopy.w();
return nbtTagCompound.e(key) ? nbtTagCompound.l(key) : "{}";
} catch (Exception e) {
return "{}";
}
}

/**
Expand All @@ -79,7 +91,11 @@ public String get(@Nonnull ItemStack itemStack) {
if (nmsCopy == null)
return "{}";

return nmsCopy.w().toString();
try {
return nmsCopy.w().toString();
} catch (Exception e) {
return "{}";
}
}

private NBTTagCompound parse(String nbt) {
Expand Down

0 comments on commit 04046ca

Please sign in to comment.