Add perf. improvments for Forge

This commit is contained in:
Kenzie Togami 2019-05-23 23:19:23 -07:00 committed by Matthew Miller
parent 8c17aab9c5
commit 27c7d488a2
7 changed files with 37 additions and 40 deletions

View File

@ -81,12 +81,6 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
return id;
}
@Nullable
@Override
public BlockState getBlockStateByInternalId(int id) {
return id >= statesById.length ? null : statesById[id];
}
public static class BukkitBlockMaterial extends PassthroughBlockMaterial {
private final Material material;

View File

@ -21,8 +21,12 @@ package com.sk89q.worldedit.internal.block;
import com.sk89q.worldedit.world.block.BlockState;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.OptionalInt;
import static com.google.common.base.Preconditions.checkState;
public class BlockStateIdAccess {
private BlockStateIdAccess() {
@ -40,7 +44,28 @@ public class BlockStateIdAccess {
}
public static OptionalInt getBlockStateId(BlockState holder) {
return blockStateStateId.getBlockStateId((BlockState) holder);
return blockStateStateId.getBlockStateId(holder);
}
public static @Nullable BlockState getBlockStateById(int id) {
return id < blockStates.length ? blockStates[id] : null;
}
private static BlockState[] blockStates = new BlockState[2 << 14];
public static void register(BlockState blockState) {
OptionalInt id = getBlockStateId(blockState);
if (id.isPresent()) {
int i = id.getAsInt();
while (i >= blockStates.length) {
blockStates = Arrays.copyOf(blockStates, blockStates.length + blockStates.length >> 1);
}
BlockState existing = blockStates[i];
checkState(existing == null || existing == blockState,
"BlockState %s is using the same block ID (%s) as BlockState %s",
blockState, i, existing);
blockStates[i] = blockState;
}
}
}

View File

@ -68,6 +68,7 @@ public class BlockState implements BlockStateHolder<BlockState> {
BlockState initializeId(BlockRegistry registry) {
this.internalId = registry.getInternalBlockStateId(this);
BlockStateIdAccess.register(this);
return this;
}

View File

@ -67,12 +67,4 @@ public interface BlockRegistry {
*/
OptionalInt getInternalBlockStateId(BlockState state);
/**
* Retrieve a block state by its internal ID, if possible.
*
* @param id The internal ID
* @return the block state, if available
*/
@Nullable
BlockState getBlockStateByInternalId(int id);
}

View File

@ -59,9 +59,4 @@ public class BundledBlockRegistry implements BlockRegistry {
return OptionalInt.empty();
}
@Override
public BlockState getBlockStateByInternalId(int id) {
return null;
}
}

View File

@ -27,23 +27,18 @@ 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.init.Blocks;
import net.minecraft.state.IProperty;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.registries.GameData;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
import java.util.TreeMap;
import javax.annotation.Nullable;
public class ForgeBlockRegistry extends BundledBlockRegistry {
private final int airId = Block.getStateId(Blocks.AIR.getDefaultState());
private Map<Material, ForgeBlockMaterial> materialMap = new HashMap<>();
@Nullable
@ -85,14 +80,4 @@ public class ForgeBlockRegistry extends BundledBlockRegistry {
IBlockState equivalent = ForgeAdapter.adapt(state);
return OptionalInt.of(Block.getStateId(equivalent));
}
@Override
public BlockState getBlockStateByInternalId(int id) {
IBlockState equivalent = Block.getStateById(id);
if (equivalent.equals(Blocks.AIR.getDefaultState()) && id != airId) {
// We didn't find a match.
return null;
}
return ForgeAdapter.adapt(equivalent);
}
}

View File

@ -19,8 +19,6 @@
package com.sk89q.worldedit.forge;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
@ -92,6 +90,7 @@ import net.minecraft.world.gen.feature.TallTaigaTreeFeature;
import net.minecraft.world.gen.feature.TreeFeature;
import net.minecraft.world.storage.WorldInfo;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@ -101,7 +100,7 @@ import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An adapter to Minecraft worlds for WorldEdit.
@ -464,13 +463,19 @@ public class ForgeWorld extends AbstractWorld {
position.getBlockZ()
);
BlockState matchingBlock = BlockStateIdAccess.getBlockStateById(Block.getStateId(mcState));
if (matchingBlock != null) {
return matchingBlock;
}
return ForgeAdapter.adapt(mcState);
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
TileEntity tile = getWorld().getTileEntity(pos);
// Avoid creation by using the CHECK mode -- if it's needed, it'll be re-created anyways
TileEntity tile = getWorld().getChunk(pos).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK);
if (tile != null) {
return getBlock(position).toBaseBlock(NBTConverter.fromNative(TileEntityUtils.copyNbtData(tile)));