Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nulli0n committed May 11, 2023
1 parent 9a2c461 commit 0a9de23
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nexmedia.engine.NexPlugin;
import su.nexmedia.engine.api.lang.LangMessage;
import su.nexmedia.engine.api.manager.IPlaceholder;
import su.nexmedia.engine.lang.EngineLang;
import su.nexmedia.engine.utils.Colorizer;
import su.nexmedia.engine.utils.StringUtil;
import su.nexmedia.engine.utils.message.NexParser;
import su.nexmedia.engine.utils.regex.RegexUtil;

import java.util.*;
Expand All @@ -25,13 +24,20 @@ public abstract class AbstractCommand<P extends NexPlugin<P>> implements IPlaceh
public static final String PLACEHOLDER_DESCRIPTION = "%command_description%";
public static final String PLACEHOLDER_LABEL = "%command_label%";

private final Map<String, Pattern> flags;
@Deprecated private static final String FLAG_PATTERN = "(.*?)(?:-|$)";

protected P plugin;
protected String[] aliases;
protected String permission;
protected Map<String, AbstractCommand<P>> childrens;
protected AbstractCommand<P> parent;
@Deprecated private final Map<String, Pattern> flags;

protected final P plugin;
protected final Map<String, AbstractCommand<P>> childrens;
protected final Set<CommandFlag<?>> commandFlags;

protected AbstractCommand<P> parent;
protected String[] aliases;
protected String permission;
protected String usage;
protected String description;
protected boolean playerOnly;

public AbstractCommand(@NotNull P plugin, @NotNull List<String> aliases) {
this(plugin, aliases.toArray(new String[0]));
Expand All @@ -58,6 +64,7 @@ public AbstractCommand(@NotNull P plugin, @NotNull String[] aliases, @Nullable S
this.aliases = Stream.of(aliases).map(String::toLowerCase).toArray(String[]::new);
this.permission = permission;
this.childrens = new TreeMap<>();
this.commandFlags = new HashSet<>();
this.flags = new HashMap<>();
}

Expand Down Expand Up @@ -113,33 +120,98 @@ public final String getPermission() {
return this.permission;
}

public void setPermission(@Nullable Permission permission) {
this.setPermission(permission == null ? null : permission.getName());
}

public void setPermission(@Nullable String permission) {
this.permission = permission;
}

@NotNull
@Deprecated
public Set<String> getFlags() {
return new HashSet<>(this.flags.keySet());
}

@NotNull
public Set<CommandFlag<?>> getCommandFlags() {
return commandFlags;
}

public final void addFlag(@NotNull CommandFlag<?>... flags) {
for (CommandFlag<?> flag : flags) this.addFlag(flag);
}

public final void addFlag(@NotNull CommandFlag<?> flag) {
this.getCommandFlags().add(flag);
}

@Deprecated
public final void registerFlag(@NotNull String... flags) {
for (String flag : flags) this.registerFlag(flag);
}

@Deprecated
public final void registerFlag(@NotNull String flag) {
this.flags.put(flag, Pattern.compile("-" + flag + NexParser.OPTION_PATTERN));
this.flags.put(flag, Pattern.compile("-" + flag + FLAG_PATTERN));//NexParser.OPTION_PATTERN));
}

public final void unregisterFlag(@NotNull String flag) {
this.flags.remove(flag);
}

@NotNull
public abstract String getUsage();
public String getUsage() {
return this.usage == null ? "" : this.usage;
}

public void setUsage(@NotNull LangMessage message) {
this.setUsage(message.getLocalized());
}

public void setUsage(@NotNull String usage) {
this.usage = usage;
}

@NotNull
public abstract String getDescription();
public String getDescription() {
return this.description == null ? "" : this.description;
}

public abstract boolean isPlayerOnly();
public void setDescription(@NotNull LangMessage message) {
this.setDescription(message.getLocalized());
}

public void setDescription(@NotNull String description) {
this.description = description;
}

public boolean isPlayerOnly() {
return this.playerOnly;
}

public void setPlayerOnly(boolean playerOnly) {
this.playerOnly = playerOnly;
}

public final boolean hasPermission(@NotNull CommandSender sender) {
return this.permission == null || sender.hasPermission(this.permission);
}

@NotNull
public List<String> getTab(@NotNull Player player, int arg, @NotNull String[] args) {
return Collections.emptyList();
return this.getCommandFlags().stream().map(CommandFlag::getNamePrefixed).toList();
}

protected abstract void onExecute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args, @NotNull Map<String, String> flags);
@Deprecated
protected void onExecute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args, @NotNull Map<String, String> flags) {

}

protected void onExecute(@NotNull CommandSender sender, @NotNull CommandResult result) {

}

public final void execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) {
if (this.isPlayerOnly() && !(sender instanceof Player)) {
Expand All @@ -160,18 +232,31 @@ public final void execute(@NotNull CommandSender sender, @NotNull String label,

Matcher matcher = RegexUtil.getMatcher(pattern, argLine);
if (RegexUtil.matcherFind(matcher)) {
flags.put(flag, Colorizer.legacyHex(matcher.group(1)));
argLine = StringUtil.oneSpace(argLine.replace(matcher.group(0), ""));
flags.put(flag, Colorizer.legacyHex(matcher.group(2)));
argLine = /*StringUtil.oneSpace(*/argLine.replace(matcher.group(1), "")/*)*/;
}
}
args = argLine.split(" ");
}

this.onExecute(sender, label, args, flags);
}

