diff --git a/lib/CitizensAPI.jar b/lib/CitizensAPI.jar index 08c4ca558..326173636 100644 Binary files a/lib/CitizensAPI.jar and b/lib/CitizensAPI.jar differ diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 11a8f7d29..5bc186e8e 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -22,6 +22,7 @@ import net.citizensnpcs.npc.CitizensTraitManager; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; +import net.citizensnpcs.trait.VillagerProfession; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Paginator; @@ -30,6 +31,7 @@ import net.citizensnpcs.util.StringHelper; import org.bukkit.ChatColor; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.entity.Villager.Profession; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @Requirements(selected = true, ownership = true) @@ -392,4 +394,24 @@ public class NPCCommands { + (npc.getTrait(Powered.class).toggle() ? "now" : "no longer"); 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."); + } + } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java index 685f7eea0..cf31f0a45 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -17,6 +17,7 @@ import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; +import net.citizensnpcs.trait.VillagerProfession; import net.citizensnpcs.trait.text.Text; 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(CurrentLocation.class).withName("location")); registerTrait(new TraitFactory(Text.class).withName("text")); + registerTrait(new TraitFactory(VillagerProfession.class).withName("profession")); registerTrait(new TraitFactory(Waypoints.class).withName("waypoints")); } diff --git a/src/main/java/net/citizensnpcs/trait/CurrentLocation.java b/src/main/java/net/citizensnpcs/trait/CurrentLocation.java index 57a12be26..211544282 100644 --- a/src/main/java/net/citizensnpcs/trait/CurrentLocation.java +++ b/src/main/java/net/citizensnpcs/trait/CurrentLocation.java @@ -53,6 +53,6 @@ public class CurrentLocation extends Trait implements Runnable { @Override public String toString() { - return "SpawnLocation{" + loc + "}"; + return "CurrentLocation{" + loc + "}"; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/VillagerProfession.java b/src/main/java/net/citizensnpcs/trait/VillagerProfession.java new file mode 100644 index 000000000..6375f8ea8 --- /dev/null +++ b/src/main/java/net/citizensnpcs/trait/VillagerProfession.java @@ -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 + "}"; + } +} \ No newline at end of file