2018-06-17 05:42:47 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-05 08:15:51 +00:00
|
|
|
package com.sk89q.worldedit.world.block;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.ArrayTable;
|
|
|
|
import com.google.common.collect.HashBasedTable;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2018-06-17 05:42:47 +00:00
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import com.google.common.collect.Table;
|
2018-08-10 10:29:06 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2018-07-18 07:39:25 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
import java.util.Collections;
|
2018-07-18 07:39:25 +00:00
|
|
|
import java.util.Comparator;
|
2018-06-17 05:42:47 +00:00
|
|
|
import java.util.HashMap;
|
2018-07-21 10:35:23 +00:00
|
|
|
import java.util.HashSet;
|
2018-07-17 14:42:09 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2018-06-18 12:51:21 +00:00
|
|
|
import java.util.List;
|
2018-06-17 05:42:47 +00:00
|
|
|
import java.util.Map;
|
2018-07-21 10:35:23 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An immutable class that represents the state a block can be in.
|
|
|
|
*/
|
2018-06-17 13:13:22 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2018-06-18 07:53:33 +00:00
|
|
|
public class BlockState implements BlockStateHolder<BlockState> {
|
2018-06-17 05:42:47 +00:00
|
|
|
|
|
|
|
private final BlockType blockType;
|
2018-07-05 23:16:52 +00:00
|
|
|
private final Map<Property<?>, Object> values;
|
2018-06-17 13:13:22 +00:00
|
|
|
private final boolean fuzzy;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-10 10:29:06 +00:00
|
|
|
private BaseBlock emptyBaseBlock;
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
// Neighbouring state table.
|
2018-07-05 23:16:52 +00:00
|
|
|
private Table<Property<?>, Object, BlockState> states;
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-08-03 13:01:56 +00:00
|
|
|
private BlockState(BlockType blockType) {
|
2018-06-17 05:42:47 +00:00
|
|
|
this.blockType = blockType;
|
2018-07-17 14:42:09 +00:00
|
|
|
this.values = new LinkedHashMap<>();
|
2018-08-10 10:29:06 +00:00
|
|
|
this.emptyBaseBlock = new BaseBlock(this);
|
2018-06-17 13:13:22 +00:00
|
|
|
this.fuzzy = false;
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
/**
|
|
|
|
* Creates a fuzzy BlockState. This can be used for partial matching.
|
|
|
|
*
|
|
|
|
* @param blockType The block type
|
|
|
|
* @param values The block state values
|
|
|
|
*/
|
2018-08-03 13:01:56 +00:00
|
|
|
private BlockState(BlockType blockType, Map<Property<?>, Object> values) {
|
2018-06-17 13:13:22 +00:00
|
|
|
this.blockType = blockType;
|
|
|
|
this.values = values;
|
|
|
|
this.fuzzy = true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:01:56 +00:00
|
|
|
static Map<Map<Property<?>, Object>, BlockState> generateStateMap(BlockType blockType) {
|
2018-07-17 14:42:09 +00:00
|
|
|
Map<Map<Property<?>, Object>, BlockState> stateMap = new LinkedHashMap<>();
|
2018-07-18 07:39:25 +00:00
|
|
|
List<? extends Property> properties = blockType.getProperties();
|
|
|
|
|
|
|
|
if (!properties.isEmpty()) {
|
|
|
|
List<List<Object>> separatedValues = Lists.newArrayList();
|
|
|
|
for (Property prop : properties) {
|
|
|
|
List<Object> vals = Lists.newArrayList();
|
|
|
|
vals.addAll(prop.getValues());
|
|
|
|
separatedValues.add(vals);
|
|
|
|
}
|
|
|
|
List<List<Object>> valueLists = Lists.cartesianProduct(separatedValues);
|
|
|
|
for (List<Object> valueList : valueLists) {
|
|
|
|
Map<Property<?>, Object> valueMap = Maps.newTreeMap(Comparator.comparing(Property::getName));
|
|
|
|
BlockState stateMaker = new BlockState(blockType);
|
|
|
|
for (int i = 0; i < valueList.size(); i++) {
|
|
|
|
Property<?> property = properties.get(i);
|
|
|
|
Object value = valueList.get(i);
|
|
|
|
valueMap.put(property, value);
|
|
|
|
stateMaker.setState(property, value);
|
|
|
|
}
|
|
|
|
stateMap.put(valueMap, stateMaker);
|
2018-07-17 14:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stateMap.isEmpty()) {
|
|
|
|
// No properties.
|
|
|
|
stateMap.put(new LinkedHashMap<>(), new BlockState(blockType));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (BlockState state : stateMap.values()) {
|
|
|
|
state.populate(stateMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateMap;
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:01:56 +00:00
|
|
|
private void populate(Map<Map<Property<?>, Object>, BlockState> stateMap) {
|
2018-07-05 23:16:52 +00:00
|
|
|
final Table<Property<?>, Object, BlockState> states = HashBasedTable.create();
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-07-05 23:16:52 +00:00
|
|
|
for(final Map.Entry<Property<?>, Object> entry : this.values.entrySet()) {
|
|
|
|
final Property property = entry.getKey();
|
2018-06-17 05:42:47 +00:00
|
|
|
|
2018-07-05 23:16:52 +00:00
|
|
|
property.getValues().forEach(value -> {
|
2018-06-17 05:42:47 +00:00
|
|
|
if(value != entry.getValue()) {
|
2018-07-18 07:39:25 +00:00
|
|
|
BlockState modifiedState = stateMap.get(this.withValue(property, value));
|
|
|
|
if (modifiedState != null) {
|
|
|
|
states.put(property, value, modifiedState);
|
|
|
|
} else {
|
|
|
|
System.out.println(stateMap);
|
|
|
|
WorldEdit.logger.warning("Found a null state at " + this.withValue(property, value));
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.states = states.isEmpty() ? states : ArrayTable.create(states);
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:16:52 +00:00
|
|
|
private <V> Map<Property<?>, Object> withValue(final Property<V> property, final V value) {
|
|
|
|
final Map<Property<?>, Object> values = Maps.newHashMap(this.values);
|
2018-06-17 05:42:47 +00:00
|
|
|
values.put(property, value);
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-06-17 05:42:47 +00:00
|
|
|
public BlockType getBlockType() {
|
|
|
|
return this.blockType;
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-07-05 23:16:52 +00:00
|
|
|
public <V> BlockState with(final Property<V> property, final V value) {
|
2018-06-17 13:13:22 +00:00
|
|
|
if (fuzzy) {
|
2018-07-05 23:16:52 +00:00
|
|
|
return setState(property, value);
|
2018-06-17 13:13:22 +00:00
|
|
|
} else {
|
2018-07-05 23:16:52 +00:00
|
|
|
BlockState result = states.get(property, value);
|
2018-06-17 13:13:22 +00:00
|
|
|
return result == null ? this : result;
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-07-05 23:16:52 +00:00
|
|
|
public <V> V getState(final Property<V> property) {
|
|
|
|
return (V) this.values.get(property);
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:53:33 +00:00
|
|
|
@Override
|
2018-07-05 23:16:52 +00:00
|
|
|
public Map<Property<?>, Object> getStates() {
|
2018-06-17 13:13:22 +00:00
|
|
|
return Collections.unmodifiableMap(this.values);
|
|
|
|
}
|
|
|
|
|
2018-06-19 01:55:35 +00:00
|
|
|
public BlockState toFuzzy() {
|
|
|
|
return new BlockState(this.getBlockType(), new HashMap<>());
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:51:21 +00:00
|
|
|
@Override
|
|
|
|
public boolean equalsFuzzy(BlockStateHolder o) {
|
|
|
|
if (!getBlockType().equals(o.getBlockType())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-21 10:35:23 +00:00
|
|
|
Set<Property> differingProperties = new HashSet<>();
|
2018-06-18 12:51:21 +00:00
|
|
|
for (Object state : o.getStates().keySet()) {
|
2018-07-05 23:16:52 +00:00
|
|
|
if (getState((Property) state) == null) {
|
|
|
|
differingProperties.add((Property) state);
|
2018-06-18 12:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 23:16:52 +00:00
|
|
|
for (Property property : getStates().keySet()) {
|
|
|
|
if (o.getState(property) == null) {
|
|
|
|
differingProperties.add(property);
|
2018-06-18 12:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 10:35:23 +00:00
|
|
|
for (Property property : getStates().keySet()) {
|
|
|
|
if (differingProperties.contains(property)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!Objects.equals(getState(property), o.getState(property))) {
|
2018-06-18 12:51:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-01 12:03:22 +00:00
|
|
|
@Override
|
|
|
|
public BlockState toImmutableState() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-08-10 10:29:06 +00:00
|
|
|
@Override
|
|
|
|
public BaseBlock toBaseBlock() {
|
|
|
|
if (this.fuzzy) {
|
|
|
|
throw new IllegalArgumentException("Can't create a BaseBlock from a fuzzy BlockState!");
|
|
|
|
}
|
|
|
|
return this.emptyBaseBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BaseBlock toBaseBlock(CompoundTag compoundTag) {
|
|
|
|
if (compoundTag == null) {
|
|
|
|
return toBaseBlock();
|
|
|
|
}
|
|
|
|
return new BaseBlock(this, compoundTag);
|
|
|
|
}
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
/**
|
|
|
|
* Internal method used for creating the initial BlockState.
|
|
|
|
*
|
|
|
|
* Sets a value. DO NOT USE THIS.
|
|
|
|
*
|
2018-07-05 23:16:52 +00:00
|
|
|
* @param property The state
|
2018-06-17 05:42:47 +00:00
|
|
|
* @param value The value
|
|
|
|
* @return The blockstate, for chaining
|
|
|
|
*/
|
2018-07-17 14:42:09 +00:00
|
|
|
private BlockState setState(final Property<?> property, final Object value) {
|
2018-07-05 23:16:52 +00:00
|
|
|
this.values.put(property, value);
|
2018-06-17 05:42:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
2018-07-23 02:48:11 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getAsString();
|
|
|
|
}
|
2018-06-17 05:42:47 +00:00
|
|
|
}
|