Search in sources :

Example 51 with EntityTag

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

the class FakeEquipCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    EquipmentOverride equipment = (EquipmentOverride) scriptEntry.getObject("equipment");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    List<PlayerTag> playersFor = (List<PlayerTag>) scriptEntry.getObject("for");
    ElementTag reset = scriptEntry.getElement("reset");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment), reset, duration, db("for", playersFor));
    }
    boolean isReset = reset != null && reset.asBoolean();
    for (PlayerTag player : playersFor) {
        HashMap<UUID, EquipmentOverride> playersMap = overrides.computeIfAbsent(player.getUUID(), (k) -> new HashMap<>());
        for (EntityTag entity : entities) {
            if (entity.isGeneric()) {
                Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
                continue;
            }
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity == null) {
                Debug.echoError(scriptEntry, "Cannot equip invalid/non-living entity " + entity.identify() + "!");
                continue;
            }
            EquipmentOverride entityData;
            if (isReset) {
                entityData = playersMap.remove(entity.getUUID());
                if (playersMap.isEmpty()) {
                    overrides.remove(player.getUUID());
                }
            } else {
                entityData = playersMap.computeIfAbsent(entity.getUUID(), (k) -> new EquipmentOverride());
                entityData.copyFrom(equipment);
            }
            if (entityData != null) {
                if (entityData.cancelTask != null && !entityData.cancelTask.isCancelled()) {
                    entityData.cancelTask.cancel();
                    entityData.cancelTask = null;
                }
                if (duration != null && duration.getTicks() > 0) {
                    entityData.cancelTask = new BukkitRunnable() {

                        @Override
                        public void run() {
                            entityData.cancelTask = null;
                            HashMap<UUID, EquipmentOverride> playersMap = overrides.get(player.getUUID());
                            if (playersMap != null) {
                                if (playersMap.remove(entity.getUUID()) != null) {
                                    if (playersMap.isEmpty()) {
                                        overrides.remove(player.getUUID());
                                    }
                                    NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
                                }
                            }
                        }
                    }.runTaskLater(Denizen.getInstance(), duration.getTicks());
                }
            }
            NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) Utilities(com.denizenscript.denizen.utilities.Utilities) java.util(java.util) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) ItemTag(com.denizenscript.denizen.objects.ItemTag) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) LivingEntity(org.bukkit.entity.LivingEntity) Argument(com.denizenscript.denizencore.objects.Argument) Denizen(com.denizenscript.denizen.Denizen) BukkitTask(org.bukkit.scheduler.BukkitTask) EntityTag(com.denizenscript.denizen.objects.EntityTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 52 with EntityTag

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

the class FlyCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    LocationTag origin = scriptEntry.getObjectTag("origin");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final List<LocationTag> destinations = scriptEntry.hasObject("destinations") ? (List<LocationTag>) scriptEntry.getObject("destinations") : new ArrayList<>();
    // Set freeflight to true only if there are no destinations
    final boolean freeflight = destinations.size() < 1;
    EntityTag controller = scriptEntry.getObjectTag("controller");
    // If freeflight is on, we need to do some checks
    if (freeflight) {
        // flying entities, so try to find a player in the entity list
        if (controller == null) {
            for (EntityTag entity : entities) {
                if (entity.isPlayer()) {
                    // the controller
                    if (entities.get(entities.size() - 1) != entity) {
                        controller = entity;
                        if (scriptEntry.dbCallShouldDebug()) {
                            Debug.echoDebug(scriptEntry, "Flight control defaulting to " + controller);
                        }
                        break;
                    }
                }
            }
            // If the controller is still null, we cannot continue
            if (controller == null) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "There is no one to control the flight's path!");
                }
                return;
            }
        } else // Else, if the controller was set, we need to make sure
        // it is among the flying entities, and add it if it is not
        {
            boolean found = false;
            for (EntityTag entity : entities) {
                if (entity.identify().equals(controller.identify())) {
                    found = true;
                    break;
                }
            }
            // Add the controller to the entity list
            if (!found) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "Adding controller " + controller + " to flying entities.");
                }
                entities.add(0, controller);
            }
        }
    }
    final double speed = ((ElementTag) scriptEntry.getObject("speed")).asDouble();
    final float rotationThreshold = ((ElementTag) scriptEntry.getObject("rotation_threshold")).asFloat();
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), origin, db("entities", entities), db("speed", speed), db("rotation threshold degrees", rotationThreshold), (freeflight ? controller : db("destinations", destinations)));
    }
    if (!cancel) {
        for (EntityTag entity : entities) {
            entity.spawnAt(origin);
        }
        Position.mount(Conversion.convertEntities(entities));
    } else {
        Position.dismount(Conversion.convertEntities(entities));
        return;
    }
    final Entity entity = entities.get(entities.size() - 1).getBukkitEntity();
    final LivingEntity finalController = controller != null ? controller.getLivingEntity() : null;
    BukkitRunnable task = new BukkitRunnable() {

        Location location = null;

        Boolean flying = true;

        public void run() {
            if (freeflight) {
                // flying where the controller is looking
                if (!entity.isEmpty() && finalController.isInsideVehicle()) {
                    location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
                } else {
                    flying = false;
                }
            } else {
                // as there are destinations left
                if (destinations.size() > 0) {
                    location = destinations.get(0);
                } else {
                    flying = false;
                }
            }
            if (flying && entity.isValid()) {
                // when it really needs to
                if (!NMSHandler.getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
                    NMSHandler.getEntityHelper().faceLocation(entity, location);
                }
                Vector v1 = entity.getLocation().toVector();
                Vector v2 = location.toVector();
                Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
                entity.setVelocity(v3);
                // to be the case
                if (!freeflight) {
                    if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2 && Math.abs(v2.getZ() - v1.getZ()) < 2) {
                        destinations.remove(0);
                    }
                }
            } else {
                flying = false;
                this.cancel();
            }
        }
    };
    task.runTaskTimer(Denizen.getInstance(), 0, 3);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) LocationTag(com.denizenscript.denizen.objects.LocationTag) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 53 with EntityTag

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

