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.world.registry.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property; 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.block.BlockType;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry; import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial; import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial;
@ -28,6 +29,7 @@ import org.bukkit.Material;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -54,6 +56,14 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
return super.getProperties(blockType); 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 { public static class BukkitBlockMaterial extends PassthroughBlockMaterial {
private final Material material; private final Material material;

View File

@ -38,6 +38,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -160,4 +161,15 @@ public interface BukkitImplAdapter {
* @return the WorldEdit BaseItemStack * @return the WorldEdit BaseItemStack
*/ */
BaseItemStack adapt(ItemStack itemStack); 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.google.common.collect.Table;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEdit; 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.registry.state.Property;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -35,6 +38,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.OptionalInt;
import java.util.Set; import java.util.Set;
/** /**
@ -42,9 +46,14 @@ import java.util.Set;
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class BlockState implements BlockStateHolder<BlockState> { public class BlockState implements BlockStateHolder<BlockState> {
static {
BlockStateIdAcess.setBlockStateStateId(x -> x.internalId);
}
private final BlockType blockType; private final BlockType blockType;
private final Map<Property<?>, Object> values; private final Map<Property<?>, Object> values;
private OptionalInt internalId = OptionalInt.empty();
private BaseBlock emptyBaseBlock; private BaseBlock emptyBaseBlock;
@ -56,8 +65,14 @@ public class BlockState implements BlockStateHolder<BlockState> {
this.values = new LinkedHashMap<>(); this.values = new LinkedHashMap<>();
this.emptyBaseBlock = new BaseBlock(this); 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) { 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<>(); Map<Map<Property<?>, Object>, BlockState> stateMap = new LinkedHashMap<>();
List<? extends Property<?>> properties = blockType.getProperties(); List<? extends Property<?>> properties = blockType.getProperties();
@ -71,7 +86,7 @@ public class BlockState implements BlockStateHolder<BlockState> {
List<List<Object>> valueLists = Lists.cartesianProduct(separatedValues); List<List<Object>> valueLists = Lists.cartesianProduct(separatedValues);
for (List<Object> valueList : valueLists) { for (List<Object> valueList : valueLists) {
Map<Property<?>, Object> valueMap = Maps.newTreeMap(Comparator.comparing(Property::getName)); 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++) { for (int i = 0; i < valueList.size(); i++) {
Property<?> property = properties.get(i); Property<?> property = properties.get(i);
Object value = valueList.get(i); Object value = valueList.get(i);
@ -84,7 +99,7 @@ public class BlockState implements BlockStateHolder<BlockState> {
if (stateMap.isEmpty()) { if (stateMap.isEmpty()) {
// No properties. // No properties.
stateMap.put(new LinkedHashMap<>(), new BlockState(blockType)); stateMap.put(new LinkedHashMap<>(), new BlockState(blockType).initializeId(registry));
} }
for (BlockState state : stateMap.values()) { for (BlockState state : stateMap.values()) {

View File

@ -20,9 +20,11 @@
package com.sk89q.worldedit.world.registry; package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.registry.state.Property; 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.block.BlockType;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -57,4 +59,12 @@ public interface BlockRegistry {
*/ */
Map<String, ? extends Property<?>> getProperties(BlockType blockType); 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; package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.registry.state.Property; 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.block.BlockType;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -52,4 +54,9 @@ public class BundledBlockRegistry implements BlockRegistry {
return Collections.emptyMap(); // Oof 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 gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter()); gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.create(); Gson gson = gsonBuilder.create();
URL url = ResourceLoader.getResource(BundledItemData.class,"items.json"); URL url = ResourceLoader.getResource(BundledItemData.class,"items.json");F
if (url == null) { if (url == null) {
throw new IOException("Could not find items.json"); throw new IOException("Could not find items.json");
} }

View File

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

View File

@ -20,17 +20,20 @@
package com.sk89q.worldedit.forge; package com.sk89q.worldedit.forge;
import com.sk89q.worldedit.registry.state.Property; 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.block.BlockType;
import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.BundledBlockRegistry; import com.sk89q.worldedit.world.registry.BundledBlockRegistry;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.state.IProperty; import net.minecraft.state.IProperty;
import net.minecraftforge.fml.loading.FMLLoader; import net.minecraftforge.fml.loading.FMLLoader;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.OptionalInt;
import java.util.TreeMap; import java.util.TreeMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -73,4 +76,10 @@ public class ForgeBlockRegistry extends BundledBlockRegistry {
return map; 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.BaseEntity;
import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.internal.Constants; import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.block.BlockStateIdAcess;
import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3; 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.item.ItemTypes;
import com.sk89q.worldedit.world.weather.WeatherType; import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldedit.world.weather.WeatherTypes; import com.sk89q.worldedit.world.weather.WeatherTypes;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves; import net.minecraft.block.BlockLeaves;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
@ -94,6 +96,8 @@ import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -173,7 +177,8 @@ public class ForgeWorld extends AbstractWorld {
Chunk chunk = world.getChunk(x >> 4, z >> 4); Chunk chunk = world.getChunk(x >> 4, z >> 4);
BlockPos pos = new BlockPos(x, y, z); BlockPos pos = new BlockPos(x, y, z);
IBlockState old = chunk.getBlockState(pos); 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); IBlockState successState = chunk.setBlockState(pos, newState, false);
boolean successful = successState != null; boolean successful = successState != null;

View File

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