Skip to content

Commit

Permalink
slight cleanup to NPCTag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 15, 2020
1 parent 6ea1b29 commit da75252
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 77 deletions.
@@ -1,16 +1,16 @@
package com.denizenscript.denizen.npc;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.entity.EntityDespawnScriptEvent;
import com.denizenscript.denizen.flags.FlagManager;
import com.denizenscript.denizen.npc.actions.ActionHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.events.OldEventManager;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
Expand Down Expand Up @@ -54,21 +54,6 @@ public ActionHandler getActionHandler() {
return actionHandler;
}

/**
* Returns a NPCTag object when given a valid NPC. DenizenNPCs have some methods
* specific to Denizen functionality as well as easy access to the attached NPC and LivingEntity.
*
* @param npc the Citizens NPC
* @return a NPCTag
*/
public static NPCTag getDenizen(NPC npc) {
return new NPCTag(npc);
}

public static NPCTag getDenizen(int id) {
return new NPCTag(CitizensAPI.getNPCRegistry().getById(id));
}

/**
* Returns a InventoryTag object from the Inventory trait of a valid NPC.
*
Expand All @@ -88,14 +73,15 @@ public static Inventory getInventory(NPC npc) {
}
else {
try {
Inventory inv = (Inventory) INVENTORY_TRAIT_VIEW.get(getDenizen(npc).getInventoryTrait());
NPCTag npcTag = new NPCTag(npc);
Inventory inv = (Inventory) INVENTORY_TRAIT_VIEW.get(npcTag.getInventoryTrait());
if (inv != null) {
return inv;
}
else {
// TODO: ???
Inventory npcInventory = Bukkit.getServer().createInventory(getDenizen(npc), InventoryType.PLAYER);
npcInventory.setContents(Arrays.copyOf(getDenizen(npc).getInventoryTrait().getContents(), npcInventory.getSize()));
Inventory npcInventory = Bukkit.getServer().createInventory(npcTag, InventoryType.PLAYER);
npcInventory.setContents(Arrays.copyOf(npcTag.getInventoryTrait().getContents(), npcInventory.getSize()));
return npcInventory;
}
}
Expand Down Expand Up @@ -177,7 +163,7 @@ public void despawn(NPCDespawnEvent event) {
@EventHandler
public void onRemove(NPCRemoveEvent event) {
NPC npc = event.getNPC();
getDenizen(npc).action("remove", null);
new NPCTag(npc).action("remove", null);
FlagManager.clearNPCFlags(npc.getId());
}

Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer;
import com.denizenscript.denizen.utilities.DenizenAPI;
Expand Down Expand Up @@ -97,7 +98,7 @@ public boolean setAssignment(String assignment, PlayerTag player) {
// Reset Constants
npc.getOrAddTrait(ConstantsTrait.class).rebuildAssignmentConstants();
// 'On Assignment' action.
DenizenNPCHelper.getDenizen(npc).action("assignment", player);
new NPCTag(npc).action("assignment", player);
return true;
}
else {
Expand Down Expand Up @@ -156,7 +157,7 @@ public boolean hasAssignment() {
* @param player the player removing the assignment, can be null
*/
public void removeAssignment(PlayerTag player) {
DenizenNPCHelper.getDenizen(npc).action("remove assignment", player);
new NPCTag(npc).action("remove assignment", player);
cachedContainer = null;
assignment = "";
}
Expand Down Expand Up @@ -271,17 +272,17 @@ else if (killerEntity instanceof Projectile) {
if (shooter instanceof Player) {
player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
}
DenizenAPI.getDenizenNPC(npc).action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
new NPCTag(npc).action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
}
}
DenizenAPI.getDenizenNPC(npc).action("death by entity", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " + killerEntity.getType().toString(), player, context);
new NPCTag(npc).action("death by entity", player, context);
new NPCTag(npc).action("death by " + killerEntity.getType().toString(), player, context);
}
else if (event instanceof EntityDamageByBlockEvent) {
DenizenAPI.getDenizenNPC(npc).action("death by block", null, context);
new NPCTag(npc).action("death by block", null, context);
}
DenizenAPI.getDenizenNPC(npc).action("death", player, context);
DenizenAPI.getDenizenNPC(npc).action("death by " + deathCause, player, context);
new NPCTag(npc).action("death", player, context);
new NPCTag(npc).action("death by " + deathCause, player, context);
}

