Search in sources :

Example 51 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class DebugBlockCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag clear = scriptEntry.getElement("clear");
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    ColorTag color = scriptEntry.getObjectTag("color");
    ElementTag alpha = scriptEntry.getElement("alpha");
    ElementTag name = scriptEntry.getElement("name");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), clear == null ? db("locations", locations) : clear, duration, db("players", players), color, alpha, name);
    }
    if (clear != null && clear.asBoolean()) {
        for (PlayerTag player : players) {
            NMSHandler.getPacketHelper().clearDebugTestMarker(player.getPlayerEntity());
        }
    } else {
        int alphaInt = (int) (alpha.asFloat() * 255);
        for (LocationTag location : locations) {
            for (PlayerTag player : players) {
                NMSHandler.getPacketHelper().showDebugTestMarker(player.getPlayerEntity(), location, color, alphaInt, name == null ? "" : name.asString(), (int) duration.getMillis());
            }
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ColorTag(com.denizenscript.denizen.objects.ColorTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 52 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class BreakCommand method execute.

// <--[action]
// @Actions
// dig
// 
// @Triggers when the NPC breaks a block with the Break Command
// 
// @Context
// <context.location> returns the location the NPC Dug
// <context.material> Returns the Block dug
// 
// -->
@Override
public void execute(ScriptEntry scriptEntry) {
    final LocationTag location = scriptEntry.getObjectTag("location");
    final NPCTag npc = scriptEntry.getObjectTag("npc");
    ElementTag radius = scriptEntry.getElement("radius");
    final HashMap<String, ObjectTag> context = new HashMap<>();
    MaterialTag material = new MaterialTag(location.getBlock());
    context.put("location", location);
    context.put("material", material);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), location, npc, radius);
    }
    final ScriptEntry se = scriptEntry;
    BlockBreaker.BlockBreakerConfiguration config = new BlockBreaker.BlockBreakerConfiguration();
    config.item(npc.getLivingEntity().getEquipment().getItemInMainHand());
    config.radius(radius.asDouble());
    config.callback(() -> {
        npc.action("dig", null, context);
        se.setFinished(true);
    });
    BlockBreaker breaker = npc.getCitizen().getBlockBreaker(location.getBlock(), config);
    if (breaker.shouldExecute()) {
        TaskRunnable run = new TaskRunnable(breaker);
        run.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), run, 0, 1);
    } else {
        se.setFinished(true);
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) HashMap(java.util.HashMap) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) BlockBreaker(net.citizensnpcs.api.npc.BlockBreaker) LocationTag(com.denizenscript.denizen.objects.LocationTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 53 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class FishCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag catchtype = scriptEntry.getElement("catch");
    ElementTag stop = scriptEntry.getElement("stop");
    ElementTag percent = scriptEntry.getElement("percent");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), location, catchtype, percent, stop);
    }
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    FishingTrait trait = npc.getFishingTrait();
    if (stop.asBoolean()) {
        trait.stopFishing();
        return;
    }
    npc.getEquipmentTrait().set(0, new ItemStack(Material.FISHING_ROD));
    trait.setCatchPercent(percent.asInt());
    trait.setCatchType(FishingHelper.CatchType.valueOf(catchtype.asString().toUpperCase()));
    trait.startFishing(location);
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) FishingTrait(com.denizenscript.denizen.npc.traits.FishingTrait) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack)

