Added /mmocore quest <cancel/start> <player> <quest>

This commit is contained in:
Indyuce 2020-04-12 22:28:44 +02:00
parent d8f1e8d9e9
commit 1112093bd9
8 changed files with 43 additions and 24 deletions

View File

@ -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;
}

View File

@ -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<String> 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());
}

View File

@ -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<String> getKeys() {
return floors.keySet();
}
public List<String> calculateTabCompletion(int n) {
public abstract CommandResult execute(CommandSender sender, String[] args);
public List<String> 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<String> 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;
}

View File

@ -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<String> readTabCompletion() {
return parameter < 0 ? new ArrayList<>() : current.calculateTabCompletion(Math.max(0, parameter - 1));
return current.calculateTabCompletion(Math.max(0, parameter - 1));
}
}
}

View File

@ -17,7 +17,10 @@ public class Parameter {
list.add("main");
});
public static final Parameter PLAYER = new Parameter("<player>", 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("<quest>",
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("<amount>", list -> {
for (int j = 0; j <= 10; j++)
list.add("" + j);

View File

@ -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");

View File

@ -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");

View File

@ -12,7 +12,7 @@ import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.quest.Quest;
public class QuestManager extends MMOManager {
private Map<String, Quest> quests = new LinkedHashMap<>();
private final Map<String, Quest> quests = new LinkedHashMap<>();
public void load(File file) {
if (file.isDirectory())