Java Code Examples for net.minecraft.tileentity.TileEntity#getCapability()

The following examples show how to use net.minecraft.tileentity.TileEntity#getCapability() . 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: ItemInventorySwapper.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos,
        EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
    if (player.isSneaking())
    {
        TileEntity te = world.getTileEntity(pos);

        if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side))
        {
            IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);

            if (world.isRemote == false && inv != null)
            {
                this.swapInventory(player.getHeldItem(hand), inv, player);
            }

            return EnumActionResult.SUCCESS;
        }
    }

    return super.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
}
 
Example 2
Source File: TileEntityFluidPipeTickable.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void pushFluidsFromTank(IPipeTile<FluidPipeType, FluidPipeProperties> pipeTile) {
    PooledMutableBlockPos blockPos = PooledMutableBlockPos.retain();
    int blockedConnections = pipeTile.getBlockedConnections();
    BlockFluidPipe blockFluidPipe = (BlockFluidPipe) pipeTile.getPipeBlock();
    for (EnumFacing side : EnumFacing.VALUES) {
        if ((blockedConnections & 1 << side.getIndex()) > 0) {
            continue; //do not dispatch energy to blocked sides
        }
        blockPos.setPos(pipeTile.getPipePos()).move(side);
        if (!pipeTile.getPipeWorld().isBlockLoaded(blockPos)) {
            continue; //do not allow cables to load chunks
        }
        TileEntity tileEntity = pipeTile.getPipeWorld().getTileEntity(blockPos);
        if (tileEntity == null) {
            continue; //do not emit into multiparts or other fluid pipes
        }
        IFluidHandler sourceHandler = pipeTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
        IFluidHandler receiverHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite());
        if (sourceHandler != null && receiverHandler != null && blockFluidPipe.canPushIntoFluidHandler(pipeTile, tileEntity, sourceHandler, receiverHandler)) {
            CoverPump.moveHandlerFluids(sourceHandler, receiverHandler, Integer.MAX_VALUE, FLUID_FILTER_ALWAYS_TRUE);
        }
    }
    blockPos.release();
}
 
Example 3
Source File: ItemQuickStacker.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos,
        EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
    if (player.isSneaking())
    {
        TileEntity te = world.getTileEntity(pos);

        if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side))
        {
            IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);

            if (world.isRemote == false && inv != null)
            {
                quickStackItems(player, player.getHeldItem(hand), inv);
            }

            return EnumActionResult.SUCCESS;
        }
    }

    return super.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
}
 
Example 4
Source File: CoverConveyor.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void update() {
    long timer = coverHolder.getTimer();
    if (timer % 5 == 0 && isWorkingAllowed && itemsLeftToTransferLastSecond > 0) {
        TileEntity tileEntity = coverHolder.getWorld().getTileEntity(coverHolder.getPos().offset(attachedSide));
        IItemHandler itemHandler = tileEntity == null ? null : tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, attachedSide.getOpposite());
        IItemHandler myItemHandler = coverHolder.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, attachedSide);
        if (itemHandler != null && myItemHandler != null) {
            int totalTransferred = doTransferItems(itemHandler, myItemHandler, itemsLeftToTransferLastSecond);
            this.itemsLeftToTransferLastSecond -= totalTransferred;
        }
    }
    if (timer % 20 == 0) {
        this.itemsLeftToTransferLastSecond = transferRate;
    }
}
 
Example 5
Source File: MetaTileEntity.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void pullFluidsFromNearbyHandlers(EnumFacing... allowedFaces) {
    PooledMutableBlockPos blockPos = PooledMutableBlockPos.retain();
    for (EnumFacing nearbyFacing : allowedFaces) {
        blockPos.setPos(getPos()).move(nearbyFacing);
        TileEntity tileEntity = getWorld().getTileEntity(blockPos);
        if (tileEntity == null) {
            continue;
        }
        IFluidHandler fluidHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, nearbyFacing.getOpposite());
        //use getCoverCapability so fluid tank index filtering and fluid filtering covers will work properly
        IFluidHandler myFluidHandler = getCoverCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, nearbyFacing);
        if (fluidHandler == null || myFluidHandler == null) {
            continue;
        }
        CoverPump.moveHandlerFluids(fluidHandler, myFluidHandler, Integer.MAX_VALUE, fluid -> true);
    }
    blockPos.release();
}
 
