Search in sources :

Example 56 with EntityTag

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

the class AgeCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    AgeType ageType = (AgeType) scriptEntry.getObject("agetype");
    int age = scriptEntry.getElement("age").asInt();
    boolean lock = scriptEntry.hasObject("lock");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (lock ? db("lock", true) : ""), (ageType != null ? db("agetype", ageType) : db("age", age)), db("entities", entities));
    }
    for (EntityTag entity : entities) {
        if (entity.isSpawned()) {
            if (EntityAge.describes(entity)) {
                EntityAge property = EntityAge.getFrom(entity);
                if (ageType != null) {
                    if (ageType.equals(AgeType.BABY)) {
                        property.setAge(-24000);
                    } else {
                        property.setAge(0);
                    }
                } else {
                    property.setAge(age);
                }
                property.setLock(lock);
            } else {
                Debug.echoError(scriptEntry, entity.identify() + " is not ageable!");
            }
        }
    }
}
Also used : EntityAge(com.denizenscript.denizen.objects.properties.entity.EntityAge) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List)

Example 57 with EntityTag

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

the class RemoveCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    WorldTag world = scriptEntry.getObjectTag("world");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), world, db("entities", entities));
    }
    for (EntityTag entity : entities) {
        if (entity.isUnique()) {
            if (entity.isFake) {
                FakeEntity fakeEnt = FakeEntity.idsToEntities.get(entity.getUUID());
                if (fakeEnt != null) {
                    fakeEnt.cancelEntity();
                }
            } else if (entity.isCitizensNPC()) {
                entity.getDenizenNPC().getCitizen().destroy();
            } else if (entity.isSpawned()) {
                entity.remove();
            } else {
                Debug.echoError("Tried to remove already-removed entity.");
            }
        } else {
            int removed = 0;
            for (Entity worldEntity : world.getEntities()) {
                if (entity.getEntityType().equals(DenizenEntityType.getByEntity(worldEntity))) {
                    worldEntity.remove();
                    removed++;
                }
            }
            Debug.echoDebug(scriptEntry, "Removed " + removed + " entities from the world.");
            if (alwaysWarnOnMassRemove) {
                Debug.echoError("Remove command 'Always warn on mass delete' in Denizen config is enabled - mass removal of '" + entity.getEntityType() + "' performed, removing " + removed + " entities.");
            }
        }
    }
}
Also used : FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(org.bukkit.entity.Entity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) WorldTag(com.denizenscript.denizen.objects.WorldTag)

Example 58 with EntityTag

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

the class RotateCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final List<EntityTag> entities = new ArrayList<>((List<EntityTag>) scriptEntry.getObject("entities"));
    final DurationTag duration = scriptEntry.getObjectTag("duration");
    final DurationTag frequency = scriptEntry.getObjectTag("frequency");
    final ElementTag yaw = scriptEntry.getElement("yaw");
    final ElementTag pitch = scriptEntry.getElement("pitch");
    boolean cancel = scriptEntry.hasObject("cancel");
    final boolean infinite = scriptEntry.hasObject("infinite");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), db("entities", entities), (infinite ? db("duration", "infinite") : duration), frequency, yaw, pitch);
    }
    for (EntityTag entity : entities) {
        if (cancel) {
            rotatingEntities.remove(entity.getUUID());
        } else {
            rotatingEntities.add(entity.getUUID());
        }
    }
    if (cancel) {
        return;
    }
    BukkitRunnable task = new BukkitRunnable() {

        int ticks = 0;

        int maxTicks = duration.getTicksAsInt();

        ArrayList<EntityTag> unusedEntities = new ArrayList<>();

        @Override
        public void run() {
            if (entities.isEmpty()) {
                scriptEntry.setFinished(true);
                this.cancel();
            } else if (infinite || ticks < maxTicks) {
                for (EntityTag entity : entities) {
                    if (entity.isSpawned() && rotatingEntities.contains(entity.getUUID())) {
                        NMSHandler.getEntityHelper().rotate(entity.getBukkitEntity(), EntityHelper.normalizeYaw(entity.getLocation().getYaw() + yaw.asFloat()), entity.getLocation().getPitch() + pitch.asFloat());
                    } else {
                        rotatingEntities.remove(entity.getUUID());
                        unusedEntities.add(entity);
                    }
                }
                if (!unusedEntities.isEmpty()) {
                    for (EntityTag unusedEntity : unusedEntities) {
                        entities.remove(unusedEntity);
                    }
                    unusedEntities.clear();
                }
                ticks = (int) (ticks + frequency.getTicks());
            } else {
                scriptEntry.setFinished(true);
                this.cancel();
            }
        }
    };
    task.runTaskTimer(Denizen.getInstance(), 0, frequency.getTicks());
}
Also used : BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 59 with EntityTag

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

