This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nulli0n
committed
May 11, 2023
1 parent
9a2c461
commit 0a9de23
Showing
14 changed files
with
454 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
NexEngine/src/main/java/su/nexmedia/engine/api/command/CommandFlag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
NexEngine/src/main/java/su/nexmedia/engine/api/command/CommandResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.