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
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.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;
}
}
}