use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommands method item.
@Command(aliases = { "npc" }, usage = "item [item] (data)", desc = "Sets the NPC's item", modifiers = { "item" }, min = 2, max = 3, flags = "", permission = "citizens.npc.item")
@Requirements(selected = true, ownership = true, types = { EntityType.DROPPED_ITEM, EntityType.ITEM_FRAME, EntityType.FALLING_BLOCK })
public void item(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Material mat = Material.matchMaterial(args.getString(1));
if (mat == null)
throw new CommandException(Messages.UNKNOWN_MATERIAL);
int data = args.getInteger(2, 0);
npc.data().setPersistent(NPC.ITEM_ID_METADATA, mat.name());
npc.data().setPersistent(NPC.ITEM_DATA_METADATA, data);
switch(npc.getEntity().getType()) {
case DROPPED_ITEM:
((org.bukkit.entity.Item) npc.getEntity()).getItemStack().setType(mat);
break;
case ITEM_FRAME:
((ItemFrame) npc.getEntity()).getItem().setType(mat);
break;
default:
break;
}
if (npc.isSpawned()) {
npc.despawn();
npc.spawn(npc.getStoredLocation());
}
Messaging.sendTr(sender, Messages.ITEM_SET, Util.prettyEnum(mat));
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class NPCCommands method copy.
@Command(aliases = { "npc" }, usage = "copy (--name newname)", desc = "Copies an NPC", modifiers = { "copy" }, min = 1, max = 1, permission = "citizens.npc.copy")
public void copy(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
String name = args.getFlag("name", npc.getFullName());
NPC copy = npc.clone();
if (!copy.getFullName().equals(name)) {
copy.setName(name);
}
if (copy.isSpawned() && args.getSenderLocation() != null) {
Location location = args.getSenderLocation();
location.getChunk().load();
copy.teleport(location, TeleportCause.COMMAND);
copy.getTrait(CurrentLocation.class).setLocation(location);
}
CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, copy) : new CommandSenderCreateNPCEvent(sender, copy);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
event.getNPC().destroy();
String reason = "Couldn't create NPC.";
if (!event.getCancelReason().isEmpty())
reason += " Reason: " + event.getCancelReason();
throw new CommandException(reason);
}
Messaging.sendTr(sender, Messages.NPC_COPIED, npc.getName());
selector.select(sender, copy);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class TemplateCommands method apply.
@Command(aliases = { "template", "tpl" }, usage = "apply [template name] (id id2...)", desc = "Applies a template to the selected NPC", modifiers = { "apply" }, min = 2, permission = "citizens.templates.apply")
@Requirements
public void apply(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Template template = Template.byName(args.getString(1));
if (template == null)
throw new CommandException(Messages.TEMPLATE_MISSING);
int appliedCount = 0;
if (args.argsLength() == 2) {
if (npc == null)
throw new CommandException(Messaging.tr(Messages.COMMAND_MUST_HAVE_SELECTED));
template.apply(npc);
appliedCount++;
} else {
String joined = args.getJoinedStrings(2, ',');
List<Integer> ids = Lists.newArrayList();
for (String id : Splitter.on(',').trimResults().split(joined)) {
int parsed = Integer.parseInt(id);
ids.add(parsed);
}
Iterable<NPC> transformed = Iterables.transform(ids, new Function<Integer, NPC>() {
@Override
public NPC apply(@Nullable Integer arg0) {
if (arg0 == null)
return null;
return CitizensAPI.getNPCRegistry().getById(arg0);
}
});
for (NPC toApply : transformed) {
template.apply(toApply);
appliedCount++;
}
}
Messaging.sendTr(sender, Messages.TEMPLATE_APPLIED, appliedCount);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class TemplateCommands method create.
@Command(aliases = { "template", "tpl" }, usage = "create [template name] (-o)", desc = "Creates a template from the selected NPC", modifiers = { "create" }, min = 2, max = 2, flags = "o", permission = "citizens.templates.create")
public void create(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
String name = args.getString(1);
if (Template.byName(name) != null)
throw new CommandException(Messages.TEMPLATE_CONFLICT);
TemplateBuilder.create(name).from(npc).override(args.hasFlag('o')).buildAndSave();
Messaging.sendTr(sender, Messages.TEMPLATE_CREATED);
}
use of net.citizensnpcs.api.command.exception.CommandException in project Citizens2 by CitizensDev.
the class TemplateCommands method delete.
@Command(aliases = { "template", "tpl" }, usage = "delete [template name]", desc = "Deletes a template", modifiers = { "delete" }, min = 2, max = 2, permission = "citizens.templates.delete")
public void delete(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
String name = args.getString(1);
if (Template.byName(name) == null)
throw new CommandException(Messages.TEMPLATE_MISSING);
Template.byName(name).delete();
Messaging.sendTr(sender, Messages.TEMPLATE_DELETED, name);
}
Aggregations