Search in sources :

Example 56 with PlayerTag

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

the class ScoreboardCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<PlayerTag> viewers = (List<PlayerTag>) scriptEntry.getObject("viewers");
    ListTag lines = scriptEntry.hasObject("lines") ? ListTag.valueOf(scriptEntry.getElement("lines").asString(), scriptEntry.getContext()) : new ListTag();
    ElementTag action = scriptEntry.getElement("action");
    ElementTag id = scriptEntry.getElement("id");
    ElementTag objective = scriptEntry.getElement("objective");
    ElementTag criteria = scriptEntry.getElement("criteria");
    ElementTag score = scriptEntry.getElement("score");
    ElementTag displaySlot = scriptEntry.getElement("displayslot");
    ElementTag displayName = scriptEntry.getElement("displayname");
    ElementTag renderType = scriptEntry.getElement("rendertype");
    Action act = Action.valueOf(action.asString().toUpperCase());
    boolean hadCriteria = criteria != null;
    boolean hadDisplaySlot = displaySlot != null;
    if (!hadCriteria) {
        criteria = new ElementTag("dummy");
    }
    if (!hadDisplaySlot) {
        displaySlot = new ElementTag("sidebar");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, id, db("viewers", viewers), objective, lines, score, objective, displaySlot, criteria, displayName, renderType);
    }
    Scoreboard board = null;
    // Get the main scoreboard by default
    if (id.asString().equalsIgnoreCase("main")) {
        board = ScoreboardHelper.getMain();
    } else if (id.asString().equalsIgnoreCase("player")) {
        board = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().getScoreboard();
    } else {
        // If this scoreboard already exists, get it
        if (ScoreboardHelper.hasScoreboard(id.asString())) {
            board = ScoreboardHelper.getScoreboard(id.asString());
        } else // Else, create a new one if Action is ADD
        if (act.equals(Action.ADD)) {
            board = ScoreboardHelper.createScoreboard(id.asString());
        }
    }
    // Don't progress if we ended up with a null board
    if (board == null) {
        Debug.echoError(scriptEntry, "Scoreboard " + id.asString() + " does not exist!");
        return;
    }
    Objective obj;
    if (act.equals(Action.ADD)) {
        if (objective != null) {
            // Try getting the objective from the board
            obj = board.getObjective(objective.asString());
            boolean existedAlready = obj != null;
            // Create the objective if it does not already exist
            if (obj == null) {
                obj = board.registerNewObjective(objective.asString(), criteria.asString());
            } else // recreate the objective
            if (hadCriteria && !obj.getCriteria().equals(criteria.asString())) {
                obj.unregister();
                obj = board.registerNewObjective(objective.asString(), criteria.asString());
            }
            // Change the objective's display slot
            if ((!existedAlready || hadDisplaySlot) && !displaySlot.asString().equalsIgnoreCase("none")) {
                obj.setDisplaySlot(DisplaySlot.valueOf(displaySlot.asString().toUpperCase()));
            }
            if (renderType != null) {
                obj.setRenderType(RenderType.valueOf(renderType.asString().toUpperCase()));
            }
            if (displayName != null) {
                obj.setDisplayName(displayName.asString());
            } else if (!existedAlready) {
                obj.setDisplayName(objective.asString());
            }
            if (!lines.isEmpty()) {
                // use a score of 0
                if (score == null) {
                    score = new ElementTag(0);
                }
                for (ObjectTag line : lines.objectForms) {
                    ScoreboardHelper.addScore(obj, checkLine(line), score.asInt());
                }
            }
        } else // the command cannot do anything at all, so print a message about that
        if (viewers == null && !lines.isEmpty()) {
            Debug.echoDebug(scriptEntry, "Cannot add lines without specifying an objective!");
        }
    } else if (act.equals(Action.REMOVE)) {
        if (objective != null) {
            // Try getting the objective from the board
            obj = board.getObjective(objective.asString());
            if (obj != null) {
                // Remove the entire objective if no lines have been specified
                if (lines.isEmpty()) {
                    Debug.echoDebug(scriptEntry, "Removing objective " + obj.getName() + " from scoreboard " + id.asString());
                    obj.unregister();
                } else {
                    for (ObjectTag line : lines.objectForms) {
                        ScoreboardHelper.removeScore(obj, checkLine(line));
                    }
                }
            } else {
                Debug.echoError(scriptEntry, "Objective " + objective.asString() + " does not exist in scoreboard " + id.asString());
            }
        } else // lines from every objective
        if (!lines.isEmpty()) {
            Debug.echoDebug(scriptEntry, "Removing lines " + lines.identify() + " from all objectives in scoreboard " + id.asString());
            for (ObjectTag line : lines.objectForms) {
                ScoreboardHelper.removePlayer(id.asString(), checkLine(line));
            }
        } else // of viewers should be removed instead)
        if (viewers == null) {
            Debug.echoDebug(scriptEntry, "Removing scoreboard " + id.asString());
            ScoreboardHelper.deleteScoreboard(id.asString());
        }
    }
    if (viewers != null) {
        for (PlayerTag viewer : viewers) {
            // Add viewers for this scoreboard
            if (act.equals(Action.ADD)) {
                // to the map of viewers saved by Denizen
                if (!id.asString().equalsIgnoreCase("main")) {
                    ScoreboardHelper.viewerMap.put(viewer.getUUID(), id.asString());
                }
                // is already online
                if (viewer.isOnline()) {
                    viewer.getPlayerEntity().setScoreboard(board);
                }
            } else // Remove viewers for this scoreboard
            if (act.equals(Action.REMOVE)) {
                // Take this player out of the map of viewers
                ScoreboardHelper.viewerMap.remove(viewer.getUUID());
                // provided by Bukkit)
                if (viewer.isOnline()) {
                    viewer.getPlayerEntity().setScoreboard(ScoreboardHelper.createScoreboard());
                }
            }
        }
    }
}
Also used : Objective(org.bukkit.scoreboard.Objective) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) Scoreboard(org.bukkit.scoreboard.Scoreboard) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 57 with PlayerTag

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

