Skip to content

Commit

Permalink
CustomNBT: more 1.20.6 updates (#2627)
Browse files Browse the repository at this point in the history
* `CustomNBT` handling fixes

* `CompoundTag#getCompound`

* Revert redundant change
  • Loading branch information
tal5 committed Jun 19, 2024
1 parent b4bdbb8 commit a72cadc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CompoundTag getCustomData(ItemStack item) { // TODO: once 1.20 is the min
}

public ItemStack setCustomData(ItemStack item, CompoundTag data) { // TODO: once 1.20 is the minimum supported version, remove default impl
throw new UnsupportedOperationException();
return setNbtData(item, data);
}

public ItemStack setPartialOldNbt(ItemStack item, CompoundTag oldTag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public CompoundTagBuilder createBuilder() {
return new CompoundTagBuilder(new HashMap<>(value));
}

public CompoundTag getCompound(String key) {
return value.get(key) instanceof CompoundTag compoundTag ? compoundTag : null;
}

/**
* Get a byte array named with the given key.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public static CompoundTagBuilder create() {
return new CompoundTagBuilder();
}

public static CompoundTagBuilder create(CompoundTag tag) {
return tag == null ? new CompoundTagBuilder() : tag.createBuilder();
}

private static void checkNotNull(Object object) {
if (object == null) {
throw new NullPointerException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTagBuilder;
import com.denizenscript.denizen.nms.util.jnbt.JNBTListTag;
import com.denizenscript.denizen.nms.util.jnbt.StringTag;
import com.denizenscript.denizen.objects.properties.entity.EntityDisabledSlots.Action;
Expand Down Expand Up @@ -74,19 +75,11 @@ public static ItemStack addCustomNBT(ItemStack itemStack, String key, String val
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.itemHelper.getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
}
else {
denizenTag = NMSHandler.instance.createCompoundTag(new HashMap<>());
}
// Add custom NBT
denizenTag = denizenTag.createBuilder().putString(CoreUtilities.toLowerCase(key), value).build();
compoundTag = compoundTag.createBuilder().put(basekey, denizenTag).build();
// Write tag back
return NMSHandler.itemHelper.setNbtData(itemStack, compoundTag);
CompoundTag customData = NMSHandler.itemHelper.getCustomData(itemStack);
CompoundTagBuilder denizenDataBuilder = CompoundTagBuilder.create(customData != null ? customData.getCompound(basekey) : null);
CompoundTag denizenData = denizenDataBuilder.putString(CoreUtilities.toLowerCase(key), value).build();
customData = CompoundTagBuilder.create(customData).put(basekey, denizenData).build();
return NMSHandler.itemHelper.setCustomData(itemStack, customData);
}

public static ItemStack clearNBT(ItemStack itemStack, String key) {
Expand All @@ -103,78 +96,67 @@ public static ItemStack removeCustomNBT(ItemStack itemStack, String key, String
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.itemHelper.getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
CompoundTag customData = NMSHandler.itemHelper.getCustomData(itemStack);
if (customData == null) {
return itemStack;
}
else {
CompoundTag denizenData = customData.getCompound(basekey);
if (denizenData == null) {
return itemStack;
}
// Remove custom NBT
denizenTag = denizenTag.createBuilder().remove(CoreUtilities.toLowerCase(key)).build();
if (denizenTag.getValue().isEmpty()) {
compoundTag = compoundTag.createBuilder().remove(basekey).build();
denizenData = denizenData.createBuilder().remove(CoreUtilities.toLowerCase(key)).build();
if (denizenData.isEmpty()) {
customData = customData.createBuilder().remove(basekey).build();
}
else {
compoundTag = compoundTag.createBuilder().put(basekey, denizenTag).build();
customData = customData.createBuilder().put(basekey, denizenData).build();
}
// Write tag back
return NMSHandler.itemHelper.setNbtData(itemStack, compoundTag);
return NMSHandler.itemHelper.setCustomData(itemStack, customData.isEmpty() ? null : customData);
}

public static boolean hasCustomNBT(ItemStack itemStack, String key, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return false;
}
CompoundTag compoundTag = NMSHandler.itemHelper.getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
}
else {
CompoundTag customData = NMSHandler.itemHelper.getCustomData(itemStack);
if (customData == null) {
return false;
}
return denizenTag.getValue().containsKey(CoreUtilities.toLowerCase(key));
CompoundTag denizenData = customData.getCompound(basekey);
return denizenData != null && denizenData.containsKey(CoreUtilities.toLowerCase(key));
}

public static String getCustomNBT(ItemStack itemStack, String key, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR || key == null) {
return null;
}
CompoundTag compoundTag = NMSHandler.itemHelper.getNbtData(itemStack);
if (compoundTag.getValue().containsKey(basekey)) {
CompoundTag denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
String lowerKey = CoreUtilities.toLowerCase(key);
if (denizenTag.containsKey(lowerKey)) {
return denizenTag.getString(lowerKey);
}
CompoundTag customData = NMSHandler.itemHelper.getCustomData(itemStack);
if (customData == null) {
return null;
}
CompoundTag denizenData = customData.getCompound(basekey);
if (denizenData == null) {
return null;
}
return null;
String lowerKey = CoreUtilities.toLowerCase(key);
return denizenData.containsKey(lowerKey) ? denizenData.getString(lowerKey) : null;
}

public static List<String> listNBT(ItemStack itemStack, String basekey) {
List<String> nbt = new ArrayList<>();
if (itemStack == null || itemStack.getType() == Material.AIR) {
return nbt;
}
CompoundTag compoundTag = NMSHandler.itemHelper.getNbtData(itemStack);
if (compoundTag.getValue().containsKey(basekey)) {
CompoundTag denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
nbt.addAll(denizenTag.getValue().keySet());
CompoundTag customData = NMSHandler.itemHelper.getCustomData(itemStack);
if (customData == null) {
return nbt;
}
return nbt;
}

public static void addCustomNBT(Entity entity, String key, String value) {
if (entity == null) {
return;
CompoundTag denizenData = customData.getCompound(basekey);
if (denizenData == null) {
return nbt;
}
CompoundTag compoundTag = NMSHandler.entityHelper.getNbtData(entity);
// Add custom NBT
compoundTag = compoundTag.createBuilder().putString(key, value).build();
// Write tag back
NMSHandler.entityHelper.setNbtData(entity, compoundTag);
nbt.addAll(denizenData.getValue().keySet());
return nbt;
}

public static void addCustomNBT(Entity entity, String key, int value) {
Expand All @@ -199,24 +181,6 @@ public static void removeCustomNBT(Entity entity, String key) {
NMSHandler.entityHelper.setNbtData(entity, compoundTag);
}

public static boolean hasCustomNBT(Entity entity, String key) {
if (entity == null) {
return false;
}
CompoundTag compoundTag = NMSHandler.entityHelper.getNbtData(entity);
// Check for key
return compoundTag.getValue().containsKey(key);
}

public static String getCustomNBT(Entity entity, String key) {
if (entity == null) {
return null;
}
CompoundTag compoundTag = NMSHandler.entityHelper.getNbtData(entity);
// Return contents of the tag
return compoundTag.getString(key);
}

public static int getCustomIntNBT(Entity entity, String key) {
if (entity == null) {
return 0;
Expand Down

0 comments on commit a72cadc

Please sign in to comment.