From 1112093bd99ab955a544f74358dced79ac581b7f Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sun, 12 Apr 2020 22:28:44 +0200 Subject: [PATCH] Added /mmocore quest --- .../net/Indyuce/mmocore/api/quest/Quest.java | 2 +- .../mmocore/command/MMOCoreCommand.java | 4 ++- .../mmocore/command/api/CommandMap.java | 27 +++++++++++++------ .../mmocore/command/api/CommandRoot.java | 23 +++++++++------- .../mmocore/command/api/Parameter.java | 5 +++- .../rpg/admin/ExperienceCommandMap.java | 2 +- .../command/rpg/admin/ResetCommandMap.java | 2 +- .../Indyuce/mmocore/manager/QuestManager.java | 2 +- 8 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/quest/Quest.java b/src/main/java/net/Indyuce/mmocore/api/quest/Quest.java index 20c7e34d..538da1f3 100644 --- a/src/main/java/net/Indyuce/mmocore/api/quest/Quest.java +++ b/src/main/java/net/Indyuce/mmocore/api/quest/Quest.java @@ -39,7 +39,7 @@ public class Quest { private FileConfiguration loaded; public Quest(String id, FileConfiguration config) { - this.id = id; + this.id = id.toLowerCase().replace("_", "-").replace(" ", "-"); loaded = config; } diff --git a/src/main/java/net/Indyuce/mmocore/command/MMOCoreCommand.java b/src/main/java/net/Indyuce/mmocore/command/MMOCoreCommand.java index c4864cda..955b85b1 100644 --- a/src/main/java/net/Indyuce/mmocore/command/MMOCoreCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/MMOCoreCommand.java @@ -16,6 +16,7 @@ import net.Indyuce.mmocore.command.rpg.ReloadCommandMap; import net.Indyuce.mmocore.command.rpg.admin.AdminCommandMap; import net.Indyuce.mmocore.command.rpg.booster.BoosterCommandMap; import net.Indyuce.mmocore.command.rpg.debug.DebugCommandMap; +import net.Indyuce.mmocore.command.rpg.quest.QuestCommandMap; import net.Indyuce.mmocore.command.rpg.waypoint.WaypointsCommandMap; public class MMOCoreCommand extends CommandRoot implements CommandExecutor, TabCompleter { @@ -29,6 +30,7 @@ public class MMOCoreCommand extends CommandRoot implements CommandExecutor, TabC addFloor(new DebugCommandMap(this)); addFloor(new BoosterCommandMap(this)); addFloor(new WaypointsCommandMap(this)); + addFloor(new QuestCommandMap(this)); } @Override @@ -50,7 +52,7 @@ public class MMOCoreCommand extends CommandRoot implements CommandExecutor, TabC if (!sender.hasPermission("mmocore.admin")) return new ArrayList<>(); - CommandReader reader = readCommand(args); + CommandParser reader = readCommand(args); List list = reader.readTabCompletion(); return args[args.length - 1].isEmpty() ? list : list.stream().filter(string -> string.toLowerCase().startsWith(args[args.length - 1].toLowerCase())).collect(Collectors.toList()); } diff --git a/src/main/java/net/Indyuce/mmocore/command/api/CommandMap.java b/src/main/java/net/Indyuce/mmocore/command/api/CommandMap.java index 9c2608ae..d45141c2 100644 --- a/src/main/java/net/Indyuce/mmocore/command/api/CommandMap.java +++ b/src/main/java/net/Indyuce/mmocore/command/api/CommandMap.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.lang.Validate; import org.bukkit.command.CommandSender; public abstract class CommandMap { @@ -14,7 +15,15 @@ public abstract class CommandMap { private final String id; private final CommandMap parent; + public static final CommandMap EMPTY = new CommandMap(null, "empty") { + public CommandResult execute(CommandSender sender, String[] args) { + return CommandResult.THROW_USAGE; + } + }; + public CommandMap(CommandMap parent, String id) { + Validate.isTrue(!(parent instanceof CommandEnd), "You cannot use a CommandEnd as a parent"); + this.id = id; this.parent = parent; } @@ -47,26 +56,28 @@ public abstract class CommandMap { floors.put(floor.getId(), floor); } - public abstract CommandResult execute(CommandSender sender, String[] args); - public Set getKeys() { return floors.keySet(); } - public List calculateTabCompletion(int n) { + public abstract CommandResult execute(CommandSender sender, String[] args); + + public List calculateTabCompletion(int parameterIndex) { /* - * add extra floor keys + * add extra floor keys. only commandEnds can have parameters, that + * means commands must be clean and cannot have both floors and + * parameters to input */ List list = new ArrayList<>(); getKeys().forEach(key -> list.add(key)); /* - * add final arguments if needed. + * if the player is at the end of a command branch, display the + * parameter with the right index that the player must input */ - if (isEnd()) - if (((CommandEnd) this).getParameters().size() > n) - ((CommandEnd) this).getParameters().get(n).autoComplete(list); + if (isEnd() && ((CommandEnd) this).getParameters().size() > parameterIndex) + ((CommandEnd) this).getParameters().get(parameterIndex).autoComplete(list); return list; } diff --git a/src/main/java/net/Indyuce/mmocore/command/api/CommandRoot.java b/src/main/java/net/Indyuce/mmocore/command/api/CommandRoot.java index 27650e32..7f0699b3 100644 --- a/src/main/java/net/Indyuce/mmocore/command/api/CommandRoot.java +++ b/src/main/java/net/Indyuce/mmocore/command/api/CommandRoot.java @@ -1,6 +1,5 @@ package net.Indyuce.mmocore.command.api; -import java.util.ArrayList; import java.util.List; import org.bukkit.ChatColor; @@ -18,29 +17,33 @@ public abstract class CommandRoot extends CommandMap { read.calculateUsageList().forEach(str -> sender.sendMessage(ChatColor.YELLOW + "/" + str)); } - public CommandReader readCommand(String[] args) { - return new CommandReader(this, args); + public CommandParser readCommand(String[] args) { + return new CommandParser(this, args); } - public class CommandReader { + public class CommandParser { private CommandMap current; private int parameter = 0; - public CommandReader(CommandRoot begin, String[] args) { + /* + * used to parse a command and identify the commandMap which is supposed + * to + */ + public CommandParser(CommandRoot begin, String[] args) { this.current = begin; for (String arg : args) /* - * check if current command map has the corresponding arg, if so - * let the next map handle the command. + * check if current command floor has the corresponding arg, if + * so let the next floor handle the command. */ if (parameter == 0 && current.hasFloor(arg)) current = current.getFloor(arg); - + /* * if the plugin cannot find a command map higher, then the - * current map will handle the command. + * current floor will handle the command. */ else parameter++; @@ -55,7 +58,7 @@ public abstract class CommandRoot extends CommandMap { } public List readTabCompletion() { - return parameter < 0 ? new ArrayList<>() : current.calculateTabCompletion(Math.max(0, parameter - 1)); + return current.calculateTabCompletion(Math.max(0, parameter - 1)); } } } diff --git a/src/main/java/net/Indyuce/mmocore/command/api/Parameter.java b/src/main/java/net/Indyuce/mmocore/command/api/Parameter.java index d7d9e005..df4c1362 100644 --- a/src/main/java/net/Indyuce/mmocore/command/api/Parameter.java +++ b/src/main/java/net/Indyuce/mmocore/command/api/Parameter.java @@ -17,7 +17,10 @@ public class Parameter { list.add("main"); }); public static final Parameter PLAYER = new Parameter("", list -> Bukkit.getOnlinePlayers().forEach(online -> list.add(online.getName()))); - public static final Parameter PLAYER_OPTIONAL = new Parameter("(player)", list -> Bukkit.getOnlinePlayers().forEach(online -> list.add(online.getName()))); + public static final Parameter QUEST = new Parameter("", + list -> MMOCore.plugin.questManager.getAll().forEach(quest -> list.add(quest.getId()))); + public static final Parameter PLAYER_OPTIONAL = new Parameter("(player)", + list -> Bukkit.getOnlinePlayers().forEach(online -> list.add(online.getName()))); public static final Parameter AMOUNT = new Parameter("", list -> { for (int j = 0; j <= 10; j++) list.add("" + j); diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandMap.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandMap.java index 5072c657..3c7ce8fb 100644 --- a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandMap.java +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ExperienceCommandMap.java @@ -15,7 +15,7 @@ import net.Indyuce.mmocore.command.api.CommandEnd; import net.Indyuce.mmocore.command.api.CommandMap; import net.Indyuce.mmocore.command.api.Parameter; -public class ExperienceCommandMap extends CommandEnd { +public class ExperienceCommandMap extends CommandMap { public ExperienceCommandMap(CommandMap parent) { super(parent, "exp"); diff --git a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ResetCommandMap.java b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ResetCommandMap.java index be7e6338..97a1cf37 100644 --- a/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ResetCommandMap.java +++ b/src/main/java/net/Indyuce/mmocore/command/rpg/admin/ResetCommandMap.java @@ -13,7 +13,7 @@ import net.Indyuce.mmocore.command.api.CommandEnd; import net.Indyuce.mmocore.command.api.CommandMap; import net.Indyuce.mmocore.command.api.Parameter; -public class ResetCommandMap extends CommandEnd { +public class ResetCommandMap extends CommandMap { public ResetCommandMap(CommandMap parent) { super(parent, "reset"); diff --git a/src/main/java/net/Indyuce/mmocore/manager/QuestManager.java b/src/main/java/net/Indyuce/mmocore/manager/QuestManager.java index a91cb161..d9bca7ba 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/QuestManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/QuestManager.java @@ -12,7 +12,7 @@ import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.quest.Quest; public class QuestManager extends MMOManager { - private Map quests = new LinkedHashMap<>(); + private final Map quests = new LinkedHashMap<>(); public void load(File file) { if (file.isDirectory())