Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean contains(Object get) {
if (!(get instanceof com.sk89q.worldedit.entity.Entity e)) {
return false;
}
UUID getUUID = e.getState().getNbtData().getUUID();
UUID getUUID = e.getState().getUUID();
for (NativeEntity entity : nativeEntities) {
UUID uuid = uuidGetter.apply(entity);
if (uuid.equals(getUUID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void removeEntity(int x, int y, int z, UUID uuid) {
Iterator<ClipboardEntity> iter = this.entities.iterator();
while (iter.hasNext()) {
ClipboardEntity entity = iter.next();
UUID entUUID = entity.getState().getNbtData().getUUID();
UUID entUUID = entity.getState().getUUID();
if (uuid.equals(entUUID)) {
iter.remove();
return;
Expand All @@ -140,7 +140,7 @@ public List<? extends Entity> getEntities() {
public void removeEntity(Entity entity) {
if (!(entity instanceof BlockArrayClipboard.ClipboardEntity)) {
Location loc = entity.getLocation();
removeEntity(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), entity.getState().getNbtData().getUUID());
removeEntity(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), entity.getState().getUUID());
} else {
this.entities.remove(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.entity.EntityTypes;
import org.enginehub.linbus.tree.LinCompoundTag;
import org.enginehub.linbus.tree.LinIntArrayTag;
import org.enginehub.linbus.tree.LinLongTag;
import org.enginehub.linbus.tree.LinTag;

import javax.annotation.Nullable;

import java.util.Map;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;

/**
Expand Down Expand Up @@ -119,6 +125,33 @@ public EntityType getType() {
public BaseEntity(CompoundTag tag) {
this(EntityTypes.parse(tag.getString("Id")), tag);
}

/**
* Attempt to retrieve the entity's UUID from its NBT tag
*
* @return entity UUID if possible to retrieve
* @since TODO
*/
@Nullable
public UUID getUUID() {
LinCompoundTag tag = getNbt();
if (tag == null) {
return null;
}
Map<String, LinTag<?>> value = tag.value();
if (value.get("UUID") instanceof LinIntArrayTag intArrayTag) {
int[] arr = intArrayTag.value();
return new UUID((long) arr[0] << 32 | (arr[1] & 0xFFFFFFFFL), (long) arr[2] << 32 | (arr[3] & 0xFFFFFFFFL));
} else if (value.get("UUIDMost") instanceof LinLongTag longTag) {
return new UUID(longTag.value(), ((LinLongTag) value.get("UUIDLeast")).value());
} else if (value.get("WorldUUIDMost") instanceof LinLongTag longTag) {
return new UUID(longTag.value(), ((LinLongTag) value.get("WorldUUIDLeast")).value());
} else if (value.get("PersistentIDMSB") instanceof LinLongTag longTag) {
return new UUID(longTag.value(), ((LinLongTag) value.get("PersistentIDLSB")).value());
} else {
return null;
}
}
//FAWE end

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import com.fastasyncworldedit.core.registry.state.PropertyKey;
import com.fastasyncworldedit.core.registry.state.PropertyKeySet;
import com.google.common.collect.ImmutableMap;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.helper.MCDirections;
Expand All @@ -46,13 +43,15 @@
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import org.apache.logging.log4j.Logger;
import org.enginehub.linbus.tree.LinCompoundTag;
import org.enginehub.linbus.tree.LinNumberTag;
import org.enginehub.linbus.tree.LinTag;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -63,6 +62,10 @@
import static com.sk89q.worldedit.util.Direction.ASCENDING_NORTH;
import static com.sk89q.worldedit.util.Direction.ASCENDING_SOUTH;
import static com.sk89q.worldedit.util.Direction.ASCENDING_WEST;
import static com.sk89q.worldedit.util.Direction.DESCENDING_EAST;
import static com.sk89q.worldedit.util.Direction.DESCENDING_NORTH;
import static com.sk89q.worldedit.util.Direction.DESCENDING_SOUTH;
import static com.sk89q.worldedit.util.Direction.DESCENDING_WEST;
import static com.sk89q.worldedit.util.Direction.DOWN;
import static com.sk89q.worldedit.util.Direction.EAST;
import static com.sk89q.worldedit.util.Direction.Flag;
Expand All @@ -75,7 +78,6 @@
import static com.sk89q.worldedit.util.Direction.UP;
import static com.sk89q.worldedit.util.Direction.WEST;
import static com.sk89q.worldedit.util.Direction.findClosest;
import static com.sk89q.worldedit.util.Direction.values;

/**
* Transforms blocks themselves (but not their position) according to a
Expand All @@ -96,7 +98,8 @@ public class BlockTransformExtent extends ResettableExtent {
PropertyKey.NORTH,
PropertyKey.EAST,
PropertyKey.SOUTH,
PropertyKey.WEST
PropertyKey.WEST,
PropertyKey.ORIENTATION
);

private static final Map<Direction, PropertyKey> directionMap = ImmutableMap.of(
Expand Down Expand Up @@ -159,12 +162,11 @@ private static long[] adapt(Long... dirs) {
return arr;
}

private static long[] getDirections(AbstractProperty property) {
if (property instanceof DirectionalProperty) {
DirectionalProperty directional = (DirectionalProperty) property;
return adapt(directional.getValues().toArray(new Direction[0]));
private static long[] getDirections(AbstractProperty<?> property) {
if (property instanceof final DirectionalProperty dir) {
return adapt(dir.getValues().toArray(new Direction[0]));
} else {
List values = property.getValues();
List<?> values = property.getValues();
PropertyKey key = property.getKey();
switch (key.getName().toLowerCase()) {
case "half": {
Expand All @@ -176,20 +178,19 @@ private static long[] getDirections(AbstractProperty property) {
case "rotation": {
List<Direction> directions = new ArrayList<>();
for (Object value : values) {
directions.add(Direction.fromRotationIndex((Integer) value).get());
directions.add(Direction.fromRotationIndex((Integer) value).orElseThrow());
}
return adapt(directions.toArray(new Direction[0]));
}
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:
return switch (property.getValues().size()) {
case 3 -> adapt(combine(EAST, WEST), combine(UP, DOWN), combine(SOUTH, NORTH));
case 2 -> adapt(combine(EAST, WEST), combine(SOUTH, NORTH));
default -> {
LOGGER.error("Invalid {} {}", property.getName(), property.getValues());
return null;
}
yield null;
}
};
}
case "facing": {
List<Direction> directions = new ArrayList<>();
Expand Down Expand Up @@ -222,28 +223,14 @@ private static long[] getDirections(AbstractProperty property) {
case "straight":
result.add(combine(NORTH, EAST, SOUTH, WEST));
continue;
case "inner_left":
case "inner_left", "inner_right":
result.add(orIndex(
combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST),
property.getIndexFor("outer_right"),
property.getIndexFor("outer_left")
));
continue;
case "inner_right":
result.add(orIndex(
combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST),
property.getIndexFor("outer_right"),
property.getIndexFor("outer_left")
));
continue;
case "outer_left":
result.add(orIndex(
combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST),
property.getIndexFor("inner_left"),
property.getIndexFor("inner_right")
));
continue;
case "outer_right":
case "outer_left", "outer_right":
result.add(orIndex(
combine(NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST),
property.getIndexFor("inner_left"),
Expand Down Expand Up @@ -298,6 +285,38 @@ private static long[] getDirections(AbstractProperty property) {
return adapt(directions.toArray(new Long[0]));
}
}
case "orientiation": {
List<Long> directions = new ArrayList<>();
for (Object value : values) {
switch (value.toString()) {
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 "descending_east":
directions.add(combine(DESCENDING_EAST));
break;
case "descending_west":
directions.add(combine(DESCENDING_WEST));
break;
case "descending_north":
directions.add(combine(DESCENDING_NORTH));
break;
case "descending_south":
directions.add(combine(DESCENDING_SOUTH));
break;
}
}
return adapt(directions.toArray(new Long[0]));
}
}
}
return null;
Expand All @@ -309,13 +328,13 @@ private static boolean hasDirection(long mask, Direction dir) {

private static long orIndex(long mask, int... indexes) {
for (int index : indexes) {
mask = mask | (1L << (index + values().length));
mask = mask | (1L << (index + Direction.values().length));
}
return mask;
}

private static boolean hasIndex(long mask, int index) {
return ((mask >> values().length) & (1 << index)) == 0;
return ((mask >> Direction.values().length) & (1L << index)) == 0;
}

@Nullable
Expand All @@ -326,7 +345,7 @@ private static Integer getNewStateIndex(Transform transform, long[] directions,
}
Integer newIndex = null;

for (Direction oldDirection : values()) {
for (Direction oldDirection : Direction.values()) {
if (!hasDirection(oldDirMask, oldDirection)) {
continue;
}
Expand Down Expand Up @@ -370,7 +389,7 @@ private static Integer getNewStateIndex(Transform transform, long[] directions,
return newIndex;
}

private static boolean isDirectional(Property property) {
private static boolean isDirectional(Property<?> property) {
if (property instanceof DirectionalProperty) {
return true;
}
Expand All @@ -381,35 +400,33 @@ private static boolean isDirectional(Property property) {
return (values.contains("top") || values.contains("left"));
}

private static BaseBlock transformBaseBlockNBT(BlockState transformed, CompoundTag tag, Transform transform) {
if (tag != null) {
if (tag.containsKey("Rot")) {
int rot = tag.asInt("Rot");

Direction direction = MCDirections.fromRotation(rot);

if (direction != null) {
Vector3 applyAbsolute = transform.apply(direction.toVector());
Vector3 applyOrigin = transform.apply(Vector3.ZERO);
applyAbsolute.mutX(applyAbsolute.x() - applyOrigin.x());
applyAbsolute.mutY(applyAbsolute.y() - applyOrigin.y());
applyAbsolute.mutZ(applyAbsolute.z() - applyOrigin.z());

Direction newDirection = Direction.findClosest(
applyAbsolute,
Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL
);

if (newDirection != null) {
Map<String, Tag<?, ?>> values = new HashMap<>(tag.getValue());
values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
tag = new CompoundTag(values);
}
private static BaseBlock transformBaseBlockNBT(BlockState transformed, @Nonnull LinCompoundTag tag, Transform transform) {
Map<String, LinTag<?>> value = tag.value();
if (value.get("Rot") instanceof LinNumberTag<?> rotTag) {
int rot = rotTag.value().intValue();

Direction direction = MCDirections.fromRotation(rot);

if (direction != null) {
Vector3 applyAbsolute = transform.apply(direction.toVector());
Vector3 applyOrigin = transform.apply(Vector3.ZERO);
applyAbsolute.mutX(applyAbsolute.x() - applyOrigin.x());
applyAbsolute.mutY(applyAbsolute.y() - applyOrigin.y());
applyAbsolute.mutZ(applyAbsolute.z() - applyOrigin.z());

Direction newDirection = Direction.findClosest(
applyAbsolute,
Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL
);

if (newDirection != null) {
LinCompoundTag.Builder builder = tag.toBuilder();
builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection));
return transformed.toBaseBlock(builder.build());
}
}
return new BaseBlock(transformed, tag);
}
return transformed.toBaseBlock();
return transformed.toBaseBlock(tag);
}

private static int transformState(BlockState state, Transform transform) {
Expand All @@ -432,7 +449,7 @@ private static int transformState(BlockState state, Transform transform) {
newMaskedId = tmp.getInternalId();
}
// True if relying on two different "directions" for the result, e.g. stairs with both facing and shape
for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
for (AbstractProperty<?> property : (List<AbstractProperty<?>>) type.getProperties()) {
if (isDirectional(property)) {
long[] directions = getDirections(property);
if (directions != null) {
Expand Down Expand Up @@ -467,7 +484,7 @@ private void cache() {
BLOCK_TRANSFORM_INVERSE[i] = ALL;
BlockType type = BlockTypes.get(i);
int bitMask = 0;
for (AbstractProperty property : (Collection<AbstractProperty>) (Collection) type.getProperties()) {
for (AbstractProperty<?> property : (Collection<AbstractProperty>) (Collection) type.getProperties()) {
if (isDirectional(property)) {
BLOCK_TRANSFORM[i] = null;
BLOCK_TRANSFORM_INVERSE[i] = null;
Expand Down Expand Up @@ -556,8 +573,8 @@ public static <B extends BlockStateHolder<B>> B transform(@Nonnull B block, @Non
int transformedId = transformState(state, transform);
BlockState transformed = BlockState.getFromInternalId(transformedId);
boolean baseBlock = block instanceof BaseBlock;
if (baseBlock && block.hasNbtData()) {
return (B) transformBaseBlockNBT(transformed, block.getNbtData(), transform);
if (baseBlock && block.getNbt() != null) {
return (B) transformBaseBlockNBT(transformed, block.getNbt(), transform);
}
return (B) (baseBlock? transformed.toBaseBlock() : transformed);
//FAWE end
Expand Down Expand Up @@ -591,16 +608,16 @@ private BlockState transform(BlockState state, int[][] transformArray, Transform

public final BaseBlock transform(BlockStateHolder<BaseBlock> block) {
BlockState transformed = transform(block.toImmutableState());
if (block.hasNbtData()) {
return transformBaseBlockNBT(transformed, block.getNbtData(), transform);
if (block.getNbt() != null) {
return transformBaseBlockNBT(transformed, block.getNbt(), transform);
}
return transformed.toBaseBlock();
}

protected final BlockStateHolder transformInverse(BlockStateHolder block) {
BlockState transformed = transformInverse(block.toImmutableState());
if (block.hasNbtData()) {
return transformBaseBlockNBT(transformed, block.getNbtData(), transformInverse);
if (block.getNbt() != null) {
return transformBaseBlockNBT(transformed, block.getNbt(), transformInverse);
}
return transformed;
}
Expand Down
Loading
Loading