static blocktype initialization

instead of using hacky reflection
This commit is contained in:
Jesse Boyd
2019-11-02 20:35:51 +01:00
parent 70efdf8c54
commit 32d837414b
35 changed files with 1128 additions and 989 deletions

View File

@ -5,6 +5,7 @@ import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import java.util.ArrayList;
import java.util.List;
@ -19,7 +20,7 @@ public abstract class ABlockMask extends AbstractExtentMask {
@Override
public String toString() {
List<String> strings = new ArrayList<>();
for (BlockType type : BlockTypes.values) {
for (BlockType type : BlockTypesCache.values) {
if (type != null) {
boolean hasAll;
List<BlockState> all = type.getAllStates();
@ -43,7 +44,7 @@ public abstract class ABlockMask extends AbstractExtentMask {
if (mask instanceof ABlockMask) {
ABlockMask other = (ABlockMask) mask;
BlockMask newMask = new BlockMask(getExtent());
for (BlockState state : BlockTypes.states) {
for (BlockState state : BlockTypesCache.states) {
if (state != null) {
if (test(state) && other.test(state)) {
newMask.add(state);
@ -62,7 +63,7 @@ public abstract class ABlockMask extends AbstractExtentMask {
if (mask instanceof ABlockMask) {
ABlockMask other = (ABlockMask) mask;
BlockMask newMask = new BlockMask(getExtent());
for (BlockState state : BlockTypes.states) {
for (BlockState state : BlockTypesCache.states) {
if (state != null) {
if (test(state) || other.test(state)) {
newMask.add(state);

View File

@ -28,6 +28,7 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import java.util.Arrays;
import java.util.Collection;
@ -51,7 +52,7 @@ public class BlockMask extends ABlockMask {
}
public BlockMask(Extent extent) {
this(extent, new boolean[BlockTypes.states.length]);
this(extent, new boolean[BlockTypesCache.states.length]);
}
public BlockMask(Extent extent, boolean[] ordinals) {
@ -86,7 +87,7 @@ public class BlockMask extends ABlockMask {
public BlockMask add(Predicate<BlockState> predicate) {
for (int i = 0; i < ordinals.length; i++) {
if (!ordinals[i]) {
BlockState state = BlockTypes.states[i];
BlockState state = BlockTypesCache.states[i];
if (state != null) {
ordinals[i] = predicate.test(state);
}
@ -221,7 +222,7 @@ public class BlockMask extends ABlockMask {
BlockType unsetType = null;
int totalTypes = 0;
for (BlockType type : BlockTypes.values) {
for (BlockType type : BlockTypesCache.values) {
if (type != null) {
totalTypes++;
boolean hasAll = true;

View File

@ -14,6 +14,7 @@ import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import java.util.*;
import java.util.function.BiPredicate;
@ -102,7 +103,7 @@ public class BlockMaskBuilder {
} else {
String regex = charSequence.toString();
blockTypeList = new ArrayList<>();
for (BlockType myType : BlockTypes.values) {
for (BlockType myType : BlockTypesCache.values) {
if (myType.getId().matches(regex)) {
blockTypeList.add(myType);
add(myType);
@ -209,7 +210,7 @@ public class BlockMaskBuilder {
if (StringMan.isAlphanumericUnd(input)) {
add(BlockTypes.parse(input));
} else {
for (BlockType myType : BlockTypes.values) {
for (BlockType myType : BlockTypesCache.values) {
if (myType.getId().matches(input)) {
add(myType);
}
@ -223,7 +224,7 @@ public class BlockMaskBuilder {
AbstractProperty prop = (AbstractProperty) property;
long[] states = bitSets[type.getInternalId()];
if (states == null) return false;
int localI = index << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = index << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
return (states == ALL || FastBitSet.get(states, localI));
}
@ -357,7 +358,7 @@ public class BlockMaskBuilder {
for (AbstractProperty prop : properties) {
List values = prop.getValues();
for (int j = 0; j < values.size(); j++) {
int localI = j << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = j << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
if (states == ALL || FastBitSet.get(states, localI)) {
if (!allowed.test(type, new AbstractMap.SimpleEntry(prop, values.get(j)))) {
if (states == ALL) {
@ -435,7 +436,7 @@ public class BlockMaskBuilder {
for (AbstractProperty prop : (List<AbstractProperty<?>>) type.getProperties()) {
List values = prop.getValues();
for (int j = 0; j < values.size(); j++) {
int localI = j << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = j << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
if (states == null || !FastBitSet.get(states, localI)) {
if (propPredicate.test(type, new AbstractMap.SimpleEntry(prop, values.get(j)))) {
if (states == null) {
@ -457,7 +458,7 @@ public class BlockMaskBuilder {
if (states == ALL) return this;
List values = property.getValues();
int localI = index << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = index << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
if (states == null || !FastBitSet.get(states, localI)) {
if (states == null) {
bitSets[type.getInternalId()] = states = FastBitSet.create(type.getMaxStateId() + 1);
@ -473,7 +474,7 @@ public class BlockMaskBuilder {
long[] states = bitSets[type.getInternalId()];
if (states == null) return this;
List values = property.getValues();
int localI = index << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = index << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
if (states == ALL || FastBitSet.get(states, localI)) {
if (states == ALL) {
bitSets[type.getInternalId()] = states = FastBitSet.create(type.getMaxStateId() + 1);
@ -495,7 +496,7 @@ public class BlockMaskBuilder {
}
} else {
for (int i = 0; i < values.size(); i++) {
int index = current.modifyIndex(state, i) >> BlockTypes.BIT_OFFSET;
int index = current.modifyIndex(state, i) >> BlockTypesCache.BIT_OFFSET;
if (set) FastBitSet.set(states, index);
else FastBitSet.clear(states, index);
}
@ -543,7 +544,7 @@ public class BlockMaskBuilder {
for (AbstractProperty prop : (List<AbstractProperty<?>>) type.getProperties()) {
List values = prop.getValues();
for (int j = 0; j < values.size(); j++) {
int localI = j << prop.getBitOffset() >> BlockTypes.BIT_OFFSET;
int localI = j << prop.getBitOffset() >> BlockTypesCache.BIT_OFFSET;
if (FastBitSet.get(bitSet, localI)) set++;
else clear++;
}
@ -558,11 +559,11 @@ public class BlockMaskBuilder {
private boolean[] getOrdinals() {
if (ordinals == null) {
ordinals = new boolean[BlockTypes.states.length];
for (int i = 0; i < BlockTypes.values.length; i++) {
ordinals = new boolean[BlockTypesCache.states.length];
for (int i = 0; i < BlockTypesCache.values.length; i++) {
long[] bitSet = bitSets[i];
if (bitSet == null) continue;
BlockType type = BlockTypes.values[i];
BlockType type = BlockTypesCache.values[i];
if (bitSet == ALL) {
for (BlockState state : type.getAllStates()) {
ordinals[state.getOrdinal()] = true;

View File

@ -5,6 +5,7 @@ import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
public class InverseSingleBlockTypeMask extends ABlockMask {
private final int internalId;
@ -26,7 +27,7 @@ public class InverseSingleBlockTypeMask extends ABlockMask {
@Override
public Mask inverse() {
return new SingleBlockTypeMask(getExtent(), BlockTypes.values[internalId]);
return new SingleBlockTypeMask(getExtent(), BlockTypesCache.values[internalId]);
}
public BlockType getBlockType() {

View File

@ -5,6 +5,7 @@ import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
public class SingleBlockTypeMask extends ABlockMask {
private final int internalId;
@ -26,7 +27,7 @@ public class SingleBlockTypeMask extends ABlockMask {
@Override
public Mask inverse() {
return new InverseSingleBlockTypeMask(getExtent(), BlockTypes.values[internalId]);
return new InverseSingleBlockTypeMask(getExtent(), BlockTypesCache.values[internalId]);
}
public BlockType getBlockType() {

View File

@ -27,6 +27,8 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import java.lang.ref.SoftReference;
/**
@ -39,7 +41,7 @@ public class WaterloggedRemover extends AbstractExtentPattern {
private synchronized BlockState[] getRemap() {
BlockState[] remap = cache.get();
if (remap != null) return remap;
cache = new SoftReference<>(remap = new BlockState[BlockTypes.states.length]);
cache = new SoftReference<>(remap = new BlockState[BlockTypesCache.states.length]);
// init
for (int i = 0; i < remap.length; i++) {