From 7d7da78dbc44cdf2a0b7189e4b0abd5b62a2d74d Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Thu, 27 Dec 2018 15:19:58 +1000 Subject: [PATCH] Fuzzier fuzzies --- .../factory/parser/DefaultBlockParser.java | 1 + .../worldedit/world/block/BlockState.java | 26 ++-- .../world/block/FuzzyBlockState.java | 132 ++++++++++++++++++ 3 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java index 79310576e..52dc9c485 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java @@ -56,6 +56,7 @@ import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; +import com.sk89q.worldedit.world.block.FuzzyBlockState; import com.sk89q.worldedit.world.registry.LegacyMapper; import java.util.Arrays; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index 969691f7d..4ca5a0e77 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -45,6 +45,14 @@ import javax.annotation.Nullable; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; /** * An immutable class that represents the state a block can be in. @@ -53,6 +61,7 @@ import java.util.stream.Stream; public class BlockState implements BlockStateHolder { private final BlockType blockType; private BaseBlock emptyBaseBlock; + BlockState(BlockType blockType) { this.blockType = blockType; this.emptyBaseBlock = new BaseBlock(this); @@ -347,15 +356,6 @@ public class BlockState implements BlockStateHolder { return getOrdinal(); } - @Override - public boolean equals(Object obj) { - return this == obj; - } - - public BlockState toFuzzy() { - return new BlockState(this.getBlockType(), new HashMap<>()); - } - @Override public boolean equalsFuzzy(BlockStateHolder o) { if (this == o) { @@ -415,4 +415,12 @@ public class BlockState implements BlockStateHolder { //? return 0; } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BlockState)) { + return false; + } + + return equalsFuzzy((BlockState) obj); + } } 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 new file mode 100644 index 000000000..d3125482e --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java @@ -0,0 +1,132 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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); + } + + @SuppressWarnings("unchecked") + @Override + public BlockState toImmutableState() { + BlockState state = getBlockType().getDefaultState(); + for (Map.Entry, Object> entry : getStates().entrySet()) { + state = state.with((Property) entry.getKey(), 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 BlockState internalState; + private Map, 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); + internalState = type.getDefaultState(); + return this; + } + + /** + * The type of the Fuzzy BlockState + * + * @param state The state + * @return The builder, for chaining + */ + public Builder type(BlockState state) { + checkNotNull(state); + internalState = state; + return this; + } + + /** + * Adds a property to the fuzzy BlockState + * + * @param property The property + * @param value The value + * @param The property type + * @return The builder, for chaining + */ + public Builder withProperty(Property property, V value) { + checkNotNull(property); + checkNotNull(value); + checkNotNull(internalState, "The type must be set before the properties!"); + values.put(property, value); + return this; + } + + /** + * Builds a FuzzyBlockState from this builder. + * + * @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()); + } + return blockState; + } + + /** + * Resets the builder. + * + * @return The builder, for chaining + */ + public Builder reset() { + this.internalState = null; + this.values.clear(); + return this; + } + } +}