private UUID entityId;
Expand Down Expand Up @@ -328,12 +329,12 @@ public void onHit(EntityDamageByEntityEvent event) {
player = PlayerTag.mirrorBukkitPlayer((Player) event.getEntity());
}
// TODO: Context containing the entity hit
DenizenAPI.getDenizenNPC(npc).action("hit", player);
DenizenAPI.getDenizenNPC(npc).action("hit on " + event.getEntityType().name(), player);
new NPCTag(npc).action("hit", player);
new NPCTag(npc).action("hit on " + event.getEntityType().name(), player);
if (event.getEntity() instanceof LivingEntity) {
if (((LivingEntity) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
DenizenAPI.getDenizenNPC(npc).action("kill", player);
DenizenAPI.getDenizenNPC(npc).action("kill of " + event.getEntityType().name(), player);
new NPCTag(npc).action("kill", player);
new NPCTag(npc).action("kill of " + event.getEntityType().name(), player);
}
}
}
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
Expand Down Expand Up @@ -48,11 +49,11 @@ public String getConstant(String name) {
if (constants.containsKey(CoreUtilities.toLowerCase(name))) // TODO: shouldDebug
{
return TagManager.tag(constants.get(CoreUtilities.toLowerCase(name)),
new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), null, true, null));
new BukkitTagContext(null, new NPCTag(npc), null, true, null));
}
else if (getAssignmentConstants().containsKey(CoreUtilities.toLowerCase(name))) {
return TagManager.tag(assignmentConstants.get(CoreUtilities.toLowerCase(name)),
new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), null, true, null));
new BukkitTagContext(null, new NPCTag(npc), null, true, null));
}
return null;
}
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void run() {
* @param location the location to fish at
*/
public void startFishing(Location location) {
DenizenAPI.getDenizenNPC(npc).action("start fishing", null);
new NPCTag(npc).action("start fishing", null);
fishingLocation = location.clone();
cast();
fishing = true;
Expand All @@ -110,7 +111,7 @@ public void startFishing(Location location) {
* Makes the stop fishing.
*/
public void stopFishing() {
DenizenAPI.getDenizenNPC(npc).action("stop fishing", null);
new NPCTag(npc).action("stop fishing", null);
reel();
reelCount = 100;
castCount = 0;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void startFishing() {
//
// -->
private void cast() {
DenizenAPI.getDenizenNPC(npc).action("cast fishing rod", null);
new NPCTag(npc).action("cast fishing rod", null);
if (fishingLocation == null) {
Debug.echoError("Fishing location not found!");
return;
Expand Down Expand Up @@ -221,7 +222,7 @@ private void cast() {
//
// -->
private void reel() {
DenizenAPI.getDenizenNPC(npc).action("reel in fishing rod", null);
new NPCTag(npc).action("reel in fishing rod", null);

int chance = (int) (Math.random() * 100);

Expand All @@ -248,7 +249,7 @@ private void reel() {
double d9 = 0.1D;
fish.setVelocity(new Vector(d5 * d9, d6 * d9 + Math.sqrt(d8) * 0.08D, d7 * d9));
}
DenizenAPI.getDenizenNPC(npc).action("catch fish", null);
new NPCTag(npc).action("catch fish", null);
}

if (npc.getEntity() instanceof Player) {
Expand Down
Expand Up @@ -269,7 +269,7 @@ public void onDamage(EntityDamageEvent event) {
return;
}

TagContext context = new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), null, true, null);
TagContext context = new BukkitTagContext(null, new NPCTag(npc), null, true, null);
loc = getRespawnLocation();// TODO: debug option?

if (loc == null) {
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.events.bukkit.ExhaustedNPCEvent;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.ai.event.NavigationBeginEvent;
import net.citizensnpcs.api.ai.event.NavigationCancelEvent;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void onMove(NavigationBeginEvent event) {
// If still exhausted, cancel navigation and fire 'On Exhausted:' action
if (!e.isCancelled()) {
npc.getNavigator().cancelNavigation();
DenizenAPI.getDenizenNPC(npc).action("exhausted", null);
new NPCTag(npc).action("exhausted", null);

// No need to progress any further.
return;
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizencore.tags.TagManager;
Expand Down Expand Up @@ -54,7 +55,7 @@ public String getNickname() {
}
else {
return TagManager.tag(nickname, // TODO: debug option?
new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), null, true, null));
new BukkitTagContext(null, new NPCTag(npc), null, true, null));
}
}

Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void NPCPush(NPCPushEvent event) {
}
}
if (pusher != null) {
DenizenAPI.getDenizenNPC(npc).action("push", PlayerTag.mirrorBukkitPlayer(pusher));
new NPCTag(npc).action("push", PlayerTag.mirrorBukkitPlayer(pusher));
pushedTimer = System.currentTimeMillis() + (delay * 1000);
}
} // End push action
Expand Down Expand Up @@ -203,7 +204,7 @@ public void NPCCompleteDestination(NavigationCompleteEvent event) {
NMS.setHeadYaw(npcEntity, returnLocation.getYaw());
pushed = false;
// Push Return action
DenizenAPI.getDenizenNPC(npc).action("push return", null);
new NPCTag(npc).action("push return", null);
}
}

Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
Expand Down Expand Up @@ -80,7 +81,7 @@ public void sit() {
return;
}

DenizenAPI.getDenizenNPC(npc).action("sit", null);
new NPCTag(npc).action("sit", null);

sit(npc.getEntity().getLocation());
}
Expand Down Expand Up @@ -115,7 +116,7 @@ private void standInternal() {
}

public void sitInternal(Location location) {
DenizenAPI.getDenizenNPC(npc).action("sit", null);
new NPCTag(npc).action("sit", null);

/*
* Teleport NPC to the location before
Expand Down Expand Up @@ -151,7 +152,7 @@ public void sit(Location location) {
* Makes the NPC stand
*/
public void stand() {
DenizenAPI.getDenizenNPC(npc).action("stand", null);
new NPCTag(npc).action("stand", null);

standInternal();
standInternal();
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void onSpawn() {
* Makes the NPC sneak
*/
public void sneak() {
DenizenAPI.getDenizenNPC(npc).action("sneak", null);
new NPCTag(npc).action("sneak", null);

if (npc.getEntity().getType() != EntityType.PLAYER) {
return;
Expand All @@ -51,7 +52,7 @@ public void sneak() {
*/
public void stand() {
// Notated in SittingTrait
DenizenAPI.getDenizenNPC(npc).action("stand", null);
new NPCTag(npc).action("stand", null);

NMSHandler.getEntityHelper().setSneaking(((Player) npc.getEntity()), false);

Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.npc.traits;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.commands.npc.EngageCommand;
import com.denizenscript.denizen.scripts.triggers.AbstractTrigger;
Expand Down Expand Up @@ -117,7 +118,7 @@ public boolean hasTrigger(String triggerName) {
}

public boolean isEnabled(String triggerName) {
if (!DenizenAPI.getDenizenNPC(npc).getAssignmentTrait().hasAssignment()) {
if (!new NPCTag(npc).getAssignmentTrait().hasAssignment()) {
return false;
}
if (enabled.containsKey(triggerName.toUpperCase())) {
Expand Down Expand Up @@ -238,7 +239,7 @@ public TriggerContext trigger(AbstractTrigger triggerClass, PlayerTag player, Ma
// On Unavailable Action

// TODO: Should this be refactored?
if (DenizenNPCHelper.getDenizen(npc).action("unavailable", player, context).equalsIgnoreCase("available")) {
if (new NPCTag(npc).action("unavailable", player, context).equalsIgnoreCase("available")) {
// If determined available, continue on...
// else, return a 'non-triggered' state.
}
Expand All @@ -252,7 +253,7 @@ public TriggerContext trigger(AbstractTrigger triggerClass, PlayerTag player, Ma
.setCooldown(npc, player, triggerClass, getCooldownDuration(trigger_type), getCooldownType(trigger_type));

// Grab the determination of the action
String determination = DenizenNPCHelper.getDenizen(npc).action(trigger_type, player, context);
String determination = new NPCTag(npc).action(trigger_type, player, context);

return new TriggerContext(determination, true);
}
Expand Down
Expand Up @@ -68,7 +68,7 @@ public void clickTrigger(NPCRightClickEvent event) {

// The rest of the methods beyond this point require a NPCTag object, which can easily be
// obtained if a valid NPC object is available:
NPCTag npc = DenizenAPI.getDenizenNPC(event.getNPC());
NPCTag npc = new NPCTag(event.getNPC());

// Now, check if the 'click trigger' specifically is enabled. 'name' is inherited from the
// super AbstractTrigger and contains the name of the trigger that was use in registration.
Expand Down
Expand Up @@ -85,7 +85,7 @@ public void damageTrigger(EntityDamageByEntityEvent event) {
context.put("damage", new ElementTag(event.getDamage()));

if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
NPCTag npc = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
if (npc == null) {
return;
}
Expand Down

0 comments on commit da75252

Please sign in to comment.