Skip to content

Commit e3ea3d6

Browse files
authored
fix: add nullable region confirm enum (#3052)
- fixes #3020
1 parent 5603693 commit e3ea3d6

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/platform/binding/Bindings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private boolean register(Method method, InjectedValueStore store, CommandManager
9292

9393
// If the method provides all parameters
9494
if (provide) {
95-
store.injectValue(key, access -> Optional.of(invoke(null, argsFunc, access, method)));
95+
store.injectValue(key, access -> Optional.ofNullable(invoke(null, argsFunc, access, method)));
9696
} else { // If the method consumes a String argument
9797
manager.registerConverter(key, new ArgumentConverter<Object>() {
9898
@Override
@@ -129,7 +129,7 @@ private Object invoke(
129129
Function<InjectedValueAccess, Object> func = argsFunc[i];
130130
if (func != null) {
131131
Optional optional = (Optional) func.apply(access);
132-
args[i] = optional.get();
132+
args[i] = optional.orElse(null);
133133
} else {
134134
args[i] = arg;
135135
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/extension/platform/binding/ProvideBindings.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fastasyncworldedit.core.util.TextureUtil;
1212
import com.fastasyncworldedit.core.util.image.ImageUtil;
1313
import com.sk89q.worldedit.EditSession;
14+
import com.sk89q.worldedit.IncompleteRegionException;
1415
import com.sk89q.worldedit.LocalSession;
1516
import com.sk89q.worldedit.WorldEdit;
1617
import com.sk89q.worldedit.command.argument.Arguments;
@@ -91,7 +92,11 @@ public EditSession editSession(LocalSession localSession, Actor actor, InjectedV
9192
@Selection
9293
@Binding
9394
public Region selection(LocalSession localSession) {
94-
return localSession.getSelection();
95+
try {
96+
return localSession.getSelection();
97+
} catch (IncompleteRegionException ignore) {
98+
return null;
99+
}
95100
}
96101

97102
@Binding

worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/preloader/AsyncPreloader.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ public void update(@Nonnull Actor actor, @Nonnull World world) {
6464
MutablePair<World, Set<BlockVector2>> existing = cancelAndGet(actor);
6565
try {
6666
Region region = session.getSelection(world);
67-
if (region == null) {
68-
return;
69-
}
7067
if (existing == null) {
7168
update.put(
7269
actor.getUniqueId(),

worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void biomeInfo(
181181
@Logging(REGION)
182182
@Preload(Preload.PreloadCheck.PRELOAD)
183183
@SynchronousSettingExpected // TODO improve using filter/chunk-based-placement
184-
@Confirm(Confirm.Processor.REGION)
184+
@Confirm(Confirm.Processor.NULLABLE_REGION)
185185
@CommandPermissions("worldedit.biome.set")
186186
public void setBiome(
187187
Actor actor, World world, LocalSession session, EditSession editSession,

worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import com.sk89q.worldedit.extension.platform.Actor;
1010
import com.sk89q.worldedit.internal.annotation.Selection;
1111
import com.sk89q.worldedit.internal.command.CommandArgParser;
12+
import com.sk89q.worldedit.internal.util.LogManagerCompat;
1213
import com.sk89q.worldedit.internal.util.Substring;
1314
import com.sk89q.worldedit.math.BlockVector3;
1415
import com.sk89q.worldedit.regions.Region;
1516
import com.sk89q.worldedit.util.formatting.text.TextComponent;
17+
import org.apache.logging.log4j.Logger;
1618
import org.enginehub.piston.exception.StopExecutionException;
1719
import org.enginehub.piston.inject.InjectAnnotation;
1820
import org.enginehub.piston.inject.InjectedValueAccess;
@@ -45,6 +47,21 @@
4547
Processor value() default Processor.ALWAYS;
4648

4749
enum Processor {
50+
NULLABLE_REGION {
51+
@Override
52+
public boolean passes(Actor actor, InjectedValueAccess context, double value) {
53+
if (checkExisting(context)) {
54+
return true;
55+
}
56+
Region region = context
57+
.injectedValue(Key.of(Region.class, Selection.class))
58+
.orElse(null);
59+
if (region == null) {
60+
return true;
61+
}
62+
return handleRegion(region, actor, context, value);
63+
}
64+
},
4865
REGION {
4966
@Override
5067
public boolean passes(Actor actor, InjectedValueAccess context, double value) {
@@ -54,18 +71,7 @@ public boolean passes(Actor actor, InjectedValueAccess context, double value) {
5471
Region region = context
5572
.injectedValue(Key.of(Region.class, Selection.class))
5673
.orElseThrow(IncompleteRegionException::new);
57-
BlockVector3 pos1 = region.getMinimumPoint();
58-
BlockVector3 pos2 = region.getMaximumPoint();
59-
long area = (pos2.x() - pos1.x()) * (pos2.z() - pos1.z() + 1)
60-
* (long) value;
61-
long max = 2 << 18;
62-
if (max != -1 && area > max) {
63-
actor.print(Caption.of("fawe.cancel.reason.confirm.region",
64-
pos1, pos2, getArgs(context), region.getHeight() * area
65-
));
66-
return confirm(actor, context);
67-
}
68-
return true;
74+
return handleRegion(region, actor, context, value);
6975
}
7076
},
7177
RADIUS {
@@ -111,6 +117,8 @@ public boolean passes(Actor actor, InjectedValueAccess context, double value) {
111117
}
112118
};
113119

120+
private static final Logger LOGGER = LogManagerCompat.getLogger();
121+
114122
public boolean passes(Actor actor, InjectedValueAccess context, double value) {
115123
return true;
116124
}
@@ -171,7 +179,7 @@ private static boolean confirm(Actor actor, InjectedValueAccess context) {
171179
Map<Key<?>, Optional<?>> memory = (Map<Key<?>, Optional<?>>) Reflect.memory.get(memoizingValueAccess);
172180
memory.put(Key.of(InterruptableCondition.class), Optional.of(wait));
173181
} catch (IllegalAccessException e) {
174-
e.printStackTrace();
182+
LOGGER.error("Error confirming command", e);
175183
}
176184
// Waits till 15 seconds then returns false unless awakened
177185
if (condition.await(15, TimeUnit.SECONDS)) {
@@ -192,10 +200,27 @@ boolean checkExisting(InjectedValueAccess context) {
192200
// in which case this is the least of our worries...
193201
return lock.isPresent();
194202
}
203+
204+
private static boolean handleRegion(Region region, Actor actor, InjectedValueAccess context, double value) {
205+
BlockVector3 pos1 = region.getMinimumPoint();
206+
BlockVector3 pos2 = region.getMaximumPoint();
207+
long area = (pos2.x() - pos1.x()) * (pos2.z() - pos1.z() + 1)
208+
* (long) value;
209+
long max = 2 << 18;
210+
if (max != -1 && area > max) {
211+
actor.print(Caption.of("fawe.cancel.reason.confirm.region",
212+
pos1, pos2, getArgs(context), region.getHeight() * area
213+
));
214+
return confirm(actor, context);
215+
}
216+
return true;
217+
}
195218
}
196219

197220
class Reflect {
198221

222+
private static final Logger LOGGER = LogManagerCompat.getLogger();
223+
199224
static final Field memory;
200225
static final Field injectedValues;
201226

@@ -205,7 +230,7 @@ class Reflect {
205230
memoryField = MemoizingValueAccess.class.getDeclaredField("memory");
206231
memoryField.setAccessible(true);
207232
} catch (NoSuchFieldException e) {
208-
e.printStackTrace();
233+
LOGGER.error("Could not find memory field", e);
209234
memoryField = null;
210235
}
211236
memory = memoryField;
@@ -216,7 +241,7 @@ class Reflect {
216241
injectedValuesField = c.getDeclaredField("injectedValues");
217242
injectedValuesField.setAccessible(true);
218243
} catch (NoSuchFieldException | ClassNotFoundException e) {
219-
e.printStackTrace();
244+
LOGGER.error("Could not find injectedValues field", e);
220245
injectedValuesField = null;
221246
}
222247
injectedValues = injectedValuesField;

0 commit comments

Comments
 (0)