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

The following are Jave code examples for showing how to use hasCapability() of the net.minecraft.tileentity.TileEntity class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pnc-repressurized   File: ProgWidgetLiquidInventoryCondition.java   View Source Code Vote up 7 votes
@Override
protected DroneAIBlockCondition getEvaluator(IDroneBase drone, IProgWidget widget) {
    return new DroneAIBlockCondition(drone, (ProgWidgetAreaItemBase) widget) {

        @Override
        protected boolean evaluate(BlockPos pos) {
            TileEntity te = drone.world().getTileEntity(pos);
            int count = 0;
            if (te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null) ) {
                IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
                for (IFluidTankProperties prop : handler.getTankProperties()) {
                    FluidStack stack = prop.getContents();
                    if (stack != null) {
                        if (ProgWidgetLiquidFilter.isLiquidValid(stack.getFluid(), widget, 1)) {
                            count += stack.amount;
                        }
                    }
                }
            } else {
                Fluid fluid = FluidRegistry.lookupFluidForBlock(drone.world().getBlockState(pos).getBlock());
                if (fluid != null && ProgWidgetLiquidFilter.isLiquidValid(fluid, widget, 1) && FluidUtils.isSourceBlock(drone.world(), pos)) {
                    count += 1000;
                }
            }
            return ((ICondition) widget).getOperator() == ICondition.Operator.EQUALS ?
                    count == ((ICondition) widget).getRequiredCount() :
                    count >= ((ICondition) widget).getRequiredCount();
        }

    };
}
 
Example 2
Project: pnc-repressurized   File: RenderSearchItemBlock.java   View Source Code Vote up 6 votes
private int getSearchedItemCount() {
        TileEntity te = world.getTileEntity(pos);
        if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
//        if (world.getTileEntity(pos) instanceof IInventory) {
            IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
            int itemCount = 0;
//            IInventory inventory = (IInventory) world.getTileEntity(pos);
            ItemStack searchStack = ItemPneumaticArmor.getSearchedStack(FMLClientHandler.instance().getClient().player.getItemStackFromSlot(EntityEquipmentSlot.HEAD));
            if (searchStack.isEmpty()) return 0;
            for (int l = 0; l < handler.getSlots(); l++) {
                if (!handler.getStackInSlot(l).isEmpty()) {
                    itemCount += getSearchedItemCount(handler.getStackInSlot(l), searchStack);
                }
            }
            return itemCount;
        }
        return 0;
    }
 
Example 3
Project: pnc-repressurized   File: SemiBlockRequester.java   View Source Code Vote up 6 votes
@Override
public int amountRequested(FluidStack stack) {
    int totalRequestingAmount = getTotalRequestedAmount(stack);
    if (totalRequestingAmount > 0) {
        TileEntity te = getTileEntity();
        int count = 0;
        for (EnumFacing facing : EnumFacing.VALUES) {
            if (te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing)) {
                IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing);
                for (IFluidTankProperties properties : handler.getTankProperties()) {
                    FluidStack contents = properties.getContents();
                    if (contents != null && contents.getFluid() == stack.getFluid()) {
                        count += contents.amount;
                    }
                }
                if (count > 0) break;
            }
        }
        if (count == 0) return 0;
        count += getIncomingFluid(stack.getFluid());
        return Math.max(0, Math.min(stack.amount, totalRequestingAmount - count));
    }
    return 0;
}
 
Example 4
Project: Machines-and-Stuff   File: GeneratorBase.java   View Source Code Vote up 6 votes
protected boolean pushEnergy() {
    boolean pushed = false;
    for(EnumFacing dir : EnumFacing.VALUES) {
        TileEntity tile = world.getTileEntity(getPos().offset(dir));
        if(tile != null)
            if(tile.hasCapability(TeslaCapabilities.CAPABILITY_CONSUMER, dir.getOpposite())) {
                BaseTeslaContainer cont = (BaseTeslaContainer) tile.getCapability(TeslaCapabilities.CAPABILITY_CONSUMER, dir.getOpposite());
                container.takePower(cont.givePower(container.takePower(container.getOutputRate(), true), false), false);
                if(!world.isRemote) {
                    tile.markDirty();
                    markDirty();
                    pushed = true;
                }
            }
    }
    return pushed;
}
 
