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());
}
}
}
}
}
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;
}
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);
}
}
}
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);
}
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();
}
Aggregations