Don't confirm twice every time

- I channelled my inner Jesse on this one.
This commit is contained in:
dordsor21 2021-01-13 23:50:29 +00:00
parent 05d7558873
commit fd1ed63703
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
3 changed files with 67 additions and 6 deletions

View File

@ -19,11 +19,15 @@ import org.enginehub.piston.exception.StopExecutionException;
import org.enginehub.piston.inject.InjectAnnotation; import org.enginehub.piston.inject.InjectAnnotation;
import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key; import org.enginehub.piston.inject.Key;
import org.enginehub.piston.inject.MemoizingValueAccess;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; 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.TimeUnit;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -45,6 +49,9 @@ public @interface Confirm {
REGION { REGION {
@Override @Override
public boolean passes(Actor actor, InjectedValueAccess context, double value) { 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); Region region = context.injectedValue(Key.of(Region.class, Selection.class)).orElseThrow(IncompleteRegionException::new);
BlockVector3 pos1 = region.getMinimumPoint(); BlockVector3 pos1 = region.getMinimumPoint();
BlockVector3 pos2 = region.getMaximumPoint(); BlockVector3 pos2 = region.getMaximumPoint();
@ -62,6 +69,9 @@ public @interface Confirm {
RADIUS { RADIUS {
@Override @Override
public boolean passes(Actor actor, InjectedValueAccess context, double value) { public boolean passes(Actor actor, InjectedValueAccess context, double value) {
if (checkExisting(context)) {
return true;
}
int max = WorldEdit.getInstance().getConfiguration().maxRadius; int max = WorldEdit.getInstance().getConfiguration().maxRadius;
if (max != -1 && value > max) { if (max != -1 && value > max) {
actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.radius", actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.radius",
@ -74,6 +84,9 @@ public @interface Confirm {
LIMIT { LIMIT {
@Override @Override
public boolean passes(Actor actor, InjectedValueAccess context, double value) { 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 int max = 50; //TODO configurable, get Key.of(Method.class) @Limit
if (max != -1 && value > max) { if (max != -1 && value > max) {
actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.limit", actor.print(Caption.of("fawe.cancel.worldedit.cancel.reason.confirm.limit",
@ -86,6 +99,9 @@ public @interface Confirm {
ALWAYS { ALWAYS {
@Override @Override
public boolean passes(Actor actor, InjectedValueAccess context, double value) { public boolean passes(Actor actor, InjectedValueAccess context, double value) {
if (checkExisting(context)) {
return true;
}
actor.print(TranslatableComponent.of("fawe.cancel.worldedit.cancel.reason.confirm")); actor.print(TranslatableComponent.of("fawe.cancel.worldedit.cancel.reason.confirm"));
return confirm(actor, context); return confirm(actor, context);
} }
@ -96,6 +112,13 @@ public @interface Confirm {
} }
public <T extends Number> T check(Actor actor, InjectedValueAccess context, T value) { public <T extends Number> 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())) { if (!passes(actor, context, value.doubleValue())) {
throw new StopExecutionException(TextComponent.empty()); throw new StopExecutionException(TextComponent.empty());
} }
@ -130,6 +153,16 @@ public @interface Confirm {
try { try {
lock.lock(); lock.lock();
actor.setMeta("cmdConfirm", wait); 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<Key<?>, Optional<?>> memory = (Map<Key<?>, 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)) { if (condition.await(15, TimeUnit.SECONDS)) {
return true; return true;
} }
@ -141,5 +174,23 @@ public @interface Confirm {
} }
return false; return false;
} }
boolean checkExisting(InjectedValueAccess context) {
Optional<ReentrantLock> 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<Key<?>, Optional<?>> memory = (Map<Key<?>, 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;
}
} }
} }

View File

@ -609,7 +609,7 @@ public final class PlatformCommandManager {
if (actor == null) { if (actor == null) {
context = globalInjectedValues; context = globalInjectedValues;
} else { } else {
context = initializeInjectedValues(args::toString, actor, null); context = initializeInjectedValues(args::toString, actor, null, false);
} }
return parseCommand(args, context); 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<Throwable> task = () -> commandManager.execute(context, ImmutableList.copyOf(split)); ThrowableSupplier<Throwable> task = () -> commandManager.execute(context, ImmutableList.copyOf(split));
@ -800,7 +800,7 @@ public final class PlatformCommandManager {
getCommandManager(), actor, "//help"); 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(); InjectedValueStore store = MapBackedValueStore.create();
store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor)); store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor));
if (actor instanceof Player) { if (actor instanceof Player) {
@ -817,6 +817,7 @@ public final class PlatformCommandManager {
localSession.tellVersion(actor); localSession.tellVersion(actor);
return Optional.of(localSession); 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(InjectedValueStore.class), ValueProvider.constant(store));
store.injectValue(Key.of(Event.class), ValueProvider.constant(event)); store.injectValue(Key.of(Event.class), ValueProvider.constant(event));
return MemoizingValueAccess.wrap( return MemoizingValueAccess.wrap(
@ -841,7 +842,7 @@ public final class PlatformCommandManager {
List<String> argStrings = split.stream() List<String> argStrings = split.stream()
.map(Substring::getSubstring) .map(Substring::getSubstring)
.collect(Collectors.toList()); .collect(Collectors.toList());
MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event); MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event, true);
ImmutableSet<Suggestion> suggestions; ImmutableSet<Suggestion> suggestions;
try { try {
suggestions = commandManager.getSuggestions(access, argStrings); suggestions = commandManager.getSuggestions(access, argStrings);

View File

@ -7,7 +7,9 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent;
import org.enginehub.piston.CommandManager; import org.enginehub.piston.CommandManager;
import org.enginehub.piston.converter.ArgumentConverter; import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.converter.ConversionResult; import org.enginehub.piston.converter.ConversionResult;
import org.enginehub.piston.converter.FailedConversion;
import org.enginehub.piston.converter.SuccessfulConversion; import org.enginehub.piston.converter.SuccessfulConversion;
import org.enginehub.piston.exception.StopExecutionException;
import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.InjectedValueStore; import org.enginehub.piston.inject.InjectedValueStore;
import org.enginehub.piston.inject.Key; import org.enginehub.piston.inject.Key;
@ -97,7 +99,11 @@ public class Bindings {
@Override @Override
public ConversionResult<Object> convert(String s, InjectedValueAccess access) { public ConversionResult<Object> 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); return method.invoke(this, args);
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e); if (!(e.getCause() instanceof StopExecutionException)) {
throw new RuntimeException(e);
}
return null;
} }
} }
} }