3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-25 10:30:04 +01:00

Don't confirm twice every time

- I channelled my inner Jesse on this one.
Dieser Commit ist enthalten in:
dordsor21 2021-01-13 23:50:29 +00:00
Ursprung 05d7558873
Commit fd1ed63703
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 1E53E88969FFCF0B
3 geänderte Dateien mit 67 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -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 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())) {
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<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)) {
return true;
}
@ -141,5 +174,23 @@ public @interface Confirm {
}
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;
}
}
}

Datei anzeigen

@ -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<Throwable> 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<String> 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<Suggestion> suggestions;
try {
suggestions = commandManager.getSuggestions(access, argStrings);

Datei anzeigen

@ -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<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);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
if (!(e.getCause() instanceof StopExecutionException)) {
throw new RuntimeException(e);
}
return null;
}
}
}