the class ChatTrigger method process.

// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// chat
// 
// @Triggers when a player chats to the NPC.
// 
// @Context
// <context.message> returns the triggering message
// <context.keyword> returns the keyword matched by a RegEx trigger
// 
// @Determine
// "CANCELLED" to stop the player from chatting.
// ElementTag to change the message.
// 
// -->
public ChatContext process(Player player, String message) {
    NPCTag npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
    if (Debug.verbose) {
        Debug.log("Processing chat trigger: valid npc? " + (npc != null));
    }
    if (npc == null) {
        return new ChatContext(false);
    }
    if (Debug.verbose) {
        Debug.log("Has trait?  " + npc.getCitizen().hasTrait(TriggerTrait.class));
    }
    if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
        return new ChatContext(false);
    }
    if (Debug.verbose) {
        Debug.log("enabled? " + npc.getCitizen().getOrAddTrait(TriggerTrait.class).isEnabled(name));
    }
    if (!npc.getCitizen().getOrAddTrait(TriggerTrait.class).isEnabled(name)) {
        return new ChatContext(false);
    }
    if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(player.getLocation())) {
        if (Debug.verbose) {
            Debug.log("Not in range");
        }
        return new ChatContext(false);
    }
    if (Settings.chatMustSeeNPC()) {
        if (!player.hasLineOfSight(npc.getEntity())) {
            if (Debug.verbose) {
                Debug.log("no LOS");
            }
            return new ChatContext(false);
        }
    }
    if (Settings.chatMustLookAtNPC()) {
        if (!NMSHandler.getEntityHelper().isFacingEntity(player, npc.getEntity(), 45)) {
            if (Debug.verbose) {
                Debug.log("Not facing");
            }
            return new ChatContext(false);
        }
    }
    boolean ret = false;
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("message", new ElementTag(message));
    TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(ChatTrigger.this, new PlayerTag(player), context);
    if (trigger.hasDetermination()) {
        if (trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
            if (Debug.verbose) {
                Debug.log("Cancelled");
            }
            return new ChatContext(true);
        }
    }
    if (!trigger.wasTriggered()) {
        if (Settings.chatGloballyIfUninteractable()) {
            if (Debug.verbose) {
                Debug.log(ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE + "The NPC is currently cooling down or engaged.");
            }
            return new ChatContext(false);
        } else {
            ret = true;
        }
    }
    if (trigger.hasDetermination()) {
        message = trigger.getDeterminations().get(0);
    }
    List<InteractScriptContainer> scripts = npc.getInteractScripts(new PlayerTag(player), ChatTrigger.class);
    if (scripts == null) {
        if (Debug.verbose) {
            Debug.log("null scripts");
        }
        return new ChatContext(message, false);
    }
    ChatContext returnable = new ChatContext(ret);
    for (InteractScriptContainer script : scripts) {
        processSingle(message, player, npc, context, script, returnable);
    }
    return returnable;
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 58 with PlayerTag

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

