Add sendCommand() on Glimmer because beta does not break line on sendMessage() line break.
Subcommands are now mapped in the class Commands. Add usage() and description() to Subcommand. Add help command.
This commit is contained in:
parent
6250c48d81
commit
0b04c0553a
12 changed files with 154 additions and 44 deletions
|
|
@ -0,0 +1,26 @@
|
|||
package io.github.adrianvic.nemesiseye.commands;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.commands.sub.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Commands {
|
||||
private static final Map<String, Subcommand> commands = new HashMap<>();
|
||||
|
||||
static {
|
||||
commands.put("help", new Help());
|
||||
commands.put("listpolicies", new ListPolicies());
|
||||
commands.put("currentpolicies", new CurrentPolicies());
|
||||
commands.put("policyinfo", new PolicyInfo());
|
||||
commands.put("reload", new Reload());
|
||||
}
|
||||
|
||||
public static Map<String, Subcommand> getAll() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
public static Subcommand get(String type) {
|
||||
return commands.get(type);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,33 +2,26 @@ package io.github.adrianvic.nemesiseye.commands;
|
|||
|
||||
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||
import io.github.adrianvic.nemesiseye.commands.sub.*;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import io.github.adrianvic.nemesiseye.commands.Commands;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import java.util.*;
|
||||
|
||||
public class EyeCore {
|
||||
public final Map<String, Subcommand> subs = new HashMap<>();
|
||||
private final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
public EyeCore() {
|
||||
register(new Reload());
|
||||
register(new ListPolicies());
|
||||
register(new PolicyInfo());
|
||||
register(new CurrentPolicies());
|
||||
}
|
||||
|
||||
private void register(Subcommand sub) {
|
||||
subs.put(sub.name(), sub);
|
||||
}
|
||||
public EyeCore() {}
|
||||
|
||||
public boolean onCommand(CommandSender commandSender, Command command, String s, String [] strings) {
|
||||
if (strings.length == 0) {
|
||||
commandSender.sendMessage("""
|
||||
glim.sendMessage(commandSender, """
|
||||
Eye of Nemesis version %s
|
||||
Usage: '/eye <command>'
|
||||
Use '/eye help' for a list of available commands
|
||||
""".formatted(Nemesis.getInstance().getDescription().getVersion()));
|
||||
} else {
|
||||
Subcommand sub = subs.get(strings[0].toLowerCase());
|
||||
Subcommand sub = Commands.get(strings[0].toLowerCase());
|
||||
if (sub == null) {
|
||||
commandSender.sendMessage("Unknown command, try '/eye help' to list available commands.");
|
||||
return true;
|
||||
|
|
@ -40,14 +33,12 @@ public class EyeCore {
|
|||
|
||||
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String [] strings) {
|
||||
if (strings.length == 1) {
|
||||
return new ArrayList<>(subs.keySet());
|
||||
return new ArrayList<>(Commands.getAll().keySet());
|
||||
}
|
||||
Subcommand sub = subs.get(strings[0].toLowerCase());
|
||||
Subcommand sub = Commands.get(strings[0].toLowerCase());
|
||||
if (sub != null) {
|
||||
return sub.onTabComplete(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
public Map<String, Subcommand> getSubs() { return subs; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,6 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public class CurrentPolicies implements Subcommand {
|
||||
@Override
|
||||
public String name() {
|
||||
return "currentpolicies";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] args) {
|
||||
List<Policy> policies = Validator.getPoliciesForEntity((HumanEntity) commandSender);
|
||||
|
|
@ -34,4 +29,14 @@ public class CurrentPolicies implements Subcommand {
|
|||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Lists policies appliying to you.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||
import io.github.adrianvic.nemesiseye.commands.Commands;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Help implements Subcommand {
|
||||
private final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] strings) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
Map<String, Subcommand> allSubcommands = Commands.getAll();
|
||||
if (allSubcommands.isEmpty()) {
|
||||
rstr.add("No commands found.");
|
||||
}
|
||||
for (Map.Entry<String, Subcommand> e : allSubcommands.entrySet()) {
|
||||
rstr.add("""
|
||||
%s - %s
|
||||
Usage: /eye %s %s
|
||||
""".formatted(e.getKey(), e.getValue().description(), e.getKey(), e.getValue()));
|
||||
}
|
||||
|
||||
glim.sendMessage(commandSender, String.join("\n", rstr));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String[] strings) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Lists all commands.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
@ -8,12 +8,6 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
public class ListPolicies implements Subcommand {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "listpolicies";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] args) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
|
|
@ -28,4 +22,14 @@ public class ListPolicies implements Subcommand {
|
|||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Lists all loaded policies.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,23 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Config;
|
||||
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||
import io.github.adrianvic.nemesiseye.policy.Policy;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PolicyInfo implements Subcommand {
|
||||
@Override
|
||||
public String name() {
|
||||
return "policyinfo";
|
||||
}
|
||||
private final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] strings) {
|
||||
List<Policy> policies = Config.getInstance().getPolicies();
|
||||
for (Policy policy : policies) {
|
||||
if (policy.name().equals(strings[0])) {
|
||||
commandSender.sendMessage(String.format("""
|
||||
glim.sendMessage(commandSender, String.format("""
|
||||
Showing info for policy "%s":
|
||||
Type: %s
|
||||
Nodes: %s
|
||||
|
|
@ -37,4 +36,14 @@ public class PolicyInfo implements Subcommand {
|
|||
}
|
||||
return rstr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Shows info for a specified policy.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return "<policy>";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import org.bukkit.command.CommandSender;
|
|||
import java.util.List;
|
||||
|
||||
public class Reload implements Subcommand {
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String [] strings) {
|
||||
Config.getInstance().load();
|
||||
|
|
@ -20,7 +19,12 @@ public class Reload implements Subcommand {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "reload";
|
||||
public String description() {
|
||||
return "Reloads the plugin configuration file.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ import org.bukkit.command.CommandSender;
|
|||
import java.util.List;
|
||||
|
||||
public interface Subcommand {
|
||||
String name();
|
||||
String description();
|
||||
String usage();
|
||||
@SuppressWarnings("SameReturnValue")
|
||||
boolean execute(CommandSender commandSender, String[] strings);
|
||||
List<String> onTabComplete(CommandSender sender, String[] strings);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package io.github.adrianvic.nemesiseye.reflection;
|
|||
import io.github.adrianvic.nemesiseye.policy.Policy;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
|
@ -12,15 +13,23 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
public interface Glimmer {
|
||||
void onLoad();
|
||||
|
||||
List<World> getWorlds();
|
||||
|
||||
// Configuration
|
||||
File loadConfigFile();
|
||||
List<Policy> loadPoliciesFromFile(File file);
|
||||
List<Policy> getApplyingPoliciesForEntity(HumanEntity entity, List<Policy> policies);
|
||||
void onLoad();
|
||||
ItemStack getItemInMainHandHumanEntity(HumanEntity entity);
|
||||
|
||||
// Items
|
||||
boolean hasItemMeta(ItemStack item);
|
||||
List<World> getWorlds();
|
||||
boolean hasEnchantment(ItemStack item, Map<String, String> valuesmap);
|
||||
boolean hasAnyEnchantment(ItemStack itemStack);
|
||||
ItemStack getItemInMainHandHumanEntity(HumanEntity entity);
|
||||
|
||||
// Commands
|
||||
void sendMessage(CommandSender commandSender, String text);
|
||||
|
||||
class Box {
|
||||
public final double x1, y1, z1, x2, y2, z2;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue