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;
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
2018-06-19 00:53:15 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
2018-06-18 12:51:21 +00:00
|
|
|
import com.sk89q.worldedit.blocks.LazyBlock;
|
2018-07-05 08:15:51 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2018-06-19 00:53:15 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Capability;
|
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-06-14 06:35:56 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.state.DirectionalState;
|
|
|
|
import com.sk89q.worldedit.world.registry.state.State;
|
|
|
|
import com.sk89q.worldedit.world.registry.state.value.DirectionalStateValue;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
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-06-19 00:53:15 +00:00
|
|
|
transform(block, reverse ? transform.inverse() : transform);
|
2014-07-09 21:14:17 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 LazyBlock getLazyBlock(Vector position) {
|
2014-07-09 21:14:17 +00:00
|
|
|
return transformBlock(super.getLazyBlock(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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-19 00:53:15 +00:00
|
|
|
Map<String, ? extends State> states = WorldEdit.getInstance().getPlatformManager()
|
|
|
|
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getStates(block);
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
if (states == null) {
|
|
|
|
return changedBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (State state : states.values()) {
|
2018-06-14 06:35:56 +00:00
|
|
|
if (state instanceof DirectionalState) {
|
|
|
|
DirectionalStateValue value = (DirectionalStateValue) block.getState(state);
|
|
|
|
if (value != null && value.getData() != null) {
|
|
|
|
DirectionalStateValue newValue = getNewStateValue((DirectionalState) state, transform, value.getDirection());
|
2014-07-09 21:14:17 +00:00
|
|
|
if (newValue != null) {
|
2018-06-18 07:53:33 +00:00
|
|
|
changedBlock.with(state, newValue);
|
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-06-14 06:35:56 +00:00
|
|
|
private static DirectionalStateValue getNewStateValue(DirectionalState 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-06-14 06:35:56 +00:00
|
|
|
DirectionalStateValue newValue = null;
|
2014-07-09 21:14:17 +00:00
|
|
|
double closest = -2;
|
|
|
|
boolean found = false;
|
|
|
|
|
2018-06-14 06:35:56 +00:00
|
|
|
for (DirectionalStateValue v : state.getValues()) {
|
2014-07-09 21:14:17 +00:00
|
|
|
if (v.getDirection() != null) {
|
|
|
|
double dot = v.getDirection().normalize().dot(newDirection);
|
|
|
|
if (dot >= closest) {
|
|
|
|
closest = dot;
|
|
|
|
newValue = v;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return newValue;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|