Example 54 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class SchematicCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    ElementTag angle = scriptEntry.argForPrefixAsElement("angle", null);
    ElementTag type = scriptEntry.getElement("type");
    ElementTag name = scriptEntry.requiredArgForPrefixAsElement("name");
    ElementTag filename = scriptEntry.argForPrefixAsElement("filename", null);
    boolean noair = scriptEntry.argAsBoolean("noair");
    boolean delayed = scriptEntry.argAsBoolean("delayed") || scriptEntry.shouldWaitFor();
    ElementTag maxDelayMs = scriptEntry.argForPrefixAsElement("max_delay_ms", "50");
    boolean copyEntities = scriptEntry.argAsBoolean("entities");
    boolean flags = scriptEntry.argAsBoolean("flags");
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag mask = scriptEntry.argForPrefixAsElement("mask", null);
    List<PlayerTag> fakeTo = scriptEntry.argForPrefixList("fake_to", PlayerTag.class, true);
    DurationTag fakeDuration = scriptEntry.argForPrefix("fake_duration", DurationTag.class, true);
    CuboidTag cuboid = scriptEntry.getObjectTag("cuboid");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), type, name, location, filename, cuboid, angle, db("noair", noair), db("delayed", delayed), maxDelayMs, db("flags", flags), db("entities", copyEntities), mask, fakeDuration, db("fake_to", fakeTo));
    }
    CuboidBlockSet set;
    Type ttype = Type.valueOf(type.asString());
    String fname = filename != null ? filename.asString() : name.asString();
    switch(ttype) {
        case CREATE:
            {
                if (schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is already loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                if (cuboid == null) {
                    Debug.echoError(scriptEntry, "Missing cuboid argument!");
                    scriptEntry.setFinished(true);
                    return;
                }
                if (location == null) {
                    Debug.echoError(scriptEntry, "Missing origin location argument!");
                    scriptEntry.setFinished(true);
                    return;
                }
                try {
                    if (delayed) {
                        set = new CuboidBlockSet();
                        set.buildDelayed(cuboid, location, () -> {
                            if (copyEntities) {
                                set.buildEntities(cuboid, location);
                            }
                            schematics.put(name.asString().toUpperCase(), set);
                            scriptEntry.setFinished(true);
                        }, maxDelayMs.asLong(), flags);
                    } else {
                        scriptEntry.setFinished(true);
                        set = new CuboidBlockSet(cuboid, location, flags);
                        if (copyEntities) {
                            set.buildEntities(cuboid, location);
                        }
                        schematics.put(name.asString().toUpperCase(), set);
                    }
                } catch (Exception ex) {
                    Debug.echoError(scriptEntry, "Error creating schematic object " + name.asString() + ".");
                    Debug.echoError(scriptEntry, ex);
                    scriptEntry.setFinished(true);
                    return;
                }
                break;
            }
        case LOAD:
            {
                if (schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is already loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                String directory = URLDecoder.decode(System.getProperty("user.dir"));
                File f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schem");
                if (!Utilities.canReadFile(f)) {
                    Debug.echoError("Cannot read from that file path due to security settings in Denizen/config.yml.");
                    scriptEntry.setFinished(true);
                    return;
                }
                if (!f.exists()) {
                    f = new File(directory + "/plugins/Denizen/schematics/" + fname + ".schematic");
                    if (!f.exists()) {
                        Debug.echoError("Schematic file " + fname + " does not exist. Are you sure it's in " + directory + "/plugins/Denizen/schematics/?");
                        scriptEntry.setFinished(true);
                        return;
                    }
                }
                File schemFile = f;
                Runnable loadRunnable = () -> {
                    try {
                        InputStream fs = new FileInputStream(schemFile);
                        CuboidBlockSet newSet;
                        newSet = SpongeSchematicHelper.fromSpongeStream(fs);
                        fs.close();
                        Runnable storeSchem = () -> {
                            schematics.put(name.asString().toUpperCase(), newSet);
                            scriptEntry.setFinished(true);
                        };
                        if (delayed) {
                            Bukkit.getScheduler().runTask(Denizen.getInstance(), storeSchem);
                        } else {
                            storeSchem.run();
                        }
                    } catch (Exception ex) {
                        Runnable showError = () -> {
                            Debug.echoError(scriptEntry, "Error loading schematic file " + name.asString() + ".");
                            Debug.echoError(scriptEntry, ex);
                        };
                        if (delayed) {
                            Bukkit.getScheduler().runTask(Denizen.getInstance(), showError);
                        } else {
                            showError.run();
                        }
                        scriptEntry.setFinished(true);
                        return;
                    }
                };
                if (delayed) {
                    Bukkit.getScheduler().runTaskAsynchronously(Denizen.getInstance(), loadRunnable);
                } else {
                    loadRunnable.run();
                    scriptEntry.setFinished(true);
                }
                break;
            }
        case UNLOAD:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                schematics.remove(name.asString().toUpperCase());
                scriptEntry.setFinished(true);
                break;
            }
        case ROTATE:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                if (angle == null) {
                    Debug.echoError(scriptEntry, "Missing angle argument!");
                    scriptEntry.setFinished(true);
                    return;
                }
                final CuboidBlockSet schematic = schematics.get(name.asString().toUpperCase());
                rotateSchem(schematic, angle.asInt(), delayed, () -> scriptEntry.setFinished(true));
                break;
            }
        case FLIP_X:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                schematics.get(name.asString().toUpperCase()).flipX();
                scriptEntry.setFinished(true);
                break;
            }
        case FLIP_Y:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                schematics.get(name.asString().toUpperCase()).flipY();
                scriptEntry.setFinished(true);
                break;
            }
        case FLIP_Z:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                schematics.get(name.asString().toUpperCase()).flipZ();
                scriptEntry.setFinished(true);
                break;
            }
        case PASTE:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    scriptEntry.setFinished(true);
                    return;
                }
                if (location == null) {
                    Debug.echoError(scriptEntry, "Missing location argument!");
                    scriptEntry.setFinished(true);
                    return;
                }
                try {
                    BlockSet.InputParams input = new BlockSet.InputParams();
                    input.centerLocation = location;
                    input.noAir = noair;
                    input.fakeTo = fakeTo;
                    if (fakeTo != null && copyEntities) {
                        Debug.echoError(scriptEntry, "Cannot fake paste entities currently.");
                        scriptEntry.setFinished(true);
                        return;
                    }
                    if (fakeDuration == null) {
                        fakeDuration = new DurationTag(0);
                    }
                    input.fakeDuration = fakeDuration;
                    if (mask != null) {
                        String maskText = mask.asString();
                        input.mask = new HashSet<>();
                        if (maskText.startsWith("li@")) {
                            // Back-compat: input used to be a list of materials
                            for (MaterialTag material : ListTag.valueOf(maskText, scriptEntry.getContext()).filter(MaterialTag.class, scriptEntry)) {
                                input.mask.add(material.getMaterial());
                            }
                        } else {
                            for (Material material : Material.values()) {
                                if (BukkitScriptEvent.tryMaterial(material, maskText)) {
                                    input.mask.add(material);
                                }
                            }
                        }
                    }
                    set = schematics.get(name.asString().toUpperCase());
                    Consumer<CuboidBlockSet> pasteRunnable = (schematic) -> {
                        if (delayed) {
                            schematic.setBlocksDelayed(() -> {
                                if (copyEntities) {
                                    schematic.pasteEntities(location);
                                }
                                scriptEntry.setFinished(true);
                            }, input, maxDelayMs.asLong());
                        } else {
                            schematic.setBlocks(input);
                            if (copyEntities) {
                                schematic.pasteEntities(location);
                            }
                            scriptEntry.setFinished(true);
                        }
                    };
                    if (angle != null) {
                        final CuboidBlockSet newSet = set.duplicate();
                        rotateSchem(newSet, angle.asInt(), delayed, () -> pasteRunnable.accept(newSet));
                    } else {
                        pasteRunnable.accept(set);
                    }
                } catch (Exception ex) {
                    Debug.echoError(scriptEntry, "Exception pasting schematic file " + name.asString() + ".");
                    Debug.echoError(scriptEntry, ex);
                    scriptEntry.setFinished(true);
                    return;
                }
                break;
            }
        case SAVE:
            {
                if (!schematics.containsKey(name.asString().toUpperCase())) {
                    Debug.echoError(scriptEntry, "Schematic file " + name.asString() + " is not loaded.");
                    return;
                }
                set = schematics.get(name.asString().toUpperCase());
                String directory = URLDecoder.decode(System.getProperty("user.dir"));
                String extension = ".schem";
                File f = new File(directory + "/plugins/Denizen/schematics/" + fname + extension);
                if (!Utilities.canWriteToFile(f)) {
                    Debug.echoError("Cannot write to that file path due to security settings in Denizen/config.yml.");
                    scriptEntry.setFinished(true);
                    return;
                }
                Runnable saveRunnable = () -> {
                    try {
                        f.getParentFile().mkdirs();
                        FileOutputStream fs = new FileOutputStream(f);
                        SpongeSchematicHelper.saveToSpongeStream(set, fs);
                        fs.flush();
                        fs.close();
                        Bukkit.getScheduler().runTask(Denizen.getInstance(), () -> scriptEntry.setFinished(true));
                    } catch (Exception ex) {
                        Bukkit.getScheduler().runTask(Denizen.getInstance(), () -> {
                            Debug.echoError(scriptEntry, "Error saving schematic file " + fname + ".");
                            Debug.echoError(scriptEntry, ex);
                        });
                        scriptEntry.setFinished(true);
                        return;
                    }
                };
                if (delayed) {
                    Bukkit.getScheduler().runTaskAsynchronously(Denizen.getInstance(), saveRunnable);
                } else {
                    scriptEntry.setFinished(true);
                    saveRunnable.run();
                }
                break;
            }
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Material(org.bukkit.Material) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) FileInputStream(java.io.FileInputStream) LocationTag(com.denizenscript.denizen.objects.LocationTag) Consumer(java.util.function.Consumer) CuboidTag(com.denizenscript.denizen.objects.CuboidTag) TagRunnable(com.denizenscript.denizencore.tags.TagRunnable) FileOutputStream(java.io.FileOutputStream) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) File(java.io.File) HashSet(java.util.HashSet)