Example 5
Project: pnc-repressurized   File: BlockPneumaticCraft.java   View Source Code Vote up 6 votes
@Override
@Optional.Method(modid = "theoneprobe")
public void addProbeInfo(ProbeMode mode, IProbeInfo probeInfo, EntityPlayer player, World world, IBlockState blockState, IProbeHitData data) {
    TileEntity te = world.getTileEntity(data.getPos());
    if(te instanceof IInfoForwarder){
        te = ((IInfoForwarder)te).getInfoTileEntity();
    }
    
    if (te instanceof IPneumaticMachine) {
        TOPCallback.handlePneumatic(mode, probeInfo, (IPneumaticMachine)te);
    }
    if (te instanceof IHeatExchanger) {
        TOPCallback.handleHeat(mode, probeInfo, (IHeatExchanger) te);
    }
    if (ConfigHandler.client.topShowsFluids && te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, data.getSideHit())) {
        IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, data.getSideHit());
        TOPCallback.handleFluidTanks(mode, probeInfo, handler);
    }
    if (te instanceof TileEntityBase) {
        TOPCallback.handleRedstoneMode(mode, probeInfo, (TileEntityBase) te);
    }
    if (te instanceof TileEntityPressureTube) {
        TOPCallback.handlePressureTube(mode, probeInfo, (TileEntityPressureTube) te, data.getSideHit());
    }
}
 
Example 6
Project: Soot   File: TileEntityEmberFunnel.java   View Source Code Vote up 6 votes
@Override
public void update() {
    this.ticksExisted ++;
    EnumFacing facing = world.getBlockState(pos).getValue(BlockEmberEmitter.facing);
    BlockPos attachPos = pos.offset(facing, -1);
    TileEntity attachTile = world.getTileEntity(attachPos);
    if (ticksExisted % 2 == 0 && attachTile != null){
        if (attachTile.hasCapability(EmberCapabilityProvider.emberCapability, null)){
            IEmberCapability cap = attachTile.getCapability(EmberCapabilityProvider.emberCapability, null);
            if (cap != null){
                if (cap.getEmber() < cap.getEmberCapacity() && capability.getEmber() > 0){
                    double added = cap.addAmount(Math.min(TRANSFER_SPEED,capability.getEmber()), true);
                    double removed = capability.removeAmount(added, true);
                    markDirty();
                    attachTile.markDirty();
                    if (!(attachTile instanceof ITileEntityBase) && !world.isRemote){
                        attachTile.markDirty(); //Idk why this is duplicated but the github source has it, so I carried it over.
                        EventManager.markTEForUpdate(attachPos, attachTile);
                    }
                }
            }
        }
    }
}
 
Example 7
Project: Metalworks   File: BlockCableBasic.java   View Source Code Vote up 5 votes
private List<EnumFacing> isSideValid(IBlockAccess world, BlockPos pos){
    List<EnumFacing> sides = new ArrayList<>();
    for(EnumFacing side : EnumFacing.values()){
        TileEntity tile = world.getTileEntity(pos.offset(side));
        if(tile != null && tile.hasCapability(CapabilityEnergy.ENERGY, side)){
            IEnergyStorage storage = tile.getCapability(CapabilityEnergy.ENERGY, side);
            if(storage != null && (storage.canReceive() || storage.canExtract())){
                sides.add(side);
            }
        }
    }
    return sides;
}
 
Example 8
Project: pnc-repressurized   File: BlockTrackEntryInventory.java   View Source Code Vote up 5 votes
@Override
public boolean shouldTrackWithThisEntry(IBlockAccess world, BlockPos pos, IBlockState state, TileEntity te) {
    if (te instanceof TileEntityChest) {
        TileEntityChest chest = (TileEntityChest) te;
        if (chest.adjacentChestXNeg != null || chest.adjacentChestZNeg != null) return false;
    }

    return te != null
            && !invBlackList.contains(TileEntity.getKey(te.getClass()))
            && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)
            && !MinecraftForge.EVENT_BUS.post(new InventoryTrackEvent(te));
}
 