Example 6
Source File: SingleFluidBucketFillHandler.java    From OpenModsLib with MIT License 6 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGH)
public void onBucketFill(FillBucketEvent evt) {
	if (evt.getResult() != Result.DEFAULT) return;

	if (evt.getEmptyBucket().getItem() != EMPTY_BUCKET) return;

	final RayTraceResult target = evt.getTarget();
	if (target == null || target.typeOfHit != RayTraceResult.Type.BLOCK) return;

	final TileEntity te = evt.getWorld().getTileEntity(target.getBlockPos());
	if (te == null) return;

	if (te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit)) {
		final IFluidHandler source = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit);

		final FluidStack drained = source.drain(containedFluid, false);
		if (containedFluid.isFluidStackIdentical(drained)) {
			source.drain(containedFluid, true);
			evt.setFilledBucket(filledBucket.copy());
			evt.setResult(Result.ALLOW);
		}
	}
}
 
Example 7
Source File: BlockFluidPipe.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected int getActiveVisualConnections(IPipeTile<FluidPipeType, FluidPipeProperties> selfTile) {
    int activeNodeConnections = 0;
    for (EnumFacing side : EnumFacing.VALUES) {
        BlockPos offsetPos = selfTile.getPipePos().offset(side);
        TileEntity tileEntity = selfTile.getPipeWorld().getTileEntity(offsetPos);
        if(tileEntity != null) {
            EnumFacing opposite = side.getOpposite();
            IFluidHandler sourceHandler = selfTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
            IFluidHandler receivedHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opposite);
            if (sourceHandler != null && receivedHandler != null) {
                activeNodeConnections |= 1 << side.getIndex();
            }
        }
    }
    return activeNodeConnections;
}
 
Example 8
Source File: CoverPlaceBehavior.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    TileEntity tileEntity = world.getTileEntity(pos);
    ICoverable coverable = tileEntity == null ? null : tileEntity.getCapability(GregtechTileCapabilities.CAPABILITY_COVERABLE, null);
    if (coverable == null) {
        return EnumActionResult.PASS;
    }
    EnumFacing coverSide = ICoverable.rayTraceCoverableSide(coverable, player);
    if (coverable.getCoverAtSide(coverSide) != null || !coverable.canPlaceCoverOnSide(coverSide)) {
        return EnumActionResult.PASS;
    }
    if (!world.isRemote) {
        ItemStack itemStack = player.getHeldItem(hand);
        boolean result = coverable.placeCoverOnSide(coverSide, itemStack, coverDefinition);
        if (result && !player.capabilities.isCreativeMode) {
            itemStack.shrink(1);
        }
        return result ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
    }
    return EnumActionResult.SUCCESS;
}
 
Example 9
Source File: TileGenerator.java    From YouTubeModdingTutorial with MIT License 6 votes vote down vote up
private void sendEnergy() {
    if (energyStorage.getEnergyStored() > 0) {
        for (EnumFacing facing : EnumFacing.VALUES) {
            TileEntity tileEntity = world.getTileEntity(pos.offset(facing));
            if (tileEntity != null && tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing.getOpposite())) {
                IEnergyStorage handler = tileEntity.getCapability(CapabilityEnergy.ENERGY, facing.getOpposite());
                if (handler != null && handler.canReceive()) {
                    int accepted = handler.receiveEnergy(energyStorage.getEnergyStored(), false);
                    energyStorage.consumePower(accepted);
                    if (energyStorage.getEnergyStored() <= 0) {
                        break;
                    }
                }
            }
        }
        markDirty();
    }
}
 
