Fix commands

This commit is contained in:
cyberpwn 2021-08-31 10:12:23 -04:00
parent 82666d62ef
commit 43f9efb9e4
27 changed files with 332 additions and 148 deletions

View File

@ -22,11 +22,19 @@ import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.commands.CommandIris; import com.volmit.iris.core.commands.CommandIris;
import com.volmit.iris.engine.data.cache.AtomicCache; import com.volmit.iris.engine.data.cache.AtomicCache;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.decree.DecreeSystem; import com.volmit.iris.util.decree.DecreeSystem;
import com.volmit.iris.util.decree.virtual.VirtualDecreeCommand; import com.volmit.iris.util.decree.virtual.VirtualDecreeCommand;
import com.volmit.iris.util.plugin.IrisService; import com.volmit.iris.util.plugin.IrisService;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import java.util.concurrent.CompletableFuture;
public class CommandSVC implements IrisService, DecreeSystem { public class CommandSVC implements IrisService, DecreeSystem {
private final KMap<String, CompletableFuture<String>> futures = new KMap<>();
private final transient AtomicCache<VirtualDecreeCommand> commandCache = new AtomicCache<>();
@Override @Override
public void onEnable() { public void onEnable() {
Iris.instance.getCommand("iris").setExecutor(this); Iris.instance.getCommand("iris").setExecutor(this);
@ -38,10 +46,30 @@ public class CommandSVC implements IrisService, DecreeSystem {
} }
private final transient AtomicCache<VirtualDecreeCommand> commandCache = new AtomicCache<>(); @EventHandler
public void on(PlayerCommandPreprocessEvent e)
{
String msg = e.getMessage().startsWith("/") ? e.getMessage().substring(1) : e.getMessage();
if(msg.startsWith("irisdecree "))
{
String[] args = msg.split("\\Q \\E");
CompletableFuture<String> future = futures.get(args[1]);
if(future != null)
{
future.complete(args[2]);
e.setCancelled(true);
}
}
}
@Override @Override
public VirtualDecreeCommand getRoot() { public VirtualDecreeCommand getRoot() {
return commandCache.aquireNastyPrint(() -> VirtualDecreeCommand.createRoot(new CommandIris())); return commandCache.aquireNastyPrint(() -> VirtualDecreeCommand.createRoot(new CommandIris()));
} }
public void post(String password, CompletableFuture<String> future) {
futures.put(password, future);
}
} }

View File

@ -246,13 +246,13 @@ public class KList<T> extends ArrayList<T> implements List<T> {
} }
if (size() == 1) { if (size() == 1) {
return get(0).toString(); return get(0) + "";
} }
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
for (String i : toStringList()) { for (String i : toStringList()) {
b.append(split).append(i); b.append(split).append(i == null ? "null" : i);
} }
return b.substring(split.length()); return b.substring(split.length());
@ -264,7 +264,7 @@ public class KList<T> extends ArrayList<T> implements List<T> {
* @return the string list * @return the string list
*/ */
public KList<String> toStringList() { public KList<String> toStringList() {
return convert((t) -> t.toString()); return convert((t) -> t + "");
} }
/** /**

View File

@ -92,7 +92,7 @@ public class DecreeParameter {
} }
public Object getDefaultValue() throws DecreeParsingException, DecreeWhichException { public Object getDefaultValue() throws DecreeParsingException, DecreeWhichException {
return param.defaultValue().trim().isEmpty() ? null : getHandler().parse(param.defaultValue().trim()); return param.defaultValue().trim().isEmpty() ? null : getHandler().parse(param.defaultValue().trim(), true);
} }
public boolean hasDefault() { public boolean hasDefault() {

View File

@ -62,7 +62,20 @@ public interface DecreeParameterHandler<T> {
* @throws DecreeParsingException Thrown when the parsing fails (ex: "oop" translated to an integer throws this) * @throws DecreeParsingException Thrown when the parsing fails (ex: "oop" translated to an integer throws this)
* @throws DecreeWhichException Thrown when multiple results are possible * @throws DecreeWhichException Thrown when multiple results are possible
*/ */
T parse(String in) throws DecreeParsingException, DecreeWhichException; default T parse(String in) throws DecreeParsingException, DecreeWhichException {
return parse(in, false);
}
/**
* Should parse a String into the designated type. You can force it to not throw a whichexception
*
* @param in The string to parse
* @param force force an option instead of throwing decreewhich
* @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
*/
T parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException;
/** /**
* Returns whether a certain type is supported by this handler<br> * Returns whether a certain type is supported by this handler<br>

View File

@ -83,8 +83,8 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter {
{ {
if(sender instanceof Player) if(sender instanceof Player)
{ {
((Player)sender).playSound(((Player)sender).getLocation(), Sound.BLOCK_ANCIENT_DEBRIS_BREAK, 1f, 0.25f); ((Player)sender).playSound(((Player)sender).getLocation(), Sound.BLOCK_AMETHYST_CLUSTER_BREAK, 0.77f, 0.25f);
((Player)sender).playSound(((Player)sender).getLocation(), Sound.BLOCK_RESPAWN_ANCHOR_DEPLETE, 0.2f, 1.95f); ((Player)sender).playSound(((Player)sender).getLocation(), Sound.BLOCK_BEACON_DEACTIVATE, 0.2f, 0.45f);
} }
} }

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class BiomeHandler implements DecreeParameterHandler<IrisBiome> { public class BiomeHandler implements DecreeParameterHandler<IrisBiome> {
@Override @Override
@ -55,25 +56,35 @@ public class BiomeHandler implements DecreeParameterHandler<IrisBiome> {
} }
@Override @Override
public IrisBiome parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisBiome parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
if (in.equals("null")) { if (in.equals("null")) {
return null; return null;
} }
try { KList<IrisBiome> options = getPossibilities(in);
KList<IrisBiome> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Biome \"" + in + "\""); throw new DecreeParsingException("Unable to find Biome \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Biome \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -54,7 +54,7 @@ public class BlockVectorHandler implements DecreeParameterHandler<BlockVector> {
} }
@Override @Override
public BlockVector parse(String in) throws DecreeParsingException, DecreeWhichException { public BlockVector parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { try {
if (in.contains(",")) { if (in.contains(",")) {
String[] comp = in.split("\\Q,\\E"); String[] comp = in.split("\\Q,\\E");

View File

@ -35,7 +35,7 @@ public class BooleanHandler implements DecreeParameterHandler<Boolean> {
} }
@Override @Override
public Boolean parse(String in) throws DecreeParsingException { public Boolean parse(String in, boolean force) throws DecreeParsingException {
try { try {
if (in.equals("null") || in.equals("other") || in.equals("flip")) { if (in.equals("null") || in.equals("other") || in.equals("flip")) {
return null; return null;

View File

@ -35,7 +35,7 @@ public class ByteHandler implements DecreeParameterHandler<Byte> {
} }
@Override @Override
public Byte parse(String in) throws DecreeParsingException { public Byte parse(String in, boolean force) throws DecreeParsingException {
try { try {
return Byte.parseByte(in); return Byte.parseByte(in);
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class DimensionHandler implements DecreeParameterHandler<IrisDimension> { public class DimensionHandler implements DecreeParameterHandler<IrisDimension> {
@Override @Override
@ -55,22 +56,32 @@ public class DimensionHandler implements DecreeParameterHandler<IrisDimension> {
} }
@Override @Override
public IrisDimension parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisDimension parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<IrisDimension> options = getPossibilities(in);
KList<IrisDimension> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Dimension \"" + in + "\""); throw new DecreeParsingException("Unable to find Dimension \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Dimension \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -33,7 +33,7 @@ public class DoubleHandler implements DecreeParameterHandler<Double> {
} }
@Override @Override
public Double parse(String in) throws DecreeParsingException { public Double parse(String in, boolean force) throws DecreeParsingException {
try { try {
AtomicReference<String> r = new AtomicReference<>(in); AtomicReference<String> r = new AtomicReference<>(in);
double m = getMultiplier(r); double m = getMultiplier(r);

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class EntityHandler implements DecreeParameterHandler<IrisEntity> { public class EntityHandler implements DecreeParameterHandler<IrisEntity> {
@ -75,22 +76,32 @@ public class EntityHandler implements DecreeParameterHandler<IrisEntity> {
* @throws DecreeWhichException Thrown when multiple results are possible * @throws DecreeWhichException Thrown when multiple results are possible
*/ */
@Override @Override
public IrisEntity parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisEntity parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<IrisEntity> options = getPossibilities(in);
KList<IrisEntity> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Entity \"" + in + "\""); throw new DecreeParsingException("Unable to find Entity \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Entity \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
/** /**

View File

@ -33,7 +33,7 @@ public class FloatHandler implements DecreeParameterHandler<Float> {
} }
@Override @Override
public Float parse(String in) throws DecreeParsingException { public Float parse(String in, boolean force) throws DecreeParsingException {
try { try {
AtomicReference<String> r = new AtomicReference<>(in); AtomicReference<String> r = new AtomicReference<>(in);
double m = getMultiplier(r); double m = getMultiplier(r);

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class GeneratorHandler implements DecreeParameterHandler<IrisGenerator> { public class GeneratorHandler implements DecreeParameterHandler<IrisGenerator> {
@Override @Override
@ -55,22 +56,32 @@ public class GeneratorHandler implements DecreeParameterHandler<IrisGenerator> {
} }
@Override @Override
public IrisGenerator parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisGenerator parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<IrisGenerator> options = getPossibilities(in);
KList<IrisGenerator> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Generator \"" + in + "\""); throw new DecreeParsingException("Unable to find Generator \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Generator \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -32,7 +32,7 @@ public class IntegerHandler implements DecreeParameterHandler<Integer> {
} }
@Override @Override
public Integer parse(String in) throws DecreeParsingException { public Integer parse(String in, boolean force) throws DecreeParsingException {
try { try {
AtomicReference<String> r = new AtomicReference<>(in); AtomicReference<String> r = new AtomicReference<>(in);
double m = getMultiplier(r); double m = getMultiplier(r);

View File

@ -32,7 +32,7 @@ public class LongHandler implements DecreeParameterHandler<Long> {
} }
@Override @Override
public Long parse(String in) throws DecreeParsingException { public Long parse(String in, boolean force) throws DecreeParsingException {
try { try {
AtomicReference<String> r = new AtomicReference<>(in); AtomicReference<String> r = new AtomicReference<>(in);
double m = getMultiplier(r); double m = getMultiplier(r);

View File

@ -26,6 +26,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.stream.Collectors;
public class PlayerHandler implements DecreeParameterHandler<Player> { public class PlayerHandler implements DecreeParameterHandler<Player> {
@Override @Override
@ -39,20 +40,32 @@ public class PlayerHandler implements DecreeParameterHandler<Player> {
} }
@Override @Override
public Player parse(String in) throws DecreeParsingException, DecreeWhichException { public Player parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<Player> options = getPossibilities(in);
KList<Player> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Player \"" + in + "\""); throw new DecreeParsingException("Unable to find Player \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (Throwable e) { {
throw new DecreeParsingException("Unable to find Player \"" + in + "\" because of an uncaught exception: " + e); throw new DecreeWhichException();
}
} }
return options.get(0);
} }
@Override @Override

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class RegionHandler implements DecreeParameterHandler<IrisRegion> { public class RegionHandler implements DecreeParameterHandler<IrisRegion> {
@Override @Override
@ -55,25 +56,35 @@ public class RegionHandler implements DecreeParameterHandler<IrisRegion> {
} }
@Override @Override
public IrisRegion parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisRegion parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
if (in.equals("null")) { if (in.equals("null")) {
return null; return null;
} }
try { KList<IrisRegion> options = getPossibilities(in);
KList<IrisRegion> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Region \"" + in + "\""); throw new DecreeParsingException("Unable to find Region \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Region \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class ScriptHandler implements DecreeParameterHandler<IrisScript> { public class ScriptHandler implements DecreeParameterHandler<IrisScript> {
@Override @Override
@ -55,22 +56,32 @@ public class ScriptHandler implements DecreeParameterHandler<IrisScript> {
} }
@Override @Override
public IrisScript parse(String in) throws DecreeParsingException, DecreeWhichException { public IrisScript parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<IrisScript> options = getPossibilities(in);
KList<IrisScript> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Script \"" + in + "\""); throw new DecreeParsingException("Unable to find Script \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Script \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -32,7 +32,7 @@ public class ShortHandler implements DecreeParameterHandler<Short> {
} }
@Override @Override
public Short parse(String in) throws DecreeParsingException { public Short parse(String in, boolean force) throws DecreeParsingException {
try { try {
AtomicReference<String> r = new AtomicReference<>(in); AtomicReference<String> r = new AtomicReference<>(in);
double m = getMultiplier(r); double m = getMultiplier(r);

View File

@ -37,7 +37,7 @@ public class StringHandler implements DecreeParameterHandler<String> {
} }
@Override @Override
public String parse(String in) throws DecreeParsingException { public String parse(String in, boolean force) throws DecreeParsingException {
return in; return in;
} }

View File

@ -55,7 +55,7 @@ public class VectorHandler implements DecreeParameterHandler<Vector> {
} }
@Override @Override
public Vector parse(String in) throws DecreeParsingException, DecreeWhichException { public Vector parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { try {
if (in.contains(",")) { if (in.contains(",")) {
String[] comp = in.split("\\Q,\\E"); String[] comp = in.split("\\Q,\\E");

View File

@ -25,6 +25,8 @@ import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import java.util.stream.Collectors;
public class WorldHandler implements DecreeParameterHandler<World> { public class WorldHandler implements DecreeParameterHandler<World> {
@Override @Override
public KList<World> getPossibilities() { public KList<World> getPossibilities() {
@ -43,22 +45,32 @@ public class WorldHandler implements DecreeParameterHandler<World> {
} }
@Override @Override
public World parse(String in) throws DecreeParsingException, DecreeWhichException { public World parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<World> options = getPossibilities(in);
KList<World> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find World \"" + in + "\""); throw new DecreeParsingException("Unable to find World \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find World \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -39,7 +39,7 @@ public class DummyHandler implements DecreeParameterHandler<Object> {
} }
@Override @Override
public Object parse(String in) throws DecreeParsingException, DecreeWhichException { public Object parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
return null; return null;
} }

View File

@ -26,6 +26,7 @@ import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
import java.io.File; import java.io.File;
import java.util.stream.Collectors;
public class ObjectHandler implements DecreeParameterHandler<String> { public class ObjectHandler implements DecreeParameterHandler<String> {
@Override @Override
@ -49,22 +50,32 @@ public class ObjectHandler implements DecreeParameterHandler<String> {
} }
@Override @Override
public String parse(String in) throws DecreeParsingException, DecreeWhichException { public String parse(String in, boolean force) throws DecreeParsingException, DecreeWhichException {
try { KList<String> options = getPossibilities(in);
KList<String> options = getPossibilities(in);
if (options.isEmpty()) { if (options.isEmpty()) {
throw new DecreeParsingException("Unable to find Object \"" + in + "\""); throw new DecreeParsingException("Unable to find Object \"" + in + "\"");
} else if (options.size() > 1) { } else if (options.size() > 1) {
throw new DecreeWhichException(); if(force)
{
try
{
return options.stream().filter((i) -> toString(i).equalsIgnoreCase(in)).collect(Collectors.toList()).get(0);
}
catch(Throwable e)
{
throw new DecreeParsingException("Unable to filter which Biome \"" + in + "\"");
}
} }
return options.get(0); else
} catch (DecreeParsingException e) { {
throw e; throw new DecreeWhichException();
} catch (Throwable e) { }
throw new DecreeParsingException("Unable to find Object \"" + in + "\" because of an uncaught exception: " + e);
} }
return options.get(0);
} }
@Override @Override

View File

@ -20,12 +20,11 @@ package com.volmit.iris.util.decree.virtual;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.service.CommandSVC;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap; import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.decree.DecreeContext; import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.decree.DecreeContextHandler; import com.volmit.iris.util.decree.*;
import com.volmit.iris.util.decree.DecreeNode;
import com.volmit.iris.util.decree.DecreeParameter;
import com.volmit.iris.util.decree.annotations.Decree; import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.exceptions.DecreeParsingException; import com.volmit.iris.util.decree.exceptions.DecreeParsingException;
import com.volmit.iris.util.decree.exceptions.DecreeWhichException; import com.volmit.iris.util.decree.exceptions.DecreeWhichException;
@ -37,13 +36,16 @@ import com.volmit.iris.util.scheduling.ChronoLatch;
import com.volmit.iris.util.scheduling.J; import com.volmit.iris.util.scheduling.J;
import com.volmit.iris.util.stream.utility.SemaphoreStream; import com.volmit.iris.util.stream.utility.SemaphoreStream;
import lombok.Data; import lombok.Data;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Semaphore; import java.util.UUID;
import java.util.concurrent.*;
@Data @Data
public class VirtualDecreeCommand { public class VirtualDecreeCommand {
@ -279,9 +281,16 @@ public class VirtualDecreeCommand {
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<>();
for (int ix = 0; ix < in.size(); ix++) { for (int ix = 0; ix < in.size(); ix++) {
String i = in.get(ix); String i = in.get(ix);
if(i == null)
{
Iris.warn("Param " + ix + " is null? (" + in.toString(",") + ")");
}
if (i.contains("=")) { if (i.contains("=")) {
String[] v = i.split("\\Q=\\E"); String[] v = i.split("\\Q=\\E");
String key = v[0]; String key = v[0];
@ -317,32 +326,35 @@ public class VirtualDecreeCommand {
key = param.getName(); key = param.getName();
try { try {
data.put(key, param.getHandler().parse(value)); data.put(key, param.getHandler().parse(value, nowhich.contains(ix)));
} 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());
e.printStackTrace();
return null; return null;
} catch (DecreeWhichException e) { } catch (DecreeWhichException e) {
KList<?> validOptions = param.getHandler().getPossibilities(value); KList<?> validOptions = param.getHandler().getPossibilities(value);
Iris.debug("Found multiple results for " + key + "=" + value + " in " + getPath() + " using the handler " + param.getHandler().getClass().getSimpleName() + " with potential matches [" + validOptions.toString(",") + "]. Asking client to define one"); Iris.debug("Found multiple results for " + key + "=" + value + " in " + getPath() + " using the handler " + param.getHandler().getClass().getSimpleName() + " with potential matches [" + validOptions.toString(",") + "]. Asking client to define one");
String update = null; // TODO: PICK ONE String update = pickValidOption(sender, validOptions, param.getHandler(), param.getName(), param.getType().getSimpleName());
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);
in.set(ix--, update); in.set(ix--, update);
} }
} else { } else {
try { try {
DecreeParameter par = getNode().getParameters().get(ix); DecreeParameter par = getNode().getParameters().get(ix);
try { try {
data.put(par.getName(), par.getHandler().parse(i)); data.put(par.getName(), par.getHandler().parse(i, nowhich.contains(ix)));
} 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() + "=" + i + " 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 \"" + i + "\" into a " + par.getType().getSimpleName());
e.printStackTrace();
return null; return null;
} catch (DecreeWhichException e) { } catch (DecreeWhichException e) {
Iris.debug("Can't parse parameter value for " + par.getName() + "=" + i + " in " + getPath() + " using handler " + par.getHandler().getClass().getSimpleName());
KList<?> validOptions = par.getHandler().getPossibilities(i); KList<?> validOptions = par.getHandler().getPossibilities(i);
String update = null; // TODO: PICK ONE String update = pickValidOption(sender, validOptions, par.getHandler(), par.getName(), par.getType().getSimpleName());
Iris.debug("Client chose " + update + " for " + par.getName() + "=" + i + " (old) in " + getPath()); Iris.debug("Client chose " + update + " for " + par.getName() + "=" + i + " (old) in " + getPath());
nowhich.add(ix);
in.set(ix--, update); in.set(ix--, update);
} }
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
@ -425,20 +437,10 @@ public class VirtualDecreeCommand {
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + i.getType().getSimpleName()); sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + i.getType().getSimpleName());
return false; return false;
} catch (DecreeWhichException e) { } catch (DecreeWhichException e) {
Iris.debug("Can't parse parameter value for " + i.getName() + "=" + i + " in " + getPath() + " using handler " + i.getHandler().getClass().getSimpleName());
KList<?> validOptions = i.getHandler().getPossibilities(i.getParam().defaultValue()); KList<?> validOptions = i.getHandler().getPossibilities(i.getParam().defaultValue());
String update = null; // TODO: PICK ONE String update = pickValidOption(sender, validOptions, i.getHandler(), i.getName(), i.getType().getSimpleName());
Iris.debug("Client chose " + update + " for " + i.getName() + "=" + i + " (old) in " + getPath()); Iris.debug("Client chose " + update + " for " + i.getName() + "=" + i + " (old) in " + getPath());
try { value = update;
value = i.getDefaultValue();
} catch (DecreeParsingException x) {
x.printStackTrace();
Iris.debug("Can't parse parameter value for " + i.getName() + "=" + i + " in " + getPath() + " using handler " + i.getHandler().getClass().getSimpleName());
sender.sendMessage(C.RED + "Cannot convert \"" + i + "\" into a " + i.getType().getSimpleName());
return false;
} catch (DecreeWhichException x) {
x.printStackTrace();
}
} }
if (i.isContextual() && value == null) { if (i.isContextual() && value == null) {
@ -495,6 +497,45 @@ public class VirtualDecreeCommand {
return true; return true;
} }
String[] gradients = new String[]{
"<gradient:#f5bc42:#45b32d>",
"<gradient:#1ed43f:#1ecbd4>",
"<gradient:#1e2ad4:#821ed4>",
"<gradient:#d41ea7:#611ed4>",
"<gradient:#1ed473:#1e55d4>",
"<gradient:#6ad41e:#9a1ed4>"
};
private String pickValidOption(VolmitSender sender, KList<?> validOptions, DecreeParameterHandler<?> handler, String name, String type) {
sender.sendHeader("Pick a " + name + " (" + type + ")");
sender.sendMessageRaw("<gradient:#1ed497:#b39427>This query will expire in 15 seconds.</gradient>");
String password = UUID.randomUUID().toString().replaceAll("\\Q-\\E", "");
int m = 0;
for(String i : validOptions.convert(handler::toStringForce))
{
sender.sendMessage( "<hover:show_text:'" + gradients[m%gradients.length] + i+"</gradient>'><click:run_command:/irisdecree "+ password + " " + i+">"+"- " + gradients[m%gradients.length] + i + "</gradient></click></hover>");
m++;
}
CompletableFuture<String> future = new CompletableFuture<>();
Iris.service(CommandSVC.class).post(password, future);
if(IrisSettings.get().getGeneral().isCommandSounds() && sender.isPlayer())
{
(sender.player()).playSound((sender.player()).getLocation(), Sound.BLOCK_AMETHYST_CLUSTER_BREAK, 0.77f, 0.65f);
(sender.player()).playSound((sender.player()).getLocation(), Sound.BLOCK_BEACON_DEACTIVATE, 0.125f, 1.99f);
}
try {
return future.get(15, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
}
return null;
}
public KList<VirtualDecreeCommand> matchAllNodes(String in) { public KList<VirtualDecreeCommand> matchAllNodes(String in) {
KList<VirtualDecreeCommand> g = new KList<>(); KList<VirtualDecreeCommand> g = new KList<>();

View File

@ -436,7 +436,7 @@ public class VolmitSender implements CommandSender {
} }
public void sendHeader(String name) { public void sendHeader(String name) {
sendHeader(name, 46); sendHeader(name, 44);
} }