Example 9
Project: pnc-repressurized   File: DroneAILiquidImport.java   View Source Code Vote up 5 votes
private boolean emptyTank(BlockPos pos, boolean simulate) {
    if (drone.getTank().getFluidAmount() == drone.getTank().getCapacity()) {
        drone.addDebugEntry("gui.progWidget.liquidImport.debug.fullDroneTank");
        abort();
        return false;
    } else {
        TileEntity te = drone.world().getTileEntity(pos);
        if (te != null) {
            for (int i = 0; i < 6; i++) {
                if (((ISidedWidget) widget).getSides()[i] && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.getFront(i))) {
                    IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.getFront(i));
                    FluidStack importedFluid = handler.drain(Integer.MAX_VALUE, false);
                    if (importedFluid != null && ((ILiquidFiltered) widget).isFluidValid(importedFluid.getFluid())) {
                        int filledAmount = drone.getTank().fill(importedFluid, false);
                        if (filledAmount > 0) {
                            if (((ICountWidget) widget).useCount())
                                filledAmount = Math.min(filledAmount, getRemainingCount());
                            if (!simulate) {
                                decreaseCount(drone.getTank().fill(handler.drain(filledAmount, true), true));
                            }
                            return true;
                        }
                    }
                }
            }
            drone.addDebugEntry("gui.progWidget.liquidImport.debug.emptiedToMax", pos);
        } else if (!((ICountWidget) widget).useCount() || getRemainingCount() >= 1000) {
            Fluid fluid = FluidRegistry.lookupFluidForBlock(drone.world().getBlockState(pos).getBlock());
            if (fluid != null && ((ILiquidFiltered) widget).isFluidValid(fluid) && drone.getTank().fill(new FluidStack(fluid, 1000), false) == 1000 && FluidUtils.isSourceBlock(drone.world(), pos)) {
                if (!simulate) {
                    decreaseCount(1000);
                    drone.getTank().fill(new FluidStack(fluid, 1000), true);
                    drone.world().setBlockToAir(pos);
                }
                return true;
            }
        }
        return false;
    }
}
 
Example 10
Project: Meltery   File: TileMeltery.java   View Source Code Vote up 5 votes
@Override
public void update() {
    if (hasFuel()) {
        ItemStack melt = inventory.getStackInSlot(0);
        MeltingRecipe recipe = MelteryHandler.getMelteryRecipe(melt);

        if (recipe != null) {
            if (progress > recipe.getUsableTemperature()) {
                FluidStack fluidStack = recipe.getResult();
                if ((tank.getCapacity() - tank.getFluidAmount()) >= tank.fill(fluidStack, false)) {
                    tank.fill(fluidStack, true);
                    world.playSound(null, pos, SoundEvents.ITEM_BUCKET_FILL_LAVA, SoundCategory.BLOCKS, 1.0f, 0.75f);
                    melt.shrink(1);
                } else {
                    world.playSound(null, pos, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 1.0f, 0.75f);
                }
                setProgress(0);
            }
            incrementProcress();
        }
    }
    if(tank.getFluidAmount() > 0) {
        for (EnumFacing facing : EnumFacing.HORIZONTALS) {
            BlockPos side = pos.offset(facing);
            if (!world.isAirBlock(side)) {
                if (world.getTileEntity(side) != null) {
                    TileEntity tile = world.getTileEntity(side);
                    if (!(tile instanceof TileMeltery) && tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite())) {
                        IFluidHandler fluidHandler = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing.getOpposite());
                        if (fluidHandler instanceof IFluidTank) {
                            FluidUtil.tryFluidTransfer(fluidHandler, tank, 140, true);
                        }
                    }

                }
            }
        }
    }
}
 
