Permalink
Browse files

Added support for axis rotations.

  • Loading branch information...
me4502 committed Nov 17, 2018
1 parent 24800a6 commit 2dc9321da65691279cbc3b65a443da108743ac8c
@@ -30,6 +30,7 @@
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.registry.state.BooleanProperty;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.EnumProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.world.block.BaseBlock;
@@ -49,8 +50,6 @@
*/
public class BlockTransformExtent extends AbstractDelegateExtent {
private static final double RIGHT_ANGLE = Math.toRadians(90);
private final Transform transform;
/**
@@ -129,15 +128,48 @@ public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws Wo
List<? extends Property> properties = block.getBlockType().getProperties();
for (Property property : properties) {
for (Property<?> property : properties) {
if (property instanceof DirectionalProperty) {
Direction value = (Direction) block.getState(property);
if (value != null) {
Vector3 newValue = getNewStateValue((DirectionalProperty) property, transform, value.toVector());
Vector3 newValue = getNewStateValue((List<Direction>) property.getValues(), transform, value.toVector());
if (newValue != null) {
changedBlock = (T) changedBlock.with(property, Direction.findClosest(newValue, Direction.Flag.ALL));
}
}
} else if (property instanceof EnumProperty) {
if (property.getName().equals("axis")) {
// We have an axis - this is something we can do the rotations to :sunglasses:
Direction value = null;
switch ((String) block.getState(property)) {
case "x":
value = Direction.EAST;
break;
case "y":
value = Direction.UP;
break;
case "z":
value = Direction.NORTH;
break;
}
if (value != null) {
Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector());
if (newValue != null) {
String axis = null;
Direction newDir = Direction.findClosest(newValue, Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL);
if (newDir == Direction.NORTH || newDir == Direction.SOUTH) {
axis = "z";
} else if (newDir == Direction.EAST || newDir == Direction.WEST) {
axis = "x";
} else if (newDir == Direction.UP || newDir == Direction.DOWN) {
axis = "y";
}
if (axis != null) {
changedBlock = (T) changedBlock.with(property, axis);
}
}
}
}
}
}
@@ -166,19 +198,19 @@ public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws Wo
/**
* Get the new value with the transformed direction.
*
* @param state the state
* @param allowedStates the allowed states
* @param transform the transform
* @param oldDirection the old direction to transform
* @return a new state or null if none could be found
*/
@Nullable
private static Vector3 getNewStateValue(DirectionalProperty state, Transform transform, Vector3 oldDirection) {
private static Vector3 getNewStateValue(List<Direction> allowedStates, Transform transform, Vector3 oldDirection) {
Vector3 newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector3.ZERO)).normalize();
Vector3 newValue = null;
double closest = -2;
boolean found = false;
for (Direction v : state.getValues()) {
for (Direction v : allowedStates) {
double dot = v.toVector().normalize().dot(newDirection);
if (dot >= closest) {
closest = dot;
@@ -22,6 +22,9 @@
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
/**
@@ -150,6 +153,23 @@ public static Direction findClosest(Vector3 vector, int flags) {
return closest;
}
/**
* Gets all directions with the given flags.
*
* @param flags The flags
* @return The directions that fit the flags
*/
public static List<Direction> valuesOf(int flags) {
List<Direction> directions = new ArrayList<>();
for (Direction direction : values()) {
if ((~flags & direction.flags) == 0) {
directions.add(direction);
}
}
return directions;
}
/**
* Flags to use with {@link #findClosest(Vector3, int)}.
*/

7 comments on commit 2dc9321

@kangarko

This comment has been minimized.

kangarko replied Nov 17, 2018

Sorry for posting here, but what you done with the Vector class?

java.lang.NoClassDefFoundError: com/sk89q/worldedit/Vector

@kenzierocks

This comment has been minimized.

Collaborator

kenzierocks replied Nov 17, 2018

@kangarko

This comment has been minimized.

kangarko replied Nov 17, 2018

Still, I cannot import the BlockVector class. I have the latest WorldEdit for Bukkit .jar as a system dependency in Maven, before it worked flawlessly.

a

@me4502

This comment has been minimized.

Collaborator

me4502 replied Nov 17, 2018

Use BlockVector3.at instead of the constructor.

@kangarko

This comment has been minimized.

kangarko replied Nov 19, 2018

Thanks @me4502 , but I am still getting that weird error above. This is my maven import:

a

and that is the error:

b

strangely my RegionManager class still has the Vector version but the import is missing. Is anything wrong with the worldedit-bukkit-7.0.0-SNAPSHOT-dist.jar file? Tried to attach core version as well without success.

c

@me4502

This comment has been minimized.

Collaborator

me4502 replied Nov 19, 2018

RegionManager is worldguard, are you sure you’re building against worldguard 7 as well?

@kangarko

This comment has been minimized.

kangarko replied Nov 24, 2018

@me4502 thank you, it was that indeed, my bad!

Please sign in to comment.