From fd1ed637032ebb73c8b51fec76556be4388e8805 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Wed, 13 Jan 2021 23:50:29 +0000 Subject: [PATCH] Don't confirm twice every time - I channelled my inner Jesse on this one. --- .../command/util/annotation/Confirm.java | 51 +++++++++++++++++++ .../platform/PlatformCommandManager.java | 9 ++-- .../extension/platform/binding/Bindings.java | 13 ++++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java index 290dca4b6..01557bb04 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java @@ -19,11 +19,15 @@ import org.enginehub.piston.exception.StopExecutionException; import org.enginehub.piston.inject.InjectAnnotation; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; +import org.enginehub.piston.inject.MemoizingValueAccess; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.lang.reflect.Field; +import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -45,6 +49,9 @@ public @interface Confirm { REGION { @Override public boolean passes(Actor actor, InjectedValueAccess context, double value) { + if (checkExisting(context)) { + return true; + } Region region = context.injectedValue(Key.of(Region.class, Selection.class)).orElseThrow(IncompleteRegionException::new); BlockVector3 pos1 = region.getMinimumPoint(); BlockVector3 pos2 = region.getMaximumPoint(); @@ -62,6 +69,9 @@ public @interface Confirm { RADIUS { @Override public boolean passes(Actor actor, InjectedValueAccess context, double value) { + if (checkExisting(context)) { + return true; + } int max = WorldEdit.getInstance().getConfiguration().maxRadius; if (max != -1 && value > max) { actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.radius", @@ -74,6 +84,9 @@ public @interface Confirm { LIMIT { @Override public boolean passes(Actor actor, InjectedValueAccess context, double value) { + if (checkExisting(context)) { + return true; + } int max = 50; //TODO configurable, get Key.of(Method.class) @Limit if (max != -1 && value > max) { actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.limit", @@ -86,6 +99,9 @@ public @interface Confirm { ALWAYS { @Override public boolean passes(Actor actor, InjectedValueAccess context, double value) { + if (checkExisting(context)) { + return true; + } actor.print(TranslatableComponent.of("fawe.cancel.worldedit.cancel.reason.confirm")); return confirm(actor, context); } @@ -96,6 +112,13 @@ public @interface Confirm { } public T check(Actor actor, InjectedValueAccess context, T value) { + boolean isSuggestion = context.injectedValue(Key.of(boolean.class)).orElse(false); + if (isSuggestion) { + return value; + } + if (checkExisting(context)) { + return value; + } if (!passes(actor, context, value.doubleValue())) { throw new StopExecutionException(TextComponent.empty()); } @@ -130,6 +153,16 @@ public @interface Confirm { try { lock.lock(); actor.setMeta("cmdConfirm", wait); + try { + // This is really dumb but also stops the double //confirm requirement... + Field f = MemoizingValueAccess.class.getDeclaredField("memory"); + f.setAccessible(true); + Map, Optional> memory = (Map, Optional>) f.get(context); + memory.put(Key.of(ReentrantLock.class), Optional.of(lock)); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + // Waits till 15 seconds then returns false unless awakened if (condition.await(15, TimeUnit.SECONDS)) { return true; } @@ -141,5 +174,23 @@ public @interface Confirm { } return false; } + + boolean checkExisting(InjectedValueAccess context) { + Optional lock = context.injectedValue(Key.of(ReentrantLock.class)); + try { + // This is really dumb but also stops the double //confirm requirement... + Field f = MemoizingValueAccess.class.getDeclaredField("memory"); + f.setAccessible(true); + Map, Optional> memory = (Map, Optional>) f.get(context); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + if (lock.isPresent()) { + // lock if locked will be held by current thread unless something has gone REALLY wrong + // in which case this is the least of our worries... + return true; + } + return false; + } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index dc4741215..3bb76fe2f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -609,7 +609,7 @@ public final class PlatformCommandManager { if (actor == null) { context = globalInjectedValues; } else { - context = initializeInjectedValues(args::toString, actor, null); + context = initializeInjectedValues(args::toString, actor, null, false); } return parseCommand(args, context); } @@ -688,7 +688,7 @@ public final class PlatformCommandManager { } } - MemoizingValueAccess context = initializeInjectedValues(event::getArguments, actor, event); + MemoizingValueAccess context = initializeInjectedValues(event::getArguments, actor, event, false); ThrowableSupplier task = () -> commandManager.execute(context, ImmutableList.copyOf(split)); @@ -800,7 +800,7 @@ public final class PlatformCommandManager { getCommandManager(), actor, "//help"); } - private MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor actor, Event event) { + private MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor actor, Event event, boolean isSuggestions) { InjectedValueStore store = MapBackedValueStore.create(); store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor)); if (actor instanceof Player) { @@ -817,6 +817,7 @@ public final class PlatformCommandManager { localSession.tellVersion(actor); return Optional.of(localSession); }); + store.injectValue(Key.of(boolean.class), context -> Optional.of(isSuggestions)); store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store)); store.injectValue(Key.of(Event.class), ValueProvider.constant(event)); return MemoizingValueAccess.wrap( @@ -841,7 +842,7 @@ public final class PlatformCommandManager { List argStrings = split.stream() .map(Substring::getSubstring) .collect(Collectors.toList()); - MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event); + MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event, true); ImmutableSet suggestions; try { suggestions = commandManager.getSuggestions(access, argStrings); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/Bindings.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/Bindings.java index ba0916961..38f31c4a3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/Bindings.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/Bindings.java @@ -7,7 +7,9 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent; import org.enginehub.piston.CommandManager; import org.enginehub.piston.converter.ArgumentConverter; import org.enginehub.piston.converter.ConversionResult; +import org.enginehub.piston.converter.FailedConversion; import org.enginehub.piston.converter.SuccessfulConversion; +import org.enginehub.piston.exception.StopExecutionException; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.InjectedValueStore; import org.enginehub.piston.inject.Key; @@ -97,7 +99,11 @@ public class Bindings { @Override public ConversionResult convert(String s, InjectedValueAccess access) { - return SuccessfulConversion.fromSingle(invoke(s, argsFunc, access, method)); + Object o = invoke(s, argsFunc, access, method); + if (o == null) { + return FailedConversion.from(new NullPointerException()); + } + return SuccessfulConversion.fromSingle(o); } }); } @@ -118,7 +124,10 @@ public class Bindings { } return method.invoke(this, args); } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); + if (!(e.getCause() instanceof StopExecutionException)) { + throw new RuntimeException(e); + } + return null; } } }