the class PlaySoundCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("entities");
    ElementTag soundElement = scriptEntry.getElement("sound");
    ElementTag volumeElement = scriptEntry.argForPrefixAsElement("volume", "1");
    ElementTag pitchElement = scriptEntry.argForPrefixAsElement("pitch", "1");
    boolean custom = scriptEntry.argAsBoolean("custom");
    ElementTag sound_category = scriptEntry.argForPrefixAsElement("sound_category", "MASTER");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("locations", locations), db("entities", players), soundElement, volumeElement, pitchElement, db("custom", custom));
    }
    String sound = soundElement.asString();
    float volume = volumeElement.asFloat();
    float pitch = pitchElement.asFloat();
    String category = sound_category.asString().toUpperCase();
    try {
        if (players == null) {
            if (custom) {
                for (LocationTag location : locations) {
                    NMSHandler.getSoundHelper().playSound(null, location, sound, volume, pitch, category);
                }
            } else {
                for (LocationTag location : locations) {
                    NMSHandler.getSoundHelper().playSound(null, location, Sound.valueOf(sound.toUpperCase()), volume, pitch, category);
                }
            }
        } else if (locations != null) {
            for (LocationTag location : locations) {
                for (PlayerTag player : players) {
                    if (custom) {
                        NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), location, sound, volume, pitch, category);
                    } else {
                        NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), location, Sound.valueOf(sound.toUpperCase()), volume, pitch, category);
                    }
                }
            }
        } else {
            for (PlayerTag player : players) {
                if (custom) {
                    NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), player.getLocation(), sound, volume, pitch, category);
                } else {
                    NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), player.getLocation(), Sound.valueOf(sound.toUpperCase()), volume, pitch, category);
                }
            }
        }
    } catch (Exception e) {
        Debug.echoDebug(scriptEntry, "Unable to play sound.");
        if (Debug.verbose) {
            Debug.echoError(e);
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 59 with PlayerTag

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

the class ProximityTrigger method onEnable.

@Override
public void onEnable() {
    Bukkit.getServer().getPluginManager().registerEvents(this, Denizen.getInstance());
    final ProximityTrigger trigger = this;
    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), () -> {
        Collection<? extends Player> allPlayers = Bukkit.getOnlinePlayers();
        for (NPC citizensNPC : CitizensAPI.getNPCRegistry()) {
            if (citizensNPC == null || !citizensNPC.isSpawned()) {
                continue;
            }
            if (!citizensNPC.hasTrait(TriggerTrait.class) || !citizensNPC.getOrAddTrait(TriggerTrait.class).isEnabled(name)) {
                continue;
            }
            NPCTag npc = new NPCTag(citizensNPC);
            TriggerTrait triggerTrait = npc.getTriggerTrait();
            for (Player bukkitPlayer : allPlayers) {
                if (!npc.getWorld().equals(bukkitPlayer.getWorld()) && hasExitedProximityOf(bukkitPlayer, npc)) {
                    continue;
                }
                if (!isCloseEnough(bukkitPlayer, npc) && hasExitedProximityOf(bukkitPlayer, npc)) {
                    continue;
                }
                PlayerTag player = PlayerTag.mirrorBukkitPlayer(bukkitPlayer);
                double entryRadius = triggerTrait.getRadius(name);
                double exitRadius = triggerTrait.getRadius(name);
                double moveRadius = triggerTrait.getRadius(name);
                Location npcLocation = npc.getLocation();
                boolean playerChangedWorlds = false;
                if (npcLocation.getWorld() != player.getWorld()) {
                    playerChangedWorlds = true;
                }
                boolean exitedProximity = hasExitedProximityOf(bukkitPlayer, npc);
                double distance = 0;
                if (!playerChangedWorlds) {
                    distance = npcLocation.distance(player.getLocation());
                }
                if (!exitedProximity && (playerChangedWorlds || distance >= exitRadius)) {
                    if (!triggerTrait.triggerCooldownOnly(trigger, player)) {
                        continue;
                    }
                    exitProximityOf(bukkitPlayer, npc);
                    npc.action("exit proximity", player);
                    parseAll(npc, player, "EXIT");
                } else if (exitedProximity && distance <= entryRadius) {
                    if (!triggerTrait.triggerCooldownOnly(trigger, player)) {
                        continue;
                    }
                    enterProximityOf(bukkitPlayer, npc);
                    npc.action("enter proximity", player);
                    parseAll(npc, player, "ENTRY");
                } else if (!exitedProximity && distance <= moveRadius) {
                    npc.action("move proximity", player);
                    parseAll(npc, player, "MOVE");
                }
            }
        }
    }, 5, 5);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Player(org.bukkit.entity.Player) TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) Location(org.bukkit.Location)

Example 60 with PlayerTag

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

the class PlayerHoldsItemEvent method run.

public void run(Player pl) {
    cancelled = false;
    player = new PlayerTag(pl);
    if (DenizenPacketHandler.raisableItems.contains(player.getHeldItem().getBukkitMaterial()) || !DenizenPacketHandler.raisableItems.contains(player.getOffhandItem().getBukkitMaterial())) {
        raised = player.getHeldItem();
    } else {
        raised = player.getOffhandItem();
    }
    fire();
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag)

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