Optimize waterlog remover

Dieser Commit ist enthalten in:
Jesse Boyd 2019-06-29 04:49:48 +10:00
Ursprung 8b9a2ff18c
Commit 3850944a81
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 59F1DE6293AF6E1F
2 geänderte Dateien mit 36 neuen und 8 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

@ -20,27 +20,59 @@
package com.sk89q.worldedit.function.pattern; package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BlockMaskBuilder;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property; 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.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.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. * Removes the waterlogged state from blocks if possible. If not possible, returns air.
*/ */
public class WaterloggedRemover extends AbstractExtentPattern { 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) { public WaterloggedRemover(Extent extent) {
super(extent); super(extent);
this.remap = getRemap();
} }
@Override @Override
public BaseBlock apply(BlockVector3 position) { public BaseBlock apply(BlockVector3 position) {
BaseBlock block = getExtent().getFullBlock(position); BaseBlock block = getExtent().getFullBlock(position);
@SuppressWarnings("unchecked") BlockState newState = remap[block.getOrdinal()];
Property<Object> prop = (Property<Object>) block.getBlockType().getPropertyMap().getOrDefault("waterlogged", null); if (newState != null) {
if (prop != null) { return newState.toBaseBlock(block.getNbtData());
return block.with(prop, false);
} }
return BlockTypes.AIR.getDefaultState().toBaseBlock(); return BlockTypes.AIR.getDefaultState().toBaseBlock();
} }