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

49 lines
2.2 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.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.npc.NpcTypeImpl;
2023-05-10 15:01:14 +00:00
import lol.pyr.znpcsplus.skin.descriptor.PrefetchedDescriptor;
2023-05-09 10:47:43 +00:00
import lol.pyr.znpcsplus.util.ZLocation;
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 {
@Override
public void run(CommandContext context) throws CommandExecutionException {
2023-05-10 15:01:14 +00:00
context.setUsage(context.getLabel() + " create <npc_id> <npc_type> <npc_name>");
Player player = context.ensureSenderIsPlayer();
String id = context.popString();
if (NpcRegistryImpl.get().get(id) != null) context.halt(Component.text("NPC with that ID already exists.", NamedTextColor.RED));
NpcTypeImpl type = context.parse(NpcTypeImpl.class);
String name = context.popString();
NpcEntryImpl entry = NpcRegistryImpl.get().create(id, player.getWorld(), type, new ZLocation(player.getLocation()));
entry.enableEverything();
NpcImpl npc = entry.getNpc();
if (type == NpcTypeImpl.PLAYER) PrefetchedDescriptor.forPlayer(name).thenAccept(skin -> npc.setProperty(EntityPropertyImpl.SKIN, skin));
npc.getHologram().addLine(Component.text(name));
context.send(Component.text("Created NPC with ID " + id + " and name " + name + ".", NamedTextColor.GREEN));
2023-05-09 10:47:43 +00:00
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
2023-05-10 13:42:23 +00:00
if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds());
if (context.argSize() == 2) return context.suggestStream(NpcTypeImpl.values().stream().map(NpcTypeImpl::getName));
return Collections.emptyList();
2023-05-09 10:47:43 +00:00
}
}