2017-12-30 08:36:36 +01:00
|
|
|
package net.citizensnpcs.commands;
|
|
|
|
|
2022-12-11 14:28:39 +01:00
|
|
|
import java.io.IOException;
|
2017-12-30 08:36:36 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-07-17 16:29:11 +02:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
|
|
|
import com.google.common.base.Splitter;
|
|
|
|
import com.google.common.collect.Iterables;
|
|
|
|
import com.google.common.collect.Lists;
|
2023-04-29 19:13:53 +02:00
|
|
|
import com.google.common.primitives.Ints;
|
2022-07-17 16:29:11 +02:00
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.Citizens;
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
|
|
import net.citizensnpcs.api.command.Command;
|
|
|
|
import net.citizensnpcs.api.command.CommandContext;
|
2022-07-17 16:29:11 +02:00
|
|
|
import net.citizensnpcs.api.command.CommandMessages;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.command.Requirements;
|
|
|
|
import net.citizensnpcs.api.command.exception.CommandException;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.api.util.Messaging;
|
|
|
|
import net.citizensnpcs.npc.Template;
|
|
|
|
import net.citizensnpcs.util.Messages;
|
|
|
|
|
|
|
|
@Requirements(selected = true, ownership = true)
|
|
|
|
public class TemplateCommands {
|
|
|
|
public TemplateCommands(Citizens plugin) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
|
|
|
aliases = { "template", "tpl" },
|
|
|
|
usage = "apply [template name] (id id2...)",
|
2024-02-29 18:03:30 +01:00
|
|
|
desc = "",
|
2017-12-30 08:36:36 +01:00
|
|
|
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)
|
2022-07-17 16:29:11 +02:00
|
|
|
throw new CommandException(Messaging.tr(CommandMessages.MUST_HAVE_SELECTED));
|
2017-12-30 08:36:36 +01:00
|
|
|
template.apply(npc);
|
|
|
|
appliedCount++;
|
|
|
|
} else {
|
|
|
|
String joined = args.getJoinedStrings(2, ',');
|
|
|
|
List<Integer> ids = Lists.newArrayList();
|
|
|
|
for (String id : Splitter.on(',').trimResults().split(joined)) {
|
2023-04-29 19:13:53 +02:00
|
|
|
ids.add(Ints.tryParse(id));
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
2023-04-29 19:13:53 +02:00
|
|
|
Iterable<NPC> transformed = Iterables.transform(ids, id -> CitizensAPI.getNPCRegistry().getById(id));
|
2017-12-30 08:36:36 +01:00
|
|
|
for (NPC toApply : transformed) {
|
|
|
|
template.apply(toApply);
|
|
|
|
appliedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Messaging.sendTr(sender, Messages.TEMPLATE_APPLIED, appliedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
|
|
|
aliases = { "template", "tpl" },
|
2022-12-11 14:28:39 +01:00
|
|
|
usage = "create [template name] (-k)",
|
2024-02-29 18:03:30 +01:00
|
|
|
desc = "",
|
2017-12-30 08:36:36 +01:00
|
|
|
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);
|
|
|
|
|
2022-12-11 14:28:39 +01:00
|
|
|
try {
|
|
|
|
Template.Builder.create(name).from(npc).override(!args.hasFlag('k')).buildAndSave();
|
|
|
|
Messaging.sendTr(sender, Messages.TEMPLATE_CREATED);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Messaging.sendError(sender, "Invalid template filename");
|
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
|
|
|
aliases = { "template", "tpl" },
|
|
|
|
usage = "delete [template name]",
|
2024-02-29 18:03:30 +01:00
|
|
|
desc = "",
|
2017-12-30 08:36:36 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
|
|
|
aliases = { "template", "tpl" },
|
|
|
|
usage = "list",
|
2024-02-29 18:03:30 +01:00
|
|
|
desc = "",
|
2017-12-30 08:36:36 +01:00
|
|
|
modifiers = { "list" },
|
|
|
|
min = 1,
|
|
|
|
max = 1,
|
|
|
|
permission = "citizens.templates.list")
|
|
|
|
public void list(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
|
|
|
|
Messaging.sendTr(sender, Messages.TEMPLATE_LIST_HEADER);
|
2022-12-11 14:28:39 +01:00
|
|
|
for (Template template : Template.getTemplates()) {
|
2017-12-30 08:36:36 +01:00
|
|
|
Messaging.send(sender, "[[-]] " + template.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|