Skip to content

Commit

Permalink
Add perf. improvments for Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal authored and wizjany committed Jun 14, 2019
1 parent 5760a50 commit 2070476
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
Expand Up @@ -81,12 +81,6 @@ public OptionalInt getInternalBlockStateId(BlockState state) {
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;
Expand Down
Expand Up @@ -21,8 +21,12 @@

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() {
Expand All @@ -40,7 +44,28 @@ public static void setBlockStateStateId(Provider blockStateStateId) {
}

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

}
Expand Up @@ -68,6 +68,7 @@ public class BlockState implements BlockStateHolder<BlockState> {

BlockState initializeId(BlockRegistry registry) {
this.internalId = registry.getInternalBlockStateId(this);
BlockStateIdAccess.register(this);
return this;
}

Expand Down
Expand Up @@ -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);
}
Expand Up @@ -59,9 +59,4 @@ public OptionalInt getInternalBlockStateId(BlockState state) {
return OptionalInt.empty();
}

@Override
public BlockState getBlockStateByInternalId(int id) {
return null;
}

}
Expand Up @@ -27,23 +27,18 @@
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
Expand Down Expand Up @@ -85,14 +80,4 @@ public OptionalInt getInternalBlockStateId(BlockState state) {
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);
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -92,6 +90,7 @@
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;
Expand All @@ -101,7 +100,7 @@
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.
Expand Down Expand Up @@ -464,13 +463,19 @@ public BlockState getBlock(BlockVector3 position) {
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)));
Expand Down

0 comments on commit 2070476

Please sign in to comment.