Example 10
Source File: ItemRailConfigurator.java    From Signals with GNU General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand){
    if(!world.isRemote) {
        TileEntity te = world.getTileEntity(pos);
        if(te != null) {
            CapabilityDestinationProvider cap = te.getCapability(CapabilityDestinationProvider.INSTANCE, null);
            if(cap != null) {
                List<IDestinationProvider> providers = cap.getApplicableDestinationProviders();
                List<IDestinationProvider> guiProviders = new ArrayList<>();
                for(IDestinationProvider provider : providers)
                    if(provider.hasGui(te)) guiProviders.add(provider);
                if(guiProviders.size() > 1) {
                    player.openGui(Signals.instance, EnumGuiId.SELECT_DESTINATION_PROVIDER.ordinal(), world, pos.getX(), pos.getY(), pos.getZ());
                    return EnumActionResult.SUCCESS;
                } else if(!guiProviders.isEmpty()) {
                    guiProviders.get(0).openGui(te, player);
                    return EnumActionResult.SUCCESS;
                }
            }
        }
    }
    return super.onItemUseFirst(player, world, pos, side, hitX, hitY, hitZ, hand);
}
 
Example 11
Source File: TileEntityQuickStackerAdvanced.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void quickStackToInventories(World world, EntityPlayer player, long enabledSlotsMask,
        List<BlockPosDistance> positions, FilterSettings filter)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    boolean movedSome = false;

    for (BlockPosDistance posDist : positions)
    {
        TileEntity te = world.getTileEntity(posDist.pos);

        if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP))
        {
            IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);

            if (inv != null)
            {
                Result result = quickStackItems(playerInv, inv, enabledSlotsMask, player.isSneaking() == false, filter);

                if (result != Result.MOVED_NONE)
                {
                    Effects.spawnParticlesFromServer(world.provider.getDimension(), posDist.pos, EnumParticleTypes.VILLAGER_HAPPY);
                    movedSome = true;
                }

                if (result == Result.MOVED_ALL)
                {
                    break;
                }
            }
        }
    }

    if (movedSome)
    {
        world.playSound(null, player.getPosition(), SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.MASTER, 0.5f, 1.8f);
    }
}
 
Example 12
Source File: CapabilityCache.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
private LazyOptional<?> requestCapability(Capability<?> capability, Direction to) {
    TileEntity tile = world.getTileEntity(pos.offset(to));
    Direction inverse = to == null ? null : to.getOpposite();
    if (tile != null) {
        return tile.getCapability(capability, inverse);
    }
    return LazyOptional.empty();
}
 
Example 13
Source File: TileEntityEngineeringTable.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
	TileEntity above = worldObj.getTileEntity(pos.add(0, 1, 0));
	if (above != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
	{
		return above.getCapability(capability, facing);
	}
	return super.getCapability(capability, facing);
}
 
Example 14
Source File: CompatibilityUtils.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Nullable
public static IFluidHandler getFluidHandler(TileEntity te, EnumFacing side) {
	if (te == null) return null;
	final IFluidHandler nativeCapability = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
	if (nativeCapability != null) return nativeCapability;

	return null;
}
 
Example 15
Source File: MachineBase.java    From EmergingTechnology with MIT License 5 votes vote down vote up
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity te = world.getTileEntity(pos);

    IItemHandler cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    for (int i = 0; i < cap.getSlots(); ++i) {
        ItemStack itemstack = cap.getStackInSlot(i);

        if (!itemstack.isEmpty()) {
            InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemstack);
        }
    }
}
 
Example 16
Source File: InventoryUtils.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static IItemHandler tryGetHandler(TileEntity te, EnumFacing side) {
	if (te == null) return null;

	if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side))
		return te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);

	if (te instanceof ISidedInventory)
		return new SidedInvWrapper((ISidedInventory)te, side);

	if (te instanceof IInventory)
		return new InvWrapper((IInventory)te);

	return null;
}
 
