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:
StrangeOne101 2021-10-01 18:40:57 +13:00
parent 734c0a37f7
commit 13532a1f0a

View File

@ -50,6 +50,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
@Data @Data
public class VirtualDecreeCommand { 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) { private KMap<String, Object> map(VolmitSender sender, KList<String> in) {
KMap<String, Object> data = new KMap<>(); KMap<String, Object> data = new KMap<>();
KSet<Integer> nowhich = new KSet<>(); KSet<Integer> nowhich = new KSet<>();
for (int ix = 0; ix < in.size(); ix++) { KList<String> unknownInputs = new KList<>(in.stream().filter(s -> !s.contains("=")).collect(Collectors.toList()));
String i = in.get(ix); KList<String> knownInputs = new KList<>(in.stream().filter(s -> s.contains("=")).collect(Collectors.toList()));
if (i == null) { //Loop known inputs
continue; for (int x = 0; x < knownInputs.size(); x++) {
} String stringParam = knownInputs.get(x);
int original = in.indexOf(stringParam);
if (i.contains("=")) { String[] v = stringParam.split("\\Q=\\E");
String[] v = i.split("\\Q=\\E");
String key = v[0]; String key = v[0];
String value = v[1]; String value = v[1];
DecreeParameter param = null; DecreeParameter param = null;
//Find decree parameter from string param
for (DecreeParameter j : getNode().getParameters()) { for (DecreeParameter j : getNode().getParameters()) {
for (String k : j.getNames()) { for (String k : j.getNames()) {
if (k.equalsIgnoreCase(key)) { 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) { if (param == null) {
for (DecreeParameter j : getNode().getParameters()) { for (DecreeParameter j : getNode().getParameters()) {
for (String k : j.getNames()) { for (String k : j.getNames()) {
@ -324,16 +333,18 @@ public class VirtualDecreeCommand {
} }
} }
//Still failed to find, error them
if (param == null) { if (param == null) {
Iris.debug("Can't find parameter key for " + key + "=" + value + " in " + getPath()); Iris.debug("Can't find parameter key for " + key + "=" + value + " in " + getPath());
sender.sendMessage(C.YELLOW + "Unknown Parameter: " + key); 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; continue;
} }
key = param.getName(); key = param.getName();
try { 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) { } catch (DecreeParsingException e) {
Iris.debug("Can't parse parameter value for " + key + "=" + value + " in " + getPath() + " using handler " + param.getHandler().getClass().getSimpleName()); 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()); 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()); Iris.debug("Client chose " + update + " for " + key + "=" + value + " (old) in " + getPath());
nowhich.add(ix); nowhich.add(original);
in.set(ix--, update); 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 { try {
DecreeParameter par = getNode().getParameters().get(ix); DecreeParameter par = decreeParameters.get(x);
try { 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) { } catch (DecreeParsingException e) {
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().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 \"" + i + "\" into a " + par.getType().getSimpleName()); sender.sendMessage(C.RED + "Cannot convert \"" + stringParam + "\" into a " + par.getType().getSimpleName());
e.printStackTrace(); e.printStackTrace();
return null; return null;
} catch (DecreeWhichException e) { } 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()); String update = pickValidOption(sender, validOptions, par.getHandler(), par.getName(), par.getType().getSimpleName());
if (update == null) { if (update == null) {
return null; return null;
} }
Iris.debug("Client chose " + update + " for " + par.getName() + "=" + i + " (old) in " + getPath()); Iris.debug("Client chose " + update + " for " + par.getName() + "=" + stringParam + " (old) in " + getPath());
nowhich.add(ix); nowhich.add(original);
in.set(ix--, update); in.set(original, update);
} }
} catch (IndexOutOfBoundsException e) { } 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)");
}
} }
} }