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

@ -21,6 +21,7 @@ package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial;
@ -28,6 +29,7 @@ import org.bukkit.Material;
import java.util.EnumMap;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
@ -54,6 +56,14 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
return super.getProperties(blockType);
}
@Override
public OptionalInt getInternalBlockStateId(BlockState state) {
if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) {
return WorldEditPlugin.getInstance().getBukkitImplAdapter().getInternalBlockStateId(state);
}
return super.getInternalBlockStateId(state);
}
public static class BukkitBlockMaterial extends PassthroughBlockMaterial {
private final Material material;

View File

@ -38,6 +38,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable;
@ -160,4 +161,15 @@ public interface BukkitImplAdapter {
* @return the WorldEdit BaseItemStack
*/
BaseItemStack adapt(ItemStack itemStack);
/**
* Retrieve the internal ID for a given state, if possible.
*
* @param state The block state
* @return the internal ID of the state
*/
default OptionalInt getInternalBlockStateId(BlockState state) {
return OptionalInt.empty();
}
}

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");
}

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.forge;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
@ -61,8 +63,6 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
public final class ForgeAdapter {
private ForgeAdapter() {

View File

@ -20,17 +20,20 @@
package com.sk89q.worldedit.forge;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.state.IProperty;
import net.minecraftforge.fml.loading.FMLLoader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
import java.util.TreeMap;
import javax.annotation.Nullable;
@ -73,4 +76,10 @@ public class ForgeBlockRegistry extends BundledBlockRegistry {
return map;
}
@Override
public OptionalInt getInternalBlockStateId(BlockState state) {
IBlockState equivalent = ForgeAdapter.adaptState(state);
return OptionalInt.of(Block.getStateId(equivalent));
}
}

View File

@ -34,6 +34,7 @@ import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.block.BlockStateIdAcess;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
@ -50,6 +51,7 @@ import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityType;
@ -94,6 +96,8 @@ import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
@ -173,7 +177,8 @@ public class ForgeWorld extends AbstractWorld {
Chunk chunk = world.getChunk(x >> 4, z >> 4);
BlockPos pos = new BlockPos(x, y, z);
IBlockState old = chunk.getBlockState(pos);
IBlockState newState = ForgeAdapter.adapt(block.toImmutableState());
OptionalInt stateId = BlockStateIdAcess.getBlockStateId(block.toImmutableState());
IBlockState newState = stateId.isPresent() ? Block.getStateById(stateId.getAsInt()) : ForgeAdapter.adaptState(block.toImmutableState());
IBlockState successState = chunk.setBlockState(pos, newState, false);
boolean successful = successState != null;

View File

@ -175,6 +175,9 @@ public class ForgeWorldEdit {
ItemCategory.REGISTRY.register(name.toString(), new ItemCategory(name.toString()));
}
}
config = new ForgeConfiguration(this);
config.load();
}
@SubscribeEvent