Example 17
Source File: PlungerBehaviour.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    TileEntity tileEntity = world.getTileEntity(pos);
    if (tileEntity == null) {
        return EnumActionResult.PASS;
    }
    IFluidHandler fluidHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
    if (fluidHandler == null) {
        return EnumActionResult.PASS;
    }
    ItemStack toolStack = player.getHeldItem(hand);
    boolean isShiftClick = player.isSneaking();
    IFluidHandler handlerToRemoveFrom = isShiftClick ?
        (fluidHandler instanceof FluidHandlerProxy ? ((FluidHandlerProxy) fluidHandler).input : null) :
        (fluidHandler instanceof FluidHandlerProxy ? ((FluidHandlerProxy) fluidHandler).output : fluidHandler);

    if (handlerToRemoveFrom != null && GTUtility.doDamageItem(toolStack, cost, false)) {
        if (!world.isRemote) {
            FluidStack drainStack = handlerToRemoveFrom.drain(1000, true);
            int amountOfFluid = drainStack == null ? 0 : drainStack.amount;
            if (amountOfFluid > 0) {
                player.playSound(SoundEvents.ENTITY_SLIME_SQUISH, 1.0f, amountOfFluid / 1000.0f);
            }
        }
        return EnumActionResult.SUCCESS;
    }
    return EnumActionResult.PASS;
}
 
Example 18
Source File: SignalsAccessor.java    From Signals with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<IDestinationProvider> getDestinationProviders(TileEntity te){
    CapabilityDestinationProvider cap = te.getCapability(CapabilityDestinationProvider.INSTANCE, null);
    return cap == null ? Collections.emptyList() : cap.getApplicableDestinationProviders();
}
 
Example 19
Source File: UtilItemModular.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the inventory that the selected Link Crystal in the given modular item is currently bound to,
 * or null in case of errors.
 */
@Nullable
public static IItemHandler getBoundInventory(ItemStack modularStack, EntityPlayer player, int chunkLoadDuration)
{
    if (modularStack.isEmpty() || (modularStack.getItem() instanceof IModular) == false)
    {
        return null;
    }

    IModular iModular = (IModular) modularStack.getItem();
    TargetData target = TargetData.getTargetFromSelectedModule(modularStack, ModuleType.TYPE_LINKCRYSTAL);

    if (target == null ||
        iModular.getSelectedModuleTier(modularStack, ModuleType.TYPE_LINKCRYSTAL) != ItemLinkCrystal.TYPE_BLOCK ||
        OwnerData.canAccessSelectedModule(modularStack, ModuleType.TYPE_LINKCRYSTAL, player) == false)
    {
        return null;
    }

    // Bound to a vanilla Ender Chest
    if ("minecraft:ender_chest".equals(target.blockName))
    {
        return new InvWrapper(player.getInventoryEnderChest());
    }

    World targetWorld = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(target.dimension);

    if (targetWorld == null)
    {
        return null;
    }

    if (chunkLoadDuration > 0)
    {
        // Chunk load the target
        ChunkLoading.getInstance().loadChunkForcedWithPlayerTicket(player, target.dimension,
                target.pos.getX() >> 4, target.pos.getZ() >> 4, chunkLoadDuration);
    }

    TileEntity te = targetWorld.getTileEntity(target.pos);

    // Block has changed since binding, or does not have IItemHandler capability
    if (te == null || te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, target.facing) == false ||
        target.isTargetBlockUnchanged() == false)
    {
        // Remove the bind
        TargetData.removeTargetTagFromSelectedModule(modularStack, ModuleType.TYPE_LINKCRYSTAL);
        player.sendStatusMessage(new TextComponentTranslation("enderutilities.chat.message.bound.block.changed"), true);
        return null;
    }

    return te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, target.facing);
}
 
Example 20
Source File: EnergyNetworkHelper.java    From EmergingTechnology with MIT License 3 votes vote down vote up
private static void tryPushEnergyToTile(World world, BlockPos position, EnumFacing facing, GeneratorEnergyStorageHandler energyStorageHandler) {
    TileEntity tileEntity = world.getTileEntity(position);

    if (tileEntity == null) return;
    if (!tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing)) return;

    IEnergyStorage energyStorage = tileEntity.getCapability(CapabilityEnergy.ENERGY, facing);

    if (energyStorage == null) return;

    int pushedEnergy = energyStorage.receiveEnergy(energyStorageHandler.getEnergyStored(), false);

    energyStorageHandler.extractEnergy(pushedEnergy, false);
}