Skip to content

Commit

Permalink
more dObject work.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed May 29, 2013
1 parent caf394e commit 3b31e13
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 195 deletions.
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.npc.traits;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.AssignmentScriptContainer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
Expand All @@ -27,10 +28,6 @@ public class AssignmentTrait extends Trait {
@Persist
private String assignment = "";

public void buildLocationContext() {
// TODO: finish this
}

public AssignmentTrait() {
super("assignment");
}
Expand All @@ -57,14 +54,14 @@ public void load(DataKey key) throws NPCLoadException {
* @return false if the assignment is invalid
*
*/
public boolean setAssignment(String assignment, Player player) {
public boolean setAssignment(String assignment, dPlayer player) {
if (ScriptRegistry.containsScript(assignment, AssignmentScriptContainer.class)) {
this.assignment = assignment.toUpperCase();
// Add Constants/Trigger trait if not already added to the NPC.
if (!npc.hasTrait(ConstantsTrait.class)) npc.addTrait(ConstantsTrait.class);
if (!npc.hasTrait(TriggerTrait.class)) npc.addTrait(TriggerTrait.class);
if (Settings.HealthTraitEnabledByDefault())
if (!npc.hasTrait(HealthTrait.class)) npc.addTrait(HealthTrait.class);
if (!npc.hasTrait(HealthTrait.class)) npc.addTrait(HealthTrait.class);
// Reset Constants
npc.getTrait(ConstantsTrait.class).rebuildAssignmentConstants();
// 'On Assignment' action.
Expand Down Expand Up @@ -106,7 +103,7 @@ public boolean hasAssignment() {
* @param player the player removing the assignment, can be null
*
*/
public void removeAssignment (Player player) {
public void removeAssignment (dPlayer player) {
assignment = "";
DenizenAPI.getCurrentInstance().getNPCRegistry().getDenizen(npc).action("remove assignment", player);
}
Expand Down Expand Up @@ -167,49 +164,42 @@ public void describe(CommandSender sender, int page) throws CommandException {
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}



// Listen for this NPC's hits on entities
@EventHandler(priority = EventPriority.MONITOR)
public void onHit(EntityDamageByEntityEvent event)
{

// Check if the damager is this NPC
if (event.getDamager() != npc.getBukkitEntity())
{
// If the damager is not this NPC, the damager could still
// be a projectile shot by this NPC, in which case we want
// to continue
if (event.getDamager() instanceof Projectile)
{
if (((Projectile) event.getDamager()).getShooter() != npc.getBukkitEntity())
return;
}
else return;
}

Player player = null;

// Check if the entity hit by this NPC is a player
if (event.getEntity() instanceof Player)
player = (Player) event.getEntity();

DenizenAPI.getDenizenNPC(npc).action("hit", player);

DenizenAPI.getDenizenNPC(npc).action("hit on "
+ event.getEntityType().toString(), player);

if (event.getEntity() instanceof LivingEntity)
{
if (((LivingEntity) event.getEntity()).getHealth()
- event.getDamage() <= 0)
{
DenizenAPI.getDenizenNPC(npc).action("kill", player);

DenizenAPI.getDenizenNPC(npc).action("kill of "
+ event.getEntityType().toString(), player);
}
}
public void onHit(EntityDamageByEntityEvent event) {

// Check if the damager is this NPC
if (event.getDamager() != npc.getBukkitEntity()) {

// If the damager is not this NPC, the damager could still be a
// projectile shot by this NPC, in which case we want to continue
if (event.getDamager() instanceof Projectile) {
if (((Projectile) event.getDamager()).getShooter() != npc.getBukkitEntity()) return;
}

else return;
}

dPlayer player = null;

// Check if the entity hit by this NPC is a player
if (event.getEntity() instanceof Player)
player = dPlayer.mirrorBukkitPlayer((Player) event.getEntity());

DenizenAPI.getDenizenNPC(npc).action("hit", player);
DenizenAPI.getDenizenNPC(npc).action("hit on " + event.getEntityType().toString(), player);

if (event.getEntity() instanceof LivingEntity) {
if (((LivingEntity) event.getEntity()).getHealth() - event.getDamage() <= 0) {
DenizenAPI.getDenizenNPC(npc).action("kill", player);
DenizenAPI.getDenizenNPC(npc).action("kill of " + event.getEntityType().toString(), player);
}
}

// All done!
}


}
73 changes: 42 additions & 31 deletions src/main/java/net/aufdemrand/denizen/npc/traits/ConstantsTrait.java
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.events.dScriptReloadEvent;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
Expand Down Expand Up @@ -30,6 +31,7 @@ public ConstantsTrait() {
super("constants");
}


/**
* Returns the value of the specified Constant, unique to this NPC. Note:
* Returns tags filled with the currently assigned NPC. See: Denizen TagManager
Expand All @@ -38,16 +40,17 @@ public ConstantsTrait() {
* @return value of the constant
*/
public String getConstant(String name) {

getAssignmentConstants();

if (constants.containsKey(name.toLowerCase()))
return DenizenAPI.getCurrentInstance().tagManager()
.tag(null, DenizenAPI.getDenizenNPC(npc), constants.get(name.toLowerCase()), false);
return TagManager.tag(null, DenizenAPI.getDenizenNPC(npc), constants.get(name.toLowerCase()), false);
else if (getAssignmentConstants().containsKey(name.toLowerCase()))
return DenizenAPI.getCurrentInstance().tagManager()
.tag(null, DenizenAPI.getDenizenNPC(npc), assignmentConstants.get(name.toLowerCase()), false);
return TagManager.tag(null, DenizenAPI.getDenizenNPC(npc), assignmentConstants.get(name.toLowerCase()), false);
return null;
}


/**
* Gets a map of the NPCs constants. Note: Does not include any constants
* inherited by the NPCs Assignment. To grab a comprehensive map of both,
Expand All @@ -59,6 +62,7 @@ public Map<String, String> getNPCConstants() {
return constants;
}


/**
* Gets a map of the NPCs constants, including those inherited by the Assignment.
* Any constants that are overridden by this NPC are taken into account, so this
Expand All @@ -74,6 +78,7 @@ public Map<String, String> getAllConstants() {
return allConstants;
}


/**
* Sets the value of a constant, as identified by the name. This will
* override any constants inherited from the NPCs Assignment.
Expand All @@ -85,6 +90,7 @@ public void setConstant(String name, String value) {
constants.put(name.toLowerCase(), value);
}


/**
* Removes an NPC-specific constant, as identified by the name. This will
* not remove any values inherited from an NPCs Assignment, only constants
Expand All @@ -97,6 +103,7 @@ public void removeConstant(String name) {
constants.remove(name.toLowerCase());
}


/**
* Checks if this NPC has any unique constants, beyond what is inherited from
* the NPCs Assignment.
Expand All @@ -107,33 +114,6 @@ public boolean hasNPCConstants() {
return !constants.isEmpty();
}

public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Constants for " + npc.getName());
paginator.addLine("<e>NPC-specific constants: " + (hasNPCConstants() ? "" : "None.") + "");
if (hasNPCConstants()) paginator.addLine("<e>Key: <a>Name <b>Value");
for (Entry<String, String> constant : constants.entrySet()) {
paginator.addLine("<a> " + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
paginator.addLine("");

if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment()) {
getAssignmentConstants();
// List constants inherited from an Assignment.
paginator.addLine("<e>Constants for assignment '" + assignment.toUpperCase() + "':");
paginator.addLine("<e>Key: <a>Name <b>Value");
for (Entry<String, String> constant : getAssignmentConstants().entrySet()) {
// If a constant from the Assignment has been overridden by a NPC constant,
// change formatting to indicate so.
if (constants.containsKey(constant.getKey()))
paginator.addLine("<m>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<r> <m>" + constant.getValue());
else paginator.addLine("<a>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
paginator.addLine("");
}

if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}

public Map<String, String> getAssignmentConstants() {
// Check to make sure NPC has an assignment
Expand All @@ -146,6 +126,7 @@ public Map<String, String> getAssignmentConstants() {
return assignmentConstants;
}


public Map<String, String> rebuildAssignmentConstants() {
// Builds a map of constants inherited from the NPCs current Assignment
if (!npc.hasTrait(AssignmentTrait.class) || !npc.getTrait(AssignmentTrait.class).hasAssignment()) {
Expand All @@ -167,6 +148,7 @@ public Map<String, String> rebuildAssignmentConstants() {
return assignmentConstants;
}


/**
* Rebuilds assignment constants on a script reload
*
Expand All @@ -177,4 +159,33 @@ public void onScriptsReload(dScriptReloadEvent event) {
rebuildAssignmentConstants();
}


public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Constants for " + npc.getName());
paginator.addLine("<e>NPC-specific constants: " + (hasNPCConstants() ? "" : "None.") + "");
if (hasNPCConstants()) paginator.addLine("<e>Key: <a>Name <b>Value");
for (Entry<String, String> constant : constants.entrySet()) {
paginator.addLine("<a> " + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
paginator.addLine("");

if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment()) {
getAssignmentConstants();
// List constants inherited from an Assignment.
paginator.addLine("<e>Constants for assignment '" + assignment.toUpperCase() + "':");
paginator.addLine("<e>Key: <a>Name <b>Value");
for (Entry<String, String> constant : getAssignmentConstants().entrySet()) {
// If a constant from the Assignment has been overridden by a NPC constant,
// change formatting to indicate so.
if (constants.containsKey(constant.getKey()))
paginator.addLine("<m>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<r> <m>" + constant.getValue());
else paginator.addLine("<a>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
paginator.addLine("");
}

if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}

}
20 changes: 11 additions & 9 deletions src/main/java/net/aufdemrand/denizen/npc/traits/HealthTrait.java
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.npc.traits;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.objects.Duration;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class HealthTrait extends Trait implements Listener {
private String respawnLocation = "<npc.location>";

// internal
private Player player = null;
private dPlayer player = null;
private boolean dying = false;
private Location loc;

Expand Down Expand Up @@ -194,13 +195,12 @@ public void setHealth(int health) {
currenthealth = health;
}

public void die()
{
public void die() {
try {
// Set the player as the killer of the NPC, for listeners
if (player != null)
((CraftLivingEntity) npc.getBukkitEntity())
.getHandle().killer = (EntityHuman) ((CraftLivingEntity) player).getHandle();
.getHandle().killer = (EntityHuman) ((CraftLivingEntity) player.getPlayerEntity()).getHandle();
} catch (Exception e) {
dB.echoError("Report this error to aufdemrand! Err: HealthTraitDie");
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public void onDeath(EntityDamageEvent event) {
// Check if the damager was a player and, if so, attach
// that player to the action's ScriptEntry
if (killerEntity instanceof Player)
player = (Player) killerEntity;
player = dPlayer.mirrorBukkitPlayer((Player) killerEntity);

// If the damager was a projectile, take its shooter into
// account as well
Expand All @@ -251,7 +251,7 @@ else if (killerEntity instanceof Projectile)
LivingEntity shooter = ((Projectile) killerEntity).getShooter();

if (shooter instanceof Player)
player = (Player) shooter;
player = dPlayer.mirrorBukkitPlayer((Player) shooter);

DenizenAPI.getDenizenNPC(npc).action("death by " +
shooter.getType().toString(), player);
Expand All @@ -266,12 +266,13 @@ else if (killerEntity instanceof Projectile)
else if (event instanceof EntityDamageByBlockEvent)
{
DenizenAPI.getDenizenNPC(npc).action("death by block", player);


// TODO:
// The line of code below should work, but a Bukkit bug makes the damager
// return null. Uncomment it once the bug is fixed.

//DenizenAPI.getDenizenNPC(npc).action("death by " +
// ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
// DenizenAPI.getDenizenNPC(npc).action("death by " +
// ((EntityDamageByBlockEvent) event).getDamager().getType().name(), null);
}

DenizenAPI.getDenizenNPC(npc).action("death", player);
Expand All @@ -284,6 +285,7 @@ else if (event instanceof EntityDamageByBlockEvent)

loc = aH.getLocationFrom(
TagManager.tag(null, DenizenAPI.getDenizenNPC(npc), respawnLocation, false));

if (loc == null) loc = npc.getBukkitEntity().getLocation();

if (animatedeath) {
Expand Down
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.npc.traits;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.commands.core.EngageCommand;
import net.aufdemrand.denizen.scripts.triggers.AbstractTrigger;
import net.aufdemrand.denizen.scripts.triggers.TriggerRegistry.CooldownType;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void describe(CommandSender sender, int page) throws CommandException {
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}

public boolean triggerCooldownOnly(AbstractTrigger triggerClass, Player player) {
public boolean triggerCooldownOnly(AbstractTrigger triggerClass, dPlayer player) {
// Check cool down, return false if not yet met
if (!DenizenAPI.getCurrentInstance().getTriggerRegistry().checkCooldown(npc, player, triggerClass, getCooldownType(triggerClass.getName())))
return false;
Expand All @@ -140,7 +141,7 @@ public boolean triggerCooldownOnly(AbstractTrigger triggerClass, Player player)
return true;
}

public boolean trigger(AbstractTrigger triggerClass, Player player) {
public boolean trigger(AbstractTrigger triggerClass, dPlayer player) {
// Check cool down, return false if not yet met
if (!DenizenAPI.getCurrentInstance().getTriggerRegistry().checkCooldown(npc, player, triggerClass, getCooldownType(triggerClass.getName())))
return false;
Expand Down

0 comments on commit 3b31e13

Please sign in to comment.