Search in sources :

Example 1 with Rotation

use of org.bukkit.Rotation in project MagicPlugin by elBukkit.

the class Schematic method load.

@SuppressWarnings("deprecation")
public void load(short width, short height, short length, short[] blockTypes, byte[] data, Collection<Object> tileEntityData, Collection<Object> entityData, Vector origin, Vector offset) {
    size = new Vector(width, height, length);
    center = new Vector(Math.floor(size.getBlockX() / 2), 0, Math.floor(size.getBlockZ() / 2));
    blocks = new MaterialAndData[width][height][length];
    entities = new ArrayList<>();
    // Load entities
    for (Object entity : entityData) {
        String type = NMSUtils.getMetaString(entity, "id");
        Vector position = NMSUtils.getPosition(entity, "Pos");
        if (position == null)
            continue;
        position = position.subtract(origin).subtract(center);
        if (type == null)
            continue;
        // Only doing paintings and item frames for now.
        if (type.equals("Painting")) {
            String motive = NMSUtils.getMetaString(entity, "Motive");
            motive = motive.toLowerCase();
            Art art = Art.ALBAN;
            for (Art test : Art.values()) {
                if (test.name().toLowerCase().replace("_", "").equals(motive)) {
                    art = test;
                    break;
                }
            }
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            EntityData painting = com.elmakers.mine.bukkit.entity.EntityData.loadPainting(position, art, getFacing(facing));
            entities.add(painting);
        } else if (type.equals("ItemFrame")) {
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            byte rotation = NMSUtils.getMetaByte(entity, "ItemRotation");
            Rotation rot = Rotation.NONE;
            if (rotation < Rotation.values().length) {
                rot = Rotation.values()[rotation];
            }
            ItemStack item = NMSUtils.getItem(NMSUtils.getNode(entity, "Item"));
            EntityData itemFrame = com.elmakers.mine.bukkit.entity.EntityData.loadItemFrame(position, item, getFacing(facing), rot);
            entities.add(itemFrame);
        }
    }
    // Map tile entity data
    Map<BlockVector, Object> tileEntityMap = new HashMap<>();
    for (Object tileEntity : tileEntityData) {
        try {
            Integer x = NMSUtils.getMetaInt(tileEntity, "x");
            Integer y = NMSUtils.getMetaInt(tileEntity, "y");
            Integer z = NMSUtils.getMetaInt(tileEntity, "z");
            if (x == null || y == null || z == null)
                continue;
            BlockVector location = new BlockVector(x, y, z);
            tileEntityMap.put(location, tileEntity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    for (int y = 0; y < height; y++) {
        for (int z = 0; z < length; z++) {
            for (int x = 0; x < width; x++) {
                int index = x + (y * length + z) * width;
                Material material = null;
                try {
                    material = Material.getMaterial(blockTypes[index]);
                } catch (Exception ex) {
                    material = null;
                    ex.printStackTrace();
                }
                if (material != null) {
                    MaterialAndData block = new com.elmakers.mine.bukkit.block.MaterialAndData(material, data[index]);
                    // Check for tile entity data
                    BlockVector blockLocation = new BlockVector(x, y, z);
                    Object tileEntity = tileEntityMap.get(blockLocation);
                    if (tileEntity != null) {
                        try {
                            if (material == Material.COMMAND) {
                                String customName = NMSUtils.getMetaString(tileEntity, "CustomName");
                                if (!customName.isEmpty()) {
                                    block.setCustomName(customName);
                                }
                                block.setCommandLine(NMSUtils.getMetaString(tileEntity, "Command"));
                            } else {
                                block.setRawData(tileEntity);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    blocks[x][y][z] = block;
                }
            }
        }
    }
    loaded = true;
}
Also used : Art(org.bukkit.Art) HashMap(java.util.HashMap) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) Material(org.bukkit.Material) Rotation(org.bukkit.Rotation) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) BlockVector(org.bukkit.util.BlockVector) ItemStack(org.bukkit.inventory.ItemStack) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Aggregations

MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)1 EntityData (com.elmakers.mine.bukkit.api.entity.EntityData)1 HashMap (java.util.HashMap)1 Art (org.bukkit.Art)1 Material (org.bukkit.Material)1 Rotation (org.bukkit.Rotation)1 ItemStack (org.bukkit.inventory.ItemStack)1 BlockVector (org.bukkit.util.BlockVector)1 Vector (org.bukkit.util.Vector)1