Skip to content

Commit

Permalink
Update Head command to 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Sep 25, 2013
1 parent e947c4d commit eac991e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 51 deletions.
Expand Up @@ -926,10 +926,10 @@ public void registerCoreMembers() {

// <--[command]
// @Name Head
// @Usage head (player) [skin:<name>]
// @Required 0
// @Usage head [<entity>|...] [skin:<player>]
// @Required 2
// @Stable Todo
// @Short Changes the NPC's head to look like a player's skin.
// @Short Makes players or NPCs wear a specific player's head.
// @Author David Cernat
// @Description
// Todo
Expand All @@ -941,7 +941,7 @@ public void registerCoreMembers() {
// Todo
// -->
registerCoreMember(HeadCommand.class,
"HEAD", "head (player) [skin:<name>]", 0);
"HEAD", "head [<entity>|...] [skin:<player>]", 2);

// <--[command]
// @Name Heal
Expand Down
Expand Up @@ -27,14 +27,14 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
// Entity arg
&& arg.matchesArgumentList(dEntity.class)) {

scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(Duration.class)) {
// add value
&& arg.matchesArgumentType(Duration.class)) {

scriptEntry.addObject("duration", arg.asType(Duration.class));
}
}
Expand Down
@@ -1,90 +1,85 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import java.util.Arrays;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
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;

/**
* Sets an NPC or player's head to that of a specific player's skin.
* Makes players or NPCs wear a specific player's head.
*
* @author David Cernat
*/

public class HeadCommand extends AbstractCommand {

private enum TargetType { NPC, PLAYER }

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

TargetType targetType = TargetType.NPC;
Integer duration = null;
String skin = null;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

for (String arg : scriptEntry.getArguments()) {
// If argument is a duration
if (aH.matchesDuration(arg)) {
duration = aH.getIntegerFrom(arg);
dB.echoDebug("...head duration set to '%s'.", arg);
}
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {

else if (aH.matchesArg("PLAYER", arg)) {
targetType = TargetType.PLAYER;
dB.echoDebug("...will affect the player!");
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (aH.matchesValueArg("skin", arg, ArgumentType.String)) {
skin = aH.getStringFrom(arg);
dB.echoDebug("...will have " + skin + "'s head");
else if (!scriptEntry.hasObject("skin")
&& (arg.matchesPrefix("skin, s"))) {

} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
scriptEntry.addObject("skin", arg.asElement());
}
}

// 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("skin", skin);
// Use player or NPC as default entity
scriptEntry.defaultObject("entities", (scriptEntry.hasPlayer() ? Arrays.asList(scriptEntry.getPlayer().getDenizenEntity()) : null),
(scriptEntry.hasNPC() ? Arrays.asList(scriptEntry.getNPC().getDenizenEntity()) : null));
}

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

TargetType target = (TargetType) scriptEntry.getObject("target");
String skin = (String) scriptEntry.getObject("skin");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
String skin = ((Element) scriptEntry.getObject("skin")).asString().replaceAll("[pP]@", "");

// Create head item with chosen skin
ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
ItemMeta itemMeta = item.getItemMeta();
((SkullMeta) itemMeta).setOwner(skin);
item.setItemMeta(itemMeta);

if (target.name().equals("NPC")) {
NPC npc = scriptEntry.getNPC().getCitizen();
// Report to dB
dB.report(getName(), aH.debugObj("entities", entities.toString()) +
aH.debugObj("skin", "p@" + skin));

if (!npc.hasTrait(Equipment.class)) npc.addTrait(Equipment.class);
Equipment trait = npc.getTrait(Equipment.class);
trait.set(1, item);
}
else {
Player player = scriptEntry.getPlayer().getPlayerEntity();
player.getInventory().setHelmet(item);
for (dEntity entity : entities) {
if (entity.isNPC()) {
if (!entity.getNPC().hasTrait(Equipment.class))
entity.getNPC().addTrait(Equipment.class);
Equipment trait = entity.getNPC().getTrait(Equipment.class);
trait.set(1, item);
}
else if (entity.isPlayer()) {
entity.getPlayer().getInventory().setHelmet(item);
}
else {
dB.echoError(getName(), "No players or NPCs have been specified!");
}
}

}

}
Expand Up @@ -48,7 +48,7 @@ else if (arg.matches("npc") && scriptEntry.hasNPC()) {
}
}

// Check to make sure required arguments have been filled
// Use player or NPC as default entity
scriptEntry.defaultObject("entities", (scriptEntry.hasPlayer() ? Arrays.asList(scriptEntry.getPlayer().getDenizenEntity()) : null),
(scriptEntry.hasNPC() ? Arrays.asList(scriptEntry.getNPC().getDenizenEntity()) : null));

Expand Down

0 comments on commit eac991e

Please sign in to comment.