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

The following examples show how to use net.minecraft.tileentity.TileEntity#hasCapability() . 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: 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 3
Source File: WrenchOverlayRenderer.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean shouldDrawOverlayForItem(ItemStack itemStack, TileEntity tileEntity) {
    if(tileEntity instanceof MetaTileEntityHolder &&
        itemStack.hasCapability(GregtechCapabilities.CAPABILITY_WRENCH, null)) {
        return true;
    }
    if(tileEntity.hasCapability(GregtechTileCapabilities.CAPABILITY_COVERABLE, null)) {
        if(itemStack.hasCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null)) {
            return true;
        }
        if (itemStack.getItem() instanceof MetaItem) {
            MetaItem<?> metaItem = (MetaItem<?>) itemStack.getItem();
            MetaItem<?>.MetaValueItem valueItem = metaItem.getItem(itemStack);
            if (valueItem != null) {
                List<IItemBehaviour> behaviourList = valueItem.getBehaviours();
                return behaviourList.stream().anyMatch(it ->
                    it instanceof CoverPlaceBehavior || it instanceof CrowbarBehaviour);
            }
        }
    }
    return false;
}
 
Example 4
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 5
Source File: TileGenericPipe.java    From Logistics-Pipes-2 with MIT License 6 votes vote down vote up
public ArrayList<Item> getItemTypesInInventory(EnumFacing face){
	ArrayList<Item> result = new ArrayList<Item>();
	
	if (!hasInventoryOnSide(face.getIndex())) {
		return result;
	}
	TileEntity te = world.getTileEntity(getPos().offset(face));
	if (!te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
		return result;
	}
	IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite());
	for(int i=0; i < itemHandler.getSlots(); i++) {
		ItemStack slotStack = itemHandler.getStackInSlot(i);
		Item item = slotStack.getItem();
		if(!slotStack.isEmpty() && !result.contains(item)) {
			result.add(item);
		}
	}
	return result;
}
 
Example 6
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 7
Source File: TileEntityInserter.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void updateValidSides(boolean markDirtyAndSync)
{
    if (this.getWorld() != null && this.getWorld().isRemote == false)
    {
        this.validSides.clear();

        World world = this.getWorld();
        BlockPos pos = this.getPos();

        for (EnumFacing side : this.enabledSides)
        {
            TileEntity te = world.getTileEntity(pos.offset(side));

            if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()))
            {
                this.validSides.add(side);
            }
        }

        if (markDirtyAndSync)
        {
            this.markDirty();
            this.syncSideConfigs();
        }
    }
}
 
Example 8
Source File: TileGenericPipe.java    From Logistics-Pipes-2 with MIT License 6 votes vote down vote up
public ArrayList<ItemStack> getItemsInInventory(EnumFacing face){
	ArrayList<ItemStack> result = new ArrayList<ItemStack>();
	
	if (!hasInventoryOnSide(face.getIndex())) {
		return result;
	}
	TileEntity te = world.getTileEntity(getPos().offset(face));
	if (!te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
		return result;
	}
	IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite());
	for(int i=0; i < itemHandler.getSlots(); i++) {
		ItemStack slotStack = itemHandler.getStackInSlot(i);
		if(!slotStack.isEmpty()) {
			result.add(slotStack);
		}
	}
	return result;
}
 
Example 9
Source File: TileFloader.java    From YouTubeModdingTutorial with MIT License 5 votes vote down vote up
private boolean findTankAndFill() {
    for (EnumFacing facing : EnumFacing.VALUES) {
        if (facing != EnumFacing.DOWN) {
            TileEntity te = world.getTileEntity(pos.offset(facing));
            if (te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite())) {
                IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite());
                // Simulate filling
                if (handler != null && handler.fill(new FluidStack(ModLiquids.fload, 100), true) != 0) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 10
Source File: ConnectionHelper.java    From Logistics-Pipes-2 with MIT License 5 votes vote down vote up
public static boolean isInventory(IBlockAccess worldIn, BlockPos pos, EnumFacing facing) {
	TileEntity target = AccessHelper.getTileSafe(worldIn, pos, facing);
	if(target !=null) {
		return(target.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()));
	}
	else {
		return false;
	}
}
 
Example 11
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Helper method to get an IFluidHandler for at tile position.
 *
 * Returns null if there is no valid fluid handler tile.
 */
@Nullable
public static IFluidHandler getFluidHandler(World world, BlockPos blockPos, @Nullable EnumFacing side) {
	IBlockState state = world.getBlockState(blockPos);
	Block block = state.getBlock();
	if (block.hasTileEntity(state)) {
		TileEntity tileEntity = world.getTileEntity(blockPos);
		if (tileEntity != null && tileEntity.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side)) {
			return tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
		}
	}
	return null;
}
 
Example 12
Source File: BlockInventoryPipe.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int getActiveNodeConnections(IBlockAccess world, BlockPos nodePos, IPipeTile<InventoryPipeType, EmptyNodeData> selfTileEntity) {
    int activeNodeConnections = 0;
    for (EnumFacing side : EnumFacing.VALUES) {
        BlockPos offsetPos = nodePos.offset(side);
        TileEntity tileEntity = world.getTileEntity(offsetPos);
        if(tileEntity != null && tileEntity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())) {
            activeNodeConnections |= 1 << side.getIndex();
        }
    }
    return activeNodeConnections;
}
 
