Initial attempt at binding state IDs

This commit is contained in:
Kenzie Togami
2019-02-20 23:47:33 -08:00
committed by Matthew Miller
parent e69ba31d6b
commit a3a175ab8c
11 changed files with 101 additions and 6 deletions

View File

@ -0,0 +1,24 @@
package com.sk89q.worldedit.internal.block;
import com.sk89q.worldedit.world.block.BlockState;
import java.util.OptionalInt;
public class BlockStateIdAcess {
public interface Provider {
OptionalInt getBlockStateId(BlockState holder);
}
private static Provider blockStateStateId;
public static void setBlockStateStateId(Provider blockStateStateId) {
BlockStateIdAcess.blockStateStateId = blockStateStateId;
}
public static OptionalInt getBlockStateId(BlockState holder) {
return blockStateStateId.getBlockStateId((BlockState) holder);
}
}

View File

@ -26,7 +26,10 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.internal.block.BlockStateIdAcess;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import java.util.Collections;
import java.util.Comparator;
@ -35,6 +38,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.Set;
/**
@ -42,9 +46,14 @@ import java.util.Set;
*/
@SuppressWarnings("unchecked")
public class BlockState implements BlockStateHolder<BlockState> {
static {
BlockStateIdAcess.setBlockStateStateId(x -> x.internalId);
}
private final BlockType blockType;
private final Map<Property<?>, Object> values;
private OptionalInt internalId = OptionalInt.empty();
private BaseBlock emptyBaseBlock;
@ -56,8 +65,14 @@ public class BlockState implements BlockStateHolder<BlockState> {
this.values = new LinkedHashMap<>();
this.emptyBaseBlock = new BaseBlock(this);
}
BlockState initializeId(BlockRegistry registry) {
this.internalId = registry.getInternalBlockStateId(this);
return this;
}
static Map<Map<Property<?>, Object>, BlockState> generateStateMap(BlockType blockType) {
BlockRegistry registry = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getRegistries().getBlockRegistry();
Map<Map<Property<?>, Object>, BlockState> stateMap = new LinkedHashMap<>();
List<? extends Property<?>> properties = blockType.getProperties();
@ -71,7 +86,7 @@ public class BlockState implements BlockStateHolder<BlockState> {
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);
BlockState stateMaker = new BlockState(blockType).initializeId(registry);
for (int i = 0; i < valueList.size(); i++) {
Property<?> property = properties.get(i);
Object value = valueList.get(i);
@ -84,7 +99,7 @@ public class BlockState implements BlockStateHolder<BlockState> {
if (stateMap.isEmpty()) {
// No properties.
stateMap.put(new LinkedHashMap<>(), new BlockState(blockType));
stateMap.put(new LinkedHashMap<>(), new BlockState(blockType).initializeId(registry));
}
for (BlockState state : stateMap.values()) {

View File

@ -20,9 +20,11 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
@ -57,4 +59,12 @@ public interface BlockRegistry {
*/
Map<String, ? extends Property<?>> getProperties(BlockType blockType);
/**
* Retrieve the internal ID for a given state, if possible.
*
* @param state The block state
* @return the internal ID of the state
*/
OptionalInt getInternalBlockStateId(BlockState state);
}

View File

@ -20,10 +20,12 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import java.util.Collections;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
@ -52,4 +54,9 @@ public class BundledBlockRegistry implements BlockRegistry {
return Collections.emptyMap(); // Oof
}
@Override
public OptionalInt getInternalBlockStateId(BlockState state) {
return OptionalInt.empty();
}
}

View File

@ -75,7 +75,7 @@ public class BundledItemData {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.create();
URL url = ResourceLoader.getResource(BundledItemData.class,"items.json");
URL url = ResourceLoader.getResource(BundledItemData.class,"items.json");F
if (url == null) {
throw new IOException("Could not find items.json");
}