From 8f11d0469b7bfc41bb3b77f5f6cbfc65015e1570 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Fri, 28 Dec 2018 22:20:12 -0800 Subject: [PATCH] Clarify state when asking for caps with no platforms (+ fuzzy system changes) --- .../extension/platform/PlatformManager.java | 9 +++- .../worldedit/world/block/BlockType.java | 4 ++ .../world/block/FuzzyBlockState.java | 45 ++++++++++++------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java index 8cdd9394b..e89d5c4ac 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java @@ -170,8 +170,13 @@ public class PlatformManager { if (platform != null) { return platform; } else { - if (preferences.isEmpty() && !platforms.isEmpty()) { - return platforms.get(0); // Use the first available if preferences have not been decided yet. + if (preferences.isEmpty()) { + // Use the first available if preferences have not been decided yet. + if (platforms.isEmpty()) { + // No platforms registered, this is being called too early! + throw new NoCapablePlatformException("No platforms have been registered yet! Please wait until WorldEdit is initialized."); + } + return platforms.get(0); } throw new NoCapablePlatformException("No platform was found supporting " + capability.name()); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java index 4479343e5..2a27cb699 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java @@ -216,6 +216,10 @@ public class BlockType implements FawePattern { } return defaultState; } + + public FuzzyBlockState getFuzzyMatcher() { + return new FuzzyBlockState(this); + } /** * Slow diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java index d3125482e..edbd65243 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java @@ -37,12 +37,26 @@ public class FuzzyBlockState extends BlockState { super(blockType); } - @SuppressWarnings("unchecked") - @Override - public BlockState toImmutableState() { + private FuzzyBlockState(BlockType blockType, Map, Object> values) { + this(blockType); + for (Map.Entry, Object> entry : values.entrySet()) { +// setState(entry.getKey(), entry.getValue()); + with((Property)entry.getKey(), entry.getValue()); + } + } + + /** + * Gets a full BlockState from this fuzzy one, filling in + * properties with default values where necessary. + * + * @return The full BlockState + */ + public BlockState getFullState() { BlockState state = getBlockType().getDefaultState(); for (Map.Entry, Object> entry : getStates().entrySet()) { - state = state.with((Property) entry.getKey(), entry.getValue()); + @SuppressWarnings("unchecked") + Property objKey = (Property) entry.getKey(); + state = state.with(objKey, entry.getValue()); } return getBlockType().getDefaultState(); } @@ -60,7 +74,7 @@ public class FuzzyBlockState extends BlockState { * Builder for FuzzyBlockState */ public static class Builder { - private BlockState internalState; + private BlockType type; private Map, Object> values = new HashMap<>(); /** @@ -71,7 +85,7 @@ public class FuzzyBlockState extends BlockState { */ public Builder type(BlockType type) { checkNotNull(type); - internalState = type.getDefaultState(); + this.type = type; return this; } @@ -83,7 +97,7 @@ public class FuzzyBlockState extends BlockState { */ public Builder type(BlockState state) { checkNotNull(state); - internalState = state; + this.type = state.getBlockType(); return this; } @@ -98,7 +112,8 @@ public class FuzzyBlockState extends BlockState { public Builder withProperty(Property property, V value) { checkNotNull(property); checkNotNull(value); - checkNotNull(internalState, "The type must be set before the properties!"); + checkNotNull(type, "The type must be set before the properties!"); + type.getProperty(property.getName()); // Verify the property is valid for this type values.put(property, value); return this; } @@ -109,13 +124,11 @@ public class FuzzyBlockState extends BlockState { * @return The fuzzy BlockState */ public FuzzyBlockState build() { - checkNotNull(internalState); - FuzzyBlockState blockState = new FuzzyBlockState(internalState.getBlockType()); - for (Map.Entry, Object> entry : values.entrySet()) { -// blockState.setState(entry.getKey(), entry.getValue()); - blockState = (FuzzyBlockState) blockState.with((Property) entry.getKey(), entry.getValue()); + checkNotNull(type); + if (values.isEmpty()) { + return type.getFuzzyMatcher(); } - return blockState; + return new FuzzyBlockState(type, values); } /** @@ -124,9 +137,9 @@ public class FuzzyBlockState extends BlockState { * @return The builder, for chaining */ public Builder reset() { - this.internalState = null; + this.type = null; this.values.clear(); return this; } } -} +} \ No newline at end of file