Skip to content

Commit

Permalink
support 1.16 nbt_attribute format
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 5, 2020
1 parent e15eb32 commit 00be9ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.util.jnbt.IntArrayTag;
import com.denizenscript.denizen.nms.util.jnbt.Tag;
import org.bukkit.Color;
import org.bukkit.Material;
Expand All @@ -14,6 +15,7 @@
import org.bukkit.potion.PotionEffectType;

import java.util.List;
import java.util.UUID;

public abstract class ItemHelper {

Expand Down Expand Up @@ -62,4 +64,12 @@ public void registerStonecuttingRecipe(String keyName, String group, ItemStack r
public void setInventoryItem(Inventory inventory, ItemStack item, int slot) {
inventory.setItem(slot, item);
}

public IntArrayTag convertUuidToNbt(UUID id) {
throw new UnsupportedOperationException();
}

public UUID convertNbtToUuid(IntArrayTag id) {
throw new UnsupportedOperationException();
}
}
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.utilities.nbt;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.util.jnbt.*;
import com.denizenscript.denizen.objects.properties.entity.EntityDisabledSlots.Action;
import com.denizenscript.denizencore.utilities.CoreUtilities;
Expand Down Expand Up @@ -68,7 +69,7 @@ public static List<AttributeReturn> getAttributes(ItemStack itemStack) {
for (int i = 0; i < attribs.size(); i++) {
CompoundTag ct = attribs.get(i);
AttributeReturn atr = new AttributeReturn();
atr.attr = (String) ct.getValue().get("AttributeName").getValue();
atr.attr = (String) ct.getValue().get(NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16) ? "Name" : "AttributeName").getValue();
atr.slot = ct.getValue().get("Slot") == null ? "mainhand" : (String) ct.getValue().get("Slot").getValue();
atr.op = (Integer) ct.getValue().get("Operation").getValue();
Tag t = ct.getValue().get("Amount");
Expand All @@ -85,19 +86,26 @@ else if (t instanceof DoubleTag) {
/// ????
atr.amt = 0;
}
t = ct.getValue().get("UUIDMost");
if (t instanceof LongTag) {
atr.uuidMost = (Long) t.getValue();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16) && ct.getValue().containsKey("UUID")) {
UUID id = NMSHandler.getItemHelper().convertNbtToUuid((IntArrayTag) ct.getValue().get("UUID"));
atr.uuidLeast = id.getLeastSignificantBits();
atr.uuidMost = id.getMostSignificantBits();
}
else if (t instanceof IntTag) {
atr.uuidMost = (Integer) t.getValue();
}
t = ct.getValue().get("UUIDLeast");
if (t instanceof LongTag) {
atr.uuidLeast = (Long) t.getValue();
}
else if (t instanceof IntTag) {
atr.uuidLeast = (Integer) t.getValue();
else if (ct.getValue().containsKey("UUIDMost")) {
t = ct.getValue().get("UUIDMost");
if (t instanceof LongTag) {
atr.uuidMost = (Long) t.getValue();
}
else if (t instanceof IntTag) {
atr.uuidMost = (Integer) t.getValue();
}
t = ct.getValue().get("UUIDLeast");
if (t instanceof LongTag) {
atr.uuidLeast = (Long) t.getValue();
}
else if (t instanceof IntTag) {
atr.uuidLeast = (Integer) t.getValue();
}
}
attrs.add(atr);
}
Expand Down Expand Up @@ -147,9 +155,15 @@ public static ItemStack addAttribute(ItemStack itemStack, String attr, String sl
tmap.put("Amount", new DoubleTag(amt));

long uuidhelp = uuidChoice(itemStack);
UUID fullUuid = new UUID(uuidhelp + 88512 + attribs.size(), uuidhelp * 2 + 1250025L + attribs.size());

tmap.put("UUIDMost", new LongTag(uuidhelp + 88512 + attribs.size()));
tmap.put("UUIDLeast", new LongTag(uuidhelp * 2 + 1250025L + attribs.size()));
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)) {
tmap.put("UUID", NMSHandler.getItemHelper().convertUuidToNbt(fullUuid));
}
else {
tmap.put("UUIDMost", new LongTag(fullUuid.getMostSignificantBits()));
tmap.put("UUIDLeast", new LongTag(fullUuid.getLeastSignificantBits()));
}

CompoundTag ct = NMSHandler.getInstance().createCompoundTag(tmap);
attribs.add(ct);
Expand Down
Expand Up @@ -256,4 +256,14 @@ public void setInventoryItem(Inventory inventory, ItemStack item, int slot) {
inventory.setItem(slot, item);
}
}

@Override
public IntArrayTag convertUuidToNbt(UUID id) {
return new IntArrayTag(GameProfileSerializer.a(id).getInts());
}

@Override
public UUID convertNbtToUuid(IntArrayTag id) {
return GameProfileSerializer.a(new NBTTagIntArray(id.getValue()));
}
}

0 comments on commit 00be9ce

Please sign in to comment.