2014-07-09 21:14:17 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.extent.transform;
|
|
|
|
|
2018-06-14 06:35:56 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2018-10-08 12:32:07 +00:00
|
|
|
import com.google.common.collect.Sets;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2018-10-14 10:40:53 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.Vector3;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2018-10-14 10:40:53 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.BooleanProperty;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.DirectionalProperty;
|
2018-11-17 02:00:19 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.EnumProperty;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
2018-10-14 10:40:53 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-10-08 12:32:07 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2018-06-14 06:35:56 +00:00
|
|
|
import javax.annotation.Nullable;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms blocks themselves (but not their position) according to a
|
|
|
|
* given transform.
|
|
|
|
*/
|
|
|
|
public class BlockTransformExtent extends AbstractDelegateExtent {
|
|
|
|
|
|
|
|
private final Transform transform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param extent the extent
|
|
|
|
*/
|
2018-06-19 00:53:15 +00:00
|
|
|
public BlockTransformExtent(Extent extent, Transform transform) {
|
2014-07-09 21:14:17 +00:00
|
|
|
super(extent);
|
|
|
|
checkNotNull(transform);
|
|
|
|
this.transform = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the transform.
|
|
|
|
*
|
|
|
|
* @return the transform
|
|
|
|
*/
|
|
|
|
public Transform getTransform() {
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a block without making a copy.
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
* @param reverse true to transform in the opposite direction
|
|
|
|
* @return the same block
|
|
|
|
*/
|
2018-06-18 12:51:21 +00:00
|
|
|
private <T extends BlockStateHolder> T transformBlock(T block, boolean reverse) {
|
2018-07-22 05:36:50 +00:00
|
|
|
return transform(block, reverse ? transform.inverse() : transform);
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-14 10:40:53 +00:00
|
|
|
public BlockState getBlock(BlockVector3 position) {
|
2014-07-09 21:14:17 +00:00
|
|
|
return transformBlock(super.getBlock(position), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-14 10:40:53 +00:00
|
|
|
public BaseBlock getFullBlock(BlockVector3 position) {
|
2018-06-18 12:51:21 +00:00
|
|
|
return transformBlock(super.getFullBlock(position), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-14 10:40:53 +00:00
|
|
|
public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException {
|
2018-06-18 12:51:21 +00:00
|
|
|
return super.setBlock(location, transformBlock(block, true));
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform the given block using the given transform.
|
|
|
|
*
|
|
|
|
* <p>The provided block is modified.</p>
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
* @param transform the transform
|
|
|
|
* @return the same block
|
|
|
|
*/
|
2018-06-19 00:53:15 +00:00
|
|
|
public static <T extends BlockStateHolder> T transform(T block, Transform transform) {
|
|
|
|
return transform(block, transform, block);
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 12:32:07 +00:00
|
|
|
private static final Set<String> directionNames = Sets.newHashSet("north", "south", "east", "west");
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
/**
|
|
|
|
* Transform the given block using the given transform.
|
|
|
|
*
|
|
|
|
* @param block the block
|
|
|
|
* @param transform the transform
|
|
|
|
* @param changedBlock the block to change
|
|
|
|
* @return the changed block
|
|
|
|
*/
|
2018-06-19 00:53:15 +00:00
|
|
|
private static <T extends BlockStateHolder> T transform(T block, Transform transform, T changedBlock) {
|
2014-07-09 21:14:17 +00:00
|
|
|
checkNotNull(block);
|
|
|
|
checkNotNull(transform);
|
|
|
|
|
2018-10-08 12:32:07 +00:00
|
|
|
List<? extends Property> properties = block.getBlockType().getProperties();
|
|
|
|
|
2018-11-17 02:00:19 +00:00
|
|
|
for (Property<?> property : properties) {
|
2018-07-05 23:16:52 +00:00
|
|
|
if (property instanceof DirectionalProperty) {
|
2018-07-22 05:36:50 +00:00
|
|
|
Direction value = (Direction) block.getState(property);
|
2018-07-05 23:16:52 +00:00
|
|
|
if (value != null) {
|
2018-11-17 02:00:19 +00:00
|
|
|
Vector3 newValue = getNewStateValue((List<Direction>) property.getValues(), transform, value.toVector());
|
2014-07-09 21:14:17 +00:00
|
|
|
if (newValue != null) {
|
2018-07-22 05:36:50 +00:00
|
|
|
changedBlock = (T) changedBlock.with(property, Direction.findClosest(newValue, Direction.Flag.ALL));
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 02:00:19 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 12:32:07 +00:00
|
|
|
|
|
|
|
List<String> directionalProperties = properties.stream()
|
2018-10-11 11:39:21 +00:00
|
|
|
.filter(prop -> prop instanceof BooleanProperty)
|
2018-10-08 12:32:07 +00:00
|
|
|
.filter(prop -> directionNames.contains(prop.getName()))
|
2018-10-11 12:04:16 +00:00
|
|
|
.filter(property -> (Boolean) block.getState(property))
|
2018-10-08 12:32:07 +00:00
|
|
|
.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) {
|
|
|
|
changedBlock = (T) changedBlock.with(block.getBlockType().getProperty(directionName), directionalProperties.contains(directionName));
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
return changedBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the new value with the transformed direction.
|
|
|
|
*
|
2018-11-17 02:00:19 +00:00
|
|
|
* @param allowedStates the allowed states
|
2014-07-09 21:14:17 +00:00
|
|
|
* @param transform the transform
|
|
|
|
* @param oldDirection the old direction to transform
|
|
|
|
* @return a new state or null if none could be found
|
|
|
|
*/
|
|
|
|
@Nullable
|
2018-11-17 02:00:19 +00:00
|
|
|
private static Vector3 getNewStateValue(List<Direction> allowedStates, Transform transform, Vector3 oldDirection) {
|
2018-10-14 10:40:53 +00:00
|
|
|
Vector3 newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector3.ZERO)).normalize();
|
|
|
|
Vector3 newValue = null;
|
2014-07-09 21:14:17 +00:00
|
|
|
double closest = -2;
|
|
|
|
boolean found = false;
|
|
|
|
|
2018-11-17 02:00:19 +00:00
|
|
|
for (Direction v : allowedStates) {
|
2018-07-17 14:42:09 +00:00
|
|
|
double dot = v.toVector().normalize().dot(newDirection);
|
2018-07-17 07:31:07 +00:00
|
|
|
if (dot >= closest) {
|
|
|
|
closest = dot;
|
2018-07-17 14:42:09 +00:00
|
|
|
newValue = v.toVector();
|
2018-07-17 07:31:07 +00:00
|
|
|
found = true;
|
2014-07-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return newValue;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|