diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java index 253fe2d48d..b824bb7cdd 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/PropertyRegistry.java @@ -1,10 +1,7 @@ package com.denizenscript.denizen.objects.properties; import com.denizenscript.denizen.objects.*; -import com.denizenscript.denizen.objects.properties.bukkit.BukkitElementProperties; -import com.denizenscript.denizen.objects.properties.bukkit.BukkitListProperties; -import com.denizenscript.denizen.objects.properties.bukkit.BukkitQueueProperties; -import com.denizenscript.denizen.objects.properties.bukkit.BukkitScriptProperties; +import com.denizenscript.denizen.objects.properties.bukkit.*; import com.denizenscript.denizen.objects.properties.entity.*; import com.denizenscript.denizen.objects.properties.inventory.*; import com.denizenscript.denizen.objects.properties.item.*; @@ -12,20 +9,19 @@ import com.denizenscript.denizen.objects.properties.trade.*; import com.denizenscript.denizen.nms.NMSHandler; import com.denizenscript.denizen.nms.NMSVersion; -import com.denizenscript.denizencore.objects.core.ElementTag; -import com.denizenscript.denizencore.objects.core.ListTag; -import com.denizenscript.denizencore.objects.core.QueueTag; -import com.denizenscript.denizencore.objects.core.ScriptTag; +import com.denizenscript.denizencore.objects.core.*; import com.denizenscript.denizencore.objects.properties.PropertyParser; public class PropertyRegistry { public static void registerMainProperties() { // register properties that add Bukkit code to core objects - PropertyParser.registerProperty(BukkitScriptProperties.class, ScriptTag.class); - PropertyParser.registerProperty(BukkitQueueProperties.class, QueueTag.class); + PropertyParser.registerProperty(BukkitBinaryTagProperties.class, BinaryTag.class); PropertyParser.registerProperty(BukkitElementProperties.class, ElementTag.class); PropertyParser.registerProperty(BukkitListProperties.class, ListTag.class); + PropertyParser.registerProperty(BukkitMapTagProperties.class, MapTag.class); + PropertyParser.registerProperty(BukkitQueueProperties.class, QueueTag.class); + PropertyParser.registerProperty(BukkitScriptProperties.class, ScriptTag.class); // register core EntityTag properties PropertyParser.registerProperty(EntityAge.class, EntityTag.class); diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitBinaryTagProperties.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitBinaryTagProperties.java new file mode 100644 index 0000000000..29d3e741b5 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitBinaryTagProperties.java @@ -0,0 +1,85 @@ +package com.denizenscript.denizen.objects.properties.bukkit; + +import com.denizenscript.denizen.nms.util.jnbt.NBTInputStream; +import com.denizenscript.denizen.nms.util.jnbt.NamedTag; +import com.denizenscript.denizen.objects.properties.item.ItemRawNBT; +import com.denizenscript.denizen.utilities.debugging.Debug; +import com.denizenscript.denizencore.objects.Mechanism; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.BinaryTag; +import com.denizenscript.denizencore.objects.properties.Property; +import com.denizenscript.denizencore.objects.properties.PropertyParser; + +import java.io.ByteArrayInputStream; + +public class BukkitBinaryTagProperties implements Property { + + public static boolean describes(ObjectTag data) { + return data instanceof BinaryTag; + } + + public static BukkitBinaryTagProperties getFrom(ObjectTag data) { + if (!describes(data)) { + return null; + } + else { + return new BukkitBinaryTagProperties((BinaryTag) data); + } + } + + private BukkitBinaryTagProperties(BinaryTag data) { + this.data = data; + } + + public static final String[] handledMechs = new String[] { + }; // None + + public BinaryTag data; + + public static void registerTags() { + + // <--[tag] + // @attribute + // @returns MapTag + // @group conversion + // @description + // Converts raw NBT binary data to a MapTag. + // This under some circumstances might not return a map, depending on the underlying data. + // Refer to <@link language Raw NBT Encoding> + // @example + // # Reads a player ".dat" file's NBT data + // - ~fileread path:data/.dat save:x + // - define data + // # Now do something with "<[data]>" + // --> + PropertyParser.registerStaticTag(ObjectTag.class, "nbt_to_map", (attribute, object) -> { + try { + ByteArrayInputStream stream = new ByteArrayInputStream(object.data.data); + NBTInputStream nbtStream = new NBTInputStream(stream); + NamedTag tag = nbtStream.readNamedTag(); + nbtStream.close(); + stream.close(); + return ItemRawNBT.jnbtTagToObject(tag.getTag()); + } + catch (Throwable ex) { + Debug.echoError(ex); + return null; + } + }); + } + + @Override + public String getPropertyString() { + return null; + } + + @Override + public String getPropertyId() { + return "BukkitBinaryTagProperties"; + } + + @Override + public void adjust(Mechanism mechanism) { + // None + } +} diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitMapTagProperties.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitMapTagProperties.java new file mode 100644 index 0000000000..0e0eb2fcd7 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitMapTagProperties.java @@ -0,0 +1,88 @@ +package com.denizenscript.denizen.objects.properties.bukkit; + +import com.denizenscript.denizen.nms.util.jnbt.NBTOutputStream; +import com.denizenscript.denizen.nms.util.jnbt.Tag; +import com.denizenscript.denizen.objects.properties.item.ItemRawNBT; +import com.denizenscript.denizen.utilities.debugging.Debug; +import com.denizenscript.denizencore.objects.Mechanism; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.BinaryTag; +import com.denizenscript.denizencore.objects.core.MapTag; +import com.denizenscript.denizencore.objects.properties.Property; +import com.denizenscript.denizencore.objects.properties.PropertyParser; + +import java.io.ByteArrayOutputStream; + +public class BukkitMapTagProperties implements Property { + + public static boolean describes(ObjectTag map) { + return map instanceof MapTag; + } + + public static BukkitMapTagProperties getFrom(ObjectTag map) { + if (!describes(map)) { + return null; + } + else { + return new BukkitMapTagProperties((MapTag) map); + } + } + + private BukkitMapTagProperties(MapTag map) { + this.map = map; + } + + public static final String[] handledMechs = new String[] { + }; // None + + public MapTag map; + + public static void registerTags() { + + // <--[tag] + // @attribute + // @returns BinaryTag + // @group conversion + // @description + // Converts the NBT-formatted MapTag to raw binary NBT. + // Refer to <@link language Raw NBT Encoding> + // @example + // # Stores a player ".dat" file's NBT data + // # NOTE: replace 'something' with your map data + // - define playerdata something + // - define data <[something].map_to_nbt.gzip_compress> + // - ~filewrite path:data/.dat data:<[data]> + // --> + PropertyParser.registerStaticTag(BinaryTag.class, "map_to_nbt", (attribute, object) -> { + try { + Tag tag = ItemRawNBT.convertObjectToNbt(object.map.toString(), attribute.context, "(root)."); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + NBTOutputStream nbtStream = new NBTOutputStream(output); + nbtStream.writeNamedTag("", tag); + nbtStream.close(); + byte[] data = output.toByteArray(); + output.close(); + return new BinaryTag(data); + } + catch (Throwable ex) { + Debug.echoError(ex); + return null; + } + }); + } + + @Override + public String getPropertyString() { + return null; + } + + @Override + public String getPropertyId() { + return "BukkitMapTagProperties"; + } + + @Override + public void adjust(Mechanism mechanism) { + // None + } +}