2019-01-31 15:08:58 +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;
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.object.extent.ResettableExtent;
|
|
|
|
import com.boydti.fawe.util.ReflectionUtils;
|
2019-01-31 15:08:58 +00:00
|
|
|
import com.google.common.collect.Sets;
|
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.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2019-01-09 07:13:44 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
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;
|
|
|
|
import com.sk89q.worldedit.registry.state.BooleanProperty;
|
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.EnumProperty;
|
|
|
|
import com.sk89q.worldedit.registry.state.IntegerProperty;
|
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.world.biome.BaseBiome;
|
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;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-01-31 15:08:58 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.OptionalInt;
|
2019-01-09 07:13:44 +00:00
|
|
|
import java.util.Set;
|
2019-01-31 15:08:58 +00:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2014-07-09 21:14:17 +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-01-31 15:08:58 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
private Transform transform;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2019-01-31 15:08:58 +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-01-31 15:08:58 +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-01-31 15:08:58 +00:00
|
|
|
/**
|
|
|
|
* Get the transform.
|
|
|
|
*
|
|
|
|
* @return the transform
|
|
|
|
*/
|
|
|
|
public Transform getTransform() {
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the transform
|
|
|
|
* @param affine
|
|
|
|
*/
|
|
|
|
public void setTransform(Transform affine) {
|
|
|
|
this.transform = affine;
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-31 15:08:58 +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);
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
2019-01-31 15:08:58 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getLazyBlock(BlockVector3 position) {
|
|
|
|
return transformFast(super.getLazyBlock(position)).toImmutableState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getLazyBlock(int x, int y, int z) {
|
|
|
|
return transformFast(super.getLazyBlock(x, y, z)).toImmutableState();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-31 15:08:58 +00:00
|
|
|
public BlockState getBlock(BlockVector3 position) {
|
|
|
|
return transformFast(super.getBlock(position)).toImmutableState();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
@Override
|
|
|
|
public BaseBlock getFullBlock(BlockVector3 position) {
|
|
|
|
return transformFast(super.getFullBlock(position).toImmutableState());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(int x, int y, int z, B block) throws WorldEditException {
|
|
|
|
return super.setBlock(x, y, z, transformFastInverse((BlockState)block));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
@Override
|
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
|
|
|
|
return super.setBlock(location, transformFastInverse((BlockState)block));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
private static final Set<String> directionNames = Sets.newHashSet("north", "south", "east", "west");
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +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
|
|
|
|
*/
|
|
|
|
public static <B extends BlockStateHolder<B>> B transform(B block, Transform transform) {
|
|
|
|
checkNotNull(block);
|
|
|
|
checkNotNull(transform);
|
|
|
|
B result = block;
|
|
|
|
List<? extends Property<?>> properties = block.getBlockType().getProperties();
|
|
|
|
|
|
|
|
for (Property<?> property : properties) {
|
|
|
|
if (property instanceof DirectionalProperty) {
|
|
|
|
DirectionalProperty dirProp = (DirectionalProperty) property;
|
|
|
|
Direction value = (Direction) block.getState(property);
|
|
|
|
if (value != null) {
|
|
|
|
Vector3 newValue = getNewStateValue(dirProp.getValues(), transform, value.toVector());
|
|
|
|
if (newValue != null) {
|
|
|
|
result = result.with(dirProp, Direction.findClosest(newValue, Direction.Flag.ALL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (property instanceof EnumProperty) {
|
|
|
|
EnumProperty enumProp = (EnumProperty) property;
|
|
|
|
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) {
|
|
|
|
result = result.with(enumProp, axis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (property instanceof IntegerProperty) {
|
|
|
|
IntegerProperty intProp = (IntegerProperty) property;
|
|
|
|
if (property.getName().equals("rotation")) {
|
|
|
|
if (intProp.getValues().size() == 16) {
|
|
|
|
Optional<Direction> direction = Direction.fromRotationIndex(block.getState(intProp));
|
|
|
|
int horizontalFlags = Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL;
|
|
|
|
if (direction.isPresent()) {
|
|
|
|
Vector3 vec = getNewStateValue(Direction.valuesOf(horizontalFlags), transform, direction.get().toVector());
|
|
|
|
if (vec != null) {
|
|
|
|
OptionalInt newRotation = Direction.findClosest(vec, horizontalFlags).toRotationIndex();
|
|
|
|
if (newRotation.isPresent()) {
|
|
|
|
result = result.with(intProp, newRotation.getAsInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
List<String> directionalProperties = properties.stream()
|
|
|
|
.filter(prop -> prop instanceof BooleanProperty)
|
|
|
|
.filter(prop -> directionNames.contains(prop.getName()))
|
|
|
|
.filter(property -> (Boolean) block.getState(property))
|
|
|
|
.map(Property::getName)
|
|
|
|
.map(String::toUpperCase)
|
|
|
|
.map(Direction::valueOf)
|
|
|
|
.map(dir -> Direction.findClosest(transform.apply(dir.toVector()), Direction.Flag.CARDINAL))
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
.map(Direction::name)
|
|
|
|
.map(String::toLowerCase)
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
if (directionalProperties.size() > 0) {
|
|
|
|
for (String directionName : directionNames) {
|
|
|
|
result = result.with(block.getBlockType().getProperty(directionName), directionalProperties.contains(directionName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final BaseBlock transformFast(BlockState block) {
|
|
|
|
BaseBlock transformed = transformBlock(block, false).toBaseBlock();
|
2018-08-12 14:03:07 +00:00
|
|
|
if (block.hasNbtData()) {
|
|
|
|
CompoundTag tag = block.getNbtData();
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
Direction direction = Direction.fromRotationIndex(rot).get();
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (direction != null) {
|
2019-01-09 07:13:44 +00:00
|
|
|
Vector3 applyAbsolute = transform.apply(direction.toVector());
|
|
|
|
Vector3 applyOrigin = transform.apply(Vector3.ZERO);
|
2019-01-31 15:08:58 +00:00
|
|
|
Vector3 newAbsolute = Vector3.at(applyAbsolute.getX() - applyOrigin.getX(), applyAbsolute.getY() - applyOrigin.getY(), applyAbsolute.getZ() - applyOrigin.getZ());
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
Direction newDirection = Direction.findClosest(newAbsolute, Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (newDirection != null) {
|
|
|
|
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
|
2019-01-31 15:08:58 +00:00
|
|
|
values.put("Rot", new ByteTag((byte) newDirection.toRotationIndex().getAsInt()));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 15:08:58 +00:00
|
|
|
transformed.setNbtData(tag);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
public final BaseBlock transformFastInverse(BlockState block) {
|
|
|
|
BaseBlock transformed = transformBlock(block, true).toBaseBlock();
|
2018-08-12 14:03:07 +00:00
|
|
|
if (block.hasNbtData()) {
|
|
|
|
CompoundTag tag = block.getNbtData();
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
Direction direction = Direction.fromRotationIndex(rot).get();
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (direction != null) {
|
2019-01-31 15:08:58 +00:00
|
|
|
Vector3 applyAbsolute = getTransform().inverse().apply(direction.toVector());
|
|
|
|
Vector3 applyOrigin = getTransform().inverse().apply(Vector3.ZERO);
|
|
|
|
|
|
|
|
Vector3 newAbsolute = Vector3.at(applyAbsolute.getX() - applyOrigin.getX(), applyAbsolute.getY() - applyOrigin.getY(), applyAbsolute.getZ() - applyOrigin.getZ());
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
Direction newDirection = Direction.findClosest(newAbsolute, Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
if (newDirection != null) {
|
|
|
|
Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
|
2019-01-31 15:08:58 +00:00
|
|
|
values.put("Rot", new ByteTag((byte) newDirection.toRotationIndex().getAsInt()));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 15:08:58 +00:00
|
|
|
transformed.setNbtData(tag);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
return transformed;
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:08:58 +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;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
for (Direction v : allowedStates) {
|
|
|
|
double dot = v.toVector().normalize().dot(newDirection);
|
|
|
|
if (dot >= closest) {
|
|
|
|
closest = dot;
|
|
|
|
newValue = v.toVector();
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-01-31 15:08:58 +00:00
|
|
|
if (found) {
|
|
|
|
return newValue;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|