Example 11
Project: SimpleTubes   File: TubeUtil.java   View Source Code Vote up 5 votes
public static Pair<ItemStack, Integer> extract(TileEntity tile, EnumFacing from, int[] col, IInventory inv, boolean fullStack) {

		if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, from)) {
			IItemHandlerModifiable handler = (IItemHandlerModifiable) tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, from);
			if (handler != null) {
				int invSize = handler.getSlots();

				for (int i = 0; i < invSize; i++) {
					ItemStack current = handler.getStackInSlot(i);

					for (int j = 0; j < inv.getSizeInventory(); j++) {
						ItemStack stack = inv.getStackInSlot(j);
						int color = -1;
						if (stack != null && stack != ItemStack.EMPTY && stack.getItem() != Items.AIR) {
							if (areItemStacksEqual(stack, current)) {
								int column = j >= 0 && j <= 3 ? 0 : j >= 4 && j <= 7 ? 1 : j >= 8 && j <= 11 ? 2 : j >= 12 && j <= 15 ? 3 : j >= 16 || j <= 19 ? 4 : -1;

								if (column == -1)
									return null;

								color = col[column];

								if (current != null && !current.isEmpty() && current.getItem() != Items.AIR) {
									ItemStack extracted = handler.extractItem(i, !fullStack ? 1 : current.getCount(), false);
									return Pair.of(extracted, color);
								}
							}
						}
					}

				}
			}
		} // TODO: TileEntities that don't have capabilities - needs testing

		return Pair.of(ItemStack.EMPTY, -1);
	}
 
Example 12
Project: pnc-repressurized   File: DroneAIExternalProgram.java   View Source Code Vote up 5 votes
@Override
protected boolean isValidPosition(BlockPos pos) {
    if (traversedPositions.add(pos)) {
        curSlot = 0;
        TileEntity te = drone.world().getTileEntity(pos);
        return te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    }
    return false;
}
 
Example 13
Project: Thermionics   File: HeatTransport.java   View Source Code Vote up 5 votes
public static IHeatStorage getStorage(World world, BlockPos pos, EnumFacing side) {
	TileEntity te = world.getTileEntity(pos);
	if (te==null) return NULL_HEAT; //If it can't Capabilities, it can't IHeatStorage
	if (te.hasCapability(Thermionics.CAPABILITY_HEATSTORAGE, side)) {
		return te.getCapability(Thermionics.CAPABILITY_HEATSTORAGE, side);
	} else {
		return NULL_HEAT; //Some day we might support other APIs. Not today.
	}
}
 
Example 14
Project: pnc-repressurized   File: ModuleCharging.java   View Source Code Vote up 5 votes
private IItemHandler getConnectedInventory() {
    if (connectedInventory == null) {
        connectedInventory = new TileEntityCache(pressureTube.world(), pressureTube.pos().offset(dir));
    }
    TileEntity te = connectedInventory.getTileEntity();
    return te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()) ?
            te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()) :
            null;
}
 
Example 15
Project: pnc-repressurized   File: ContainerProgrammer.java   View Source Code Vote up 5 votes
@Override
public void detectAndSendChanges() {
    super.detectAndSendChanges();
    if (te.getWorld().getTotalWorldTime() % 20 == 0) {
        for (EnumFacing d : EnumFacing.VALUES) {
            TileEntity neighbor = te.getWorld().getTileEntity(te.getPos().offset(d));
            if (neighbor != null && neighbor.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, d.getOpposite())) {
                sendToContainerListeners(new PacketSendNBTPacket(neighbor));
            }
        }
    }
}
 
Example 16
Project: MeeCreeps   File: InventoryTools.java   View Source Code Vote up 5 votes
public static boolean isInventory(World world, BlockPos pos) {
    if (pos == null) {
        return false;
    }
    TileEntity te = world.getTileEntity(pos);
    return (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP));
}
 
Example 17
Project: pnc-repressurized   File: IOHelper.java   View Source Code Vote up 5 votes
public static IItemHandler getInventoryForTE(TileEntity te, EnumFacing facing) {
    if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)) {
        return te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
    } else {
        return null;
    }
}
 
