Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix logic when checking execute type.
- Loading branch information
1 parent
7ac97e7
commit b6b0327
Showing
7 changed files
with
178 additions
and
26 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/net/aufdemrand/denizen/scripts/commands/core/HurtCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/net/aufdemrand/denizen/tags/core/NotableLocationTags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters