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.Vector;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2018-10-11 11:39:21 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.BooleanProperty;
|
2018-08-10 10:29:06 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2018-07-05 23:16:52 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.DirectionalProperty;
|
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-07-17 14:42:09 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
|
|
|
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 static final double RIGHT_ANGLE = Math.toRadians(90);
|
|
|
|
|
|
|
|
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-06-18 12:51:21 +00:00
|
|
|
public BlockState getBlock(Vector position) {
|
2014-07-09 21:14:17 +00:00
|
|
|
return transformBlock(super.getBlock(position), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-18 12:51:21 +00:00
|
|
|
public BaseBlock getFullBlock(Vector position) {
|
|
|
|
return transformBlock(super.getFullBlock(position), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setBlock(Vector location, BlockStateHolder block) throws WorldEditException {
|
|
|
|
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();
|
|
|
|
|
|
|
|
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-07-22 05:36:50 +00:00
|
|
|
Vector newValue = getNewStateValue((DirectionalProperty) property, 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-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.
|
|
|
|
*
|
|
|
|
* @param state the state
|
|
|
|
* @param transform the transform
|
|
|
|
* @param oldDirection the old direction to transform
|
|
|
|
* @return a new state or null if none could be found
|
|
|
|
*/
|
|
|
|
@Nullable
|
2018-07-17 07:31:07 +00:00
|
|
|
private static Vector getNewStateValue(DirectionalProperty state, Transform transform, Vector oldDirection) {
|
2014-07-11 04:05:16 +00:00
|
|
|
Vector newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector.ZERO)).normalize();
|
2018-07-17 07:31:07 +00:00
|
|
|
Vector newValue = null;
|
2014-07-09 21:14:17 +00:00
|
|
|
double closest = -2;
|
|
|
|
boolean found = false;
|
|
|
|
|
2018-07-17 14:42:09 +00:00
|
|
|
for (Direction v : state.getValues()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|