2019-06-04 15:48:30 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
package com.sk89q.worldedit.extent.transform;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.object.extent.ResettableExtent;
|
|
|
|
import com.boydti.fawe.util.ReflectionUtils;
|
2019-06-25 17:07:47 +00:00
|
|
|
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2019-04-07 07:41:26 +00:00
|
|
|
import com.sk89q.worldedit.internal.helper.MCDirections;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.Vector3;
|
2019-01-31 15:08:58 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.AffineTransform;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2019-04-07 07:41:26 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.AbstractProperty;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.DirectionalProperty;
|
2019-01-31 15:08:58 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2019-04-07 07:41:26 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.PropertyKey;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
2019-06-25 17:07:47 +00:00
|
|
|
import static com.sk89q.worldedit.util.Direction.*;
|
2019-01-31 15:08:58 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-08-21 17:22:37 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2019-04-07 07:41:26 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockType;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2019-07-06 00:46:48 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.util.List;
|
2019-04-07 07:41:26 +00:00
|
|
|
import java.util.Map;
|
2019-01-31 15:08:58 +00:00
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
/**
|
|
|
|
* Transforms blocks themselves (but not their position) according to a
|
|
|
|
* given transform.
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
public class BlockTransformExtent extends ResettableExtent {
|
2019-07-06 00:46:48 +00:00
|
|
|
|
2019-04-03 13:25:16 +00:00
|
|
|
private Transform transform;
|
2019-07-06 00:46:48 +00:00
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
private Transform transformInverse;
|
|
|
|
private int[] BLOCK_ROTATION_BITMASK;
|
|
|
|
private int[][] BLOCK_TRANSFORM;
|
|
|
|
private int[][] BLOCK_TRANSFORM_INVERSE;
|
|
|
|
private int[] ALL = new int[0];
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
public BlockTransformExtent(Extent parent) {
|
|
|
|
this(parent, new AffineTransform());
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2019-06-04 15:48:30 +00:00
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param extent the extent
|
|
|
|
*/
|
|
|
|
public BlockTransformExtent(Extent extent, Transform transform) {
|
|
|
|
super(extent);
|
|
|
|
checkNotNull(transform);
|
2014-07-09 21:14:17 +00:00
|
|
|
this.transform = transform;
|
2019-04-07 07:41:26 +00:00
|
|
|
this.transformInverse = this.transform.inverse();
|
|
|
|
cache();
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
private static long combine(Direction... directions) {
|
2019-06-28 18:20:48 +00:00
|
|
|
long mask = 0;
|
|
|
|
for (Direction dir : directions) {
|
|
|
|
mask = mask | (1L << dir.ordinal());
|
|
|
|
}
|
|
|
|
return mask;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
private static long[] adapt(Direction... dirs) {
|
2019-07-06 00:46:48 +00:00
|
|
|
return Arrays.stream(dirs).mapToLong(dir -> 1L << dir.ordinal()).toArray();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
private static long[] adapt(Long... dirs) {
|
2019-07-06 00:46:48 +00:00
|
|
|
return Arrays.stream(dirs).mapToLong(dir -> dir).toArray();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
private static long[] getDirections(AbstractProperty property) {
|
2019-04-07 07:41:26 +00:00
|
|
|
if (property instanceof DirectionalProperty) {
|
|
|
|
DirectionalProperty directional = (DirectionalProperty) property;
|
|
|
|
return adapt(directional.getValues().toArray(new Direction[0]));
|
|
|
|
} else {
|
|
|
|
List values = property.getValues();
|
|
|
|
switch (property.getKey()) {
|
|
|
|
case HALF:
|
|
|
|
return adapt(UP, DOWN);
|
|
|
|
case ROTATION: {
|
|
|
|
List<Direction> directions = new ArrayList<>();
|
|
|
|
for (Object value : values) {
|
|
|
|
directions.add(Direction.fromRotationIndex((Integer) value).get());
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
return adapt(directions.toArray(new Direction[0]));
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
case AXIS:
|
|
|
|
switch (property.getValues().size()) {
|
|
|
|
case 3:
|
2019-04-11 15:00:03 +00:00
|
|
|
return adapt(combine(EAST, WEST), combine(UP, DOWN), combine(SOUTH, NORTH));
|
2019-04-07 07:41:26 +00:00
|
|
|
case 2:
|
|
|
|
return adapt(combine(EAST, WEST), combine(SOUTH, NORTH));
|
|
|
|
default:
|
|
|
|
System.out.println("Invalid " + property.getName() + " " + property.getValues());
|
|
|
|
return null;
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
case FACING: {
|
|
|
|
List<Direction> directions = new ArrayList<>();
|
|
|
|
for (Object value : values) {
|
|
|
|
directions.add(Direction.valueOf(value.toString().toUpperCase()));
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
return adapt(directions.toArray(new Direction[0]));
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
case SHAPE:
|
|
|
|
if (values.contains("straight")) {
|
|
|
|
ArrayList<Long> result = new ArrayList<>();
|
|
|
|
for (Object value : values) {
|
|
|
|
// [straight, inner_left, inner_right, outer_left, outer_right]
|
|
|
|
switch (value.toString()) {
|
|
|
|
case "straight":
|
|
|
|
result.add(combine(NORTH, EAST, SOUTH, WEST));
|
|
|
|
continue;
|
|
|
|
case "inner_left":
|
2019-04-07 10:43:59 +00:00
|
|
|
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
|
2019-04-07 07:41:26 +00:00
|
|
|
continue;
|
|
|
|
case "inner_right":
|
2019-04-07 10:43:59 +00:00
|
|
|
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
|
2019-04-07 07:41:26 +00:00
|
|
|
continue;
|
|
|
|
case "outer_left":
|
2019-04-07 10:43:59 +00:00
|
|
|
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
|
2019-04-07 07:41:26 +00:00
|
|
|
continue;
|
|
|
|
case "outer_right":
|
2019-04-07 10:43:59 +00:00
|
|
|
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
|
2019-04-07 07:41:26 +00:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
System.out.println("Unknown direction " + value);
|
2019-06-04 15:48:30 +00:00
|
|
|
result.add(0L);
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
return adapt(result.toArray(new Long[0]));
|
|
|
|
} else {
|
|
|
|
List<Long> directions = new ArrayList<>();
|
|
|
|
for (Object value : values) {
|
|
|
|
switch (value.toString()) {
|
|
|
|
case "north_south":
|
|
|
|
directions.add(combine(NORTH, SOUTH));
|
|
|
|
break;
|
|
|
|
case "east_west":
|
|
|
|
directions.add(combine(EAST, WEST));
|
|
|
|
break;
|
|
|
|
case "ascending_east":
|
|
|
|
directions.add(combine(ASCENDING_EAST));
|
|
|
|
break;
|
|
|
|
case "ascending_west":
|
|
|
|
directions.add(combine(ASCENDING_WEST));
|
|
|
|
break;
|
|
|
|
case "ascending_north":
|
|
|
|
directions.add(combine(ASCENDING_NORTH));
|
|
|
|
break;
|
|
|
|
case "ascending_south":
|
|
|
|
directions.add(combine(ASCENDING_SOUTH));
|
|
|
|
break;
|
|
|
|
case "south_east":
|
|
|
|
directions.add(combine(SOUTHEAST));
|
|
|
|
break;
|
|
|
|
case "south_west":
|
|
|
|
directions.add(combine(SOUTHWEST));
|
|
|
|
break;
|
|
|
|
case "north_west":
|
|
|
|
directions.add(combine(NORTHWEST));
|
|
|
|
break;
|
|
|
|
case "north_east":
|
|
|
|
directions.add(combine(NORTHEAST));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
System.out.println("Unknown direction " + value);
|
2019-06-04 15:48:30 +00:00
|
|
|
directions.add(0L);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return adapt(directions.toArray(new Long[0]));
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
private static boolean hasDirection(long mask, Direction dir) {
|
|
|
|
return (mask & (1L << dir.ordinal())) != 0;
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:43:59 +00:00
|
|
|
private static long notIndex(long mask, int... indexes) {
|
2019-06-28 18:20:48 +00:00
|
|
|
for (int index : indexes) {
|
|
|
|
mask = mask | (1L << (index + values().length));
|
|
|
|
}
|
2019-04-07 10:43:59 +00:00
|
|
|
return mask;
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
private static boolean hasIndex(long mask, int index) {
|
|
|
|
return ((mask >> values().length) & (1 << index)) == 0;
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
@Nullable
|
2019-04-07 07:41:26 +00:00
|
|
|
private static Integer getNewStateIndex(Transform transform, long[] directions, int oldIndex) {
|
|
|
|
long oldDirMask = directions[oldIndex];
|
|
|
|
if (oldDirMask == 0) {
|
2019-04-07 10:43:59 +00:00
|
|
|
return null;
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
2019-04-07 10:43:59 +00:00
|
|
|
Integer newIndex = null;
|
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
for (Direction oldDirection : values()) {
|
2019-04-07 10:43:59 +00:00
|
|
|
if (!hasDirection(oldDirMask, oldDirection)) {
|
|
|
|
continue;
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
Vector3 oldVector = oldDirection.toVector();
|
|
|
|
Vector3 newVector = transform.apply(oldVector).subtract(transform.apply(Vector3.ZERO)).normalize();
|
2019-04-07 10:43:59 +00:00
|
|
|
boolean flip = false;
|
|
|
|
|
|
|
|
if (transform instanceof AffineTransform) {
|
|
|
|
flip = ((AffineTransform) transform).isScaled(oldVector);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldVector.equals(newVector)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
double closest = oldVector.normalize().dot(newVector);
|
|
|
|
for (int i = 0; i < directions.length; i++) {
|
|
|
|
int j = (oldIndex + i) % directions.length;
|
|
|
|
long newDirMask = directions[j];
|
2019-04-07 10:43:59 +00:00
|
|
|
if (!hasIndex(oldDirMask, j)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
for (Direction v : Direction.values()) {
|
|
|
|
// Check if it's one of the current directions
|
2019-04-07 10:43:59 +00:00
|
|
|
if (!hasDirection(newDirMask, v)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
// Check if the old mask excludes it
|
|
|
|
double dot = v.toVector().normalize().dot(newVector);
|
2019-04-07 10:43:59 +00:00
|
|
|
if (dot > closest || (flip && dot >= closest)) { //
|
2019-04-07 07:41:26 +00:00
|
|
|
closest = dot;
|
|
|
|
newIndex = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 10:43:59 +00:00
|
|
|
if (newIndex != null) return newIndex;
|
2019-01-31 15:08:58 +00:00
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
return newIndex;
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
private static boolean isDirectional(Property property) {
|
2019-04-07 07:41:26 +00:00
|
|
|
if (property instanceof DirectionalProperty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
switch (property.getKey()) {
|
|
|
|
case HALF:
|
|
|
|
case ROTATION:
|
|
|
|
case AXIS:
|
|
|
|
case FACING:
|
|
|
|
case SHAPE:
|
|
|
|
case NORTH:
|
|
|
|
case EAST:
|
|
|
|
case SOUTH:
|
|
|
|
case WEST:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:48:30 +00:00
|
|
|
private static BaseBlock transformBaseBlockNBT(BlockState transformed, CompoundTag tag, Transform transform) {
|
2019-04-09 16:31:05 +00:00
|
|
|
if (tag != null) {
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
|
|
|
Direction direction = MCDirections.fromRotation(rot);
|
|
|
|
|
|
|
|
if (direction != null) {
|
|
|
|
Vector3 applyAbsolute = transform.apply(direction.toVector());
|
|
|
|
Vector3 applyOrigin = transform.apply(Vector3.ZERO);
|
|
|
|
applyAbsolute.mutX(applyAbsolute.getX() - applyOrigin.getX());
|
|
|
|
applyAbsolute.mutY(applyAbsolute.getY() - applyOrigin.getY());
|
|
|
|
applyAbsolute.mutZ(applyAbsolute.getZ() - applyOrigin.getZ());
|
|
|
|
|
|
|
|
Direction newDirection = Direction.findClosest(applyAbsolute, Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
|
|
|
|
|
|
|
|
if (newDirection != null) {
|
|
|
|
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
|
|
|
|
values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new BaseBlock(transformed, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return transformed.toBaseBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int transformState(BlockState state, Transform transform) {
|
|
|
|
int newMaskedId = state.getInternalId();
|
|
|
|
|
|
|
|
BlockType type = state.getBlockType();
|
|
|
|
// Rotate North, East, South, West
|
|
|
|
if (type.hasProperty(PropertyKey.NORTH) && type.hasProperty(PropertyKey.EAST) && type.hasProperty(PropertyKey.SOUTH) && type.hasProperty(PropertyKey.WEST)) {
|
|
|
|
Direction newNorth = findClosest(transform.apply(NORTH.toVector()), Flag.CARDINAL);
|
|
|
|
Direction newEast = findClosest(transform.apply(EAST.toVector()), Flag.CARDINAL);
|
|
|
|
Direction newSouth = findClosest(transform.apply(SOUTH.toVector()), Flag.CARDINAL);
|
|
|
|
Direction newWest = findClosest(transform.apply(WEST.toVector()), Flag.CARDINAL);
|
|
|
|
|
|
|
|
BlockState tmp = state;
|
|
|
|
|
|
|
|
Object northState = tmp.getState(PropertyKey.NORTH);
|
|
|
|
Object eastState = tmp.getState(PropertyKey.EAST);
|
|
|
|
Object southState = tmp.getState(PropertyKey.SOUTH);
|
|
|
|
Object westState = tmp.getState(PropertyKey.WEST);
|
|
|
|
|
|
|
|
tmp = tmp.with(PropertyKey.valueOf(newNorth.name().toUpperCase()), northState);
|
|
|
|
tmp = tmp.with(PropertyKey.valueOf(newEast.name().toUpperCase()), eastState);
|
|
|
|
tmp = tmp.with(PropertyKey.valueOf(newSouth.name().toUpperCase()), southState);
|
|
|
|
tmp = tmp.with(PropertyKey.valueOf(newWest.name().toUpperCase()), westState);
|
|
|
|
|
|
|
|
newMaskedId = tmp.getInternalId();
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:48:30 +00:00
|
|
|
for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
|
2019-04-09 16:31:05 +00:00
|
|
|
if (isDirectional(property)) {
|
|
|
|
long[] directions = getDirections(property);
|
|
|
|
if (directions != null) {
|
|
|
|
Integer newIndex = getNewStateIndex(transform, directions, property.getIndex(state.getInternalId()));
|
|
|
|
if (newIndex != null) {
|
|
|
|
newMaskedId = property.modifyIndex(newMaskedId, newIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newMaskedId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
private void cache() {
|
|
|
|
BLOCK_ROTATION_BITMASK = new int[BlockTypes.size()];
|
|
|
|
BLOCK_TRANSFORM = new int[BlockTypes.size()][];
|
|
|
|
BLOCK_TRANSFORM_INVERSE = new int[BlockTypes.size()][];
|
|
|
|
for (int i = 0; i < BLOCK_TRANSFORM.length; i++) {
|
|
|
|
BLOCK_TRANSFORM[i] = ALL;
|
|
|
|
BLOCK_TRANSFORM_INVERSE[i] = ALL;
|
|
|
|
BlockType type = BlockTypes.get(i);
|
|
|
|
int bitMask = 0;
|
|
|
|
for (AbstractProperty property : (Collection<AbstractProperty>) (Collection) type.getProperties()) {
|
|
|
|
if (isDirectional(property)) {
|
|
|
|
BLOCK_TRANSFORM[i] = null;
|
|
|
|
BLOCK_TRANSFORM_INVERSE[i] = null;
|
|
|
|
bitMask |= property.getBitMask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bitMask != 0) {
|
|
|
|
BLOCK_ROTATION_BITMASK[i] = bitMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
/**
|
|
|
|
* Get the transform.
|
|
|
|
*
|
|
|
|
* @return the transform
|
|
|
|
*/
|
2019-04-07 07:41:26 +00:00
|
|
|
public Transform getTransform() {
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
/**
|
|
|
|
* Transform a block without making a copy.
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
* @param reverse true to transform in the opposite direction
|
|
|
|
* @return the same block
|
|
|
|
*/
|
|
|
|
private <T extends BlockStateHolder<T>> T transformBlock(T block, boolean reverse) {
|
|
|
|
return transform(block, reverse ? transform.inverse() : transform);
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
@Override
|
|
|
|
public BlockState getBlock(BlockVector3 position) {
|
2019-07-06 00:46:48 +00:00
|
|
|
return transformBlock(super.getBlock(position), false);
|
2019-06-25 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BaseBlock getFullBlock(BlockVector3 position) {
|
2019-07-06 00:46:48 +00:00
|
|
|
return transformBlock(super.getFullBlock(position), false);
|
2019-06-25 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
|
2019-07-06 00:46:48 +00:00
|
|
|
return super.setBlock(location, transformBlock(block, true));
|
2019-06-25 17:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:41:26 +00:00
|
|
|
public void setTransform(Transform affine) {
|
|
|
|
this.transform = affine;
|
|
|
|
this.transformInverse = this.transform.inverse();
|
|
|
|
cache();
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
/**
|
|
|
|
* Transform the given block using the given transform.
|
|
|
|
*
|
|
|
|
* <p>The provided block is <em>not</em> modified.</p>
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
* @param transform the transform
|
|
|
|
* @return the same block
|
|
|
|
*/
|
2019-07-06 00:46:48 +00:00
|
|
|
public static <B extends BlockStateHolder<B>> B transform(@NotNull B block, @NotNull Transform transform) {
|
2019-06-28 18:20:48 +00:00
|
|
|
// performance critical
|
2019-06-25 17:07:47 +00:00
|
|
|
BlockState state = block.toImmutableState();
|
|
|
|
|
|
|
|
int transformedId = transformState(state, transform);
|
|
|
|
BlockState transformed = BlockState.getFromInternalId(transformedId);
|
|
|
|
if (block.hasNbtData()) {
|
|
|
|
return (B) transformBaseBlockNBT(transformed, block.getNbtData(), transform);
|
|
|
|
}
|
|
|
|
return (B) (block instanceof BaseBlock ? transformed.toBaseBlock() : transformed);
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:48:30 +00:00
|
|
|
private BlockState transform(BlockState state, int[][] transformArray, Transform transform) {
|
2019-04-09 16:31:05 +00:00
|
|
|
int typeId = state.getInternalBlockTypeId();
|
|
|
|
int[] arr = transformArray[typeId];
|
|
|
|
if (arr == ALL) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
if (arr == null) {
|
|
|
|
arr = transformArray[typeId] = new int[state.getBlockType().getMaxStateId() + 1];
|
|
|
|
Arrays.fill(arr, -1);
|
|
|
|
}
|
|
|
|
int mask = BLOCK_ROTATION_BITMASK[typeId];
|
|
|
|
int internalId = state.getInternalId();
|
2019-04-07 07:41:26 +00:00
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
int maskedId = internalId & mask;
|
|
|
|
int newMaskedId = arr[maskedId >> BlockTypes.BIT_OFFSET];
|
|
|
|
if (newMaskedId != -1) {
|
|
|
|
return BlockState.getFromInternalId(newMaskedId | (internalId & (~mask)));
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
2019-04-09 16:31:05 +00:00
|
|
|
|
2019-04-10 19:14:58 +00:00
|
|
|
newMaskedId = transformState(state, transform);
|
|
|
|
|
2019-04-09 16:31:05 +00:00
|
|
|
arr[maskedId >> BlockTypes.BIT_OFFSET] = newMaskedId & mask;
|
|
|
|
return BlockState.getFromInternalId(newMaskedId);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 10:43:59 +00:00
|
|
|
public final BaseBlock transform(BlockStateHolder block) {
|
2019-04-07 07:41:26 +00:00
|
|
|
BlockState transformed = transform(block.toImmutableState());
|
|
|
|
if (block.hasNbtData()) {
|
2019-04-09 16:31:05 +00:00
|
|
|
return transformBaseBlockNBT(transformed, block.getNbtData(), transform);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
return transformed.toBaseBlock();
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
protected final BlockStateHolder transformInverse(BlockStateHolder block) {
|
2019-04-07 10:43:59 +00:00
|
|
|
BlockState transformed = transformInverse(block.toImmutableState());
|
2019-04-07 07:41:26 +00:00
|
|
|
if (block.hasNbtData()) {
|
2019-04-09 16:31:05 +00:00
|
|
|
return transformBaseBlockNBT(transformed, block.getNbtData(), transformInverse);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:43:59 +00:00
|
|
|
public final BlockState transform(BlockState block) {
|
|
|
|
return transform(block, BLOCK_TRANSFORM, transform);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 17:07:47 +00:00
|
|
|
private BlockState transformInverse(BlockState block) {
|
2019-04-07 10:43:59 +00:00
|
|
|
return transform(block, BLOCK_TRANSFORM_INVERSE, transformInverse);
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getLazyBlock(int x, int y, int z) {
|
2019-04-07 10:43:59 +00:00
|
|
|
return transform(super.getLazyBlock(x, y, z));
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getLazyBlock(BlockVector3 position) {
|
2019-04-07 10:43:59 +00:00
|
|
|
return transform(super.getLazyBlock(position));
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setBlock(int x, int y, int z, BlockStateHolder block) throws WorldEditException {
|
2019-04-07 10:43:59 +00:00
|
|
|
return super.setBlock(x, y, z, transformInverse(block));
|
2019-04-07 07:41:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
/**
|
|
|
|
* Get the new value with the transformed direction.
|
|
|
|
*
|
|
|
|
* @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(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 : allowedStates) {
|
|
|
|
double dot = v.toVector().normalize().dot(newDirection);
|
|
|
|
if (dot >= closest) {
|
|
|
|
closest = dot;
|
|
|
|
newValue = v.toVector();
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return newValue;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 07:41:26 +00:00
|
|
|
|
|
|
|
|
2019-06-04 15:48:30 +00:00
|
|
|
}
|