2014-03-30 10:08:06 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2014-03-30 10:08:06 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* 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 General Public License for more details.
|
2014-03-30 10:08:06 +00:00
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2014-03-30 10:08:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.function.block;
|
|
|
|
|
2019-06-25 13:00:44 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTagBuilder;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2016-05-19 17:16:59 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2014-03-30 10:08:06 +00:00
|
|
|
import com.sk89q.worldedit.function.RegionFunction;
|
2016-05-19 17:16:59 +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;
|
2014-03-30 10:08:06 +00:00
|
|
|
import com.sk89q.worldedit.math.transform.Transform;
|
2016-05-19 17:16:59 +00:00
|
|
|
import com.sk89q.worldedit.util.Direction;
|
|
|
|
import com.sk89q.worldedit.util.Direction.Flag;
|
2018-10-14 10:40:53 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2014-03-30 10:08:06 +00:00
|
|
|
|
2020-07-14 02:50:59 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2014-03-30 10:08:06 +00:00
|
|
|
/**
|
|
|
|
* Copies blocks from one extent to another.
|
|
|
|
*/
|
|
|
|
public class ExtentBlockCopy implements RegionFunction {
|
|
|
|
|
|
|
|
private final Extent source;
|
|
|
|
private final Extent destination;
|
2018-12-23 16:19:33 +00:00
|
|
|
private final BlockVector3 from;
|
|
|
|
private final BlockVector3 to;
|
2014-03-30 10:08:06 +00:00
|
|
|
private final Transform transform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a new copy.
|
|
|
|
*
|
2019-06-25 13:00:44 +00:00
|
|
|
* @param source the source extent
|
|
|
|
* @param from the source offset
|
2014-03-30 10:08:06 +00:00
|
|
|
* @param destination the destination extent
|
2019-06-25 13:00:44 +00:00
|
|
|
* @param to the destination offset
|
|
|
|
* @param transform a transform to apply to positions (after source offset, before destination offset)
|
2014-03-30 10:08:06 +00:00
|
|
|
*/
|
2018-12-23 16:19:33 +00:00
|
|
|
public ExtentBlockCopy(Extent source, BlockVector3 from, Extent destination, BlockVector3 to, Transform transform) {
|
2014-03-30 10:08:06 +00:00
|
|
|
checkNotNull(source);
|
|
|
|
checkNotNull(from);
|
|
|
|
checkNotNull(destination);
|
|
|
|
checkNotNull(to);
|
|
|
|
checkNotNull(transform);
|
|
|
|
this.source = source;
|
|
|
|
this.from = from;
|
|
|
|
this.destination = destination;
|
|
|
|
this.to = to;
|
|
|
|
this.transform = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-23 16:19:33 +00:00
|
|
|
public boolean apply(BlockVector3 position) throws WorldEditException {
|
2019-07-06 00:46:48 +00:00
|
|
|
BaseBlock block = source.getFullBlock(position);
|
2018-12-23 16:19:33 +00:00
|
|
|
BlockVector3 orig = position.subtract(from);
|
|
|
|
BlockVector3 transformed = transform.apply(orig.toVector3()).toBlockPoint();
|
2016-05-19 17:16:59 +00:00
|
|
|
|
|
|
|
// Apply transformations to NBT data if necessary
|
2019-07-06 00:46:48 +00:00
|
|
|
block = transformNbtData(block);
|
2019-06-25 13:00:44 +00:00
|
|
|
|
2014-07-02 09:56:21 +00:00
|
|
|
return destination.setBlock(transformed.add(to), block);
|
2014-03-30 10:08:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 17:16:59 +00:00
|
|
|
/**
|
|
|
|
* Transform NBT data in the given block state and return a new instance
|
|
|
|
* if the NBT data needs to be transformed.
|
|
|
|
*
|
|
|
|
* @param state the existing state
|
|
|
|
* @return a new state or the existing one
|
|
|
|
*/
|
2019-03-25 17:31:12 +00:00
|
|
|
private BaseBlock transformNbtData(BaseBlock state) {
|
2016-05-19 17:16:59 +00:00
|
|
|
CompoundTag tag = state.getNbtData();
|
2019-03-25 17:31:12 +00:00
|
|
|
|
2016-05-19 17:16:59 +00:00
|
|
|
if (tag != null) {
|
|
|
|
// Handle blocks which store their rotation in NBT
|
|
|
|
if (tag.containsKey("Rot")) {
|
|
|
|
int rot = tag.asInt("Rot");
|
|
|
|
|
|
|
|
Direction direction = MCDirections.fromRotation(rot);
|
|
|
|
|
|
|
|
if (direction != null) {
|
2018-12-23 16:19:33 +00:00
|
|
|
Vector3 vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector3.ZERO)).normalize();
|
|
|
|
Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL | Flag.ORDINAL | Flag.SECONDARY_ORDINAL);
|
2016-05-19 17:16:59 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
if (newDirection != null) {
|
2019-03-25 17:31:12 +00:00
|
|
|
CompoundTagBuilder builder = tag.createBuilder();
|
|
|
|
|
|
|
|
builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection));
|
|
|
|
|
|
|
|
return state.toBaseBlock(builder.build());
|
2016-05-19 17:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:31:12 +00:00
|
|
|
|
2016-05-19 17:16:59 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2014-03-30 10:08:06 +00:00
|
|
|
}
|