Example 18
Project: pnc-repressurized   File: TileEntityLiquidHopper.java   View Source Code Vote up 4 votes
@Override
protected boolean doExport(int maxItems) {
    EnumFacing dir = getRotation();
    if (tank.getFluid() != null) {
        TileEntity neighbor = IOHelper.getNeighbor(this, dir);
        if (neighbor != null && neighbor.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, dir.getOpposite())) {
            IFluidHandler fluidHandler = neighbor.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, dir.getOpposite());
            int amount = Math.min(maxItems * 100, tank.getFluid().amount - (leaveMaterial ? 1000 : 0));
            FluidStack transferred = FluidUtil.tryFluidTransfer(fluidHandler, tank, amount, true);
            return transferred != null && transferred.amount > 0;
        }
    }

    if (getWorld().isAirBlock(getPos().offset(dir))) {
        for (EntityItem entity : getNeighborItems(this, dir)) {
            if (!entity.isDead) {
                NonNullList<ItemStack> returnedItems = NonNullList.create();
                if (FluidUtils.tryFluidExtraction(tank, entity.getItem(), returnedItems)) {
                    if (entity.getItem().getCount() <= 0) entity.setDead();
                    for (ItemStack stack : returnedItems) {
                        EntityItem item = new EntityItem(getWorld(), entity.posX, entity.posY, entity.posZ, stack);
                        item.motionX = entity.motionX;
                        item.motionY = entity.motionY;
                        item.motionZ = entity.motionZ;
                        getWorld().spawnEntity(item);
                    }
                    return true;
                }
            }
        }
    }

    if (getUpgrades(EnumUpgrade.DISPENSER) > 0) {
        if (getWorld().isAirBlock(getPos().offset(dir))) {
            FluidStack extractedFluid = tank.drain(1000, false);
            if (extractedFluid != null && extractedFluid.amount == 1000) {
                Block fluidBlock = extractedFluid.getFluid().getBlock();
                if (fluidBlock != null) {
                    tank.drain(1000, true);
                    getWorld().setBlockState(getPos().offset(dir), fluidBlock.getDefaultState());
                }
            }
        }
    }

    return false;
}
 
Example 19
Project: Thermionics   File: RotaryTransport.java   View Source Code Vote up 4 votes
public static boolean isConsumer(World world, BlockPos pos, EnumFacing facing) {
	TileEntity te = world.getTileEntity(pos);
	if (te==null) return false;
	return te.hasCapability(Thermionics.CAPABILITY_ROTARYPOWER_CONSUMER, facing);
}
 
Example 20
Project: pnc-repressurized   File: DroneAILiquidExport.java   View Source Code Vote up 4 votes
private boolean fillTank(BlockPos pos, boolean simulate) {
    if (drone.getTank().getFluidAmount() == 0) {
        drone.addDebugEntry("gui.progWidget.liquidExport.debug.emptyDroneTank");
        abort();
        return false;
    } else {
        TileEntity te = drone.world().getTileEntity(pos);
        if (te != null) {
            FluidStack exportedFluid = drone.getTank().drain(Integer.MAX_VALUE, false);
            if (exportedFluid != null && ((ILiquidFiltered) widget).isFluidValid(exportedFluid.getFluid())) {
                for (int i = 0; i < 6; i++) {
                    if (((ISidedWidget) widget).getSides()[i] && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.getFront(i))) {
                        IFluidHandler tank = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.getFront(i));
                        int filledAmount = tank.fill(exportedFluid, false);
                        if (filledAmount > 0) {
                            if (((ICountWidget) widget).useCount()) {
                                filledAmount = Math.min(filledAmount, getRemainingCount());
                            }
                            if (!simulate) {
                                decreaseCount(tank.fill(drone.getTank().drain(filledAmount, true), true));
                            }
                            return true;
                        }
                    }
                }
                drone.addDebugEntry("gui.progWidget.liquidExport.debug.filledToMax", pos);
            } else {
                drone.addDebugEntry("gui.progWidget.liquidExport.debug.noValidFluid");
            }
        } else if (((ILiquidExport) widget).isPlacingFluidBlocks() && (!((ICountWidget) widget).useCount() || getRemainingCount() >= 1000)) {
            Block fluidBlock = drone.getTank().getFluid().getFluid().getBlock();
            if (drone.getTank().getFluidAmount() >= 1000 && fluidBlock != null && drone.world().isAirBlock(pos)) {
                if (!simulate) {
                    decreaseCount(1000);
                    drone.getTank().drain(1000, true);
                    drone.world().setBlockState(pos, fluidBlock.getDefaultState()); //TODO 1.8 test
                }
                return true;
            }
        }
        return false;
    }
}