state stuff

This commit is contained in:
kashike
2018-07-05 16:16:52 -07:00
committed by Matthew Miller
parent 40a665a509
commit ee6af8ee76
15 changed files with 99 additions and 218 deletions

View File

@ -23,8 +23,7 @@ import com.google.common.collect.ArrayTable;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.sk89q.worldedit.registry.state.State;
import com.sk89q.worldedit.registry.state.value.StateValue;
import com.sk89q.worldedit.registry.state.Property;
import java.util.ArrayList;
import java.util.Collections;
@ -39,11 +38,11 @@ import java.util.Map;
public class BlockState implements BlockStateHolder<BlockState> {
private final BlockType blockType;
private final Map<State, StateValue> values;
private final Map<Property<?>, Object> values;
private final boolean fuzzy;
// Neighbouring state table.
private Table<State, StateValue, BlockState> states;
private Table<Property<?>, Object, BlockState> states;
BlockState(BlockType blockType) {
this.blockType = blockType;
@ -57,21 +56,21 @@ public class BlockState implements BlockStateHolder<BlockState> {
* @param blockType The block type
* @param values The block state values
*/
public BlockState(BlockType blockType, Map<State, StateValue> values) {
public BlockState(BlockType blockType, Map<Property<?>, Object> values) {
this.blockType = blockType;
this.values = values;
this.fuzzy = true;
}
public void populate(Map<Map<State, StateValue>, BlockState> stateMap) {
final Table<State, StateValue, BlockState> states = HashBasedTable.create();
public void populate(Map<Map<Property<?>, Object>, BlockState> stateMap) {
final Table<Property<?>, Object, BlockState> states = HashBasedTable.create();
for(final Map.Entry<State, StateValue> entry : this.values.entrySet()) {
final State state = entry.getKey();
for(final Map.Entry<Property<?>, Object> entry : this.values.entrySet()) {
final Property property = entry.getKey();
state.getValues().forEach(value -> {
property.getValues().forEach(value -> {
if(value != entry.getValue()) {
states.put(state, (StateValue) value, stateMap.get(this.withValue(state, (StateValue) value)));
states.put(property, value, stateMap.get(this.withValue(property, value)));
}
});
}
@ -79,8 +78,8 @@ public class BlockState implements BlockStateHolder<BlockState> {
this.states = states.isEmpty() ? states : ArrayTable.create(states);
}
private Map<State, StateValue> withValue(final State property, final StateValue value) {
final Map<State, StateValue> values = Maps.newHashMap(this.values);
private <V> Map<Property<?>, Object> withValue(final Property<V> property, final V value) {
final Map<Property<?>, Object> values = Maps.newHashMap(this.values);
values.put(property, value);
return values;
}
@ -91,22 +90,22 @@ public class BlockState implements BlockStateHolder<BlockState> {
}
@Override
public BlockState with(State state, StateValue value) {
public <V> BlockState with(final Property<V> property, final V value) {
if (fuzzy) {
return setState(state, value);
return setState(property, value);
} else {
BlockState result = states.get(state, value);
BlockState result = states.get(property, value);
return result == null ? this : result;
}
}
@Override
public StateValue getState(State state) {
return this.values.get(state);
public <V> V getState(final Property<V> property) {
return (V) this.values.get(property);
}
@Override
public Map<State, StateValue> getStates() {
public Map<Property<?>, Object> getStates() {
return Collections.unmodifiableMap(this.values);
}
@ -120,20 +119,20 @@ public class BlockState implements BlockStateHolder<BlockState> {
return false;
}
List<State> differingStates = new ArrayList<>();
List<Property> differingProperties = new ArrayList<>();
for (Object state : o.getStates().keySet()) {
if (getState((State) state) == null) {
differingStates.add((State) state);
if (getState((Property) state) == null) {
differingProperties.add((Property) state);
}
}
for (State state : getStates().keySet()) {
if (o.getState(state) == null) {
differingStates.add(state);
for (Property property : getStates().keySet()) {
if (o.getState(property) == null) {
differingProperties.add(property);
}
}
for (State state : differingStates) {
if (!getState(state).equals(o.getState(state))) {
for (Property property : differingProperties) {
if (!getState(property).equals(o.getState(property))) {
return false;
}
}
@ -151,12 +150,12 @@ public class BlockState implements BlockStateHolder<BlockState> {
*
* Sets a value. DO NOT USE THIS.
*
* @param state The state
* @param property The state
* @param value The value
* @return The blockstate, for chaining
*/
private BlockState setState(State state, StateValue value) {
this.values.put(state, value);
private <V> BlockState setState(final Property<V> property, final V value) {
this.values.put(property, value);
return this;
}
}

View File

@ -19,8 +19,7 @@
package com.sk89q.worldedit.world.block;
import com.sk89q.worldedit.registry.state.State;
import com.sk89q.worldedit.registry.state.value.StateValue;
import com.sk89q.worldedit.registry.state.Property;
import java.util.Map;
@ -36,26 +35,26 @@ public interface BlockStateHolder<T extends BlockStateHolder> {
/**
* Returns a BlockState with the given state and value applied.
*
* @param state The state
* @param property The state
* @param value The value
* @return The modified state, or same if could not be applied
*/
T with(State state, StateValue value);
<V> T with(final Property<V> property, final V value);
/**
* Gets the value at the given state
*
* @param state The state
* @param property The state
* @return The value
*/
StateValue getState(State state);
<V> V getState(Property<V> property);
/**
* Gets an immutable collection of the states.
*
* @return The states
*/
Map<State, StateValue> getStates();
Map<Property<?>, Object> getStates();
/**
* Checks if the type is the same, and if the matched states are the same.

View File

@ -22,7 +22,7 @@ package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.registry.state.State;
import com.sk89q.worldedit.registry.state.Property;
import java.util.Map;
@ -58,6 +58,6 @@ public interface BlockRegistry {
* @return a map of states where the key is the state's ID
*/
@Nullable
Map<String, ? extends State> getStates(BlockStateHolder block);
Map<String, ? extends Property> getStates(BlockStateHolder block);
}

View File

@ -25,9 +25,9 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.gson.VectorAdapter;
import com.sk89q.worldedit.registry.state.SimpleState;
import com.sk89q.worldedit.registry.state.State;
import com.sk89q.worldedit.registry.state.AbstractProperty;
import javax.annotation.Nullable;
import java.io.IOException;
@ -128,7 +128,7 @@ public class BundledBlockData {
* @return the block's states, or null if no information is available
*/
@Nullable
public Map<String, ? extends State> getStatesById(String id) {
public Map<String, ? extends Property> getStatesById(String id) {
BlockEntry entry = findById(id);
if (entry != null) {
return entry.states;
@ -151,11 +151,11 @@ public class BundledBlockData {
private String unlocalizedName;
public String localizedName;
private List<String> aliases;
public Map<String, SimpleState> states = new HashMap<>();
public Map<String, AbstractProperty> states = new HashMap<>();
private SimpleBlockMaterial material = new SimpleBlockMaterial();
void postDeserialization() {
for (Map.Entry<String, SimpleState> state : states.entrySet()) {
for (Map.Entry<String, AbstractProperty> state : states.entrySet()) {
state.getValue().setName(state.getKey());
}
}

View File

@ -20,10 +20,10 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.registry.state.State;
import java.util.Map;
@ -49,7 +49,7 @@ public class BundledBlockRegistry implements BlockRegistry {
@Nullable
@Override
public Map<String, ? extends State> getStates(BlockStateHolder block) {
public Map<String, ? extends Property> getStates(BlockStateHolder block) {
return BundledBlockData.getInstance().getStatesById(block.getBlockType().getId());
}