mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-09 17:26:22 +00:00
Merge remote-tracking branch 'upstream/master' into DecreeCommands
This commit is contained in:
@@ -65,6 +65,12 @@ public interface DecreeParameterHandler<T> {
|
||||
*/
|
||||
default KList<T> getPossibilities(String input)
|
||||
{
|
||||
if(input.trim().isEmpty())
|
||||
{
|
||||
KList<T> f = getPossibilities();
|
||||
return f == null ? new KList<>() : f;
|
||||
}
|
||||
|
||||
input = input.trim();
|
||||
KList<T> possible = getPossibilities();
|
||||
KList<T> matches = new KList<>();
|
||||
|
||||
@@ -47,9 +47,13 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
@Nullable
|
||||
@Override
|
||||
default List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||
return new KList<>();
|
||||
KList<String> enhanced = new KList<>(args);
|
||||
KList<String> v = getRoot().tabComplete(enhanced, enhanced.toString(" "));
|
||||
v.removeDuplicates();
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
default boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
J.aBukkit(() -> {
|
||||
@@ -62,6 +66,11 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
}
|
||||
|
||||
static KList<String> enhanceArgs(String[] args)
|
||||
{
|
||||
return enhanceArgs(args, true);
|
||||
}
|
||||
|
||||
static KList<String> enhanceArgs(String[] args, boolean trim)
|
||||
{
|
||||
KList<String> a = new KList<>();
|
||||
|
||||
@@ -73,15 +82,26 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
StringBuilder flat = new StringBuilder();
|
||||
for(String i : args)
|
||||
{
|
||||
if(i.trim().isEmpty())
|
||||
if(trim)
|
||||
{
|
||||
continue;
|
||||
if(i.trim().isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
flat.append(" ").append(i.trim());
|
||||
}
|
||||
|
||||
flat.append(" ").append(i.trim());
|
||||
else
|
||||
{
|
||||
if(i.endsWith(" "))
|
||||
{
|
||||
flat.append(" ").append(i.trim()).append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flat = new StringBuilder(flat.substring(1).trim());
|
||||
flat = new StringBuilder(flat.length() > 0 ? trim ? flat.toString().trim().length() > 0 ?flat.substring(1).trim() : flat.toString().trim() : flat.substring(1) : flat);
|
||||
StringBuilder arg = new StringBuilder();
|
||||
boolean quoting = false;
|
||||
|
||||
@@ -93,7 +113,7 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
|
||||
if(i == ' ' && !quoting)
|
||||
{
|
||||
if(!arg.toString().trim().isEmpty())
|
||||
if(!arg.toString().trim().isEmpty() && trim)
|
||||
{
|
||||
a.add(arg.toString().trim());
|
||||
arg = new StringBuilder();
|
||||
@@ -113,7 +133,7 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
|
||||
if(hasNext && j == ' ')
|
||||
{
|
||||
if(!arg.toString().trim().isEmpty())
|
||||
if(!arg.toString().trim().isEmpty() && trim)
|
||||
{
|
||||
a.add(arg.toString().trim());
|
||||
arg = new StringBuilder();
|
||||
@@ -122,7 +142,7 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
|
||||
else if(!hasNext)
|
||||
{
|
||||
if(!arg.toString().trim().isEmpty())
|
||||
if(!arg.toString().trim().isEmpty() && trim)
|
||||
{
|
||||
a.add(arg.toString().trim());
|
||||
arg = new StringBuilder();
|
||||
@@ -137,7 +157,7 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
|
||||
}
|
||||
}
|
||||
|
||||
if(!arg.toString().trim().isEmpty())
|
||||
if(!arg.toString().trim().isEmpty() && trim)
|
||||
{
|
||||
a.add(arg.toString().trim());
|
||||
}
|
||||
|
||||
@@ -1,74 +1,61 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.decree.handlers;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.decree.DecreeParameterHandler;
|
||||
import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
|
||||
import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
|
||||
|
||||
import java.util.Locale;
|
||||
import com.volmit.iris.util.math.M;
|
||||
import com.volmit.iris.util.math.RNG;
|
||||
|
||||
public class BooleanHandler implements DecreeParameterHandler<Boolean> {
|
||||
private static final KList<String> trues = new KList<>(
|
||||
"true",
|
||||
"yes",
|
||||
"t",
|
||||
"1"
|
||||
);
|
||||
private static final KList<String> falses = new KList<>(
|
||||
"false",
|
||||
"no",
|
||||
"f",
|
||||
"0"
|
||||
);
|
||||
|
||||
/**
|
||||
* Should return the possible values for this type
|
||||
*
|
||||
* @return Possibilities for this type.
|
||||
*/
|
||||
@Override
|
||||
public KList<Boolean> getPossibilities() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converting the type back to a string (inverse of the {@link #parse(String) parse} method)
|
||||
*
|
||||
* @param aBoolean The input of the designated type to convert to a String
|
||||
* @return The resulting string
|
||||
*/
|
||||
@Override
|
||||
public String toString(Boolean aBoolean) {
|
||||
return aBoolean.toString();
|
||||
public String toString(Boolean aByte) {
|
||||
return aByte.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Should parse a String into the designated type
|
||||
*
|
||||
* @param in The string to parse
|
||||
* @return The value extracted from the string, of the designated type
|
||||
* @throws DecreeParsingException Thrown when the parsing fails (ex: "oop" translated to an integer throws this)
|
||||
* @throws DecreeWhichException Thrown when multiple results are possible
|
||||
*/
|
||||
@Override
|
||||
public Boolean parse(String in) throws DecreeParsingException, DecreeWhichException {
|
||||
if (trues.contains(in.toLowerCase())){
|
||||
return true;
|
||||
public Boolean parse(String in) throws DecreeParsingException {
|
||||
try
|
||||
{
|
||||
return Boolean.parseBoolean(in);
|
||||
}
|
||||
if (falses.contains(in.toLowerCase())){
|
||||
return false;
|
||||
|
||||
catch(Throwable e)
|
||||
{
|
||||
throw new DecreeParsingException("Unable to parse boolean \"" + in + "\"");
|
||||
}
|
||||
throw new DecreeParsingException("Cannot convert \"" + in + "\" to a boolean (" + trues.toString(", ") + " / " + falses.toString(", ") + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a certain type is supported by this handler<br>
|
||||
*
|
||||
* @param type The type to check
|
||||
* @return True if supported, false if not
|
||||
*/
|
||||
@Override
|
||||
public boolean supports(Class<?> type) {
|
||||
return type.equals(boolean.class) || type.equals(Boolean.class);
|
||||
return type.equals(Boolean.class) || type.equals(boolean.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRandomDefault()
|
||||
{
|
||||
return M.r(0.5) + "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,6 +171,141 @@ public class VirtualDecreeCommand {
|
||||
return node != null;
|
||||
}
|
||||
|
||||
public KList<String> tabComplete(KList<String> args, String raw)
|
||||
{
|
||||
KList<Integer> skip = new KList<>();
|
||||
KList<String> tabs = new KList<>();
|
||||
invokeTabComplete(args, skip, tabs, raw);
|
||||
return tabs;
|
||||
}
|
||||
|
||||
private boolean invokeTabComplete(KList<String> args, KList<Integer> skip, KList<String> tabs, String raw)
|
||||
{
|
||||
if(isStudio() && !IrisSettings.get().isStudio())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(isNode())
|
||||
{
|
||||
tab(args, tabs);
|
||||
skip.add(hashCode());
|
||||
return false;
|
||||
}
|
||||
|
||||
if(args.isEmpty())
|
||||
{
|
||||
tab(args, tabs);
|
||||
return true;
|
||||
}
|
||||
|
||||
String head = args.get(0);
|
||||
|
||||
if (args.size() > 1 || head.endsWith(" "))
|
||||
{
|
||||
VirtualDecreeCommand match = matchNode(head, skip);
|
||||
|
||||
if(match != null)
|
||||
{
|
||||
args.pop();
|
||||
return match.invokeTabComplete(args, skip, tabs, raw);
|
||||
}
|
||||
|
||||
skip.add(hashCode());
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
tab(args, tabs);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void tab(KList<String> args, KList<String> tabs) {
|
||||
String last = null;
|
||||
KList<DecreeParameter> ignore = new KList<>();
|
||||
Runnable la = () -> {
|
||||
|
||||
};
|
||||
for(String a : args)
|
||||
{
|
||||
la.run();
|
||||
last = a;
|
||||
la = () -> {
|
||||
if(isNode())
|
||||
{
|
||||
String sea = a.contains("=") ? a.split("\\Q=\\E")[0] : a;
|
||||
sea = sea.trim();
|
||||
|
||||
searching: for(DecreeParameter i : getNode().getParameters())
|
||||
{
|
||||
for(String m : i.getNames())
|
||||
{
|
||||
if(m.equalsIgnoreCase(sea) || m.toLowerCase().contains(sea.toLowerCase()) || sea.toLowerCase().contains(m.toLowerCase()))
|
||||
{
|
||||
ignore.add(i);
|
||||
continue searching;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if(last != null)
|
||||
{
|
||||
if (isNode()) {
|
||||
for(DecreeParameter i : getNode().getParameters())
|
||||
{
|
||||
if(ignore.contains(i))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int g = 0;
|
||||
|
||||
if(last.contains("="))
|
||||
{
|
||||
String[] vv = last.trim().split("\\Q=\\E");
|
||||
String vx = vv.length == 2 ? vv[1] : "";
|
||||
for(String f : i.getHandler().getPossibilities(vx).convert((v) -> i.getHandler().toStringForce(v)))
|
||||
{
|
||||
g++;
|
||||
tabs.add(i.getName() +"="+ f);
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for(String f : i.getHandler().getPossibilities("").convert((v) -> i.getHandler().toStringForce(v)))
|
||||
{
|
||||
g++;
|
||||
tabs.add(i.getName() +"="+ f);
|
||||
}
|
||||
}
|
||||
|
||||
if(g == 0)
|
||||
{
|
||||
tabs.add(i.getName() + "=");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for(VirtualDecreeCommand i : getNodes())
|
||||
{
|
||||
String m = i.getName();
|
||||
if(m.equalsIgnoreCase(last) || m.toLowerCase().contains(last.toLowerCase()) || last.toLowerCase().contains(m.toLowerCase()))
|
||||
{
|
||||
tabs.addAll(i.getNames());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private KMap<String, Object> map(VolmitSender sender, KList<String> in)
|
||||
{
|
||||
KMap<String, Object> data = new KMap<>();
|
||||
@@ -396,8 +531,43 @@ public class VirtualDecreeCommand {
|
||||
return true;
|
||||
}
|
||||
|
||||
public KList<VirtualDecreeCommand> matchAllNodes(String in)
|
||||
{
|
||||
KList<VirtualDecreeCommand> g = new KList<>();
|
||||
|
||||
if(in.trim().isEmpty())
|
||||
{
|
||||
g.addAll(nodes);
|
||||
return g;
|
||||
}
|
||||
|
||||
for(VirtualDecreeCommand i : nodes)
|
||||
{
|
||||
if(i.matches(in))
|
||||
{
|
||||
g.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
for(VirtualDecreeCommand i : nodes)
|
||||
{
|
||||
if(i.deepMatches(in))
|
||||
{
|
||||
g.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
g.removeDuplicates();
|
||||
return g;
|
||||
}
|
||||
|
||||
public VirtualDecreeCommand matchNode(String in, KList<Integer> skip)
|
||||
{
|
||||
if(in.trim().isEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
for(VirtualDecreeCommand i : nodes)
|
||||
{
|
||||
if(skip.contains(i.hashCode()))
|
||||
|
||||
Reference in New Issue
Block a user