Search in sources :

Example 1 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class BukkitScriptEvent method getTagContext.

public BukkitTagContext getTagContext(ScriptPath path) {
    BukkitTagContext context = (BukkitTagContext) getScriptEntryData().getTagContext().clone();
    context.script = new ScriptTag(path.container);
    context.debug = path.container.shouldDebug();
    return context;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Example 2 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class PlayerConsumesScriptEvent method applyDetermination.

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
    String determination = determinationObj.toString();
    if (ItemTag.matches(determination)) {
        BukkitTagContext context = new BukkitTagContext(EntityTag.getPlayerFrom(event.getPlayer()), null, new ScriptTag(path.container));
        ItemTag newitem = ItemTag.valueOf(determination, context);
        if (newitem != null) {
            event.setItem(newitem.getItemStack());
            return true;
        } else {
            Debug.echoError("Invalid event 'item' check [" + getName() + "] ('determine item ????'): '" + determination + "' for " + path.container.getName());
        }
    }
    return super.applyDetermination(path, determinationObj);
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 3 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talk.

@Override
public void talk(SpeechContext speechContext) {
    if (!(speechContext instanceof DenizenSpeechContext)) {
        return;
    }
    DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
    Talkable talker = context.getTalker();
    if (talker == null) {
        return;
    }
    ScriptEntry entry = context.getScriptEntry();
    ScriptQueue queue = entry.getResidingQueue();
    String defTalker = null;
    if (queue.hasDefinition("talker")) {
        defTalker = queue.getDefinition("talker");
    }
    queue.addDefinition("talker", new EntityTag(talker.getEntity()).identify());
    String defMessage = null;
    if (queue.hasDefinition("message")) {
        defMessage = queue.getDefinition("message");
    }
    queue.addDefinition("message", context.getMessage());
    // Chat to the world using Denizen chat settings
    if (!context.hasRecipients()) {
        String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry));
        talkToBystanders(talker, text, context);
    } else // Single recipient
    if (context.size() <= 1) {
        // Send chat to target
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        // Check if bystanders hear targeted chat
        if (context.isBystandersEnabled()) {
            String defTarget = null;
            if (queue.hasDefinition("target")) {
                defTarget = queue.getDefinition("target");
            }
            queue.addDefinition("target", new EntityTag(context.iterator().next().getEntity()).identify());
            String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTarget != null) {
                queue.addDefinition("target", defTarget);
            }
        }
    } else // Multiple recipients
    {
        // Send chat to targets
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        if (context.isBystandersEnabled()) {
            String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
            if (format.length <= 1) {
                Debug.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
            }
            StringBuilder parsed = new StringBuilder();
            Iterator<Talkable> iter = context.iterator();
            int i = 0;
            while (iter.hasNext()) {
                if (i == format.length - 1) {
                    parsed.append(format[i]);
                    break;
                }
                parsed.append(format[i]).append(new EntityTag(iter.next().getEntity()).getName());
                i++;
            }
            String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry));
            String defTargets = null;
            if (queue.hasDefinition("targets")) {
                defTargets = queue.getDefinition("targets");
            }
            queue.addDefinition("targets", targets);
            String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTargets != null) {
                queue.addDefinition("targets", defTargets);
            }
        }
    }
    if (defMessage != null) {
        queue.addDefinition("message", defMessage);
    }
    if (defTalker != null) {
        queue.addDefinition("talker", defTalker);
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) Iterator(java.util.Iterator) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) EntityTag(com.denizenscript.denizen.objects.EntityTag) Talkable(net.citizensnpcs.api.ai.speech.Talkable) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 4 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext 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 5 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class ActionBarCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    String text = scriptEntry.getElement("text").asString();
    ScriptTag formatObj = scriptEntry.getObjectTag("format");
    ElementTag perPlayerObj = scriptEntry.getElement("per_player");
    boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
    BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
    if (!perPlayer) {
        text = TagManager.tag(text, context);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("message", text), db("targets", targets), formatObj, perPlayerObj);
    }
    FormatScriptContainer format = formatObj == null ? null : (FormatScriptContainer) formatObj.getContainer();
    for (PlayerTag player : targets) {
        if (player != null) {
            if (!player.isOnline()) {
                Debug.echoDebug(scriptEntry, "Player is offline, can't send actionbar to them. Skipping.");
                continue;
            }
            String personalText = text;
            if (perPlayer) {
                context.player = player;
                personalText = TagManager.tag(personalText, context);
            }
            player.getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE));
        } else {
            Debug.echoError("Sent actionbar to non-existent player!?");
        }
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) FormatScriptContainer(com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Aggregations

BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)28 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)16 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)9 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)5 TagContext (com.denizenscript.denizencore.tags.TagContext)5 Player (org.bukkit.entity.Player)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 ItemTag (com.denizenscript.denizen.objects.ItemTag)4 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)4 List (java.util.List)4 EventHandler (org.bukkit.event.EventHandler)4 FormatScriptContainer (com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer)3 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)3 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)3 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)3 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)2 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2