Added ability to change a villager NPC profession. This addresses CITIZENS-14.

This commit is contained in:
aPunch 2012-03-15 06:02:53 -05:00
parent 2614b07208
commit 09a09eeede
5 changed files with 72 additions and 1 deletions

Binary file not shown.

View File

@ -22,6 +22,7 @@ import net.citizensnpcs.npc.CitizensTraitManager;
import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered; import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.Paginator;
@ -30,6 +31,7 @@ import net.citizensnpcs.util.StringHelper;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Villager.Profession;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@Requirements(selected = true, ownership = true) @Requirements(selected = true, ownership = true)
@ -391,4 +393,24 @@ public class NPCCommands {
+ (npc.getTrait(Powered.class).toggle() ? "now" : "no longer"); + (npc.getTrait(Powered.class).toggle() ? "now" : "no longer");
Messaging.send(player, msg += " be powered."); Messaging.send(player, msg += " be powered.");
} }
@Command(
aliases = { "npc" },
usage = "profession [profession]",
desc = "Set a NPC's profession",
modifiers = { "profession" },
min = 2,
max = 2,
permission = "npc.profession")
@Requirements(selected = true, ownership = true, types = { EntityType.VILLAGER })
public void profession(CommandContext args, Player player, NPC npc) throws CommandException {
String profession = args.getString(1);
try {
npc.getTrait(VillagerProfession.class).setProfession(Profession.valueOf(profession.toUpperCase()));
Messaging.send(player, StringHelper.wrap(npc.getName()) + " is now the profession "
+ StringHelper.wrap(profession.toUpperCase()) + ".");
} catch (IllegalArgumentException ex) {
throw new CommandException("'" + profession + "' is not a valid profession.");
}
}
} }

View File

@ -17,6 +17,7 @@ import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered; import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.trait.waypoint.Waypoints; import net.citizensnpcs.trait.waypoint.Waypoints;
@ -35,6 +36,7 @@ public class CitizensTraitManager implements TraitManager {
registerTrait(new TraitFactory(Spawned.class).withName("spawned")); registerTrait(new TraitFactory(Spawned.class).withName("spawned"));
registerTrait(new TraitFactory(CurrentLocation.class).withName("location")); registerTrait(new TraitFactory(CurrentLocation.class).withName("location"));
registerTrait(new TraitFactory(Text.class).withName("text")); registerTrait(new TraitFactory(Text.class).withName("text"));
registerTrait(new TraitFactory(VillagerProfession.class).withName("profession"));
registerTrait(new TraitFactory(Waypoints.class).withName("waypoints")); registerTrait(new TraitFactory(Waypoints.class).withName("waypoints"));
} }

View File

@ -53,6 +53,6 @@ public class CurrentLocation extends Trait implements Runnable {
@Override @Override
public String toString() { public String toString() {
return "SpawnLocation{" + loc + "}"; return "CurrentLocation{" + loc + "}";
} }
} }

View File

@ -0,0 +1,47 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
public class VillagerProfession extends Trait {
private Profession profession = Profession.FARMER;
private final NPC npc;
public VillagerProfession(NPC npc) {
this.npc = npc;
}
@Override
public void load(DataKey key) throws NPCLoadException {
try {
profession = Profession.valueOf(key.getString(""));
} catch (IllegalArgumentException ex) {
throw new NPCLoadException("Invalid profession.");
}
}
@Override
public void save(DataKey key) {
key.setString("", profession.name());
}
@Override
public void onNPCSpawn() {
if (npc.getBukkitEntity() instanceof Villager)
((Villager) npc.getBukkitEntity()).setProfession(profession);
}
public void setProfession(Profession profession) {
this.profession = profession;
}
@Override
public String toString() {
return "Profession{" + profession + "}";
}
}