Search in sources :

Example 1 with IntNode

use of org.cubeengine.converter.node.IntNode in project modules-extra by CubeEngine.

the class ItemStackConverter method toNode.

@Override
public Node toNode(ItemStack itemStack) throws ConversionException {
    if (itemStack == null || itemStack.getItem() == Material.AIR) {
        return NullNode.emptyNode();
    }
    MapNode item = MapNode.emptyMap();
    item.set("Count", new IntNode(itemStack.getAmount()));
    item.set("Damage", new IntNode(itemStack.getDurability()));
    item.set("Item", StringNode.of(itemStack.getType().name()));
    net.minecraft.server.v1_8_R2.ItemStack nmsCopy = CraftItemStack.asNMSCopy(itemStack);
    if (nmsCopy == null) {
        CubeEngine.getLog().error("NMSCopy is unexpectedly null! " + itemStack);
        return null;
    }
    NBTTagCompound tag = nmsCopy.getTag();
    item.set("tag", tag == null ? MapNode.emptyMap() : NBTUtils.convertNBTToNode(tag));
    return item;
}
Also used : IntNode(org.cubeengine.converter.node.IntNode) MapNode(org.cubeengine.converter.node.MapNode)

Example 2 with IntNode

use of org.cubeengine.converter.node.IntNode in project core by CubeEngine.

the class Vector3iConverter method toNode.

@Override
public Node toNode(Vector3i blockVector3, ConverterManager converterManager) throws ConversionException {
    MapNode mapNode = MapNode.emptyMap();
    mapNode.set("x", new IntNode(blockVector3.getX()));
    mapNode.set("y", new IntNode(blockVector3.getY()));
    mapNode.set("z", new IntNode(blockVector3.getZ()));
    return mapNode;
}
Also used : IntNode(org.cubeengine.converter.node.IntNode) MapNode(org.cubeengine.converter.node.MapNode)

Example 3 with IntNode

use of org.cubeengine.converter.node.IntNode in project modules-extra by CubeEngine.

the class ItemStackConverter method fromNode.

@Override
public ItemStack fromNode(Node node) throws ConversionException {
    if (node instanceof MapNode) {
        Node count = ((MapNode) node).get("Count");
        Node damage = ((MapNode) node).get("Damage");
        Node item = ((MapNode) node).get("Item");
        Node tag = ((MapNode) node).get("tag");
        if (count instanceof IntNode && damage instanceof IntNode && item instanceof StringNode && (tag instanceof MapNode)) {
            try {
                ItemStack itemStack = new ItemStack(Material.valueOf(item.asText()));
                itemStack.setDurability(((IntNode) damage).getValue().shortValue());
                itemStack.setAmount(((IntNode) count).getValue());
                net.minecraft.server.v1_8_R2.ItemStack nms = CraftItemStack.asNMSCopy(itemStack);
                nms.setTag(((MapNode) tag).isEmpty() ? null : (NBTTagCompound) NBTUtils.convertNodeToNBT(tag));
                return CraftItemStack.asBukkitCopy(nms);
            } catch (IllegalArgumentException e) {
                throw ConversionException.of(this, item, "Unknown Material!");
            }
        } else {
            throw ConversionException.of(this, node, "Invalid SubNodes!");
        }
    } else if (node instanceof NullNode) {
        return null;
    } else {
        throw ConversionException.of(this, node, "Node is not a MapNode!");
    }
}
Also used : IntNode(org.cubeengine.converter.node.IntNode) MapNode(org.cubeengine.converter.node.MapNode) NullNode(org.cubeengine.converter.node.NullNode) StringNode(org.cubeengine.converter.node.StringNode) IntNode(org.cubeengine.converter.node.IntNode) Node(org.cubeengine.converter.node.Node) StringNode(org.cubeengine.converter.node.StringNode) MapNode(org.cubeengine.converter.node.MapNode) ItemStack(org.spongepowered.api.item.inventory.ItemStack) NullNode(org.cubeengine.converter.node.NullNode)

Aggregations

IntNode (org.cubeengine.converter.node.IntNode)3 MapNode (org.cubeengine.converter.node.MapNode)3 Node (org.cubeengine.converter.node.Node)1 NullNode (org.cubeengine.converter.node.NullNode)1 StringNode (org.cubeengine.converter.node.StringNode)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1