Initial commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package com.alttd.commands;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.commands.subcommands.CommandCreateVillager;
|
||||
import com.alttd.commands.subcommands.CommandHelp;
|
||||
import com.alttd.commands.subcommands.CommandReload;
|
||||
import com.alttd.config.Config;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.command.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandManager implements CommandExecutor, TabExecutor {
|
||||
private final List<SubCommand> subCommands;
|
||||
private final MiniMessage miniMessage;
|
||||
|
||||
public CommandManager() {
|
||||
VillagerUI villagerUI = VillagerUI.getInstance();
|
||||
|
||||
PluginCommand command = villagerUI.getCommand("villagerui");
|
||||
if (command == null) {
|
||||
subCommands = null;
|
||||
miniMessage = null;
|
||||
villagerUI.getLogger().severe("Unable to find villager ui command.");
|
||||
return;
|
||||
}
|
||||
command.setExecutor(this);
|
||||
command.setTabCompleter(this);
|
||||
|
||||
subCommands = Arrays.asList(
|
||||
new CommandHelp(this),
|
||||
new CommandCreateVillager(),
|
||||
new CommandReload());
|
||||
miniMessage = MiniMessage.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
if (args.length == 0) {
|
||||
commandSender.sendMessage(miniMessage.parse(Config.HELP_MESSAGE_WRAPPER, Template.of("commands", subCommands.stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getHelpMessage)
|
||||
.collect(Collectors.joining("\n")))));
|
||||
return true;
|
||||
}
|
||||
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
|
||||
if (!commandSender.hasPermission(subCommand.getPermission())) {
|
||||
commandSender.sendMessage(miniMessage.parse(Config.NO_PERMISSION));
|
||||
return true;
|
||||
}
|
||||
|
||||
return subCommand.onCommand(commandSender, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
|
||||
if (args.length <= 1) {
|
||||
res.addAll(subCommands.stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getName)
|
||||
.filter(name -> args.length == 0 || name.startsWith(args[0]))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
} else {
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission()))
|
||||
res.addAll(subCommand.getTabComplete(commandSender, args));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<SubCommand> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
private SubCommand getSubCommand(String cmdName) {
|
||||
return subCommands.stream()
|
||||
.filter(subCommand -> subCommand.getName().equals(cmdName))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alttd.commands;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SubCommand {
|
||||
|
||||
private final MiniMessage miniMessage;
|
||||
|
||||
public SubCommand() {
|
||||
miniMessage = MiniMessage.get();
|
||||
}
|
||||
|
||||
public abstract boolean onCommand(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public String getPermission() {
|
||||
return "villagerui." + getName();
|
||||
}
|
||||
|
||||
public abstract List<String> getTabComplete(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getHelpMessage();
|
||||
|
||||
protected MiniMessage getMiniMessage() {
|
||||
return miniMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.config.VillagerConfig;
|
||||
import com.alttd.objects.LoadedVillagers;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Utilities;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandCreateVillager extends SubCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (args.length < 8) {
|
||||
commandSender.sendMessage(getMiniMessage().parse(getHelpMessage()));
|
||||
return true;
|
||||
}
|
||||
|
||||
Optional<VillagerType> first = VillagerType.getVillagerTypes().stream().filter(villagerType -> villagerType.getName().equalsIgnoreCase(args[1])).findFirst();
|
||||
if (first.isEmpty()) {
|
||||
commandSender.sendMessage(getMiniMessage().parse(getHelpMessage()));
|
||||
return true;
|
||||
}
|
||||
VillagerType villagerType = first.get();
|
||||
|
||||
World world = Bukkit.getServer().getWorld(args[7]);
|
||||
if (world == null) {
|
||||
commandSender.sendMessage(getMiniMessage().parse(getHelpMessage()));
|
||||
return true;
|
||||
}
|
||||
Location location = new Location(world, Double.parseDouble(args[2]),Double.parseDouble(args[3]),Double.parseDouble(args[4]), Float.parseFloat(args[5]), Float.parseFloat(args[6]));
|
||||
Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER, CreatureSpawnEvent.SpawnReason.CUSTOM);
|
||||
villager.setPersistent(true);
|
||||
villager.setInvulnerable(true);
|
||||
// villager.setVillagerType(Villager.Type.); TODO choose villager type?
|
||||
villager.setRemoveWhenFarAway(false);
|
||||
villager.customName(getMiniMessage().parse(Config.VILLAGER_NAME, Template.of("name", villagerType.getDisplayName())));
|
||||
villager.setCustomNameVisible(true);
|
||||
villager.setAI(false);
|
||||
|
||||
UUID uuid = villager.getUniqueId();
|
||||
|
||||
LoadedVillagers.addLoadedVillager(uuid, villagerType);
|
||||
VillagerConfig.addVillager(uuid, villagerType);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "createvillager";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
switch (args.length) {
|
||||
case 2 -> res.addAll(VillagerType.getVillagerTypes().stream()
|
||||
.map(VillagerType::getName)
|
||||
.collect(Collectors.toList()));
|
||||
case 3 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(String.valueOf(Utilities.round(player.getLocation().getX(), 2)));
|
||||
}
|
||||
}
|
||||
case 4 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(String.valueOf(Utilities.round(player.getLocation().getY(), 1)));
|
||||
}
|
||||
}
|
||||
case 5 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(String.valueOf(Utilities.round(player.getLocation().getZ(), 2)));
|
||||
}
|
||||
}
|
||||
case 6 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(String.valueOf(Utilities.round(player.getLocation().getYaw(), 2)));
|
||||
}
|
||||
}
|
||||
case 7 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(String.valueOf(Utilities.round(player.getLocation().getPitch(), 2)));
|
||||
}
|
||||
}
|
||||
case 8 -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
res.add(player.getLocation().getWorld().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.CREATE_VILLAGER_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.commands.CommandManager;
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandHelp extends SubCommand {
|
||||
|
||||
private final CommandManager commandManager;
|
||||
|
||||
public CommandHelp(CommandManager commandManager) {
|
||||
super();
|
||||
this.commandManager = commandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
commandSender.sendMessage(getMiniMessage().parse(Config.HELP_MESSAGE_WRAPPER, Template.of("commands", commandManager.getSubCommands().stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getHelpMessage)
|
||||
.collect(Collectors.joining("\n")))));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPermission() {
|
||||
return "villagerui.use";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.HELP_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.config.VillagerConfig;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandReload extends SubCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
Config.reload();
|
||||
VillagerConfig.reload();
|
||||
WorthConfig.reload();
|
||||
commandSender.sendMessage(getMiniMessage().parse("<green>Reloaded VillagerShopUI config.</green>"));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "reload";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.RELOAD_MESSAGE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.commands.subcommands;
|
||||
|
||||
public class CommandRemoveVillager {
|
||||
}
|
||||
Reference in New Issue
Block a user