use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class ScoreboardHelper method _recallScoreboards.
/*
* Called on server startup or /denizen reload saves
*/
public static void _recallScoreboards() {
// Clear every existing ingame scoreboard
for (Map.Entry<String, Scoreboard> entry : scoreboardMap.entrySet()) {
clearScoreboard(entry.getValue());
}
Scoreboard emptyBoard = createScoreboard();
// Clear every viewer's set scoreboard
for (Map.Entry<UUID, String> entry : viewerMap.entrySet()) {
OfflinePlayer player = Bukkit.getPlayer(entry.getKey());
if (player != null && player.isOnline()) {
player.getPlayer().setScoreboard(emptyBoard);
}
}
// Delete the contents of the scoreboardMap and viewerMap
scoreboardMap.clear();
viewerMap.clear();
ConfigurationSection rootSection = Denizen.getInstance().getScoreboards().getConfigurationSection("Scoreboards");
// Go no further if we have no scoreboards saved
if (rootSection == null) {
return;
}
Scoreboard board;
// Iterate through scoreboards
for (String id : rootSection.getKeys(false)) {
// Create a scoreboard with this id
board = createScoreboard(id);
// Get the list of viewers
List<String> viewerList = rootSection.getStringList(id + ".Viewers");
// and make them see this scoreboard if they are online
for (String viewer : viewerList) {
if (PlayerTag.matches(viewer)) {
PlayerTag player = PlayerTag.valueOf(viewer, CoreUtilities.basicContext);
viewerMap.put(player.getUUID(), id);
if (player.isOnline()) {
player.getPlayerEntity().setScoreboard(board);
}
}
}
ConfigurationSection objSection = rootSection.getConfigurationSection(id + ".Objectives");
// Go no further if we have no objectives saved
if (objSection == null) {
return;
}
// Iterate through objectives
for (String obj : objSection.getKeys(false)) {
// Get display slot and criteria for this objective
String displaySlot = objSection.getString(obj + ".Display slot");
String criteria = objSection.getString(obj + ".Criteria");
// Use default criteria if necessary
if (criteria == null) {
criteria = "dummy";
}
// Use default display slot if necessary
if (displaySlot == null) {
displaySlot = "NONE";
}
// Register the objective and set it up
Objective o = board.registerNewObjective(obj, criteria);
o.setDisplayName(obj);
// Only set display slot if it's valid
if (Argument.valueOf(displaySlot).matchesEnum(DisplaySlot.class)) {
o.setDisplaySlot(DisplaySlot.valueOf(displaySlot.toUpperCase()));
}
ConfigurationSection scoreSection = objSection.getConfigurationSection(obj + ".Scores");
if (scoreSection != null) {
// Iterate through scores and add them to this objective
for (String scoreName : scoreSection.getKeys(false)) {
int scoreInt = scoreSection.getInt(scoreName);
addScore(o, scoreName, scoreInt);
}
}
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method onItemSmithing.
@EventHandler(priority = EventPriority.LOW)
public void onItemSmithing(PrepareSmithingEvent event) {
if (!isItemscript(event.getInventory().getItem(0))) {
return;
}
Recipe recipe = event.getInventory().getRecipe();
SmithingRecipe smithRecipe = (SmithingRecipe) recipe;
if (smithRecipe == null || !(smithRecipe.getKey().getNamespace().equals("denizen"))) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
ItemScriptContainer realResult = recipeIdToItemScript.get(smithRecipe.getKey().toString());
if (realResult == null) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
String[] retain = smithingRetain.get(smithRecipe.getKey().getKey());
if (retain == null) {
Debug.echoError("Smithing recipe mis-registered for script item: " + realResult.getName());
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
PlayerTag player = null;
if (!event.getInventory().getViewers().isEmpty()) {
HumanEntity human = event.getInventory().getViewers().get(0);
if (!EntityTag.isNPC(human) && human instanceof Player) {
player = new PlayerTag((Player) human);
}
}
ItemTag got = realResult.getItemFrom(new BukkitTagContext(player, null, new ScriptTag(realResult)));
if (got == null) {
return;
}
if (retain.length > 0) {
ItemMeta originalMeta = event.getInventory().getItem(0).getItemMeta();
ItemMeta newMeta = got.getItemMeta();
for (String retainable : retain) {
switch(retainable) {
case "display":
newMeta.setDisplayName(originalMeta.hasDisplayName() ? originalMeta.getDisplayName() : null);
break;
case "enchantments":
if (newMeta.hasEnchants()) {
for (Enchantment enchant : new ArrayList<>(newMeta.getEnchants().keySet())) {
newMeta.removeEnchant(enchant);
}
}
if (originalMeta.hasEnchants()) {
for (Map.Entry<Enchantment, Integer> enchant : originalMeta.getEnchants().entrySet()) {
newMeta.addEnchant(enchant.getKey(), enchant.getValue(), true);
}
}
break;
}
}
got.setItemMeta(newMeta);
}
event.setResult(got.getItemStack());
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class ClickTrigger method clickTrigger.
// <--[language]
// @name Click Triggers
// @group NPC Interact Scripts
// @description
// Click Triggers are triggered when a player right clicks the NPC.
//
// These are very basic with no extraneous complexity.
//
// <code>
// click trigger:
// script:
// - narrate "hi <player.name>"
// </code>
//
// They can optionally have an item matcher with multiple triggers, for the item in the player's hand. For example:
// <code>
// click trigger:
// 1:
// trigger: my_item_script
// script:
// - narrate "Nice item script"
// 2:
// trigger: stone
// script:
// - narrate "Nice vanilla item"
// 3:
// script:
// - narrate "I don't recognize your held item"
// </code>
//
// -->
// <--[action]
// @Actions
// no click trigger
//
// @Triggers when the NPC is clicked but no click trigger fires.
//
// @Context
// None
//
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// click
//
// @Triggers when the NPC is clicked by a player.
//
// @Context
// None
//
// @Determine
// "cancelled" to cancel the click event completely.
//
// -->
@EventHandler
public void clickTrigger(NPCRightClickEvent event) {
if (!event.getNPC().hasTrait(TriggerTrait.class)) {
return;
}
NPCTag npc = new NPCTag(event.getNPC());
if (!npc.getTriggerTrait().isEnabled(name)) {
return;
}
PlayerTag player = PlayerTag.mirrorBukkitPlayer(event.getClicker());
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, player);
if (!trigger.wasTriggered()) {
return;
}
if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
event.setCancelled(true);
return;
}
List<InteractScriptContainer> scripts = npc.getInteractScripts(player, ClickTrigger.class);
boolean any = false;
if (scripts != null) {
for (InteractScriptContainer script : scripts) {
String id = null;
Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, player);
if (!idMap.isEmpty()) {
ItemTag heldItem = new ItemTag(player.getPlayerEntity().getEquipment().getItemInMainHand());
for (Map.Entry<String, String> entry : idMap.entrySet()) {
String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(player, npc, null, false, null));
boolean isMatch = entry_value.isEmpty() || BukkitScriptEvent.tryItem(heldItem, entry_value);
if (script.shouldDebug()) {
Debug.echoDebug(script, "Comparing click trigger '<A>" + entry_value + "<W>' with item '<A>" + heldItem.debuggable() + "<W>': " + (isMatch ? "<GR>Match!" : "<Y>Not a match"));
}
if (isMatch) {
id = entry.getKey();
break;
}
}
}
if (parse(npc, player, script, id)) {
any = true;
}
}
}
if (!any) {
npc.action("no click trigger", player);
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class NPCTagBase method onHit.
// <--[action]
// @Actions
// hit
// hit on <entity>
//
// @Triggers when the NPC hits an enemy.
//
// @Context
// None
//
// -->
// <--[action]
// @Actions
// kill
// kill of <entity>
//
// @Triggers when the NPC kills an enemy.
//
// @Context
// None
//
// -->
@EventHandler(priority = EventPriority.MONITOR)
public void onHit(EntityDamageByEntityEvent event) {
NPC citizen = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
if (citizen == null) {
if (event.getDamager() instanceof Projectile) {
if (((Projectile) event.getDamager()).getShooter() instanceof Entity) {
citizen = CitizensAPI.getNPCRegistry().getNPC((Entity) ((Projectile) event.getDamager()).getShooter());
}
}
}
if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
return;
}
NPCTag npc = new NPCTag(citizen);
PlayerTag player = null;
if (event.getEntity() instanceof Player) {
player = PlayerTag.mirrorBukkitPlayer((Player) event.getEntity());
}
// TODO: Context containing the entity hit
npc.action("hit", player);
npc.action("hit on " + event.getEntityType().name(), player);
if (event.getEntity() instanceof LivingEntity) {
if (((LivingEntity) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
npc.action("kill", player);
npc.action("kill of " + event.getEntityType().name(), player);
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class NPCTagBase method navBegin.
// <--[action]
// @Actions
// begin navigation
//
// @Triggers when the NPC has received a 'walk' command,
// or is about to follow a path.
//
// @Context
// None
//
// -->
@EventHandler
public void navBegin(NavigationBeginEvent event) {
if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
return;
}
NPCTag npc = new NPCTag(event.getNPC());
npc.action("begin navigation", null);
if (event.getNPC().getNavigator().getTargetType() == TargetType.ENTITY) {
LivingEntity entity = (LivingEntity) event.getNPC().getNavigator().getEntityTarget().getTarget();
// and that entity is not dead, trigger "on attack" command
if (event.getNPC().getNavigator().getEntityTarget().isAggressive() && !entity.isDead()) {
PlayerTag player = null;
// Check if the entity attacked by this NPC is a player
if (entity instanceof Player) {
player = PlayerTag.mirrorBukkitPlayer((Player) entity);
}
// <--[action]
// @Actions
// attack
// attack on <entity>
//
// @Triggers when the NPC is about to attack an enemy.
//
// @Context
// None
//
// -->
npc.action("attack", player);
npc.action("attack on " + entity.getType().toString(), player);
}
previousLocations.put(event.getNPC().getId(), npc.getLocation());
}
}
Aggregations