Search in sources :

Example 21 with DurationTag

use of com.denizenscript.denizencore.objects.core.DurationTag in project Denizen-For-Bukkit by DenizenScript.

the class PushableCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NPCTag denizenNPC = Utilities.getEntryNPC(scriptEntry);
    if (denizenNPC == null) {
        Debug.echoError("No valid NPC attached to this queue!");
        return;
    }
    PushableTrait trait = denizenNPC.getPushableTrait();
    ElementTag state = scriptEntry.getElement("state");
    DurationTag delay = scriptEntry.getObjectTag("delay");
    ElementTag returnable = scriptEntry.getElement("return");
    if (state == null && delay == null && returnable == null) {
        state = new ElementTag("TOGGLE");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), denizenNPC, state, delay, returnable);
    }
    if (delay != null) {
        trait.setDelay(delay.getSecondsAsInt());
    }
    if (returnable != null) {
        trait.setReturnable(returnable.asBoolean());
    }
    if (state != null) {
        switch(Toggle.valueOf(state.asString().toUpperCase())) {
            case TRUE:
            case ON:
                trait.setPushable(true);
                break;
            case FALSE:
            case OFF:
                trait.setPushable(false);
                break;
            case TOGGLE:
                trait.setPushable(!trait.isPushable());
                break;
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) PushableTrait(com.denizenscript.denizen.npc.traits.PushableTrait)

Example 22 with DurationTag

use of com.denizenscript.denizencore.objects.core.DurationTag 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 23 with DurationTag

use of com.denizenscript.denizencore.objects.core.DurationTag in project Denizen-For-Bukkit by DenizenScript.

the class TimeCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    DurationTag value = scriptEntry.getObjectTag("value");
    DurationTag resetAfter = scriptEntry.getObjectTag("reset_after");
    WorldTag world = scriptEntry.getObjectTag("world");
    ElementTag type_element = scriptEntry.getElement("type");
    ElementTag reset = scriptEntry.getElement("reset");
    ElementTag freeze = scriptEntry.getElement("freeze");
    Type type = Type.valueOf(type_element.asString().toUpperCase());
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), type_element, reset, value, freeze, resetAfter, world);
    }
    if (type.equals(Type.GLOBAL)) {
        world.getWorld().setTime(value.getTicks());
    } else {
        if (!Utilities.entryHasPlayer(scriptEntry)) {
            Debug.echoError("Must have a valid player link!");
        } else {
            Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
            if (reset != null && reset.asBoolean()) {
                player.resetPlayerTime();
            } else {
                Integer existingTask = resetTasks.get(player.getUniqueId());
                if (existingTask != null) {
                    Bukkit.getScheduler().cancelTask(existingTask);
                    resetTasks.remove(player.getUniqueId());
                }
                player.setPlayerTime(value.getTicks(), freeze == null || !freeze.asBoolean());
                if (resetAfter != null) {
                    int newTask = Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), player::resetPlayerTime, resetAfter.getTicks());
                    resetTasks.put(player.getUniqueId(), newTask);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) WorldTag(com.denizenscript.denizen.objects.WorldTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 24 with DurationTag

use of com.denizenscript.denizencore.objects.core.DurationTag in project Denizen-For-Bukkit by DenizenScript.

the class WorldBorderCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    WorldTag world = scriptEntry.getObjectTag("world");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    LocationTag center = scriptEntry.getObjectTag("center");
    ElementTag size = scriptEntry.getElement("size");
    ElementTag currSize = scriptEntry.getElement("current_size");
    ElementTag damage = scriptEntry.getElement("damage");
    ElementTag damagebuffer = scriptEntry.getElement("damagebuffer");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag warningdistance = scriptEntry.getElement("warningdistance");
    DurationTag warningtime = scriptEntry.getObjectTag("warningtime");
    ElementTag reset = scriptEntry.getObjectTag("reset");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), world, db("players", players), center, size, currSize, damage, damagebuffer, warningdistance, warningtime, duration, reset);
    }
    if (players != null) {
        if (reset.asBoolean()) {
            for (PlayerTag player : players) {
                NMSHandler.getPacketHelper().resetWorldBorder(player.getPlayerEntity());
            }
            return;
        }
        WorldBorder wb;
        for (PlayerTag player : players) {
            wb = player.getWorld().getWorldBorder();
            NMSHandler.getPacketHelper().setWorldBorder(player.getPlayerEntity(), (center != null ? center : wb.getCenter()), (size != null ? size.asDouble() : wb.getSize()), (currSize != null ? currSize.asDouble() : wb.getSize()), duration.getMillis(), (warningdistance != null ? warningdistance.asInt() : wb.getWarningDistance()), (warningtime != null ? warningtime.getSecondsAsInt() : wb.getWarningTime()));
        }
        return;
    }
    WorldBorder worldborder = world.getWorld().getWorldBorder();
    if (reset.asBoolean()) {
        worldborder.reset();
        return;
    }
    if (center != null) {
        worldborder.setCenter(center);
    }
    if (size != null) {
        if (currSize != null) {
            worldborder.setSize(currSize.asDouble());
        }
        worldborder.setSize(size.asDouble(), duration.getSecondsAsInt());
    }
    if (damage != null) {
        worldborder.setDamageAmount(damage.asDouble());
    }
    if (damagebuffer != null) {
        worldborder.setDamageBuffer(damagebuffer.asDouble());
    }
    if (warningdistance != null) {
        worldborder.setWarningDistance(warningdistance.asInt());
    }
    if (warningtime != null) {
        worldborder.setWarningTime(warningtime.getSecondsAsInt());
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) WorldBorder(org.bukkit.WorldBorder) List(java.util.List) WorldTag(com.denizenscript.denizen.objects.WorldTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 25 with DurationTag

use of com.denizenscript.denizencore.objects.core.DurationTag in project Denizen-For-Bukkit by DenizenScript.

the class SwitchCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ListTag interactLocations = scriptEntry.getObjectTag("locations");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    final SwitchState switchState = SwitchState.valueOf(scriptEntry.getElement("switchstate").asString());
    ElementTag noPhysics = scriptEntry.getElement("no_physics");
    // Switch the Block
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), interactLocations, duration, noPhysics, db("switchstate", switchState.name()));
    }
    final boolean physics = noPhysics == null || !noPhysics.asBoolean();
    for (final LocationTag interactLocation : interactLocations.filter(LocationTag.class, scriptEntry)) {
        switchBlock(scriptEntry, interactLocation, switchState, physics);
        // If duration set, schedule a delayed task.
        if (duration.getTicks() > 0) {
            // If this block already had a delayed task, cancel it.
            if (taskMap.containsKey(interactLocation)) {
                try {
                    Bukkit.getScheduler().cancelTask(taskMap.get(interactLocation));
                } catch (Exception e) {
                }
            }
            Debug.echoDebug(scriptEntry, "Setting delayed task 'SWITCH' for " + interactLocation.identify());
            // Store new delayed task ID, for checking against, then schedule new delayed task.
            taskMap.put(interactLocation, Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> switchBlock(scriptEntry, interactLocation, SwitchState.TOGGLE, physics), duration.getTicks()));
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Aggregations

DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)54 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)39 LocationTag (com.denizenscript.denizen.objects.LocationTag)19 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)16 ListTag (com.denizenscript.denizencore.objects.core.ListTag)16 EntityTag (com.denizenscript.denizen.objects.EntityTag)15 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)13 List (java.util.List)11 Player (org.bukkit.entity.Player)7 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)6 ColorTag (com.denizenscript.denizen.objects.ColorTag)5 PotionEffect (org.bukkit.potion.PotionEffect)5 PotionEffectType (org.bukkit.potion.PotionEffectType)5 ItemTag (com.denizenscript.denizen.objects.ItemTag)4 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 WorldTag (com.denizenscript.denizen.objects.WorldTag)4 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)4 LivingEntity (org.bukkit.entity.LivingEntity)4 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3