the class DisguiseCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    EntityTag entity = scriptEntry.getObjectTag("entity");
    EntityTag as = scriptEntry.argForPrefix("as", EntityTag.class, true);
    boolean cancel = scriptEntry.argAsBoolean("cancel");
    boolean global = scriptEntry.argAsBoolean("global");
    boolean self = scriptEntry.argAsBoolean("self");
    List<PlayerTag> players = scriptEntry.argForPrefixList("players", PlayerTag.class, true);
    if (as == null && !cancel) {
        throw new InvalidArgumentsRuntimeException("Must specify a valid type to disguise as!");
    }
    if (players == null && !global) {
        PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
        if (player != null && player.isOnline()) {
            players = Collections.singletonList(player);
        } else {
            throw new InvalidArgumentsRuntimeException("Must have a valid player attached, or 'global' set!");
        }
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), entity, db("cancel", cancel), as, db("global", global), db("self", self), db("players", players));
    }
    HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
    if (playerMap != null) {
        if (global) {
            for (Map.Entry<UUID, TrackedDisguise> entry : playerMap.entrySet()) {
                entry.getValue().isActive = false;
                if (entry.getKey() == null) {
                    if (entry.getValue().toOthers != null) {
                        FakeEntity.idsToEntities.remove(entry.getValue().toOthers.overrideUUID);
                    }
                    for (Player player : entity.getWorld().getPlayers()) {
                        if (!EntityTag.isNPC(player)) {
                            entry.getValue().removeFor(new PlayerTag(player));
                        }
                    }
                } else {
                    PlayerTag player = new PlayerTag(entry.getKey());
                    entry.getValue().removeFor(player);
                }
            }
            disguises.remove(entity.getUUID());
        } else {
            for (PlayerTag player : players) {
                TrackedDisguise disguise = playerMap.remove(player.getUUID());
                if (disguise != null) {
                    disguise.isActive = false;
                    disguise.removeFor(player);
                    if (disguise.toOthers != null) {
                        FakeEntity.idsToEntities.remove(disguise.toOthers.overrideUUID);
                    }
                    if (playerMap.isEmpty()) {
                        disguises.remove(entity.getUUID());
                    }
                }
            }
        }
    }
    if (!cancel) {
        TrackedDisguise disguise = new TrackedDisguise(entity, as);
        disguise.as.entity = NMSHandler.getPlayerHelper().sendEntitySpawn(new ArrayList<>(), as.getEntityType(), entity.getLocation(), as.mechanisms == null ? null : new ArrayList<>(as.mechanisms), -1, null, false).entity.getBukkitEntity();
        if (global) {
            playerMap = disguises.computeIfAbsent(entity.getUUID(), k -> new HashMap<>());
            playerMap.put(null, disguise);
            disguise.isActive = true;
            ArrayList<PlayerTag> playerSet = players == null ? new ArrayList<>() : new ArrayList<>(players);
            for (Player player : entity.getWorld().getPlayers()) {
                if (!EntityTag.isNPC(player) && !playerSet.contains(new PlayerTag(player)) && (self || !player.getUniqueId().equals(entity.getUUID()))) {
                    playerSet.add(new PlayerTag(player));
                }
            }
            disguise.sendTo(playerSet);
        } else {
            for (PlayerTag player : players) {
                playerMap = disguises.computeIfAbsent(entity.getUUID(), k -> new HashMap<>());
                playerMap.put(player.getUUID(), disguise);
                disguise.isActive = true;
            }
            disguise.sendTo(players);
        }
    }
}
Also used : HandlerList(org.bukkit.event.HandlerList) Utilities(com.denizenscript.denizen.utilities.Utilities) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) java.util(java.util) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) EventHandler(org.bukkit.event.EventHandler) Location(org.bukkit.Location) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EntityType(org.bukkit.entity.EntityType) Denizen(com.denizenscript.denizen.Denizen) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) EventPriority(org.bukkit.event.EventPriority) PlayerTeleportEvent(org.bukkit.event.player.PlayerTeleportEvent) EntityTag(com.denizenscript.denizen.objects.EntityTag) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) EntityTag(com.denizenscript.denizen.objects.EntityTag)

Example 60 with EntityTag

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

the class GlowCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    final ArrayList<EntityTag> entities = (ArrayList<EntityTag>) scriptEntry.getObject("entities");
    ElementTag glowing = scriptEntry.getElement("glowing");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), glowing);
    }
    boolean shouldGlow = glowing.asBoolean();
    final UUID puuid = Utilities.getEntryPlayer(scriptEntry).getUUID();
    for (EntityTag ent : entities) {
        if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(ent.getLivingEntity())) {
            CitizensAPI.getNPCRegistry().getNPC(ent.getLivingEntity()).data().setPersistent(NPC.GLOWING_METADATA, shouldGlow);
        }
        if (shouldGlow) {
            HashSet<UUID> players = glowViewers.computeIfAbsent(ent.getLivingEntity().getEntityId(), k -> new HashSet<>());
            players.add(puuid);
        } else {
            HashSet<UUID> players = glowViewers.get(ent.getLivingEntity().getEntityId());
            if (players != null) {
                players.remove(puuid);
                shouldGlow = !players.isEmpty();
                if (!shouldGlow) {
                    glowViewers.remove(ent.getLivingEntity().getEntityId());
                }
            }
        }
        ent.getLivingEntity().setGlowing(shouldGlow);
    }
}
Also used : ArrayList(java.util.ArrayList) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID)

Aggregations

EntityTag (com.denizenscript.denizen.objects.EntityTag)142 EventHandler (org.bukkit.event.EventHandler)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)56 LocationTag (com.denizenscript.denizen.objects.LocationTag)45 List (java.util.List)27 Entity (org.bukkit.entity.Entity)25 ItemTag (com.denizenscript.denizen.objects.ItemTag)17 ListTag (com.denizenscript.denizencore.objects.core.ListTag)17 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)15 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)12 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)10 ArrayList (java.util.ArrayList)10 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)10 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)9 Player (org.bukkit.entity.Player)9 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)8 Argument (com.denizenscript.denizencore.objects.Argument)8 LivingEntity (org.bukkit.entity.LivingEntity)8 Item (org.bukkit.entity.Item)7