Search in sources :

Example 6 with PlayerTag

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

the class PlayerPreLoginScriptEvent method onPlayerLogin.

@EventHandler
public void onPlayerLogin(AsyncPlayerPreLoginEvent event) {
    if (!Bukkit.isPrimaryThread()) {
        PlayerPreLoginScriptEvent altEvent = (PlayerPreLoginScriptEvent) clone();
        Future future = Bukkit.getScheduler().callSyncMethod(Denizen.getInstance(), () -> {
            altEvent.onPlayerLogin(event);
            return null;
        });
        try {
            future.get(30, TimeUnit.SECONDS);
            while (altEvent.needsToWait()) {
                Thread.sleep(50);
            }
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
        return;
    }
    waitForQueues = new ArrayList<>();
    OfflinePlayer bukkitPlayer = Bukkit.getOfflinePlayer(event.getUniqueId());
    if (bukkitPlayer != null && bukkitPlayer.getName() != null) {
        player = new PlayerTag(bukkitPlayer);
    } else {
        player = null;
    }
    this.event = event;
    fire(event);
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) Future(java.util.concurrent.Future) OfflinePlayer(org.bukkit.OfflinePlayer) EventHandler(org.bukkit.event.EventHandler)

Example 7 with PlayerTag

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

the class BukkitScriptProperties method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <ScriptTag.cooled_down[<player>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the script is currently cooled down for the player. Any global
    // cooldown present on the script will also be taken into account. Not specifying a player will result in
    // using the attached player available in the script entry. Not having a valid player will result in 'null'.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "cooled_down", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        if (player != null && player.isValid()) {
            return new ElementTag(CooldownCommand.checkCooldown(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.cooldown[<player>]>
    // @returns DurationTag
    // @description
    // Returns the time left for the player to cooldown for the script.
    // -->
    PropertyParser.<BukkitScriptProperties, DurationTag>registerTag(DurationTag.class, "cooldown", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        return CooldownCommand.getCooldownDuration(player, script.script.getName());
    });
    // <--[tag]
    // @attribute <ScriptTag.step[(<player>)]>
    // @returns ElementTag
    // @description
    // Returns the name of a script step that the player is currently on.
    // Must be an INTERACT script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "step", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return new ElementTag(InteractScriptHelper.getCurrentStep(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.step_expiration[(<player>)]>
    // @returns TimeTag
    // @description
    // Returns the time that an interact script step expires at, if applied by <@link command zap> with a duration limit.
    // -->
    PropertyParser.<BukkitScriptProperties, TimeTag>registerTag(TimeTag.class, "step_expiration", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return InteractScriptHelper.getStepExpiration(player, script.script.getContainer().getName());
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.default_step>
    // @returns ElementTag
    // @description
    // Returns the name of the default step of an interact script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerStaticTag(ElementTag.class, "default_step", (attribute, script) -> {
        String step = ((InteractScriptContainer) script.script.getContainer()).getDefaultStepName();
        return new ElementTag(step);
    });
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 8 with PlayerTag

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

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

the class AnimateCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for");
    PlayerAnimation animation = scriptEntry.hasObject("animation") ? (PlayerAnimation) scriptEntry.getObject("animation") : null;
    EntityEffect effect = scriptEntry.hasObject("effect") ? (EntityEffect) scriptEntry.getObject("effect") : null;
    String nmsAnimation = scriptEntry.hasObject("nms_animation") ? (String) scriptEntry.getObject("nms_animation") : null;
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (animation != null ? db("animation", animation.name()) : effect != null ? db("effect", effect.name()) : db("animation", nmsAnimation)), db("entities", entities), db("for", forPlayers));
    }
    for (EntityTag entity : entities) {
        if (entity.isSpawned()) {
            try {
                if (animation != null && entity.getBukkitEntity() instanceof Player) {
                    Player player = (Player) entity.getBukkitEntity();
                    animation.play(player);
                } else if (effect != null) {
                    if (forPlayers != null) {
                        for (PlayerTag player : forPlayers) {
                            NMSHandler.getPacketHelper().sendEntityEffect(player.getPlayerEntity(), entity.getBukkitEntity(), effect.getData());
                        }
                    } else {
                        entity.getBukkitEntity().playEffect(effect);
                    }
                } else if (nmsAnimation != null) {
                    EntityAnimation entityAnimation = NMSHandler.getAnimationHelper().getEntityAnimation(nmsAnimation);
                    entityAnimation.play(entity.getBukkitEntity());
                } else {
                    Debug.echoError("No way to play the given animation on entity '" + entity + "'");
                }
            } catch (Exception e) {
                Debug.echoError(scriptEntry, "Error playing that animation!");
                Debug.echoError(e);
            }
        }
    }
}
Also used : EntityEffect(org.bukkit.EntityEffect) EntityAnimation(com.denizenscript.denizen.nms.interfaces.EntityAnimation) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) PlayerAnimation(net.citizensnpcs.util.PlayerAnimation) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 10 with PlayerTag

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

Aggregations

PlayerTag (com.denizenscript.denizen.objects.PlayerTag)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)41 Player (org.bukkit.entity.Player)26 List (java.util.List)23 NPCTag (com.denizenscript.denizen.objects.NPCTag)22 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 ListTag (com.denizenscript.denizencore.objects.core.ListTag)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)12 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)11 EventHandler (org.bukkit.event.EventHandler)11 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)8 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)7 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)6 Argument (com.denizenscript.denizencore.objects.Argument)6