Citizens2/main/src/main/java/net/citizensnpcs/commands/TemplateCommands.java

89 lines
3.5 KiB
Java
Raw Normal View History

package net.citizensnpcs.commands;
2024-03-04 13:46:36 +01:00
import java.util.Collection;
import java.util.stream.Collectors;
import org.bukkit.command.CommandSender;
2024-03-04 13:46:36 +01:00
import com.google.common.base.Joiner;
import net.citizensnpcs.Citizens;
2024-03-04 13:46:36 +01:00
import net.citizensnpcs.api.command.Arg;
import net.citizensnpcs.api.command.Arg.CompletionsProvider;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.NPC;
2024-03-04 13:46:36 +01:00
import net.citizensnpcs.api.npc.templates.Template;
import net.citizensnpcs.api.npc.templates.TemplateRegistry;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
@Requirements(selected = true, ownership = true)
public class TemplateCommands {
2024-03-04 13:46:36 +01:00
private final TemplateRegistry registry;
public TemplateCommands(Citizens plugin) {
2024-03-04 13:46:36 +01:00
this.registry = plugin.getTemplateRegistry();
}
@Command(
aliases = { "template", "tpl" },
2024-03-04 13:46:36 +01:00
usage = "apply (template namespace:)[template name]",
2024-02-29 18:03:30 +01:00
desc = "",
modifiers = { "apply" },
min = 2,
permission = "citizens.templates.apply")
2024-03-04 13:46:36 +01:00
public void apply(CommandContext args, CommandSender sender, NPC npc,
@Arg(value = 1, completionsProvider = TemplateCompletions.class) String templateName)
throws CommandException {
Template template = null;
if (templateName.contains(":")) {
template = registry.getTemplateByNamespacedKey(templateName);
} else {
2024-03-04 13:46:36 +01:00
Collection<Template> templates = registry.getTemplates(templateName);
if (templates.isEmpty())
throw new CommandException(Messages.TEMPLATE_MISSING);
if (templates.size() > 1)
throw new CommandException(Messages.TEMPLATE_PICKER, templateName, Joiner.on(", ").join(templates
.stream().map(t -> t.getNamespace() + ":" + t.getName()).collect(Collectors.toList())));
template = templates.iterator().next();
}
2024-03-04 13:46:36 +01:00
if (template == null)
throw new CommandException(Messages.TEMPLATE_MISSING);
2024-03-04 13:46:36 +01:00
template.apply(npc);
Messaging.sendTr(sender, Messages.TEMPLATE_APPLIED, template.getName(), npc.getName());
}
@Command(
aliases = { "template", "tpl" },
usage = "list",
2024-02-29 18:03:30 +01:00
desc = "",
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);
2024-03-04 13:46:36 +01:00
for (Template template : registry.getAllTemplates()) {
Messaging.send(sender, "[[-]] " + template.getNamespace() + ":" + template.getName());
}
}
public static class TemplateCompletions implements CompletionsProvider {
private final TemplateRegistry templateRegistry;
public TemplateCompletions(Citizens plugin) {
templateRegistry = plugin.getTemplateRegistry();
}
@Override
public Collection<String> getCompletions(CommandContext args, CommandSender sender, NPC npc) {
return templateRegistry.getAllTemplates().stream().map(t -> t.getNamespace() + ":" + t.getName())
.collect(Collectors.toList());
}
}
}