Search in sources :

Example 1 with IBPPartBlock

use of com.bluepowermod.api.multipart.IBPPartBlock in project BluePower by Qmunity.

the class ItemBPPart method place.

@Override
public ActionResultType place(BlockItemUseContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    BlockState thisState = getBlock().getStateForPlacement(context);
    if (state.getBlock() instanceof IBPPartBlock && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Save the Tile Entity Data
        CompoundNBT nbt = new CompoundNBT();
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity != null) {
            nbt = tileEntity.save(nbt);
        }
        // Replace with Multipart
        context.getLevel().setBlockAndUpdate(context.getClickedPos(), BPBlocks.multipart.defaultBlockState());
        tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            // Add the original State to the Multipart
            ((TileBPMultipart) tileEntity).addState(state);
            // Restore the Tile Entity Data
            TileEntity tile = ((TileBPMultipart) tileEntity).getTileForState(state);
            if (tile != null)
                tile.load(state, nbt);
            // Add the new State
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
        }
        // Update Self
        state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
        context.getItemInHand().shrink(1);
        // Place Sound
        context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return ActionResultType.SUCCESS;
    } else if (state.getBlock() instanceof BlockBPMultipart && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Add to the Existing Multipart
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
            // Update Neighbors
            for (Direction dir : Direction.values()) {
                context.getLevel().getBlockState(context.getClickedPos().relative(dir)).neighborChanged(context.getLevel(), context.getClickedPos().relative(dir), state.getBlock(), context.getClickedPos(), false);
            }
            // Update Self
            state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
            context.getItemInHand().shrink(1);
            // Place Sound
            context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResultType.SUCCESS;
        }
    }
    return super.place(context);
}
Also used : IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Direction(net.minecraft.util.Direction)

Example 2 with IBPPartBlock

use of com.bluepowermod.api.multipart.IBPPartBlock in project BluePower by Qmunity.

the class BlockBPMultipart method removedByPlayer.

@Override
public boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid) {
    BlockState partState = MultipartUtils.getClosestState(player, pos);
    TileEntity te = world.getBlockEntity(pos);
    if (partState != null && partState.getBlock() instanceof IBPPartBlock && te instanceof TileBPMultipart) {
        // Remove Selected Part
        ((TileBPMultipart) te).removeState(partState);
        // Call onMultipartReplaced
        ((IBPPartBlock) partState.getBlock()).onMultipartReplaced(partState, world, pos, state, false);
        // Play Break Sound
        world.playSound(null, pos, SoundEvents.STONE_BREAK, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return false;
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart)

Aggregations

IBPPartBlock (com.bluepowermod.api.multipart.IBPPartBlock)2 TileBPMultipart (com.bluepowermod.tile.TileBPMultipart)2 TileEntity (net.minecraft.tileentity.TileEntity)2 BlockBPMultipart (com.bluepowermod.block.BlockBPMultipart)1 BlockState (net.minecraft.block.BlockState)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 Direction (net.minecraft.util.Direction)1