Example 55 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class SchematicCommand method schematicTags.

public void schematicTags(ReplaceableTagEvent event) {
    if (!event.matches("schematic")) {
        return;
    }
    Attribute attribute = event.getAttributes();
    String id = attribute.hasParam() ? attribute.getParam().toUpperCase() : null;
    attribute = attribute.fulfill(1);
    // -->
    if (attribute.startsWith("list")) {
        event.setReplaced(new ListTag(schematics.keySet()).getAttribute(attribute.fulfill(1)));
    }
    if (id == null) {
        return;
    }
    if (!schematics.containsKey(id)) {
        // Meta below
        if (attribute.startsWith("exists")) {
            event.setReplaced(new ElementTag(false).getAttribute(attribute.fulfill(1)));
            return;
        }
        Debug.echoError(attribute.getScriptEntry(), "Schematic file " + id + " is not loaded.");
        return;
    }
    CuboidBlockSet set = schematics.get(id);
    // -->
    if (attribute.startsWith("exists")) {
        event.setReplaced(new ElementTag(true).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("height")) {
        event.setReplaced(new ElementTag(set.y_length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("length")) {
        event.setReplaced(new ElementTag(set.z_height).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("width")) {
        event.setReplaced(new ElementTag(set.x_width).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("block")) {
        if (attribute.hasParam() && LocationTag.matches(attribute.getParam())) {
            LocationTag location = attribute.paramAsType(LocationTag.class);
            FullBlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
            event.setReplaced(new MaterialTag(block.data).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("origin")) {
        event.setReplaced(new LocationTag(null, set.center_x, set.center_y, set.center_z).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("blocks")) {
        event.setReplaced(new ElementTag(set.blocks.length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("cuboid") && attribute.hasParam()) {
        LocationTag origin = attribute.paramAsType(LocationTag.class);
        event.setReplaced(set.getCuboid(origin).getAttribute(attribute.fulfill(1)));
        return;
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Attribute(com.denizenscript.denizencore.tags.Attribute) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

LocationTag (com.denizenscript.denizen.objects.LocationTag)133 EventHandler (org.bukkit.event.EventHandler)69 EntityTag (com.denizenscript.denizen.objects.EntityTag)45 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)40 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)33 List (java.util.List)21 ItemTag (com.denizenscript.denizen.objects.ItemTag)18 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)13 NPCTag (com.denizenscript.denizen.objects.NPCTag)12 Location (org.bukkit.Location)11 ArrayList (java.util.ArrayList)8 Entity (org.bukkit.entity.Entity)8 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)6 Player (org.bukkit.entity.Player)6 Vector (org.bukkit.util.Vector)6 UUID (java.util.UUID)5 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)5