Example 13
Source File: ItemEnderBucket.java    From enderutilities 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)
{
    ItemStack stack = player.getHeldItem(hand);

    if (LinkMode.fromStack(stack) == LinkMode.ENABLED &&
        OwnerData.canAccessSelectedModule(stack, ModuleType.TYPE_LINKCRYSTAL, player) == false)
    {
        return EnumActionResult.FAIL;
    }

    if (world.isRemote == false)
    {
        if (this.useBucketOnFluidBlock(world, player, stack) == EnumActionResult.SUCCESS)
        {
            return EnumActionResult.SUCCESS;
        }

        TileEntity te = world.getTileEntity(pos);

        if (te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side))
        {
            // If we are in bind mode, bind the bucket to the targeted tank and then return
            if (BucketMode.fromStack(stack) == BucketMode.BIND)
            {
                return super.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
            }

            return this.useBucketOnTank(world, pos, side, player, stack);
        }
    }

    return EnumActionResult.PASS;
}
 
Example 14
Source File: TileEnergyPipe.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
@Override
public boolean canInject(EnumFacing dir, TileEntity e) {
	return e.hasCapability(CapabilityEnergy.ENERGY, dir) && e.getCapability(CapabilityEnergy.ENERGY, dir).canReceive() && !(e instanceof TileEnergyPipe);
}
 
Example 15
Source File: Helper.java    From HoloInventory with MIT License 4 votes vote down vote up
public static boolean accept(TileEntity te)
{
    return te != null && (te instanceof IInventory || te instanceof BlockJukebox.TileEntityJukebox || te instanceof TileEntityEnderChest || te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null));
}
 
Example 16
Source File: TileLiquidPipe.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
@Override
public boolean canExtract(EnumFacing dir, TileEntity e) {
	return e.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, dir);
}
 
Example 17
Source File: TileEnergyPipe.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
@Override
public boolean canExtract(EnumFacing dir, TileEntity e) {
	
	return e.hasCapability(CapabilityEnergy.ENERGY, dir) && e.getCapability(CapabilityEnergy.ENERGY, dir).canExtract() && !(e instanceof TileEnergyPipe);
}
 
Example 18
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 19
Source File: TileEntityInserter.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tries to pull in items from an inventory on the input side.<br>
 * <b>NOTE: ONLY call this when the internal slot is empty, as this method doesn't check it!</b><br>
 * Does <b>NOT</b> pull items from another Inserter.
 * @return true if the operation succeeded and at least some items were moved, false otherwise
 */
private boolean tryPullInItems(World world, BlockPos posSelf)
{
    TileEntity te = world.getTileEntity(posSelf.offset(this.facingOpposite));
    //System.out.printf("tryPullInItems() @ %s, from %s\n", posSelf, posSelf.offset(this.facingOpposite));

    // We don't ever want to pull in items from another Inserter,
    // but instead we operate on a push-basis. (The extract() method returns null anyways.)
    if (te != null && (te instanceof TileEntityInserter) == false &&
        te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, this.getFacing()))
    {
        IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, this.getFacing());

        if (inv != null)
        {
            ItemStack stack;

            this.disableUpdateScheduling = true;

            if (this.isFiltered && this.isFilterSettingEnabled(FilterSetting.IS_INPUT_FILTER))
            {
                stack = this.tryPullInItemsThatPassFilters(inv);
            }
            else
            {
                stack = InventoryUtils.getItemsFromFirstNonEmptySlot(inv, this.itemHandlerBase.getInventoryStackLimit(), false);
            }

            if (stack.isEmpty() == false)
            {
                this.itemHandlerBase.insertItem(0, stack, false);
                this.disableUpdateScheduling = false;
                return true;
            }

            this.disableUpdateScheduling = false;
        }
    }

    return false;
}
 
Example 20
Source File: TileRequest.java    From HoloInventory with MIT License 4 votes vote down vote up
@Override
public ResponseMessage onMessage(TileRequest message, MessageContext ctx)
{
    World world = DimensionManager.getWorld(message.dim);
    if (world == null) return null;
    TileEntity te = world.getTileEntity(message.pos);
    if (te == null) return null;

    if (Helper.banned.contains(te.getClass().getCanonicalName())) return null;
    if (te instanceof ILockableContainer && !ctx.getServerHandler().player.canOpen(((ILockableContainer) te).getLockCode())) return null;

    if (te instanceof TileEntityEnderChest)
    {
        return new PlainInventory(message.pos, ctx.getServerHandler().player.getInventoryEnderChest());
    }
    else if (te instanceof BlockJukebox.TileEntityJukebox)
    {
        InventoryBasic ib = new InventoryBasic("minecraft:jukebox", false, 1);
        ib.setInventorySlotContents(0, ((BlockJukebox.TileEntityJukebox) te).getRecord());
        return new PlainInventory(message.pos, ib).setName(Blocks.JUKEBOX.getUnlocalizedName());
    }
    else if (te instanceof TileEntityChest)
    {
        Block b = world.getBlockState(message.pos).getBlock();
        if (b instanceof BlockChest)
        {
            IInventory i = ((BlockChest) b).getLockableContainer(world, message.pos);
            if (i != null) return new PlainInventory(message.pos, i);
            return null;
        }
        return new PlainInventory(message.pos, ((TileEntityChest) te));
    }
    else if (te instanceof IInventory)
    {
        return new PlainInventory(message.pos, (IInventory) te);
    }
    else if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
    {
        IItemHandler iih = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (iih == null)
        {
            HoloInventory.getLogger().warn("Error: Block at {} (Class: {} Te: {} Block: {}) returned null after indicating the capability is available.", message.pos, te.getClass().getName(), te, te.getBlockType());
            return null;
        }
        if (te instanceof INamedItemHandler) {
        	INamedItemHandler namedHandler = (INamedItemHandler) te;
        	return new PlainInventory(message.pos, namedHandler.getItemHandlerName(), iih);
        }
        return new PlainInventory(message.pos, te.getBlockType().getUnlocalizedName(), iih);
    }

    return null;
}