Search in sources :

Example 1 with CuboidBlockSet

use of net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet in project Denizen-For-Bukkit by DenizenScript.

the class SchematicCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    Element angle = scriptEntry.getElement("angle");
    Element type = scriptEntry.getElement("type");
    Element name = scriptEntry.getElement("name");
    Element filename = scriptEntry.getElement("filename");
    Element noair = scriptEntry.getElement("noair");
    Element delayed = scriptEntry.getElement("delayed");
    dLocation location = scriptEntry.getdObject("location");
    dCuboid cuboid = scriptEntry.getdObject("cuboid");
    dB.report(scriptEntry, getName(), type.debug() + name.debug() + (location != null ? location.debug() : "") + (filename != null ? filename.debug() : "") + (cuboid != null ? cuboid.debug() : "") + (angle != null ? angle.debug() : "") + (noair != null ? noair.debug() : "") + (delayed != null ? delayed.debug() : ""));
    CuboidBlockSet set;
    Type ttype = Type.valueOf(type.asString());
    if (scriptEntry.shouldWaitFor() && ttype != Type.PASTE) {
        dB.echoError("Tried to wait for a non-paste schematic command.");
        scriptEntry.setFinished(true);
    }
    String fname = filename != null ? filename.asString() : name.asString();
    switch(ttype) {
        case CREATE:
            if (schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
                return;
            }
            if (cuboid == null) {
                dB.echoError(scriptEntry.getResidingQueue(), "Missing cuboid argument!");
                return;
            }
            if (location == null) {
                dB.echoError(scriptEntry.getResidingQueue(), "Missing origin location argument!");
                return;
            }
            try {
                // TODO: Make me waitable!
                set = new CuboidBlockSet(cuboid, location);
                schematics.put(name.asString().toUpperCase(), set);
            } catch (Exception ex) {
                dB.echoError(scriptEntry.getResidingQueue(), "Error creating schematic object " + name.asString() + ".");
                dB.echoError(scriptEntry.getResidingQueue(), ex);
                return;
            }
            break;
        case LOAD:
            if (schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
                return;
            }
            try {
                String directory = URLDecoder.decode(System.getProperty("user.dir"));
                File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
                if (!f.exists()) {
                    dB.echoError("Schematic file " + fname + " does not exist. Are you sure it's in " + directory + "/plugins/Denizen/schematics/?");
                    return;
                }
                InputStream fs = new FileInputStream(f);
                // TODO: Make me waitable!
                set = CuboidBlockSet.fromMCEditStream(fs);
                fs.close();
                schematics.put(name.asString().toUpperCase(), set);
            } catch (Exception ex) {
                dB.echoError(scriptEntry.getResidingQueue(), "Error loading schematic file " + name.asString() + ".");
                dB.echoError(scriptEntry.getResidingQueue(), ex);
                return;
            }
            break;
        case UNLOAD:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            schematics.remove(name.asString().toUpperCase());
            break;
        case ROTATE:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            if (angle == null) {
                dB.echoError(scriptEntry.getResidingQueue(), "Missing angle argument!");
                return;
            }
            // TODO: Make me waitable!
            int ang = angle.asInt();
            if (ang < 0) {
                ang = 360 + ang;
            }
            while (ang > 0) {
                ang -= 90;
                schematics.get(name.asString().toUpperCase()).rotateOne();
            }
            break;
        case FLIP_X:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            schematics.get(name.asString().toUpperCase()).flipX();
            break;
        case FLIP_Y:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            schematics.get(name.asString().toUpperCase()).flipY();
            break;
        case FLIP_Z:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            schematics.get(name.asString().toUpperCase()).flipZ();
            break;
        case PASTE:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            if (location == null) {
                dB.echoError(scriptEntry.getResidingQueue(), "Missing location argument!");
                return;
            }
            try {
                if (delayed != null && delayed.asBoolean()) {
                    schematics.get(name.asString().toUpperCase()).setBlocksDelayed(location, new Runnable() {

                        @Override
                        public void run() {
                            scriptEntry.setFinished(true);
                        }
                    }, noair != null && noair.asBoolean());
                } else {
                    scriptEntry.setFinished(true);
                    schematics.get(name.asString().toUpperCase()).setBlocks(location, noair != null && noair.asBoolean());
                }
            } catch (Exception ex) {
                dB.echoError(scriptEntry.getResidingQueue(), "Exception pasting schematic file " + name.asString() + ".");
                dB.echoError(scriptEntry.getResidingQueue(), ex);
                return;
            }
            break;
        case SAVE:
            if (!schematics.containsKey(name.asString().toUpperCase())) {
                dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is not loaded.");
                return;
            }
            try {
                set = schematics.get(name.asString().toUpperCase());
                String directory = URLDecoder.decode(System.getProperty("user.dir"));
                File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
                f.getParentFile().mkdirs();
                // TODO: Make me waitable!
                FileOutputStream fs = new FileOutputStream(f);
                set.saveMCEditFormatToStream(fs);
                fs.flush();
                fs.close();
            } catch (Exception ex) {
                dB.echoError(scriptEntry.getResidingQueue(), "Error saving schematic file " + fname + ".");
                dB.echoError(scriptEntry.getResidingQueue(), ex);
                return;
            }
            break;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(net.aufdemrand.denizencore.objects.Element) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) net.aufdemrand.denizen.objects.dCuboid(net.aufdemrand.denizen.objects.dCuboid) File(java.io.File) CuboidBlockSet(net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet)

