Search in sources :

Example 41 with ListTag

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

the class PushCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    EntityTag originEntity = scriptEntry.getObjectTag("origin_entity");
    LocationTag originLocation = scriptEntry.hasObject("origin_location") ? (LocationTag) scriptEntry.getObject("origin_location") : new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()).subtract(0, 0.4, 0));
    boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();
    final boolean no_damage = scriptEntry.hasObject("no_damage") && scriptEntry.getElement("no_damage").asBoolean();
    // If there is no destination set, but there is a shooter, get a point in front of the shooter and set it as the destination
    final LocationTag destination = scriptEntry.hasObject("destination") ? (LocationTag) scriptEntry.getObject("destination") : (originEntity != null ? new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection().multiply(30))) : null);
    // TODO: Should this be checked in argument parsing?
    if (destination == null) {
        Debug.echoError("No destination specified!");
        scriptEntry.setFinished(true);
        return;
    }
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final ScriptTag script = scriptEntry.getObjectTag("script");
    final ListTag definitions = scriptEntry.getObjectTag("definitions");
    ElementTag speedElement = scriptEntry.getElement("speed");
    DurationTag duration = (DurationTag) scriptEntry.getObject("duration");
    ElementTag force_along = scriptEntry.getElement("force_along");
    ElementTag precision = scriptEntry.getElement("precision");
    ElementTag ignore_collision = scriptEntry.getElement("ignore_collision");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("origin", originEntity != null ? originEntity : originLocation), db("entities", entities), destination, speedElement, duration, script, force_along, precision, (no_rotate ? db("no_rotate", "true") : ""), (no_damage ? db("no_damage", "true") : ""), ignore_collision, definitions);
    }
    final boolean ignoreCollision = ignore_collision != null && ignore_collision.asBoolean();
    final double speed = speedElement.asDouble();
    final int maxTicks = duration.getTicksAsInt();
    final boolean forceAlong = force_along.asBoolean();
    // Keep a ListTag of entities that can be called using <entry[name].pushed_entities> later in the script queue
    final ListTag entityList = new ListTag();
    for (EntityTag entity : entities) {
        entity.spawnAt(originLocation);
        entityList.addObject(entity);
        if (!no_rotate) {
            NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
        }
        if (entity.isProjectile() && originEntity != null) {
            entity.setShooter(originEntity);
        }
    }
    scriptEntry.addObject("pushed_entities", entityList);
    Position.mount(Conversion.convertEntities(entities));
    final EntityTag lastEntity = entities.get(entities.size() - 1);
    final Vector v2 = destination.toVector();
    final Vector Origin = originLocation.toVector();
    final int prec = precision.asInt();
    BukkitRunnable task = new BukkitRunnable() {

        int runs = 0;

        LocationTag lastLocation;

        @Override
        public void run() {
            if (runs < maxTicks && lastEntity.isValid()) {
                Vector v1 = lastEntity.getLocation().toVector();
                Vector v3 = v2.clone().subtract(v1).normalize();
                if (forceAlong) {
                    Vector newDest = v2.clone().subtract(Origin).normalize().multiply(runs * speed).add(Origin);
                    lastEntity.teleport(new Location(lastEntity.getLocation().getWorld(), newDest.getX(), newDest.getY(), newDest.getZ(), lastEntity.getLocation().getYaw(), lastEntity.getLocation().getPitch()));
                }
                runs += prec;
                // Check if the entity is close to its destination
                if (Math.abs(v2.getX() - v1.getX()) < 1.5f && Math.abs(v2.getY() - v1.getY()) < 1.5f && Math.abs(v2.getZ() - v1.getZ()) < 1.5f) {
                    runs = maxTicks;
                    return;
                }
                Vector newVel = v3.multiply(speed);
                lastEntity.setVelocity(newVel);
                if (!ignoreCollision && lastEntity.isValid()) {
                    BoundingBox box = lastEntity.getBukkitEntity().getBoundingBox().expand(newVel);
                    Location ref = lastEntity.getLocation().clone();
                    for (int x = (int) Math.floor(box.getMinX()); x < Math.ceil(box.getMaxX()); x++) {
                        ref.setX(x);
                        for (int y = (int) Math.floor(box.getMinY()); y < Math.ceil(box.getMaxY()); y++) {
                            ref.setY(y);
                            for (int z = (int) Math.floor(box.getMinZ()); z < Math.ceil(box.getMaxZ()); z++) {
                                ref.setZ(z);
                                if (!isSafeBlock(ref)) {
                                    runs = maxTicks;
                                }
                            }
                        }
                    }
                }
                if (no_damage && lastEntity.isLivingEntity()) {
                    lastEntity.getLivingEntity().setFallDistance(0);
                }
                // Record the location in case the entity gets lost (EG, if a pushed arrow hits a mob)
                lastLocation = lastEntity.getLocation();
            } else {
                this.cancel();
                if (script != null) {
                    Consumer<ScriptQueue> configure = (queue) -> {
                        if (lastEntity.getLocation() != null) {
                            queue.addDefinition("location", lastEntity.getLocation());
                        } else {
                            queue.addDefinition("location", lastLocation);
                        }
                        queue.addDefinition("pushed_entities", entityList);
                        queue.addDefinition("last_entity", lastEntity);
                    };
                    ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
                }
                scriptEntry.setFinished(true);
            }
        }
    };
    task.runTaskTimer(Denizen.getInstance(), 0, prec);
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) BoundingBox(org.bukkit.util.BoundingBox) LocationTag(com.denizenscript.denizen.objects.LocationTag) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Holdable(com.denizenscript.denizencore.scripts.commands.Holdable) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) Location(org.bukkit.Location) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Position(com.denizenscript.denizen.utilities.entity.Position) Denizen(com.denizenscript.denizen.Denizen) Vector(org.bukkit.util.Vector) Consumer(java.util.function.Consumer) List(java.util.List) ScriptUtilities(com.denizenscript.denizencore.utilities.ScriptUtilities) EntityTag(com.denizenscript.denizen.objects.EntityTag) com.denizenscript.denizencore.objects(com.denizenscript.denizencore.objects) Debug(com.denizenscript.denizen.utilities.debugging.Debug) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) Conversion(com.denizenscript.denizen.utilities.Conversion) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) BoundingBox(org.bukkit.util.BoundingBox) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 42 with ListTag

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

