Skip to content

Commit

Permalink
Make Equip command work with armor and streamline its usage. New exam…
Browse files Browse the repository at this point in the history
…ple usage: - equip hand:iron_sword head:iron_helmet boots:iron_boots
  • Loading branch information
davidcernat committed Jun 4, 2013
1 parent cbb04a3 commit 7ba26f3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 44 deletions.
Expand Up @@ -95,10 +95,10 @@ public void registerCoreMembers() {
"ENGAGE", "engage (duration:#) (npcid:#)", 0);

registerCoreMember(EngraveCommand.class,
"ENGRAVE", "engrave (SET|REMOVE) (TARGET:player_name)", 0);
"ENGRAVE", "engrave (set|remove) (target:player_name)", 0);

registerCoreMember(EquipCommand.class,
"EQUIP", "equip ({HAND}|BOOTS|LEGS|CHEST|HEAD) [item:#(:#)|item:material(:#)]", 1);
"EQUIP", "equip (hand:[#|material](:#)) (head:[#|material](:#)) (chest:[#|material](:#)) (legs:[#|material](:#)) (boots:[#|material](:#))", 1);

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Jun 4, 2013

Contributor

I love this! Great idea!


registerCoreMember(ExecuteCommand.class,
"EXECUTE", "execute [as_player|as_op|as_npc|as_server] [\"Bukkit command\"]", 2);
Expand Down
Expand Up @@ -6,68 +6,103 @@
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.dItem;
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 net.citizensnpcs.api.trait.trait.Equipment;

public class EquipCommand extends AbstractCommand{
public enum EquipType { HAND, BOOTS, LEGS, CHEST, HEAD }

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

EquipType equipType = EquipType.HAND;
dItem item = null;
dItem hand = null;
dItem head = null;
dItem chest = null;
dItem legs = null;
dItem boots = null;

for (String arg : scriptEntry.getArguments()) {
if (aH.matchesItem(arg)) {
item = aH.getItemFrom(arg);
dB.echoDebug(Messages.DEBUG_SET_ITEM, arg);
} else if (aH.matchesArg("ITEMINHAND, HAND, HOLDING", arg)) {
try {
equipType = EquipType.valueOf(arg);
dB.echoDebug("... equipping for " + equipType.name());
} catch (Exception e) { throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); }
}

if (aH.matchesValueArg("ITEMINHAND, HAND, HOLDING", arg, ArgumentType.String)) {

arg = "ITEM:" + arg.split(":")[1];

if (aH.matchesItem(arg)) {
hand = aH.getItemFrom(arg);
}
}
else if (aH.matchesValueArg("HEAD, HELMET", arg, ArgumentType.String)) {

arg = "ITEM:" + arg.split(":")[1];

if (aH.matchesItem(arg)) {
head = aH.getItemFrom(arg);
}
}
else if (aH.matchesValueArg("CHEST, CHESTPLATE", arg, ArgumentType.String)) {

arg = "ITEM:" + arg.split(":")[1];

if (aH.matchesItem(arg)) {
chest = aH.getItemFrom(arg);
}
}
else if (aH.matchesValueArg("LEGS, LEGGINGS", arg, ArgumentType.String)) {

arg = "ITEM:" + arg.split(":")[1];

if (aH.matchesItem(arg)) {
legs = aH.getItemFrom(arg);
}
}
else if (aH.matchesValueArg("BOOTS", arg, ArgumentType.String)) {

arg = "ITEM:" + arg.split(":")[1];

if (aH.matchesItem(arg)) {
boots = aH.getItemFrom(arg);
}
}
}

if (item == null) {
dB.echoError("...no item specified to equip!");
}

scriptEntry.addObject("item", item)
.addObject("equipType", equipType);

scriptEntry.addObject("hand", hand)
.addObject("head", head)
.addObject("chest", chest)
.addObject("legs", legs)
.addObject("boots", boots);
}

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

EquipType equipType = (EquipType) scriptEntry.getObject("equipType");
dItem item = (dItem) scriptEntry.getObject("item");
dItem hand = (dItem) scriptEntry.getObject("hand");
dItem head = (dItem) scriptEntry.getObject("head");
dItem chest = (dItem) scriptEntry.getObject("chest");
dItem legs = (dItem) scriptEntry.getObject("legs");
dItem boots = (dItem) scriptEntry.getObject("boots");
NPC npc = scriptEntry.getNPC().getCitizen();

if (!npc.hasTrait(Equipment.class)) npc.addTrait(Equipment.class);
Equipment trait = npc.getTrait(Equipment.class);

switch (equipType) {
case BOOTS:
trait.set(4, item.getItemStack());
break;
case CHEST:
trait.set(2, item.getItemStack());
break;
case HAND:
trait.set(0, item.getItemStack());
break;
case HEAD:
trait.set(1, item.getItemStack());
break;
case LEGS:
trait.set(3, item.getItemStack());
break;
if (hand != null) {
trait.set(0, hand.getItemStack());
}
if (head != null) {
trait.set(1, head.getItemStack());
}
if (chest != null) {
trait.set(2, chest.getItemStack());
}
if (legs != null) {
trait.set(3, legs.getItemStack());
}
if (boots != null) {
trait.set(4, boots.getItemStack());
}

}
Expand Down
Expand Up @@ -50,8 +50,6 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

for (FireworkEffect.Type typeValue : FireworkEffect.Type.values()) {

dB.echoApproval("Checking if " + typeValue.name() + " matches " + arg.split(":", 2)[1].toUpperCase());

if (arg.split(":", 2)[1].toUpperCase().matches(typeValue.name())) {

type = typeValue;
Expand Down Expand Up @@ -82,7 +80,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
primary.add(aH.getColorFrom(element));
}
else {
dB.echoError("Invalid color" + element + "!");
dB.echoError("Invalid color " + element + "!");
}
}
} else if (aH.matchesValueArg("FADE", arg, ArgumentType.String)) {
Expand All @@ -93,7 +91,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
fade.add(aH.getColorFrom(element));
}
else {
dB.echoError("Invalid color" + element + "!");
dB.echoError("Invalid color " + element + "!");
}
}
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
Expand Down
Expand Up @@ -391,7 +391,7 @@ else if (entityGroupUpper.startsWith("NPC.")
}

else if (entityGroupUpper.startsWith("PLAYER.")
|| entityGroupUpper.startsWith("P@.")) {
|| entityGroupUpper.startsWith("P@")) {
LivingEntity returnable = getPlayerFrom(entityGroup.split("\\.")[1]);
if (returnable != null) return returnable;
else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?");
Expand Down

0 comments on commit 7ba26f3

Please sign in to comment.