Search in sources :

Example 16 with Direction

use of com.sk89q.worldedit.util.Direction in project FastAsyncWorldEdit by IntellectualSites.

the class SkullBlockCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    boolean isWall = block.getBlockType() == BlockTypes.SKELETON_WALL_SKULL;
    Tag typeTag = values.get("SkullType");
    if (typeTag instanceof ByteTag) {
        String skullType = convertSkullType(((ByteTag) typeTag).getValue(), isWall);
        if (skullType != null) {
            BlockType type = BlockTypes.get("minecraft:" + skullType);
            if (type != null) {
                BlockState state = type.getDefaultState();
                if (isWall) {
                    Property<Direction> newProp = type.getProperty("facing");
                    state = state.with(newProp, block.getState(FacingProperty));
                } else {
                    Tag rotTag = values.get("Rot");
                    if (rotTag instanceof ByteTag) {
                        Property<Integer> newProp = type.getProperty("rotation");
                        state = state.with(newProp, (int) ((ByteTag) rotTag).getValue());
                    }
                }
                values.remove("SkullType");
                values.remove("Rot");
                return state;
            }
        }
    }
    return block;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) ByteTag(com.sk89q.jnbt.ByteTag) BlockType(com.sk89q.worldedit.world.block.BlockType) ByteTag(com.sk89q.jnbt.ByteTag) Tag(com.sk89q.jnbt.Tag) Direction(com.sk89q.worldedit.util.Direction)

Example 17 with Direction

use of com.sk89q.worldedit.util.Direction in project FastAsyncWorldEdit by IntellectualSites.

the class BedBlockCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    Tag typeTag = values.get("color");
    if (typeTag instanceof IntTag) {
        String bedType = convertBedType(((IntTag) typeTag).getValue());
        if (bedType != null) {
            BlockType type = BlockTypes.get("minecraft:" + bedType);
            if (type != null) {
                BlockState state = type.getDefaultState();
                Property<Direction> facingProp = type.getProperty("facing");
                state = state.with(facingProp, block.getState(FACING_PROPERTY));
                Property<Boolean> occupiedProp = type.getProperty("occupied");
                state = state.with(occupiedProp, false);
                Property<String> partProp = type.getProperty("part");
                state = state.with(partProp, block.getState(PART_PROPERTY));
                values.remove("color");
                return state;
            }
        }
    }
    return block;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) Direction(com.sk89q.worldedit.util.Direction) IntTag(com.sk89q.jnbt.IntTag)

Example 18 with Direction

use of com.sk89q.worldedit.util.Direction in project FastAsyncWorldEdit by IntellectualSites.

the class ExtentBlockCopy method transformNbtData.

/**
 * 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
 */
private BaseBlock transformNbtData(BaseBlock state) {
    // FAWE start - Replace CompoundTag with CompoundBinaryTag
    CompoundBinaryTag tag = state.getNbt();
    if (tag != null) {
        // Handle blocks which store their rotation in NBT
        BinaryTag rotTag = tag.get("Rot");
        if (rotTag instanceof NumberBinaryTag) {
            int rot = ((NumberBinaryTag) rotTag).intValue();
            Direction direction = MCDirections.fromRotation(rot);
            if (direction != null) {
                Vector3 vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector3.ZERO)).normalize();
                Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL | Flag.ORDINAL | Flag.SECONDARY_ORDINAL);
                if (newDirection != null) {
                    return state.toBaseBlock(tag.putByte("Rot", (byte) MCDirections.toRotation(newDirection)));
                }
            }
        }
    }
    return state;
}
Also used : NumberBinaryTag(com.sk89q.worldedit.util.nbt.NumberBinaryTag) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) BinaryTag(com.sk89q.worldedit.util.nbt.BinaryTag) NumberBinaryTag(com.sk89q.worldedit.util.nbt.NumberBinaryTag) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Direction(com.sk89q.worldedit.util.Direction) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag)

Example 19 with Direction

use of com.sk89q.worldedit.util.Direction in project FastAsyncWorldEdit by IntellectualSites.

the class ExtentEntityCopy method transformNbtData.

/**
 * Transform NBT data in the given entity 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
 */