the class RenameCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ElementTag name = scriptEntry.getElement("name");
    ElementTag perPlayer = scriptEntry.getElement("per_player");
    ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
    ListTag targets = scriptEntry.getObjectTag("targets");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    if (perPlayer != null && perPlayer.asBoolean()) {
        NetworkInterceptHelper.enable();
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
        }
        for (ObjectTag target : targets.objectForms) {
            EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
            if (entity != null) {
                Entity bukkitEntity = entity.getBukkitEntity();
                if (bukkitEntity == null) {
                    Debug.echoError("Invalid entity in rename command.");
                    continue;
                }
                if (name.asString().equals("cancel")) {
                    customNames.remove(bukkitEntity.getUniqueId());
                    if (bukkitEntity.isCustomNameVisible()) {
                        if (players == null) {
                            for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                                NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        } else {
                            for (PlayerTag player : players) {
                                NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        }
                    } else {
                        bukkitEntity.setCustomNameVisible(true);
                        // Force a metadata update
                        bukkitEntity.setCustomNameVisible(false);
                    }
                } else {
                    final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
                    HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
                    Function<Player, String> nameGetter = p -> {
                        originalContext.player = new PlayerTag(p);
                        return TagManager.tag(name.asString(), originalContext);
                    };
                    RenameData renamer = new RenameData();
                    renamer.nameFunction = nameGetter;
                    renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
                    if (players == null) {
                        playerToFuncMap.put(null, renamer);
                    } else {
                        for (PlayerTag player : players) {
                            playerToFuncMap.put(player.getUUID(), renamer);
                        }
                    }
                    if (players == null) {
                        for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                            NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
                        }
                    } else {
                        for (PlayerTag player : players) {
                            NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
                        }
                    }
                }
            }
        }
        return;
    }
    String nameString = TagManager.tag(name.asString(), scriptEntry.context);
    if (nameString.length() > 256) {
        nameString = nameString.substring(0, 256);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("name", nameString), listNameOnly, targets);
    }
    for (ObjectTag target : targets.objectForms) {
        EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
        if (entity == null) {
            entity = target.asType(NPCTag.class, scriptEntry.context);
        } else {
            entity = ((EntityTag) entity).getDenizenObject();
        }
        if (entity == null) {
            Debug.echoError("Invalid entity in rename command.");
            continue;
        }
        if (entity instanceof NPCTag) {
            NPC npc = ((NPCTag) entity).getCitizen();
            if (npc.isSpawned()) {
                Location prev = npc.getStoredLocation().clone();
                npc.despawn(DespawnReason.PENDING_RESPAWN);
                npc.setName(nameString);
                npc.spawn(prev);
            } else {
                npc.setName(nameString);
            }
        } else if (entity instanceof PlayerTag) {
            if (listNameOnly != null && listNameOnly.asBoolean()) {
                AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
            } else {
                String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
                NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
            }
        } else {
            Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
            customNames.remove(bukkitEntity.getUniqueId());
            bukkitEntity.setCustomName(nameString);
            bukkitEntity.setCustomNameVisible(true);
        }
    }
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) TagManager(com.denizenscript.denizencore.tags.TagManager) AdvancedTextImpl(com.denizenscript.denizen.utilities.AdvancedTextImpl) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Function(java.util.function.Function) Argument(com.denizenscript.denizencore.objects.Argument) NPC(net.citizensnpcs.api.npc.NPC) Location(org.bukkit.Location) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) Entity(org.bukkit.entity.Entity) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) UUID(java.util.UUID) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) DespawnReason(net.citizensnpcs.api.event.DespawnReason) NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Location(org.bukkit.Location)

