Skip to content

Commit

Permalink
Fix logic when checking execute type.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Apr 11, 2013
1 parent 7ac97e7 commit b6b0327
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 26 deletions.
@@ -0,0 +1,86 @@
package net.aufdemrand.denizen.scripts.commands.core;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.npc.traits.HealthTrait;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.craftbukkit.v1_5_R2.entity.CraftLivingEntity;
import org.bukkit.entity.Player;

public class HurtCommand extends AbstractCommand {

private enum TargetType { NPC, PLAYER }

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

TargetType targetType = TargetType.PLAYER;
int amount = 0;

for (String arg : scriptEntry.getArguments()) {

if (aH.matchesQuantity(arg) || aH.matchesInteger(arg)
|| aH.matchesValueArg("amt", arg, ArgumentType.Integer))
amount = aH.getIntegerFrom(arg);

else if (aH.matchesValueArg("target", arg, ArgumentType.String)) {
try {
targetType = TargetType.valueOf(aH.getStringFrom(arg).toUpperCase());
} catch (Exception e) { dB.echoError("Invalid TARGET! Valid: NPC, PLAYER"); }

} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

// If TARGET is NPC/PLAYER and no NPC/PLAYER available, throw exception.
if (targetType == TargetType.PLAYER && scriptEntry.getPlayer() == null)
throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER);

else if (targetType == TargetType.NPC && scriptEntry.getNPC() == null)
throw new InvalidArgumentsException(Messages.ERROR_NO_NPCID);

scriptEntry.addObject("target", targetType)
.addObject("amount", amount);
}


@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

TargetType target = (TargetType) scriptEntry.getObject("target");
Integer amount = (Integer) scriptEntry.getObject("amount");

dB.report(getName(),
aH.debugObj("Target", (target == TargetType.PLAYER ? scriptEntry.getPlayer().getName()
: scriptEntry.getNPC().getName()))
+ aH.debugObj("Amount", (amount == Integer.MAX_VALUE ? "Full"
: String.valueOf(amount))));

