Skip to content

Commit d960fa2

Browse files
authored
Add property suggestions for hand,offhand,pos1 patterns (#2269)
1 parent 98fd5dc commit d960fa2

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public static Stream<String> getBlockCategorySuggestions(String tag, boolean all
6565
return Stream.empty();
6666
}
6767

68-
public static Stream<String> getBlockPropertySuggestions(String blockType, String props) {
69-
BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));
68+
public static Stream<String> getBlockPropertySuggestions(String blockTypeString, BlockType type, String props) {
7069
if (type == null) {
7170
return Stream.empty();
7271
}
@@ -83,7 +82,7 @@ public static Stream<String> getBlockPropertySuggestions(String blockType, Strin
8382
// suggest for next property
8483
String previous = Arrays.stream(propParts, 0, propParts.length - 1).collect(Collectors.joining(","))
8584
+ (propParts.length == 1 ? "" : ",");
86-
String lastValidInput = (blockType + "[" + previous).toLowerCase(Locale.ROOT);
85+
String lastValidInput = (blockTypeString + "[" + previous).toLowerCase(Locale.ROOT);
8786
if (propVal.length == 1) {
8887
// only property, no value yet
8988
final List<? extends Property<?>> matchingProps = propertyMap.entrySet().stream()

worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.Maps;
2323
import com.sk89q.worldedit.IncompleteRegionException;
24+
import com.sk89q.worldedit.LocalSession;
2425
import com.sk89q.worldedit.NotABlockException;
2526
import com.sk89q.worldedit.WorldEdit;
2627
import com.sk89q.worldedit.WorldEditException;
@@ -222,23 +223,52 @@ private static Map<Property<?>, Object> parseProperties(BlockType type, String[]
222223
}
223224

224225
@Override
225-
public Stream<String> getSuggestions(String input) {
226+
public Stream<String> getSuggestions(String input, ParserContext context) {
226227
final int idx = input.lastIndexOf('[');
227228
if (idx < 0) {
228229
return SuggestionHelper.getNamespacedRegistrySuggestions(BlockType.REGISTRY, input);
229230
}
230231
String blockType = input.substring(0, idx);
231232
BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));
232233
if (type == null) {
233-
return Stream.empty();
234+
var lowerBlockType = blockType.toLowerCase(Locale.ROOT);
235+
switch (lowerBlockType) {
236+
case "hand", "offhand" -> {
237+
var actor = context.getActor();
238+
if (actor instanceof Player player) {
239+
var itemInHand = player.getItemInHand(lowerBlockType.equals("hand") ? HandSide.MAIN_HAND : HandSide.OFF_HAND);
240+
if (itemInHand.getType().hasBlockType()) {
241+
type = itemInHand.getType().getBlockType();
242+
}
243+
}
244+
}
245+
case "pos1" -> {
246+
// Get the block type from the "primary position"
247+
World world = context.getWorld();
248+
LocalSession session = context.getSession();
249+
if (world != null && session != null) {
250+
try {
251+
BlockVector3 primaryPosition = session.getRegionSelector(world).getPrimaryPosition();
252+
type = world.getBlock(primaryPosition).getBlockType();
253+
} catch (IncompleteRegionException ignored) {
254+
}
255+
}
256+
}
257+
default -> {
258+
}
259+
}
260+
261+
if (type == null) {
262+
return Stream.empty();
263+
}
234264
}
235265

236266
String props = input.substring(idx + 1);
237267
if (props.isEmpty()) {
238268
return type.getProperties().stream().map(p -> input + p.getName() + "=");
239269
}
240270

241-
return SuggestionHelper.getBlockPropertySuggestions(blockType, props);
271+
return SuggestionHelper.getBlockPropertySuggestions(blockType, type, props);
242272
}
243273

244274
private BaseBlock parseLogic(String input, ParserContext context) throws InputParseException {

worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/TypeOrStateApplyingPatternParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
import com.sk89q.worldedit.internal.registry.InputParser;
3333
import com.sk89q.worldedit.util.formatting.text.TextComponent;
3434
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
35+
import com.sk89q.worldedit.world.block.BlockType;
36+
import com.sk89q.worldedit.world.block.BlockTypes;
3537

3638
import java.util.HashMap;
39+
import java.util.Locale;
3740
import java.util.Map;
3841
import java.util.stream.Stream;
3942

@@ -63,7 +66,8 @@ public Stream<String> getSuggestions(String input, ParserContext context) {
6366
if (type.isEmpty()) {
6467
return Stream.empty(); // without knowing a type, we can't really suggest states
6568
} else {
66-
return SuggestionHelper.getBlockPropertySuggestions(type, parts[1]).map(s -> "^" + s);
69+
BlockType blockType = BlockTypes.get(type.toLowerCase(Locale.ROOT));
70+
return SuggestionHelper.getBlockPropertySuggestions(type, blockType, parts[1]).map(s -> "^" + s);
6771
}
6872
}
6973
}

worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ public E parseFromInput(String input, ParserContext context) throws InputParseEx
8686

8787
@Deprecated
8888
public List<String> getSuggestions(String input) {
89-
return parsers.stream().flatMap(
90-
p -> p.getSuggestions(input)
91-
).collect(Collectors.toList());
89+
return getSuggestions(input, new ParserContext());
9290
}
9391

9492
public List<String> getSuggestions(String input, ParserContext context) {
95-
return getSuggestions(input);
93+
return parsers.stream().flatMap(
94+
p -> p.getSuggestions(input, context)
95+
).collect(Collectors.toList());
9696
}
9797

9898
/**

0 commit comments

Comments
 (0)