Switch from PropertyKey enum to class (#971)

* Switch from PropertyKey enum to class

* Fix generic toArray
This commit is contained in:
Hannes Greule
2021-03-14 20:38:11 +01:00
committed by GitHub
parent 786baf09f4
commit d974164204
8 changed files with 409 additions and 209 deletions

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.extent.transform;
import com.boydti.fawe.object.extent.ResettableExtent;
import com.google.common.collect.ImmutableMap;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
@ -34,6 +35,7 @@ import com.sk89q.worldedit.registry.state.AbstractProperty;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
import com.sk89q.worldedit.registry.state.PropertyKeySet;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
@ -50,6 +52,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
@ -78,6 +81,25 @@ import static org.slf4j.LoggerFactory.getLogger;
*/
public class BlockTransformExtent extends ResettableExtent {
private static final Set<PropertyKey> directional = PropertyKeySet.of(
PropertyKey.HALF,
PropertyKey.ROTATION,
PropertyKey.AXIS,
PropertyKey.FACING,
PropertyKey.SHAPE,
PropertyKey.NORTH,
PropertyKey.EAST,
PropertyKey.SOUTH,
PropertyKey.WEST
);
private static final Map<Direction, PropertyKey> directionMap = ImmutableMap.of(
NORTH, PropertyKey.NORTH,
EAST, PropertyKey.EAST,
SOUTH, PropertyKey.SOUTH,
WEST, PropertyKey.WEST
);
private Transform transform;
private Transform transformInverse;
@ -134,101 +156,103 @@ public class BlockTransformExtent extends ResettableExtent {
return adapt(directional.getValues().toArray(new Direction[0]));
} else {
List values = property.getValues();
switch (property.getKey()) {
case HALF:
return adapt(UP, DOWN);
case ROTATION: {
List<Direction> directions = new ArrayList<>();
for (Object value : values) {
directions.add(Direction.fromRotationIndex((Integer) value).get());
}
return adapt(directions.toArray(new Direction[0]));
PropertyKey key = property.getKey();
if (key == PropertyKey.HALF) {
return adapt(UP, DOWN);
}
if (key == PropertyKey.ROTATION) {
List<Direction> directions = new ArrayList<>();
for (Object value : values) {
directions.add(Direction.fromRotationIndex((Integer) value).get());
}
case AXIS:
switch (property.getValues().size()) {
case 3:
return adapt(combine(EAST, WEST), combine(UP, DOWN), combine(SOUTH, NORTH));
case 2:
return adapt(combine(EAST, WEST), combine(SOUTH, NORTH));
default:
getLogger(BlockTransformExtent.class).error("Invalid {} {}", property.getName(), property.getValues());
return null;
}
case FACING: {
List<Direction> directions = new ArrayList<>();
for (Object value : values) {
directions.add(Direction.valueOf(value.toString().toUpperCase(Locale.ROOT)));
}
return adapt(directions.toArray(new Direction[0]));
return adapt(directions.toArray(new Direction[0]));
}
if (key == PropertyKey.AXIS) {
switch (property.getValues().size()) {
case 3:
return adapt(combine(EAST, WEST), combine(UP, DOWN), combine(SOUTH, NORTH));
case 2:
return adapt(combine(EAST, WEST), combine(SOUTH, NORTH));
default:
getLogger(BlockTransformExtent.class).error("Invalid {} {}", property.getName(), property.getValues());
return null;
}
case SHAPE:
if (values.contains("straight")) {
ArrayList<Long> result = new ArrayList<>();
for (Object value : values) {
// [straight, inner_left, inner_right, outer_left, outer_right]
switch (value.toString()) {
case "straight":
result.add(combine(NORTH, EAST, SOUTH, WEST));
continue;
case "inner_left":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
continue;
case "inner_right":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
continue;
case "outer_left":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
continue;
case "outer_right":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
continue;
default:
getLogger(BlockTransformExtent.class).warn("Unknown direction {}", value);
result.add(0L);
}
}
if (key == PropertyKey.FACING) {
List<Direction> directions = new ArrayList<>();
for (Object value : values) {
directions.add(Direction.valueOf(value.toString().toUpperCase(Locale.ROOT)));
}
return adapt(directions.toArray(new Direction[0]));
}
if (key == PropertyKey.SHAPE) {
if (values.contains("straight")) {
ArrayList<Long> result = new ArrayList<>();
for (Object value : values) {
// [straight, inner_left, inner_right, outer_left, outer_right]
switch (value.toString()) {
case "straight":
result.add(combine(NORTH, EAST, SOUTH, WEST));
continue;
case "inner_left":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
continue;
case "inner_right":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("outer_right"), property.getIndexFor("outer_left")));
continue;
case "outer_left":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
continue;
case "outer_right":
result.add(notIndex(combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST), property.getIndexFor("inner_left"), property.getIndexFor("inner_right")));
continue;
default:
getLogger(BlockTransformExtent.class).warn("Unknown direction {}", value);
result.add(0L);
}
return adapt(result.toArray(new Long[0]));
} else {
List<Long> directions = new ArrayList<>();
for (Object value : values) {
switch (value.toString()) {
case "north_south":
directions.add(combine(NORTH, SOUTH));
break;
case "east_west":
directions.add(combine(EAST, WEST));
break;
case "ascending_east":
directions.add(combine(ASCENDING_EAST));
break;
case "ascending_west":
directions.add(combine(ASCENDING_WEST));
break;
case "ascending_north":
directions.add(combine(ASCENDING_NORTH));
break;
case "ascending_south":
directions.add(combine(ASCENDING_SOUTH));
break;
case "south_east":
directions.add(combine(SOUTHEAST));
break;
case "south_west":
directions.add(combine(SOUTHWEST));
break;
case "north_west":
directions.add(combine(NORTHWEST));
break;
case "north_east":
directions.add(combine(NORTHEAST));
break;
default:
getLogger(BlockTransformExtent.class).warn("Unknown direction {}", value);
directions.add(0L);
}
}
return adapt(directions.toArray(new Long[0]));
}
return adapt(result.toArray(new Long[0]));
} else {
List<Long> directions = new ArrayList<>();
for (Object value : values) {
switch (value.toString()) {
case "north_south":
directions.add(combine(NORTH, SOUTH));
break;
case "east_west":
directions.add(combine(EAST, WEST));
break;
case "ascending_east":
directions.add(combine(ASCENDING_EAST));
break;
case "ascending_west":
directions.add(combine(ASCENDING_WEST));
break;
case "ascending_north":
directions.add(combine(ASCENDING_NORTH));
break;
case "ascending_south":
directions.add(combine(ASCENDING_SOUTH));
break;
case "south_east":
directions.add(combine(SOUTHEAST));
break;
case "south_west":
directions.add(combine(SOUTHWEST));
break;
case "north_west":
directions.add(combine(NORTHWEST));
break;
case "north_east":
directions.add(combine(NORTHEAST));
break;
default:
getLogger(BlockTransformExtent.class).warn("Unknown direction {}", value);
directions.add(0L);
}
}
return adapt(directions.toArray(new Long[0]));
}
}
}
return null;
@ -304,20 +328,7 @@ public class BlockTransformExtent extends ResettableExtent {
if (property instanceof DirectionalProperty) {
return true;
}
switch (property.getKey()) {
case HALF:
case ROTATION:
case AXIS:
case FACING:
case SHAPE:
case NORTH:
case EAST:
case SOUTH:
case WEST:
return true;
default:
return false;
}
return directional.contains(property.getKey());
}
private static BaseBlock transformBaseBlockNBT(BlockState transformed, CompoundTag tag, Transform transform) {
@ -366,10 +377,10 @@ public class BlockTransformExtent extends ResettableExtent {
Object southState = tmp.getState(PropertyKey.SOUTH);
Object westState = tmp.getState(PropertyKey.WEST);
tmp = tmp.with(PropertyKey.valueOf(newNorth.name().toUpperCase(Locale.ROOT)), northState);
tmp = tmp.with(PropertyKey.valueOf(newEast.name().toUpperCase(Locale.ROOT)), eastState);
tmp = tmp.with(PropertyKey.valueOf(newSouth.name().toUpperCase(Locale.ROOT)), southState);
tmp = tmp.with(PropertyKey.valueOf(newWest.name().toUpperCase(Locale.ROOT)), westState);
tmp = tmp.with(directionMap.get(newNorth), northState);
tmp = tmp.with(directionMap.get(newEast), eastState);
tmp = tmp.with(directionMap.get(newSouth), southState);
tmp = tmp.with(directionMap.get(newWest), westState);
newMaskedId = tmp.getInternalId();
}