Search in sources :

Example 1 with NBTUtilBC

use of buildcraft.lib.misc.NBTUtilBC in project BuildCraft by BuildCraft.

the class SchematicBlockDefault method build.

@Override
@SuppressWarnings("Duplicates")
public boolean build(World world, BlockPos blockPos) {
    if (placeBlock == Blocks.AIR) {
        return true;
    }
    world.profiler.startSection("prepare block");
    IBlockState newBlockState = blockState;
    if (placeBlock != blockState.getBlock()) {
        newBlockState = placeBlock.getDefaultState();
        for (IProperty<?> property : blockState.getPropertyKeys()) {
            if (newBlockState.getPropertyKeys().contains(property)) {
                newBlockState = BlockUtil.copyProperty(property, newBlockState, blockState);
            }
        }
    }
    for (IProperty<?> property : ignoredProperties) {
        newBlockState = BlockUtil.copyProperty(property, newBlockState, placeBlock.getDefaultState());
    }
    world.profiler.endSection();
    world.profiler.startSection("place block");
    boolean b = world.setBlockState(blockPos, newBlockState, 11);
    world.profiler.endSection();
    if (b) {
        world.profiler.startSection("notify");
        updateBlockOffsets.stream().map(blockPos::add).forEach(updatePos -> world.notifyNeighborsOfStateChange(updatePos, placeBlock, false));
        world.profiler.endSection();
        if (tileNbt != null && blockState.getBlock().hasTileEntity(blockState)) {
            world.profiler.startSection("prepare tile");
            Set<JsonRule> rules = RulesLoader.getRules(blockState, tileNbt);
            NBTTagCompound replaceNbt = rules.stream().map(rule -> rule.replaceNbt).filter(Objects::nonNull).map(NBTBase.class::cast).reduce(NBTUtilBC::merge).map(NBTTagCompound.class::cast).orElse(null);
            NBTTagCompound newTileNbt = new NBTTagCompound();
            tileNbt.getKeySet().stream().map(key -> Pair.of(key, tileNbt.getTag(key))).forEach(kv -> newTileNbt.setTag(kv.getKey(), kv.getValue()));
            newTileNbt.setInteger("x", blockPos.getX());
            newTileNbt.setInteger("y", blockPos.getY());
            newTileNbt.setInteger("z", blockPos.getZ());
            world.profiler.endSection();
            world.profiler.startSection("place tile");
            TileEntity tileEntity = TileEntity.create(world, replaceNbt != null ? (NBTTagCompound) NBTUtilBC.merge(newTileNbt, replaceNbt) : newTileNbt);
            if (tileEntity != null) {
                tileEntity.setWorld(world);
                world.setTileEntity(blockPos, tileEntity);
                if (tileRotation != Rotation.NONE) {
                    tileEntity.rotate(tileRotation);
                }
            }
            world.profiler.endSection();
        }
        return true;
    }
    return false;
}
Also used : Blocks(net.minecraft.init.Blocks) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) IProperty(net.minecraft.block.properties.IProperty) Pair(org.apache.commons.lang3.tuple.Pair) Block(net.minecraft.block.Block) Nonnull(javax.annotation.Nonnull) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockFalling(net.minecraft.block.BlockFalling) BlockUtil(buildcraft.lib.misc.BlockUtil) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Collection(java.util.Collection) NBTBase(net.minecraft.nbt.NBTBase) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) Set(java.util.Set) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) Stream(java.util.stream.Stream) Rotation(net.minecraft.util.Rotation) ResourceLocation(net.minecraft.util.ResourceLocation) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) NBTUtil(net.minecraft.nbt.NBTUtil) TileEntity(net.minecraft.tileentity.TileEntity) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) IBlockState(net.minecraft.block.state.IBlockState) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Objects(java.util.Objects)

Example 2 with NBTUtilBC

use of buildcraft.lib.misc.NBTUtilBC in project BuildCraft by BuildCraft.

the class SchematicEntityDefault method build.

@Override
public Entity build(World world, BlockPos basePos) {
    Set<JsonRule> rules = RulesLoader.getRules(new ResourceLocation(entityNbt.getString("id")), entityNbt);
    NBTTagCompound replaceNbt = rules.stream().map(rule -> rule.replaceNbt).filter(Objects::nonNull).map(NBTBase.class::cast).reduce(NBTUtilBC::merge).map(NBTTagCompound.class::cast).orElse(null);
    Vec3d placePos = new Vec3d(basePos).add(pos);
    BlockPos placeHangingPos = basePos.add(hangingPos);
    NBTTagCompound newEntityNbt = new NBTTagCompound();
    entityNbt.getKeySet().stream().map(key -> Pair.of(key, entityNbt.getTag(key))).forEach(kv -> newEntityNbt.setTag(kv.getKey(), kv.getValue()));
    newEntityNbt.setTag("Pos", NBTUtilBC.writeVec3d(placePos));
    newEntityNbt.setUniqueId("UUID", UUID.randomUUID());
    boolean rotate = false;
    if (Stream.of("TileX", "TileY", "TileZ", "Facing").allMatch(newEntityNbt::hasKey)) {
        newEntityNbt.setInteger("TileX", placeHangingPos.getX());
        newEntityNbt.setInteger("TileY", placeHangingPos.getY());
        newEntityNbt.setInteger("TileZ", placeHangingPos.getZ());
        newEntityNbt.setByte("Facing", (byte) hangingFacing.getHorizontalIndex());
    } else {
        rotate = true;
    }
    Entity entity = EntityList.createEntityFromNBT(replaceNbt != null ? (NBTTagCompound) NBTUtilBC.merge(newEntityNbt, replaceNbt) : newEntityNbt, world);
    if (entity != null) {
        if (rotate) {
            entity.setLocationAndAngles(placePos.x, placePos.y, placePos.z, entity.rotationYaw + (entity.rotationYaw - entity.getRotatedYaw(entityRotation)), entity.rotationPitch);
        }
        world.spawnEntity(entity);
    }
    return entity;
}
Also used : SchematicEntityContext(buildcraft.api.schematics.SchematicEntityContext) RotationUtil(buildcraft.lib.misc.RotationUtil) ItemStack(net.minecraft.item.ItemStack) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) Pair(org.apache.commons.lang3.tuple.Pair) Vec3d(net.minecraft.util.math.Vec3d) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) EntityHanging(net.minecraft.entity.EntityHanging) Nonnull(javax.annotation.Nonnull) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Entity(net.minecraft.entity.Entity) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Collection(java.util.Collection) NBTBase(net.minecraft.nbt.NBTBase) EntityList(net.minecraft.entity.EntityList) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) Set(java.util.Set) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Rotation(net.minecraft.util.Rotation) ResourceLocation(net.minecraft.util.ResourceLocation) FluidStack(net.minecraftforge.fluids.FluidStack) NBTUtil(net.minecraft.nbt.NBTUtil) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) Entity(net.minecraft.entity.Entity) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Objects(java.util.Objects) BlockPos(net.minecraft.util.math.BlockPos) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)2 NBTUtilBC (buildcraft.lib.misc.NBTUtilBC)2 Collection (java.util.Collection)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Nonnull (javax.annotation.Nonnull)2 ItemStack (net.minecraft.item.ItemStack)2 NBTBase (net.minecraft.nbt.NBTBase)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 NBTUtil (net.minecraft.nbt.NBTUtil)2 EnumFacing (net.minecraft.util.EnumFacing)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 Rotation (net.minecraft.util.Rotation)2 BlockPos (net.minecraft.util.math.BlockPos)2 World (net.minecraft.world.World)2 FluidStack (net.minecraftforge.fluids.FluidStack)2