mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-19 02:36:59 +00:00
Improved Decree Commands
- Improved parsing of inputs that aren't specified. Now, all arguments that are specified are parsed first, which allows us to assume the non specified ones more easily - Fixes #662
This commit is contained in:
parent
734c0a37f7
commit
13532a1f0a
@ -50,6 +50,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class VirtualDecreeCommand {
|
||||
@ -287,23 +288,30 @@ public class VirtualDecreeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the input a player typed to the parameters of this command
|
||||
* @param sender The sender
|
||||
* @param in The input
|
||||
* @return A map of all the parameter names and their values
|
||||
*/
|
||||
private KMap<String, Object> map(VolmitSender sender, KList<String> in) {
|
||||
KMap<String, Object> data = new KMap<>();
|
||||
KSet<Integer> nowhich = new KSet<>();
|
||||
|
||||
for (int ix = 0; ix < in.size(); ix++) {
|
||||
String i = in.get(ix);
|
||||
KList<String> unknownInputs = new KList<>(in.stream().filter(s -> !s.contains("=")).collect(Collectors.toList()));
|
||||
KList<String> knownInputs = new KList<>(in.stream().filter(s -> s.contains("=")).collect(Collectors.toList()));
|
||||
|
||||
if (i == null) {
|
||||
continue;
|
||||
}
|
||||
//Loop known inputs
|
||||
for (int x = 0; x < knownInputs.size(); x++) {
|
||||
String stringParam = knownInputs.get(x);
|
||||
int original = in.indexOf(stringParam);
|
||||
|
||||
if (i.contains("=")) {
|
||||
String[] v = i.split("\\Q=\\E");
|
||||
String[] v = stringParam.split("\\Q=\\E");
|
||||
String key = v[0];
|
||||
String value = v[1];
|
||||
DecreeParameter param = null;
|
||||
|
||||
//Find decree parameter from string param
|
||||
for (DecreeParameter j : getNode().getParameters()) {
|
||||
for (String k : j.getNames()) {
|
||||
if (k.equalsIgnoreCase(key)) {
|
||||
@ -313,6 +321,7 @@ public class VirtualDecreeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
//If it failed, see if we can find it by checking if the names contain the param
|
||||
if (param == null) {
|
||||
for (DecreeParameter j : getNode().getParameters()) {
|
||||
for (String k : j.getNames()) {
|
||||
@ -324,16 +333,18 @@ public class VirtualDecreeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
//Still failed to find, error them
|
||||
if (param == null) {
|
||||
Iris.debug("Can't find parameter key for " + key + "=" + value + " in " + getPath());
|
||||
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + key);
|
||||
unknownInputs.add(value); //Add the value to the unknowns and see if we can assume it later
|
||||
continue;
|
||||
}
|
||||
|
||||
key = param.getName();
|
||||
|
||||
try {
|
||||
data.put(key, param.getHandler().parse(value, nowhich.contains(ix)));
|
||||
data.put(key, param.getHandler().parse(value, nowhich.contains(original))); //Parse and put
|
||||
} catch (DecreeParsingException e) {
|
||||
Iris.debug("Can't parse parameter value for " + key + "=" + value + " in " + getPath() + " using handler " + param.getHandler().getClass().getSimpleName());
|
||||
sender.sendMessage(C.RED + "Cannot convert \"" + value + "\" into a " + param.getType().getSimpleName());
|
||||
@ -349,34 +360,42 @@ public class VirtualDecreeCommand {
|
||||
}
|
||||
|
||||
Iris.debug("Client chose " + update + " for " + key + "=" + value + " (old) in " + getPath());
|
||||
nowhich.add(ix);
|
||||
in.set(ix--, update);
|
||||
nowhich.add(original);
|
||||
in.set(original, update);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
//Make a list of decree params that haven't been identified
|
||||
KList<DecreeParameter> decreeParameters = new KList<>(getNode().getParameters().stream().filter(param -> !data.contains(param.getName())).collect(Collectors.toList()));
|
||||
|
||||
//Loop Unknown inputs
|
||||
for (int x = 0; x < unknownInputs.size(); x++) {
|
||||
String stringParam = unknownInputs.get(x);
|
||||
int original = in.indexOf(stringParam);
|
||||
try {
|
||||
DecreeParameter par = getNode().getParameters().get(ix);
|
||||
DecreeParameter par = decreeParameters.get(x);
|
||||
|
||||
try {
|
||||
data.put(par.getName(), par.getHandler().parse(i, nowhich.contains(ix)));
|
||||
data.put(par.getName(), par.getHandler().parse(stringParam, nowhich.contains(original)));
|
||||
} catch (DecreeParsingException e) {
|
||||
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
|
||||
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + par.getType().getSimpleName());
|
||||
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + stringParam + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
|
||||
sender.sendMessage(C.RED + "Cannot convert \"" + stringParam + "\" into a " + par.getType().getSimpleName());
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (DecreeWhichException e) {
|
||||
KList<?> validOptions = par.getHandler().getPossibilities(i);
|
||||
KList<?> validOptions = par.getHandler().getPossibilities(stringParam);
|
||||
String update = pickValidOption(sender, validOptions, par.getHandler(), par.getName(), par.getType().getSimpleName());
|
||||
|
||||
if (update == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Iris.debug("Client chose " + update + " for " + par.getName() + "=" + i + " (old) in " + getPath());
|
||||
nowhich.add(ix);
|
||||
in.set(ix--, update);
|
||||
Iris.debug("Client chose " + update + " for " + par.getName() + "=" + stringParam + " (old) in " + getPath());
|
||||
nowhich.add(original);
|
||||
in.set(original, update);
|
||||
}
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + i + " (" + Form.getNumberSuffixThStRd(ix + 1) + " argument)");
|
||||
}
|
||||
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + stringParam + " (" + Form.getNumberSuffixThStRd(x + 1) + " argument)");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user