Plex-FAWE/worldedit-core/src/main/java/com/boydti/fawe/object/mask/AdjacentAnyMask.java

80 lines
2.2 KiB
Java
Raw Normal View History

package com.boydti.fawe.object.mask;
import com.sk89q.worldedit.function.mask.AbstractMask;
import com.sk89q.worldedit.function.mask.Mask;
2018-12-23 16:19:33 +00:00
import com.sk89q.worldedit.math.BlockVector3;
2019-03-31 16:09:20 +00:00
import com.sk89q.worldedit.math.MutableBlockVector3;
/**
* Just an optimized version of the Adjacent Mask for single adjacency
*/
public class AdjacentAnyMask extends AbstractMask implements ResettableMask {
private final CachedMask mask;
2019-03-31 16:09:20 +00:00
private transient MutableBlockVector3 mutable = new MutableBlockVector3();
public AdjacentAnyMask(Mask mask) {
this.mask = CachedMask.cache(mask);
2019-03-31 16:09:20 +00:00
mutable = new MutableBlockVector3();
}
@Override
public void reset() {
2019-03-31 16:09:20 +00:00
mutable = new MutableBlockVector3();
}
public CachedMask getParentMask() {
return mask;
}
@Override
2018-12-23 16:19:33 +00:00
public boolean test(BlockVector3 v) {
int x = v.getBlockX();
int y = v.getBlockY();
int z = v.getBlockZ();
if (mask.test(x + 1, y, z)) {
return true;
}
if (mask.test(x - 1, y, z)) {
return true;
}
if (mask.test(x, y, z + 1)) {
return true;
}
if (mask.test(x, y, z - 1)) {
return true;
}
if (y < 256 && mask.test(x, y + 1, z)) {
return true;
}
if (y > 0 && mask.test(x, y - 1, z)) {
return true;
}
return false;
}
2018-12-23 16:19:33 +00:00
public BlockVector3 direction(BlockVector3 v) {
int x = v.getBlockX();
int y = v.getBlockY();
int z = v.getBlockZ();
if (mask.test(x + 1, y, z)) {
2019-01-09 07:13:44 +00:00
mutable.setComponents(1, 0, 0);
}else
if (mask.test(x - 1, y, z)) {
2019-01-09 07:13:44 +00:00
mutable.setComponents(-1, 0, 0);
}else
if (mask.test(x, y, z + 1)) {
2019-01-09 07:13:44 +00:00
mutable.setComponents(0, 0, 1);
}else
if (mask.test(x, y, z - 1)) {
2019-01-09 07:13:44 +00:00
mutable.setComponents(0, 0, -1);
}else
if (y < 256 && mask.test(x, y + 1, z)) {
mutable.setComponents(0, 1, 0);
}else
if (y > 0 && mask.test(x, y - 1, z)) {
mutable.setComponents(0, -1, 0);
}
2019-03-31 16:09:20 +00:00
return (mutable.getX() == 0 && mutable.getY() == 0 && mutable.getZ() == 0) ? null : mutable;
}
}