codechicken.lib.raytracer.RayTracer Java Examples

The following examples show how to use codechicken.lib.raytracer.RayTracer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockTranslocator.java    From Translocators with MIT License 6 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
    if (world.isRemote)
        return true;

    MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
    TileTranslocator ttrans = (TileTranslocator) world.getTileEntity(x, y, z);

    if (hit != null) {
        if (hit.subHit < 6) {
            Vector3 vhit = new Vector3(hit.hitVec);
            vhit.add(-x - 0.5, -y - 0.5, -z - 0.5);
            vhit.apply(sideRotations[hit.subHit % 6].inverse());
            if (MathHelper.between(-2 / 16D, vhit.x, 2 / 16D) && MathHelper.between(-2 / 16D, vhit.z, 2 / 16D))
                hit.subHit += 6;
        }

        return ttrans.attachments[hit.subHit % 6].activate(player, hit.subHit / 6);
    }

    return false;
}
 
Example #2
Source File: BlockMachine.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
    CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
    ItemStack itemStack = playerIn.getHeldItem(hand);
    if (metaTileEntity == null || rayTraceResult == null) {
        return false;
    }

    if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null)) {
        IScrewdriverItem screwdriver = itemStack.getCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null);

        if (screwdriver.damageItem(DamageValues.DAMAGE_FOR_SCREWDRIVER, true) &&
            metaTileEntity.onCoverScrewdriverClick(playerIn, hand, rayTraceResult)) {
            screwdriver.damageItem(DamageValues.DAMAGE_FOR_SCREWDRIVER, false);
            return true;
        }
        return false;
    }

    if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_WRENCH, null)) {
        IWrenchItem wrenchItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_WRENCH, null);
        EnumFacing wrenchDirection = ICoverable.determineGridSideHit(rayTraceResult);

        if (wrenchItem.damageItem(DamageValues.DAMAGE_FOR_WRENCH, true) &&
            metaTileEntity.onWrenchClick(playerIn, hand, wrenchDirection, rayTraceResult)) {
            wrenchItem.damageItem(DamageValues.DAMAGE_FOR_SCREWDRIVER, false);
            return true;
        }
        return false;
    }

    return metaTileEntity.onCoverRightClick(playerIn, hand, rayTraceResult);
}
 
Example #3
Source File: BlockMachine.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
    MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
    if (metaTileEntity == null) return;
    CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
    if (rayTraceResult != null) {
        metaTileEntity.onCoverLeftClick(playerIn, rayTraceResult);
    }
}
 
Example #4
Source File: ICoverable.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
static EnumFacing rayTraceCoverableSide(ICoverable coverable, EntityPlayer player) {
    RayTraceResult result = RayTracer.retraceBlock(coverable.getWorld(), player, coverable.getPos());
    if (result == null || result.typeOfHit != Type.BLOCK) {
        return null;
    }
    return traceCoverSide(result);
}
 
Example #5
Source File: BlockPipe.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
    CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
    if (rayTraceResult == null || pipeTile == null) {
        return false;
    }
    return onPipeActivated(playerIn, hand, rayTraceResult, pipeTile);
}
 
Example #6
Source File: BlockPipe.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
    IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
    CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
    if (pipeTile == null || rayTraceResult == null) {
        return;
    }
    EnumFacing coverSide = ICoverable.traceCoverSide(rayTraceResult);
    CoverBehavior coverBehavior = coverSide == null ? null : pipeTile.getCoverableImplementation().getCoverAtSide(coverSide);

    if (coverBehavior != null) {
        coverBehavior.onLeftClick(playerIn, rayTraceResult);
    }
}
 
Example #7
Source File: ScannerBehavior.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Pair<BlockPos, IBlockState> getHitBlock(EntityPlayer entityPlayer) {
    RayTraceResult result = RayTracer.retrace(entityPlayer);
    if (result.typeOfHit == Type.BLOCK) {
        BlockPos blockPos = result.getBlockPos();
        IBlockState blockState = entityPlayer.world.getBlockState(blockPos);
        if (blockState.getBlock() instanceof IScannableBlock) {
            return Pair.of(blockPos, blockState);
        }
    }
    return null;
}
 
Example #8
Source File: BlockTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willharvest) {
    MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
    if (hit == null)
        return false;

    TileTranslocator ttrans = (TileTranslocator) world.getTileEntity(x, y, z);
    return ttrans.harvestPart(hit.subHit % 6, !player.capabilities.isCreativeMode);
}
 
Example #9
Source File: BlockCraftingGrid.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
    if (world.isRemote)
        return true;

    MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
    TileCraftingGrid tcraft = (TileCraftingGrid) world.getTileEntity(x, y, z);

    if (hit != null) {
        if (hit.subHit > 0)
            tcraft.activate(hit.subHit - 1, player);
        return true;
    }
    return false;
}
 
