Search in sources :

Example 1 with CompoundTagBuilder

use of com.sk89q.jnbt.CompoundTagBuilder in project FastAsyncWorldEdit by IntellectualSites.

the class BannerBlockCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    Tag typeTag = values.get("Base");
    if (typeTag instanceof IntTag) {
        boolean isWall = block.getBlockType() == BlockTypes.WHITE_WALL_BANNER;
        String bannerType = convertBannerType(((IntTag) typeTag).getValue(), isWall);
        if (bannerType != null) {
            BlockType type = BlockTypes.get("minecraft:" + bannerType);
            if (type != null) {
                BlockState state = type.getDefaultState();
                if (isWall) {
                    Property<Direction> facingProp = type.getProperty("facing");
                    state = state.with(facingProp, block.getState(FacingProperty));
                } else {
                    Property<Integer> rotationProp = type.getProperty("rotation");
                    state = state.with(rotationProp, block.getState(RotationProperty));
                }
                values.remove("Base");
                Tag patternsTag = values.get("Patterns");
                if (patternsTag instanceof ListTag) {
                    List<Tag> tempList = new ArrayList<>();
                    for (Tag pattern : ((ListTag) patternsTag).getValue()) {
                        if (pattern instanceof CompoundTag) {
                            Map<String, Tag> patternMap = ((CompoundTag) pattern).getValue();
                            Tag colorTag = patternMap.get("Color");
                            CompoundTagBuilder builder = CompoundTagBuilder.create();
                            builder.putAll(patternMap);
                            if (colorTag instanceof IntTag) {
                                builder.putInt("Color", 15 - ((IntTag) colorTag).getValue());
                            }
                            tempList.add(builder.build());
                        } else {
                            tempList.add(pattern);
                        }
                    }
                    values.put("Patterns", new ListTag(((ListTag) patternsTag).getType(), tempList));
                }
                return state;
            }
        }
    }
    return block;
}
Also used : ArrayList(java.util.ArrayList) Direction(com.sk89q.worldedit.util.Direction) ListTag(com.sk89q.jnbt.ListTag) CompoundTagBuilder(com.sk89q.jnbt.CompoundTagBuilder) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 2 with CompoundTagBuilder

use of com.sk89q.jnbt.CompoundTagBuilder in project FastAsyncWorldEdit by IntellectualSites.

the class Pre13HangingCompatibilityHandler method updateNBT.

@Override
public CompoundTag updateNBT(EntityType type, CompoundTag tag) {
    boolean hasLegacyDir = tag.containsKey("Dir");
    boolean hasLegacyDirection = tag.containsKey("Direction");
    boolean hasPre113Facing = tag.containsKey("Facing");
    Direction newDirection;
    if (hasLegacyDir) {
        newDirection = MCDirections.fromPre13Hanging(MCDirections.fromLegacyHanging((byte) tag.asInt("Dir")));
    } else if (hasLegacyDirection) {
        newDirection = MCDirections.fromPre13Hanging(tag.asInt("Direction"));
    } else if (hasPre113Facing) {
        newDirection = MCDirections.fromPre13Hanging(tag.asInt("Facing"));
    } else {
        return tag;
    }
    byte hangingByte = (byte) MCDirections.toHanging(newDirection);
    CompoundTagBuilder builder = tag.createBuilder();
    builder.putByte("Facing", hangingByte);
    return builder.build();
}
Also used : Direction(com.sk89q.worldedit.util.Direction) CompoundTagBuilder(com.sk89q.jnbt.CompoundTagBuilder)

Example 3 with CompoundTagBuilder

use of com.sk89q.jnbt.CompoundTagBuilder 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

CompoundTagBuilder (com.sk89q.jnbt.CompoundTagBuilder)3 Direction (com.sk89q.worldedit.util.Direction)3 CompoundTag (com.sk89q.jnbt.CompoundTag)2 ListTag (com.sk89q.jnbt.ListTag)2 Tag (com.sk89q.jnbt.Tag)2 FloatTag (com.sk89q.jnbt.FloatTag)1 IntTag (com.sk89q.jnbt.IntTag)1 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Vector3 (com.sk89q.worldedit.math.Vector3)1 Location (com.sk89q.worldedit.util.Location)1 BlockState (com.sk89q.worldedit.world.block.BlockState)1 BlockType (com.sk89q.worldedit.world.block.BlockType)1 ArrayList (java.util.ArrayList)1