ZNPCsPlus/plugin/src/main/java/lol/pyr/znpcsplus/commands/CreateCommand.java

49 lines
2.0 KiB
Java
Raw Normal View History

2023-05-09 10:47:43 +00:00
package lol.pyr.znpcsplus.commands;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.adventure.command.CommandHandler;
import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.npc.NpcTypeImpl;
2023-05-21 15:41:38 +00:00
import lol.pyr.znpcsplus.npc.NpcTypeRegistryImpl;
2023-05-21 12:45:43 +00:00
import lol.pyr.znpcsplus.util.NpcLocation;
2023-05-09 10:47:43 +00:00
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
2023-05-10 13:42:23 +00:00
import java.util.Collections;
2023-05-09 10:47:43 +00:00
import java.util.List;
public class CreateCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
2023-05-21 15:41:38 +00:00
private final NpcTypeRegistryImpl typeRegistry;
2023-05-21 15:41:38 +00:00
public CreateCommand(NpcRegistryImpl npcRegistry, NpcTypeRegistryImpl typeRegistry) {
this.npcRegistry = npcRegistry;
2023-05-21 12:45:43 +00:00
this.typeRegistry = typeRegistry;
}
2023-05-09 10:47:43 +00:00
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " create <id> <type>");
2023-05-10 15:01:14 +00:00
Player player = context.ensureSenderIsPlayer();
String id = context.popString();
if (npcRegistry.get(id) != null) context.halt(Component.text("NPC with that ID already exists.", NamedTextColor.RED));
2023-05-10 15:01:14 +00:00
NpcTypeImpl type = context.parse(NpcTypeImpl.class);
2023-05-21 12:45:43 +00:00
NpcEntryImpl entry = npcRegistry.create(id, player.getWorld(), type, new NpcLocation(player.getLocation()));
2023-05-10 15:01:14 +00:00
entry.enableEverything();
context.send(Component.text("Created a " + type.getName() + " NPC with ID " + id + ".", NamedTextColor.GREEN));
2023-05-09 10:47:43 +00:00
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
2023-05-21 12:45:43 +00:00
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestStream(typeRegistry.getAll().stream().map(NpcTypeImpl::getName));
2023-05-10 13:42:23 +00:00
return Collections.emptyList();
2023-05-09 10:47:43 +00:00
}
}