Search in sources :

Example 1 with RobotEvent

use of buildcraft.api.events.RobotEvent in project BuildCraft by BuildCraft.

the class ItemRobot method onItemUse.

@Override
public boolean onItemUse(ItemStack currentItem, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!world.isRemote) {
        Block b = world.getBlockState(pos).getBlock();
        if (!(b instanceof BlockGenericPipe)) {
            return false;
        }
        Pipe<?> pipe = BlockGenericPipe.getPipe(world, pos);
        if (pipe == null) {
            return false;
        }
        // BlockGenericPipe pipeBlock = (BlockGenericPipe) b;
        // BlockGenericPipe.RaytraceResult rayTraceResult = pipeBlock.doRayTrace(world, pos, player);
        PipePluggable pluggable = pipe.container.getPipePluggable(side);
        if (pluggable instanceof RobotStationPluggable) {
            RobotStationPluggable robotPluggable = (RobotStationPluggable) pluggable;
            DockingStation station = robotPluggable.getStation();
            if (!station.isTaken()) {
                RedstoneBoardRobotNBT robotNBT = ItemRobot.getRobotNBT(currentItem);
                if (robotNBT == RedstoneBoardRegistry.instance.getEmptyRobotBoard()) {
                    return true;
                }
                EntityRobot robot = ((ItemRobot) currentItem.getItem()).createRobot(currentItem, world);
                RobotEvent.Place robotEvent = new RobotEvent.Place(robot, player);
                MinecraftForge.EVENT_BUS.post(robotEvent);
                if (robotEvent.isCanceled()) {
                    return true;
                }
                if (robot != null && robot.getRegistry() != null) {
                    robot.setUniqueRobotId(robot.getRegistry().getNextRobotId());
                    float px = pos.getX() + 0.5F + side.getFrontOffsetX() * 0.5F;
                    float py = pos.getY() + 0.5F + side.getFrontOffsetY() * 0.5F;
                    float pz = pos.getZ() + 0.5F + side.getFrontOffsetZ() * 0.5F;
                    robot.setPosition(px, py, pz);
                    station.takeAsMain(robot);
                    robot.dock(robot.getLinkedStation());
                    world.spawnEntityInWorld(robot);
                    if (!player.capabilities.isCreativeMode) {
                        player.getCurrentEquippedItem().stackSize--;
                    }
                }
            }
            return true;
        }
    }
    return false;
}
Also used : DockingStation(buildcraft.api.robots.DockingStation) PipePluggable(buildcraft.api.transport.pluggable.PipePluggable) BlockGenericPipe(buildcraft.transport.BlockGenericPipe) Block(net.minecraft.block.Block) RedstoneBoardRobotNBT(buildcraft.api.boards.RedstoneBoardRobotNBT) RobotEvent(buildcraft.api.events.RobotEvent)

Example 2 with RobotEvent

use of buildcraft.api.events.RobotEvent in project LogisticsPipes by RS485.

the class BuildCraftProxy method checkRobot.

/**
	 * @see buildcraft.robotics.ItemRobot#onItemUse(ItemStack, EntityPlayer,
	 *      World, int, int, int, int, float, float, float)
	 */
private boolean checkRobot(World world, int x, int y, int z, EntityPlayer player, ItemStack currentItem) {
    if (!world.isRemote) {
        Pipe<?> bcPipe = BlockGenericPipe.getPipe(world, x, y, z);
        if (bcPipe == null) {
            return false;
        }
        BlockGenericPipe pipeBlock = BuildCraftTransport.genericPipeBlock;
        BlockGenericPipe.RaytraceResult rayTraceResult = pipeBlock.doRayTrace(world, x, y, z, player);
        if (rayTraceResult != null && rayTraceResult.hitPart == BlockGenericPipe.Part.Pluggable && bcPipe.container.getPipePluggable(rayTraceResult.sideHit) instanceof RobotStationPluggable) {
            RobotStationPluggable pluggable = (RobotStationPluggable) bcPipe.container.getPipePluggable(rayTraceResult.sideHit);
            DockingStation station = pluggable.getStation();
            if (!station.isTaken()) {
                RedstoneBoardRobotNBT robotNBT = ItemRobot.getRobotNBT(currentItem);
                if (robotNBT == RedstoneBoardRegistry.instance.getEmptyRobotBoard()) {
                    return true;
                }
                EntityRobot robot = ((ItemRobot) currentItem.getItem()).createRobot(currentItem, world);
                RobotEvent.Place robotEvent = new RobotEvent.Place(robot, player);
                FMLCommonHandler.instance().bus().post(robotEvent);
                if (robotEvent.isCanceled()) {
                    return true;
                }
                if (robot != null && robot.getRegistry() != null) {
                    robot.setUniqueRobotId(robot.getRegistry().getNextRobotId());
                    float px = x + 0.5F + rayTraceResult.sideHit.offsetX * 0.5F;
                    float py = y + 0.5F + rayTraceResult.sideHit.offsetY * 0.5F;
                    float pz = z + 0.5F + rayTraceResult.sideHit.offsetZ * 0.5F;
                    robot.setPosition(px, py, pz);
                    station.takeAsMain(robot);
                    robot.dock(robot.getLinkedStation());
                    world.spawnEntityInWorld(robot);
                    if (!player.capabilities.isCreativeMode) {
                        player.getCurrentEquippedItem().stackSize--;
                    }
                }
            }
            return true;
        }
    }
    return false;
}
Also used : RobotStationPluggable(buildcraft.robotics.RobotStationPluggable) DockingStation(buildcraft.api.robots.DockingStation) EntityRobot(buildcraft.robotics.EntityRobot) BlockGenericPipe(buildcraft.transport.BlockGenericPipe) RedstoneBoardRobotNBT(buildcraft.api.boards.RedstoneBoardRobotNBT) ItemRobot(buildcraft.robotics.ItemRobot) RobotEvent(buildcraft.api.events.RobotEvent)

Aggregations

RedstoneBoardRobotNBT (buildcraft.api.boards.RedstoneBoardRobotNBT)2 RobotEvent (buildcraft.api.events.RobotEvent)2 DockingStation (buildcraft.api.robots.DockingStation)2 BlockGenericPipe (buildcraft.transport.BlockGenericPipe)2 PipePluggable (buildcraft.api.transport.pluggable.PipePluggable)1 EntityRobot (buildcraft.robotics.EntityRobot)1 ItemRobot (buildcraft.robotics.ItemRobot)1 RobotStationPluggable (buildcraft.robotics.RobotStationPluggable)1 Block (net.minecraft.block.Block)1