/
BlockTransformExtent.java
229 lines (203 loc) · 8.46 KB
/
BlockTransformExtent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* 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;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Sets;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.registry.state.BooleanProperty;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.EnumProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
/**
* 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
*/
public BlockTransformExtent(Extent extent, Transform transform) {
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
*/
private <T extends BlockStateHolder> T transformBlock(T block, boolean reverse) {
return transform(block, reverse ? transform.inverse() : transform);
}
@Override
public BlockState getBlock(BlockVector3 position) {
return transformBlock(super.getBlock(position), false);
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
return transformBlock(super.getFullBlock(position), false);
}
@Override
public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException {
return super.setBlock(location, transformBlock(block, true));
}
/**
* 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
*/
public static <T extends BlockStateHolder> T transform(T block, Transform transform) {
return transform(block, transform, block);
}
private static final Set<String> directionNames = Sets.newHashSet("north", "south", "east", "west");
/**
* 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
*/
private static <T extends BlockStateHolder> T transform(T block, Transform transform, T changedBlock) {
checkNotNull(block);
checkNotNull(transform);
List<? extends Property> properties = block.getBlockType().getProperties();
for (Property<?> property : properties) {
if (property instanceof DirectionalProperty) {
Direction value = (Direction) block.getState(property);
if (value != null) {
Vector3 newValue = getNewStateValue((List<Direction>) property.getValues(), transform, value.toVector());
if (newValue != null) {
changedBlock = (T) changedBlock.with(property, Direction.findClosest(newValue, Direction.Flag.ALL));
}
}
} 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);
}
}
}
}
}
}
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) {
changedBlock = (T) changedBlock.with(block.getBlockType().getProperty(directionName), directionalProperties.contains(directionName));
}
}
return changedBlock;
}
/**
* 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;
for (Direction v : allowedStates) {
double dot = v.toVector().normalize().dot(newDirection);
if (dot >= closest) {
closest = dot;
newValue = v.toVector();
found = true;
}
}
if (found) {
return newValue;
} else {
return null;
}
}
}