Search in sources :

Example 16 with MaterialTag

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

the class MaterialMode method describes.

public static boolean describes(ObjectTag material) {
    if (!(material instanceof MaterialTag)) {
        return false;
    }
    MaterialTag mat = (MaterialTag) material;
    if (!mat.hasModernData()) {
        return false;
    }
    BlockData data = mat.getModernData();
    return data instanceof Comparator || data instanceof PistonHead || data instanceof BubbleColumn || data instanceof StructureBlock || data instanceof DaylightDetector || data instanceof CommandBlock || (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17) && (data instanceof SculkSensor || data instanceof BigDripleaf));
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) BlockData(org.bukkit.block.data.BlockData)

Example 17 with MaterialTag

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

the class ItemCooldownCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ArrayList<MaterialTag> materials = (ArrayList<MaterialTag>) scriptEntry.getObject("materials");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    if (player == null) {
        Debug.echoError("Invalid linked player.");
        return;
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("materials", materials), duration);
    }
    for (MaterialTag mat : materials) {
        player.getPlayerEntity().setCooldown(mat.getMaterial(), duration.getTicksAsInt());
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ArrayList(java.util.ArrayList) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 18 with MaterialTag

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

the class ShowFakeCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag cancel = scriptEntry.getElement("cancel");
    List<MaterialTag> materials = (List<MaterialTag>) scriptEntry.getObject("materials");
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), duration, cancel, db("materials", materials), db("locations", locations), db("players", players));
    }
    boolean shouldCancel = cancel.asBoolean();
    int i = 0;
    for (LocationTag loc : locations) {
        if (!shouldCancel) {
            FakeBlock.showFakeBlockTo(players, loc.getBlockLocation(), materials.get(i % materials.size()), duration, locations.size() < 5);
        } else {
            FakeBlock.stopShowingTo(players, loc.getBlockLocation());
        }
        i++;
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 19 with MaterialTag

use of com.denizenscript.denizen.objects.MaterialTag 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 20 with MaterialTag

use of com.denizenscript.denizen.objects.MaterialTag 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)

Aggregations

MaterialTag (com.denizenscript.denizen.objects.MaterialTag)51 LocationTag (com.denizenscript.denizen.objects.LocationTag)33 EventHandler (org.bukkit.event.EventHandler)21 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)9 EntityTag (com.denizenscript.denizen.objects.EntityTag)8 Material (org.bukkit.Material)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)7 BlockData (org.bukkit.block.data.BlockData)7 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)6 ListTag (com.denizenscript.denizencore.objects.core.ListTag)6 ItemStack (org.bukkit.inventory.ItemStack)6 List (java.util.List)4 Block (org.bukkit.block.Block)4 PlayerChangesSignScriptEvent (com.denizenscript.denizen.events.player.PlayerChangesSignScriptEvent)3 ItemTag (com.denizenscript.denizen.objects.ItemTag)3 SignChangeEvent (org.bukkit.event.block.SignChangeEvent)3 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)2 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)2 HashSet (java.util.HashSet)2 BlockPos (net.minecraft.core.BlockPos)2