switch (target) {

case NPC:
NPC npc = scriptEntry.getNPC().getCitizen();
if (!npc.hasTrait(HealthTrait.class)) npc.addTrait(HealthTrait.class);
// Set health to max
if (amount == Integer.MAX_VALUE)
npc.getTrait(HealthTrait.class).setHealth(npc.getTrait(HealthTrait.class).getMaxhealth());
// else, set Health
else npc.getTrait(HealthTrait.class).heal(amount);
return;

case PLAYER:
Player player = scriptEntry.getPlayer();
// Set to max health
if (amount == Integer.MAX_VALUE) player.setHealth(player.getMaxHealth());
// else, increase health
else ((CraftLivingEntity) player).getHandle().setHealth(player.getHealth() + amount);
return;
}

}
}
Expand Up @@ -37,14 +37,8 @@
* <br/>
*/
public class TeleportCommand extends AbstractCommand {
public static final String NPC_ARG = "NPC";

/**
* This method is called when the command is enabled.
*/
@Override
public void onEnable() {
}
public static final String NPC_ARG = "NPC";

/**
* This method will parse the arguments needed to execute the Teleport
Expand Down
Expand Up @@ -345,9 +345,10 @@ public void playerHitByEntity(EntityDamageByEntityEvent event) {
event.setDamage(aH.getIntegerFrom(determination));

if (CitizensAPI.getNPCRegistry().isNPC(event.getDamager())) {
context.put("damager", String.valueOf(CitizensAPI.getNPCRegistry().getNPC(event.getDamager()).getId()));
dNPC npc = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(event.getDamager()));
context.put("damager", String.valueOf(npc.getId()));

determination = doEvent("player damaged by npc", null, (Player) event.getEntity(), context);
determination = doEvent("player damaged by npc", npc, (Player) event.getEntity(), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
if (aH.matchesValueArg("DAMAGE", determination, aH.ArgumentType.Integer))
Expand Down Expand Up @@ -390,9 +391,15 @@ public void playerEat(EntityRegainHealthEvent event) {
event.setCancelled(true);
if (aH.matchesValueArg("AMOUNT", determination, aH.ArgumentType.Integer))
event.setAmount(aH.getIntegerFrom(determination));

}

}

@EventHandler
public void bucketFill(PlayerBucketEvent event) {



}


Expand Down
Expand Up @@ -133,6 +133,7 @@ public void navBegin(NavigationBeginEvent event) {
if (!event.getNPC().hasTrait(AssignmentTrait.class)) return;
dNPC npc = DenizenAPI.getDenizenNPC(event.getNPC());
npc.action("begin navigation", null);

if (event.getNPC().getNavigator().getTargetType().toString() == "ENTITY")
{
LivingEntity entity = event.getNPC().getNavigator().getEntityTarget().getTarget();
Expand Down
@@ -0,0 +1,48 @@
package net.aufdemrand.denizen.tags.core;

import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.events.ReplaceableTagEvent;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.trait.Anchors;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class NotableLocationTags implements Listener {

public NotableLocationTags(Denizen denizen) {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}

@EventHandler
public void constantTags(ReplaceableTagEvent event) {
if (!event.matches("NOTABLE")) return;

String tag = event.raw_tag;

String id = null;
if (event.hasValue()) {
id = event.getValue();
tag = tag.split(":", 2)[1];
}

else if (event.hasNameContext()) id = event.getNameContext();

if (dLocation.isSavedLocation(id)) {
dB.echoError("Notable tag '" + event.raw_tag + "': id was not found.");
}

dLocation location = dLocation.getSavedLocation(id);

Attribute attribute = new Attribute(event.raw_tag, event.getScriptEntry());

attribute.fulfill(1);

event.setReplaced(location.getAttribute(attribute));

}
}
Expand Up @@ -97,7 +97,7 @@ public static dEntity valueOf(String string) {

if (entityGroupUpper.startsWith("N@")) {
LivingEntity returnable = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(4))).getBukkitEntity();
.getById(Integer.valueOf(m.group(3))).getBukkitEntity();

if (returnable != null) return new dEntity(returnable);
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
Expand All @@ -112,8 +112,8 @@ else if (entityGroupUpper.startsWith("P@")) {

// Assume entity
else {
if (aH.matchesInteger(m.group(4))) {
int entityID = Integer.valueOf(m.group(4));
if (aH.matchesInteger(m.group(3))) {
int entityID = Integer.valueOf(m.group(3));
Entity entity = null;

for (World world : Bukkit.getWorlds()) {
Expand Down
Expand Up @@ -43,6 +43,7 @@ public static String isSavedLocation(org.bukkit.Location location) {
* @return true if it exists, false if not
*/
public static boolean isSavedLocation(String id) {
if (id == null) return false;
return locations.containsKey(id.toLowerCase());
}

Expand Down Expand Up @@ -86,18 +87,20 @@ public static dLocation valueOf(String string) {
string = string.split(":", 2)[1];
// Split values
String[] split = string.split(",");
// If 5 values, contains an id

if (split.length == 5)
try {
return new dLocation(split[0], Bukkit.getWorld(split[4]),
Double.valueOf(split[1]),
Double.valueOf(split[2]),
Double.valueOf(split[3]));
} catch(Exception e) {
return null;
}
// If 4 values, standard id-less dScript location format
else if (split.length == 4) {
// If 5 values, contains an id with standard dScript location format
try {
return new dLocation(split[0], Bukkit.getWorld(split[4]),
Double.valueOf(split[1]),
Double.valueOf(split[2]),
Double.valueOf(split[3]));
} catch(Exception e) {
return null;
}

else if (split.length == 4)
// If 4 values, standard id-less dScript location format
try {
return new dLocation(Bukkit.getWorld(split[3]),
Double.valueOf(split[0]),
Expand All @@ -106,7 +109,20 @@ else if (split.length == 4) {
} catch(Exception e) {
return null;
}
}


else if (split.length == 6)
// If 6 values, id-less location with pitch/yaw
try {
return new dLocation(split[0], Bukkit.getWorld(split[4]),
Double.valueOf(split[1]),
Double.valueOf(split[2]),
Double.valueOf(split[3]));
} catch(Exception e) {
return null;
}


return null;
}

Expand Down Expand Up @@ -172,7 +188,7 @@ public dLocation(String id, World world, double x, double y, double z) {
public dLocation(World world, double x, double y, double z) {
super(world, x, y, z);
}

public dLocation(World world, double x, double y, double z, float yaw, float pitch) {
super(world, x, y, z, yaw, pitch);
}
Expand Down

0 comments on commit b6b0327

Please sign in to comment.