Example 43 with ListTag

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

the class ActionCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ListTag actions = scriptEntry.getObjectTag("actions");
    ListTag context = scriptEntry.getObjectTag("context");
    List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), actions, context, db("npcs", npcs));
    }
    if (context.size() % 2 == 1) {
        // Size is uneven!
        context.add("null");
    }
    // Change the context input to a list of objects
    Map<String, ObjectTag> context_map = new HashMap<>();
    for (int i = 0; i < context.size(); i += 2) {
        context_map.put(context.get(i), ObjectFetcher.pickObjectFor(context.get(i + 1), scriptEntry.getContext()));
    }
    for (NPCTag npc : npcs) {
        for (String action : actions) {
            npc.action(action, Utilities.getEntryPlayer(scriptEntry), context_map);
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 44 with ListTag

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

the class ResetCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    // We allow players to be a single player or multiple players
    ObjectTag player = scriptEntry.getObjectTag("players");
    ListTag players;
    if (player instanceof PlayerTag) {
        players = new ListTag(player);
    } else {
        players = scriptEntry.getObjectTag("players");
    }
    Type type = (Type) scriptEntry.getObject("type");
    ScriptTag script = scriptEntry.getObjectTag("script");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), players, db("type", type), script);
    }
    // Deal with GLOBAL_COOLDOWN reset first, since there's no player/players involved
    if (type == Type.GLOBAL_COOLDOWN) {
        CooldownCommand.setCooldown(null, new DurationTag(0), script.getName(), true);
        return;
    }
    // Now deal with the rest
    for (String object : players) {
        PlayerTag resettable = PlayerTag.valueOf(object, scriptEntry.context);
        if (resettable.isValid()) {
            switch(type) {
                case PLAYER_COOLDOWN:
                    CooldownCommand.setCooldown(resettable, new DurationTag(0), script.getName(), false);
                    return;
            }
        }
    }
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 45 with ListTag

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

the class ItemSkullskin method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("skull_skin")) {
        ListTag list = mechanism.valueAsType(ListTag.class);
        String idString = list.get(0);
        String texture = null;
        if (list.size() > 1) {
            texture = list.get(1);
        }
        PlayerProfile profile;
        if (CoreUtilities.contains(idString, '-')) {
            UUID uuid = UUID.fromString(idString);
            String name = null;
            if (list.size() > 2) {
                name = list.get(2);
            }
            profile = new PlayerProfile(name, uuid, texture);
        } else {
            profile = new PlayerProfile(idString, null, texture);
        }
        profile = NMSHandler.getInstance().fillPlayerProfile(profile);
        if (texture != null) {
            // Ensure we didn't get overwritten
            profile.setTexture(texture);
        }
        if (profile.getTexture() == null) {
            // Can't set a skull skin to nothing.
            return;
        }
        item.setItemStack(NMSHandler.getItemHelper().setSkullSkin(item.getItemStack(), profile));
    }
}
Also used : PlayerProfile(com.denizenscript.denizen.nms.util.PlayerProfile) UUID(java.util.UUID) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

ListTag (com.denizenscript.denizencore.objects.core.ListTag)122 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)71 MapTag (com.denizenscript.denizencore.objects.core.MapTag)21 ItemStack (org.bukkit.inventory.ItemStack)18 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)14 List (java.util.List)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)13 Player (org.bukkit.entity.Player)13 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)12 LocationTag (com.denizenscript.denizen.objects.LocationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)9 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)9 ArrayList (java.util.ArrayList)9 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)7 NPC (net.citizensnpcs.api.npc.NPC)7 Entity (org.bukkit.entity.Entity)7