the class HealthCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag quantity = scriptEntry.getElement("quantity");
    ElementTag action = scriptEntry.getElement("action");
    ElementTag heal = scriptEntry.getElement("heal");
    List<EntityTag> targets = (List<EntityTag>) scriptEntry.getObject("target");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), quantity, action, heal, db("target", targets));
    }
    if (quantity == null && action == null) {
        Debug.echoError(scriptEntry, "Null quantity!");
    }
    if (action == null) {
        action = new ElementTag(true);
    }
    for (EntityTag target : targets) {
        if (target.isCitizensNPC()) {
            if (action.asString().equalsIgnoreCase("true")) {
                target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
            } else if (action.asString().equalsIgnoreCase("false")) {
                target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
            } else if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
                target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
            } else {
                target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
            }
        }
        if (quantity != null) {
            if (target.isCitizensNPC()) {
                if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
                    HealthTrait trait = target.getDenizenNPC().getCitizen().getOrAddTrait(HealthTrait.class);
                    trait.setMaxhealth(quantity.asInt());
                    if (heal.asBoolean()) {
                        trait.setHealth(quantity.asDouble());
                    }
                } else {
                    Debug.echoError(scriptEntry, "NPC doesn't have health trait!");
                }
            } else if (target.isLivingEntity()) {
                target.getLivingEntity().setMaxHealth(quantity.asDouble());
                if (heal.asBoolean()) {
                    target.getLivingEntity().setHealth(quantity.asDouble());
                }
            } else {
                Debug.echoError(scriptEntry, "Entity '" + target.identify() + "'is not alive!");
            }
        }
    }
}
Also used : EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) HealthTrait(com.denizenscript.denizen.npc.traits.HealthTrait) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 54 with EntityTag

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

the class LeashCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    EntityTag holder = null;
    LocationTag holderLoc = null;
    Entity Holder = null;
    Object holderObject = scriptEntry.getObject("holder");
    if (holderObject instanceof EntityTag) {
        holder = scriptEntry.getObjectTag("holder");
        Holder = holder.getBukkitEntity();
    } else if (holderObject instanceof LocationTag) {
        holderLoc = scriptEntry.getObjectTag("holder");
        Material material = holderLoc.getBlock().getType();
        if (material.name().endsWith("_FENCE")) {
            Holder = holderLoc.getWorld().spawn(holderLoc, LeashHitch.class);
        } else {
            Debug.echoError(scriptEntry, "Bad holder location specified - only fences are permitted!");
            return;
        }
    }
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", "true") : ""), db("entities", entities), db("holder", holder), db("holder", holderLoc));
    }
    for (EntityTag entity : entities) {
        if (entity.isSpawned() && entity.isLivingEntity()) {
            if (cancel) {
                entity.getLivingEntity().setLeashHolder(null);
            } else {
                entity.getLivingEntity().setLeashHolder(Holder);
            }
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Entity(org.bukkit.entity.Entity) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) Material(org.bukkit.Material)

Example 55 with EntityTag

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

the class SneakCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag fake = scriptEntry.getElement("fake");
    ElementTag stopfake = scriptEntry.getElement("stopfake");
    ElementTag mode = scriptEntry.getElement("mode");
    List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for_players");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), mode, db("entities", entities), db("for_players", forPlayers), fake, stopfake);
    }
    boolean shouldSneak = mode.asString().equalsIgnoreCase("start");
    boolean shouldFake = fake != null && fake.asBoolean();
    boolean shouldStopFake = stopfake != null && stopfake.asBoolean();
    for (EntityTag entity : entities) {
        if (shouldFake || shouldStopFake) {
            if (forPlayers == null) {
                updateFakeSneak(entity.getUUID(), null, shouldSneak, shouldFake);
                for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(entity.getBukkitEntity())) {
                    NMSHandler.getPacketHelper().sendEntityMetadataFlagsUpdate(player, entity.getBukkitEntity());
                }
            } else {
                for (PlayerTag player : forPlayers) {
                    updateFakeSneak(entity.getUUID(), player.getUUID(), shouldSneak, shouldFake);
                    NMSHandler.getPacketHelper().sendEntityMetadataFlagsUpdate(player.getPlayerEntity(), entity.getBukkitEntity());
                }
            }
        } else if (entity.isCitizensNPC()) {
            SneakingTrait trait = entity.getDenizenNPC().getCitizen().getOrAddTrait(SneakingTrait.class);
            if (shouldSneak) {
                trait.sneak();
            } else {
                trait.stand();
            }
        } else if (entity.isSpawned()) {
            NMSHandler.getEntityHelper().setSneaking(entity.getBukkitEntity(), shouldSneak);
        } else {
            Debug.echoError("Cannot make unspawned entity sneak.");
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) SneakingTrait(com.denizenscript.denizen.npc.traits.SneakingTrait) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

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