private BaseEntity transformNbtData(BaseEntity state) {
    CompoundTag tag = state.getNbtData();
    if (tag != null) {
        // Handle leashed entities
        Tag leashTag = tag.getValue().get("Leash");
        if (leashTag instanceof CompoundTag) {
            CompoundTag leashCompound = (CompoundTag) leashTag;
            if (leashCompound.containsKey("X")) {
                // leashed to a fence
                Vector3 tilePosition = Vector3.at(leashCompound.asInt("X"), leashCompound.asInt("Y"), leashCompound.asInt("Z"));
                BlockVector3 newLeash = transform.apply(tilePosition.subtract(from)).add(to).toBlockPoint();
                return new BaseEntity(state.getType(), tag.createBuilder().put("Leash", leashCompound.createBuilder().putInt("X", newLeash.getBlockX()).putInt("Y", newLeash.getBlockY()).putInt("Z", newLeash.getBlockZ()).build()).build());
            }
        }
        // Handle hanging entities (paintings, item frames, etc.)
        boolean hasTilePosition = tag.containsKey("TileX") && tag.containsKey("TileY") && tag.containsKey("TileZ");
        boolean hasFacing = tag.containsKey("Facing");
        // FAWE Start
        boolean hasRotation = tag.containsKey("Rotation");
        if (hasTilePosition) {
            Vector3 tilePosition = Vector3.at(tag.asInt("TileX"), tag.asInt("TileY"), tag.asInt("TileZ"));
            BlockVector3 newTilePosition = transform.apply(tilePosition.subtract(from)).add(to).toBlockPoint();
            CompoundTagBuilder builder = tag.createBuilder().putInt("TileX", newTilePosition.getBlockX()).putInt("TileY", newTilePosition.getBlockY()).putInt("TileZ", newTilePosition.getBlockZ());
            if (hasFacing) {
                // Paintings have different facing values
                boolean isPainting = state.getType() == EntityTypes.PAINTING;
                Direction direction = isPainting ? MCDirections.fromHorizontalHanging(tag.asInt("Facing")) : MCDirections.fromHanging(tag.asInt("Facing"));
                if (direction != null) {
                    Vector3 vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector3.ZERO)).normalize();
                    Direction newDirection = Direction.findClosest(vector, Flag.CARDINAL);
                    if (newDirection != null) {
                        builder.putByte("Facing", (byte) (isPainting ? MCDirections.toHorizontalHanging(newDirection) : MCDirections.toHanging(newDirection)));
                    }
                }
            }
            // FAWE start
            if (hasRotation) {
                ListTag orgrot = state.getNbtData().getListTag("Rotation");
                Vector3 orgDirection = new Location(source, 0, 0, 0, orgrot.getFloat(0), orgrot.getFloat(1)).getDirection();
                Vector3 newDirection = transform.apply(orgDirection).subtract(transform.apply(Vector3.ZERO)).normalize();
                builder.put("Rotation", new ListTag(FloatTag.class, Arrays.asList(new FloatTag((float) newDirection.toYaw()), new FloatTag((float) newDirection.toPitch()))));
            }
            return new BaseEntity(state.getType(), builder.build());
        } else if (hasRotation) {
            // armor stands do not have a tile pos
            CompoundTagBuilder builder = tag.createBuilder();
            ListTag orgrot = state.getNbtData().getListTag("Rotation");
            Vector3 orgDirection = new Location(source, 0, 0, 0, orgrot.getFloat(0), orgrot.getFloat(1)).getDirection();
            Vector3 newDirection = transform.apply(orgDirection).subtract(transform.apply(Vector3.ZERO)).normalize();
            builder.put("Rotation", new ListTag(FloatTag.class, Arrays.asList(new FloatTag((float) newDirection.toYaw()), new FloatTag((float) newDirection.toPitch()))));
            return new BaseEntity(state.getType(), builder.build());
        // FAWE end
        }
    }
    return state;
}
Also used : FloatTag(com.sk89q.jnbt.FloatTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) ListTag(com.sk89q.jnbt.ListTag) FloatTag(com.sk89q.jnbt.FloatTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Direction(com.sk89q.worldedit.util.Direction) ListTag(com.sk89q.jnbt.ListTag) CompoundTag(com.sk89q.jnbt.CompoundTag) CompoundTagBuilder(com.sk89q.jnbt.CompoundTagBuilder) Location(com.sk89q.worldedit.util.Location)

Aggregations

Direction (com.sk89q.worldedit.util.Direction)19 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)6 Tag (com.sk89q.jnbt.Tag)5 DirectionalProperty (com.sk89q.worldedit.registry.state.DirectionalProperty)5 BlockState (com.sk89q.worldedit.world.block.BlockState)5 BlockType (com.sk89q.worldedit.world.block.BlockType)5 Map (java.util.Map)5 Vector3 (com.sk89q.worldedit.math.Vector3)4 BooleanProperty (com.sk89q.worldedit.registry.state.BooleanProperty)4 EnumProperty (com.sk89q.worldedit.registry.state.EnumProperty)4 IntegerProperty (com.sk89q.worldedit.registry.state.IntegerProperty)4 Property (com.sk89q.worldedit.registry.state.Property)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CompoundTag (com.sk89q.jnbt.CompoundTag)3 CompoundTagBuilder (com.sk89q.jnbt.CompoundTagBuilder)3 Location (com.sk89q.worldedit.util.Location)3 List (java.util.List)3 PropertyKey (com.fastasyncworldedit.core.registry.state.PropertyKey)2 ByteTag (com.sk89q.jnbt.ByteTag)2