Skip to content

Commit

Permalink
nbt_to_map, map_to_nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 12, 2022
1 parent 6657465 commit f579d02
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 10 deletions.
@@ -1,31 +1,27 @@
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.*;
import com.denizenscript.denizen.objects.properties.material.*;
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);
Expand Down
@@ -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 <BinaryTag.nbt_to_map>
// @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/<player.uuid>.dat save:x
// - define data <entry[x].data.gzip_decompress.nbt_to_map>
// # Now do something with "<[data]>"
// -->
PropertyParser.<BukkitBinaryTagProperties, ObjectTag>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
}
}
@@ -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 <MapTag.map_to_nbt>
// @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/<player.uuid>.dat data:<[data]>
// -->
PropertyParser.<BukkitMapTagProperties, BinaryTag>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
}
}

0 comments on commit f579d02

Please sign in to comment.