public final boolean hasPermission(@NotNull CommandSender sender) {
return this.permission == null || sender.hasPermission(this.permission);
// API UPGRADE
CommandResult result = new CommandResult(label, args, new HashMap<>());
String argLine = String.join(" ", args);
for (CommandFlag<?> flag : this.getCommandFlags()) {
String name = flag.getName();
Pattern pattern = flag.getPattern();

Matcher matcher = RegexUtil.getMatcher(pattern, argLine);
if (RegexUtil.matcherFind(matcher)) {
result.getFlags().put(flag, matcher.group(2).trim());
argLine = argLine.replace(matcher.group(0), "");
}
}
result.setArgs(argLine.isEmpty() ? new String[0] : argLine.trim().split(" "));

this.onExecute(sender, result);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package su.nexmedia.engine.api.command;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import su.nexmedia.engine.utils.Colorizer;
import su.nexmedia.engine.utils.StringUtil;

import java.util.function.Function;
import java.util.regex.Pattern;

public class CommandFlag<T> {

private final String name;
private final Pattern pattern;
private final Function<String, T> parser;

public CommandFlag(@NotNull String name, @NotNull Function<String, T> parser) {
this.name = name;
//this.pattern = Pattern.compile("(-" + name + "(.*?))(?:-|$)");
//this.pattern = Pattern.compile("-" + name + "(.*?(?:-|$|\\S+))");

//this.pattern = Pattern.compile("-" + name + "(\\s|$)((?=-)|\\S*)"); valid
this.pattern = Pattern.compile("-" + name + "(\\s|$)([^-]*)"); // experimantal
this.parser = parser;
}

@NotNull
public static CommandFlag<World> worldFlag(@NotNull String name) {
return new CommandFlag<>(name, Bukkit::getWorld);
}

@NotNull
public static CommandFlag<String> stringFlag(@NotNull String name) {
return new CommandFlag<>(name, Function.identity());
}

@NotNull
public static CommandFlag<String> textFlag(@NotNull String name) {
return new CommandFlag<>(name, Colorizer::apply);
}

@NotNull
public static CommandFlag<Integer> intFlag(@NotNull String name) {
return new CommandFlag<>(name, str -> StringUtil.getInteger(str, 0, true));
}

@NotNull
public static CommandFlag<Double> doubleFlag(@NotNull String name) {
return new CommandFlag<>(name, str -> StringUtil.getDouble(str, 0, true));
}

@NotNull
public static CommandFlag<Boolean> booleanFlag(@NotNull String name) {
return new CommandFlag<>(name, str -> true);
}

@NotNull
public String getName() {
return name;
}

@NotNull
public String getNamePrefixed() {
return "-" + name;
}

@NotNull
public Pattern getPattern() {
return pattern;
}

@NotNull
public Function<String, T> getParser() {
return parser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package su.nexmedia.engine.api.command;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nexmedia.engine.utils.StringUtil;

import java.util.Map;

public class CommandResult {

private final String label;
private final Map<CommandFlag<?>, String> flags;

private String[] args;

public CommandResult(@NotNull String label, String[] args, @NotNull Map<CommandFlag<?>, String> flags) {
this.label = label;
this.flags = flags;
this.setArgs(args);
}

public int length() {
return this.args.length;
}

public void setArgs(@NotNull String[] args) {
this.args = args;
}

@NotNull
public String getArg(int index) {
return this.getArgs()[index];
}

@NotNull
public String getArg(int index, @NotNull String def) {
if (index >= this.length()) return def;

return this.getArgs()[index];
}

public int getInt(int index, int def) {
return StringUtil.getInteger(this.getArg(index, ""), def, true);
}

public double getDouble(int index, double def) {
return StringUtil.getDouble(this.getArg(index, ""), def, true);
}

public boolean hasFlag(@NotNull CommandFlag<?> flag) {
return this.getFlags().containsKey(flag);
}

@Nullable
public <T> T getFlag(@NotNull CommandFlag<T> flag) {
String value = this.getFlags().get(flag);
if (value == null) return null;

return flag.getParser().apply(value);
}

@NotNull
public <T> T getFlag(@NotNull CommandFlag<T> flag, @NotNull T def) {
T value = this.getFlag(flag);
return value == null ? def : value;
}

@NotNull
public String getLabel() {
return label;
}

public String[] getArgs() {
return args;
}

@NotNull
public Map<CommandFlag<?>, String> getFlags() {
return flags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

public abstract class GeneralCommand<P extends NexPlugin<P>> extends AbstractCommand<P> implements CommandExecutor, TabExecutor {

private Command fallback;
private AbstractCommand<P> defaultCommand;

public GeneralCommand(@NotNull P plugin, @NotNull List<String> aliases) {
Expand Down Expand Up @@ -49,6 +50,14 @@ public void addDefaultCommand(@NotNull AbstractCommand<P> command) {
this.defaultCommand = command;
}

public Command getFallback() {
return fallback;
}

public void setFallback(@NotNull Command fallback) {
this.fallback = fallback;
}

@NotNull
public AbstractCommand<P> findChildren(@NotNull String[] args) {
AbstractCommand<P> command = this;//.defaultCommand;
Expand Down
Loading

0 comments on commit 0a9de23

Please sign in to comment.