Example 2 with CuboidBlockSet

use of net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet in project Denizen-For-Bukkit by DenizenScript.

the class SchematicCommand method schematicTags.

@TagManager.TagEvents
public void schematicTags(ReplaceableTagEvent event) {
    if (!event.matches("schematic, schem")) {
        return;
    }
    String id = event.hasNameContext() ? event.getNameContext().toUpperCase() : null;
    Attribute attribute = event.getAttributes().fulfill(1);
    // -->
    if (attribute.startsWith("list")) {
        event.setReplaced(new dList(schematics.keySet()).getAttribute(attribute.fulfill(1)));
    }
    if (id == null) {
        return;
    }
    if (!schematics.containsKey(id)) {
        // Meta below
        if (attribute.startsWith("exists")) {
            event.setReplaced(new Element(false).getAttribute(attribute.fulfill(1)));
            return;
        }
        dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Schematic file " + id + " is not loaded.");
        return;
    }
    CuboidBlockSet set = schematics.get(id);
    // -->
    if (attribute.startsWith("exists")) {
        event.setReplaced(new Element(true).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("height")) {
        event.setReplaced(new Element(set.y_length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("length")) {
        event.setReplaced(new Element(set.z_height).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("width")) {
        event.setReplaced(new Element(set.x_width).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("block")) {
        if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) {
            dLocation location = dLocation.valueOf(attribute.getContext(1));
            BlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
            event.setReplaced(dMaterial.getMaterialFrom(block.getMaterial(), block.getData()).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("origin")) {
        event.setReplaced(new dLocation(null, set.center_x, set.center_y, set.center_z).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("blocks")) {
        event.setReplaced(new Element(set.blocks.size()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("cuboid") && attribute.hasContext(1)) {
        dLocation origin = dLocation.valueOf(attribute.getContext(1));
        event.setReplaced(set.getCuboid(origin).getAttribute(attribute.fulfill(1)));
        return;
    }
}
Also used : Attribute(net.aufdemrand.denizencore.tags.Attribute) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData) CuboidBlockSet(net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet)

Aggregations

net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)2 CuboidBlockSet (net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet)2 Element (net.aufdemrand.denizencore.objects.Element)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 BlockData (net.aufdemrand.denizen.nms.interfaces.BlockData)1 net.aufdemrand.denizen.objects.dCuboid (net.aufdemrand.denizen.objects.dCuboid)1 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)1 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 Attribute (net.aufdemrand.denizencore.tags.Attribute)1