Example #10
Source File: FMPPlacementListener.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
public static boolean place(EntityPlayer player, World world){
    MovingObjectPosition hit = RayTracer.reTrace(world, player);
    if(hit == null) return false;

    BlockCoord pos = new BlockCoord(hit.blockX, hit.blockY, hit.blockZ);
    ItemStack held = player.getHeldItem();
    PartPressureTube part = null;
    if(held == null) return false;

    Block heldBlock = Block.getBlockFromItem(held.getItem());
    if(heldBlock == Blockss.pressureTube) {
        part = new PartPressureTube();
    } else if(heldBlock == Blockss.advancedPressureTube) {
        part = new PartAdvancedPressureTube();
    }

    if(part == null) return false;

    if(world.isRemote && !player.isSneaking())//attempt to use block activated like normal and tell the server the right stuff
    {
        Vector3 f = new Vector3(hit.hitVec).add(-hit.blockX, -hit.blockY, -hit.blockZ);
        Block block = world.getBlock(hit.blockX, hit.blockY, hit.blockZ);
        if(!ignoreActivate(block) && block.onBlockActivated(world, hit.blockX, hit.blockY, hit.blockZ, player, hit.sideHit, (float)f.x, (float)f.y, (float)f.z)) {
            player.swingItem();
            PacketCustom.sendToServer(new C08PacketPlayerBlockPlacement(hit.blockX, hit.blockY, hit.blockZ, hit.sideHit, player.inventory.getCurrentItem(), (float)f.x, (float)f.y, (float)f.z));
            return true;
        }
    }

    TileMultipart tile = TileMultipart.getOrConvertTile(world, pos);
    if(tile == null || !tile.canAddPart(part)) {
        pos = pos.offset(hit.sideHit);
        tile = TileMultipart.getOrConvertTile(world, pos);
        if(tile == null || !tile.canAddPart(part)) return false;
    }

    if(!world.isRemote) {
        TileMultipart.addPart(world, pos, part);
        world.playSoundEffect(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, Blockss.pressureTube.stepSound.func_150496_b(), (Blockss.pressureTube.stepSound.getVolume() + 1.0F) / 2.0F, Blockss.pressureTube.stepSound.getPitch() * 0.8F);
        if(!player.capabilities.isCreativeMode) {
            held.stackSize--;
            if(held.stackSize == 0) {
                player.inventory.mainInventory[player.inventory.currentItem] = null;
                MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, held));
            }
        }
    } else {
        player.swingItem();
        NetworkHandler.sendToServer(new PacketFMPPlacePart());
    }
    return true;
}
 
Example #11
Source File: BlockMachine.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
public RayTraceResult collisionRayTrace(IBlockState blockState, World worldIn, BlockPos pos, Vec3d start, Vec3d end) {
    return RayTracer.rayTraceCuboidsClosest(start, end, pos, getCollisionBox(worldIn, pos));
}
 
Example #12
Source File: BlockTranslocator.java    From Translocators with MIT License 4 votes vote down vote up
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onBlockHighlight(DrawBlockHighlightEvent event) {
    if (event.target.typeOfHit == MovingObjectType.BLOCK && event.player.worldObj.getBlock(event.target.blockX, event.target.blockY, event.target.blockZ) == this)
        RayTracer.retraceBlock(event.player.worldObj, event.player, event.target.blockX, event.target.blockY, event.target.blockZ);
}
 
Example #13
Source File: BlockCraftingGrid.java    From Translocators with MIT License 4 votes vote down vote up
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onBlockHighlight(DrawBlockHighlightEvent event) {
    if (event.target.typeOfHit == MovingObjectType.BLOCK && event.player.worldObj.getBlock(event.target.blockX, event.target.blockY, event.target.blockZ) == this)
        RayTracer.retraceBlock(event.player.worldObj, event.player, event.target.blockX, event.target.blockY, event.target.blockZ);
}
 
Example #14
Source File: PlayerCommand.java    From CodeChickenCore with MIT License 4 votes vote down vote up
public static Entity traceEntity(EntityPlayerMP player, float reach) {
    return RayTracer.retrace(player, reach).entityHit;
}
 
Example #15
Source File: PlayerCommand.java    From CodeChickenCore with MIT License 4 votes vote down vote up
public static BlockPos traceBlock(EntityPlayerMP player, float reach) {
    return RayTracer.retrace(player, reach).getBlockPos();
}
 
Example #16
Source File: BlockEnderStorage.java    From EnderStorage with MIT License 4 votes vote down vote up
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onBlockHighlight(DrawBlockHighlightEvent event) {
    if (event.target.typeOfHit == MovingObjectType.BLOCK && event.player.worldObj.getBlock(event.target.blockX, event.target.blockY, event.target.blockZ) == this)
        RayTracer.retraceBlock(event.player.worldObj, event.player, event.target.blockX, event.target.blockY, event.target.blockZ);
}
 
Example #17
Source File: ToolUtility.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static EnumFacing getSideHit(World world, BlockPos blockPos, EntityPlayer harvester) {
    RayTraceResult result = RayTracer.retraceBlock(world, harvester, blockPos);
    return result == null ? harvester.getHorizontalFacing() : result.sideHit;
}
 
Example #18
Source File: BlockPipe.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
public RayTraceResult collisionRayTrace(IBlockState blockState, World worldIn, BlockPos pos, Vec3d start, Vec3d end) {
    return RayTracer.rayTraceCuboidsClosest(start, end, pos, getCollisionBox(worldIn, pos));
}