Optimize waterlog remover

This commit is contained in:
Jesse Boyd 2019-06-29 04:49:48 +10:00
parent 8b9a2ff18c
commit 3850944a81
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
2 changed files with 36 additions and 8 deletions

View File

@ -257,10 +257,6 @@ public class BlockMaskBuilder {
this.bitSets = bitSets;
}
public BlockMaskBuilder parse(String input) {
return this;
}
public BlockMaskBuilder addAll() {
for (int i = 0; i < bitSets.length; i++) {
bitSets[i] = BlockMask.ALL;

View File

@ -20,27 +20,59 @@
package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BlockMaskBuilder;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
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 java.lang.ref.SoftReference;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
/**
* Removes the waterlogged state from blocks if possible. If not possible, returns air.
*/
public class WaterloggedRemover extends AbstractExtentPattern {
private static SoftReference<BlockState[]> cache = new SoftReference<>(null);
private synchronized BlockState[] getRemap() {
BlockState[] remap = this.cache.get();
if (remap != null) return remap;
this.cache = new SoftReference<>(remap = new BlockState[BlockTypes.states.length]);
// init
for (int i = 0; i < remap.length; i++) {
BlockState state = remap[i];
BlockType type = state.getBlockType();
if (!type.hasProperty(PropertyKey.WATERLOGGED)) {
continue;
}
if (state.getState(PropertyKey.WATERLOGGED) == Boolean.TRUE) {
remap[i] = state.with(PropertyKey.WATERLOGGED, false);
}
}
return remap;
}
private final BlockState[] remap;
public WaterloggedRemover(Extent extent) {
super(extent);
this.remap = getRemap();
}
@Override
public BaseBlock apply(BlockVector3 position) {
BaseBlock block = getExtent().getFullBlock(position);
@SuppressWarnings("unchecked")
Property<Object> prop = (Property<Object>) block.getBlockType().getPropertyMap().getOrDefault("waterlogged", null);
if (prop != null) {
return block.with(prop, false);
BlockState newState = remap[block.getOrdinal()];
if (newState != null) {
return newState.toBaseBlock(block.getNbtData());
}
return BlockTypes.AIR.getDefaultState().toBaseBlock();
}