-
-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from sk89q/feature/fuzzier-fuzzies
Refactor the Fuzzy system to be immutable
- Loading branch information
Showing
5 changed files
with
160 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* WorldEdit, a Minecraft world manipulation toolkit | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) WorldEdit team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.worldedit.world.block; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.sk89q.worldedit.registry.state.Property; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A Fuzzy BlockState. Used for partial matching. | ||
* | ||
* Immutable, construct with {@link FuzzyBlockState.Builder}. | ||
*/ | ||
public class FuzzyBlockState extends BlockState { | ||
|
||
FuzzyBlockState(BlockType blockType) { | ||
super(blockType); | ||
} | ||
|
||
private FuzzyBlockState(BlockType blockType, Map<Property<?>, Object> values) { | ||
this(blockType); | ||
for (Map.Entry<Property<?>, Object> entry : values.entrySet()) { | ||
setState(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<Property<?>, Object> entry : getStates().entrySet()) { | ||
@SuppressWarnings("unchecked") | ||
Property<Object> objKey = (Property<Object>) entry.getKey(); | ||
state = state.with(objKey, entry.getValue()); | ||
} | ||
return getBlockType().getDefaultState(); | ||
} | ||
|
||
/** | ||
* Gets an instance of a builder. | ||
* | ||
* @return The builder | ||
*/ | ||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* Builder for FuzzyBlockState | ||
*/ | ||
public static class Builder { | ||
private BlockType type; | ||
private Map<Property<?>, Object> values = new HashMap<>(); | ||
|
||
/** | ||
* The type of the Fuzzy BlockState | ||
* | ||
* @param type The type | ||
* @return The builder, for chaining | ||
*/ | ||
public Builder type(BlockType type) { | ||
checkNotNull(type); | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
/** | ||
* The type of the Fuzzy BlockState | ||
* | ||
* @param state The state | ||
* @return The builder, for chaining | ||
*/ | ||
public Builder type(BlockState state) { | ||
checkNotNull(state); | ||
this.type = state.getBlockType(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a property to the fuzzy BlockState | ||
* | ||
* @param property The property | ||
* @param value The value | ||
* @param <V> The property type | ||
* @return The builder, for chaining | ||
*/ | ||
public <V> Builder withProperty(Property<V> property, V value) { | ||
checkNotNull(property); | ||
checkNotNull(value); | ||
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; | ||
} | ||
|
||
/** | ||
* Builds a FuzzyBlockState from this builder. | ||
* | ||
* @return The fuzzy BlockState | ||
*/ | ||
public FuzzyBlockState build() { | ||
checkNotNull(type); | ||
if (values.isEmpty()) { | ||
return type.getFuzzyMatcher(); | ||
} | ||
return new FuzzyBlockState(type, values); | ||
} | ||
|
||
/** | ||
* Resets the builder. | ||
* | ||
* @return The builder, for chaining | ||
*/ | ||
public Builder reset() { | ||
this.type = null; | ||
this.values.clear(); | ||
return this; | ||
} | ||
} | ||
} |