net.minecraftforge.fluids.capability.IFluidHandlerItem Java Examples

The following examples show how to use net.minecraftforge.fluids.capability.IFluidHandlerItem. 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: BlockTFStorage.java    From TofuCraftReload with MIT License 6 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (worldIn.isRemote) {

        return true;

    } else {
        ItemStack stack = playerIn.getHeldItem(hand);
        TileEntity tileentity = worldIn.getTileEntity(pos);
        if (tileentity instanceof TileEntityTFStorage) {
            IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));

            if (handler != null) {
                FluidUtil.interactWithFluidHandler(playerIn, hand, tileentity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));

                return true;
            } else {
                playerIn.openGui(TofuMain.instance, TofuGuiHandler.ID_STORAGE_MACHINE_GUI, worldIn, pos.getX(), pos.getY(), pos.getZ());
            }
        }
        return true;
    }
}
 
Example #2
Source File: BlockBarrelDistillation.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
  public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
      if (world.isRemote) {
          return true;
      }
ItemStack stack = player.getHeldItem(hand);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityDistillation) {
    IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
    if (handler != null) {
        FluidUtil.interactWithFluidHandler(player, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));
        return true;
    }
    player.openGui(SakuraMain.instance, SakuraGuiHandler.ID_DISTILLATION, world, pos.getX(), pos.getY(), pos.getZ());
    return true;
}
return true;
  }
 
Example #3
Source File: BlockBarrel.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
  public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
      if (world.isRemote) {
          return true;
      }
ItemStack stack = player.getHeldItem(hand);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityBarrel) {
    IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
    if (handler != null) {
        FluidUtil.interactWithFluidHandler(player, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));
        return true;
    }
    player.openGui(SakuraMain.instance, SakuraGuiHandler.ID_BARREL, world, pos.getX(), pos.getY(), pos.getZ());
    return true;
}
return true;
  }
 
Example #4
Source File: BlockTFBattery.java    From TofuCraftReload with MIT License 6 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) {
    if (!worldIn.isRemote) {
        ItemStack stack = playerIn.getHeldItem(hand);
        TileEntity tileentity = worldIn.getTileEntity(pos);
        if (tileentity instanceof TileEntityTofuBattery) {
            IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
            if (handler != null) {
                FluidUtil.interactWithFluidHandler(playerIn, hand, tileentity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing));
                return true;
            } else {
                playerIn.openGui(TofuMain.instance, TofuGuiHandler.ID_BATTERY_GUI, worldIn, pos.getX(), pos.getY(), pos.getZ());
            }
        }
    }
    return true;
}
 
Example #5
Source File: UpgradeCobbestone.java    From BetterChests with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Predicate<ItemStack> getFluidPredicate(Fluid fluid) {
	return new Predicate<ItemStack>() {
		@Override
		public boolean test(ItemStack itemStack) {
			IFluidHandlerItem handler = FluidUtil.getFluidHandler(itemStack);
			if (handler == null) {
				return false;
			}
			for (IFluidTankProperties properties : handler.getTankProperties()) {
				FluidStack stack = properties.getContents();
				if (stack != null && stack.getFluid() == fluid) {
					return true;
				}
			}
			return false;
		}
	};
}
 
Example #6
Source File: PhantomFluidWidget.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
    if (id == 1) {
        ItemStack itemStack = gui.entityPlayer.inventory.getItemStack().copy();
        if (!itemStack.isEmpty()) {
            itemStack.setCount(1);
            IFluidHandlerItem fluidHandler = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
            if (fluidHandler != null) {
                FluidStack resultFluid = fluidHandler.drain(Integer.MAX_VALUE, false);
                fluidStackUpdater.accept(resultFluid);
            }
        } else {
            fluidStackUpdater.accept(null);
        }
    } else if (id == 2) {
        FluidStack fluidStack;
        try {
            fluidStack = FluidStack.loadFluidStackFromNBT(buffer.readCompoundTag());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fluidStackUpdater.accept(fluidStack);
    }
}
 
Example #7
Source File: RecipeShapedFluid.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
	NonNullList<ItemStack> remains = ForgeHooks.defaultRecipeGetRemainingItems(inv);
	for (int i = 0; i < height * width; i++) {
		ItemStack stack = inv.getStackInSlot(i);
		NonNullList<Ingredient> matchedIngredients = this.input;
		if (matchedIngredients.get(i) instanceof IngredientFluidStack) {
			if (!stack.isEmpty()) {
				ItemStack copy = stack.copy();
				copy.setCount(1);
				remains.set(i, copy);
			}
			IFluidHandlerItem handler = FluidUtil.getFluidHandler(remains.get(i));
			if (handler != null) {
				FluidStack fluid = ((IngredientFluidStack) matchedIngredients.get(i)).getFluid();
				handler.drain(fluid.amount, true);
				remains.set(i, handler.getContainer());
			}
		}
	}
	return remains;
}
 
Example #8
Source File: ItemFluidContainer.java    From customstuff4 with GNU General Public License v3.0 6 votes vote down vote up
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(@Nullable CreativeTabs tab, @Nonnull NonNullList<ItemStack> subItems)
{
    if (isInCreativeTab(tab))
    {
        subItems.add(new ItemStack(this));

        for (Fluid fluid : FluidRegistry.getRegisteredFluids().values())
        {
            if (!fluid.getName().equals("milk"))
            {
                // add all fluids that the bucket can be filled  with
                FluidStack fs = new FluidStack(fluid, content.capacity);
                ItemStack stack = new ItemStack(this);
                IFluidHandlerItem fluidHandler = new FluidHandlerItemStack(stack, content.capacity);
                if (fluidHandler.fill(fs, true) == fs.amount)
                {
                    ItemStack filled = fluidHandler.getContainer();
                    subItems.add(filled);
                }
            }
        }
    }
}
 
Example #9
Source File: MetaTileEntityQuantumTank.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void update() {
    super.update();
    if (!getWorld().isRemote && getTimer() % 5 == 0) {
        ItemStack itemStack = containerInventory.getStackInSlot(0);
        Capability<IFluidHandlerItem> capability = CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY;
        if (!itemStack.isEmpty() && itemStack.hasCapability(capability, null)) {
            //if we can't drain anything, try filling container. Otherwise, drain it into the quantum chest tank
            if (itemStack.getCapability(capability, null).drain(Integer.MAX_VALUE, false) == null) {
                fillContainerFromInternalTank(containerInventory, containerInventory, 0, 1);
                pushFluidsIntoNearbyHandlers(getFrontFacing());
            } else {
                fillInternalTankFromFluidContainer(containerInventory, containerInventory, 0, 1);
                pullFluidsFromNearbyHandlers(getFrontFacing());
            }
        }
    }
}
 
Example #10
Source File: MetaItem.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack) {
    if (stack.getItemDamage() >= metaItemOffset) {
        T item = getItem(stack);
        if (item == null) {
            return "invalid item";
        }
        String unlocalizedName = String.format("metaitem.%s.name", item.unlocalizedName);
        if (item.getNameProvider() != null) {
            return item.getNameProvider().getItemStackDisplayName(stack, unlocalizedName);
        }
        IFluidHandlerItem fluidHandlerItem = ItemHandlerHelper.copyStackWithSize(stack, 1)
            .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
        if (fluidHandlerItem != null) {
            FluidStack fluidInside = fluidHandlerItem.drain(Integer.MAX_VALUE, false);
            return I18n.format(unlocalizedName, fluidInside == null ? I18n.format("metaitem.fluid_cell.empty") : fluidInside.getLocalizedName());
        }
        return I18n.format(unlocalizedName);
    }
    return super.getItemStackDisplayName(stack);
}
 
Example #11
Source File: GlassBottleFluidHandler.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
private IFluidHandlerItem getNextInChain() {
    if(rawCapabilityProviders != null) {
        boolean foundMyself = false;
        for(ICapabilityProvider provider : rawCapabilityProviders) {
            IFluidHandlerItem fluidHandlerItem = provider.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
            if(fluidHandlerItem != null) {
                if(fluidHandlerItem == this) {
                    foundMyself = true;
                } else if(foundMyself) {
                    this.nextHandlerInChain = fluidHandlerItem;
                    break;
                }
            }
        }
        this.rawCapabilityProviders = null;
    }
    return nextHandlerInChain;
}
 
Example #12
Source File: GlassBottleFluidHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public IFluidTankProperties[] getTankProperties() {
    IFluidHandlerItem nextInChain = getNextInChain();
    IFluidTankProperties[] properties = nextInChain == null ? new IFluidTankProperties[0] : nextInChain.getTankProperties();
    IFluidTankProperties ownProperties = new FluidTankProperties(null, PotionFluids.POTION_ITEM_FLUID_AMOUNT, true, false);
    return ArrayUtils.add(properties, ownProperties);
}
 
Example #13
Source File: ContainerBucketFillHandler.java    From OpenModsLib with MIT License 5 votes vote down vote up
@SubscribeEvent
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 (!canFill(evt.getWorld(), target.getBlockPos(), te)) { return; }

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

		final FluidStack fluidInContainer = source.drain(Fluid.BUCKET_VOLUME, false);

		if (fluidInContainer != null) {
			final ItemStack filledBucket = getFilledBucket(fluidInContainer);
			if (!filledBucket.isEmpty()) {
				final IFluidHandlerItem container = FluidUtil.getFluidHandler(filledBucket);
				if (container != null) {
					final FluidStack fluidInBucket = container.drain(Integer.MAX_VALUE, false);
					if (fluidInBucket != null && fluidInBucket.isFluidStackIdentical(source.drain(fluidInBucket, false))) {
						source.drain(fluidInBucket, true);
						evt.setFilledBucket(filledBucket.copy());
						evt.setResult(Result.ALLOW);
					}
				}
			}
		}
	}
}
 
Example #14
Source File: GlassBottleFluidHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int fill(FluidStack resource, boolean doFill) {
    if(resource == null || resource.amount <= 0) {
        return 0;
    }
    int myOwnResult = fillImpl(resource, doFill);
    if(myOwnResult == 0) {
        IFluidHandlerItem nextInChain = getNextInChain();
        return nextInChain == null ? 0 : nextInChain.fill(resource, doFill);
    }
    return 0;
}
 
Example #15
Source File: GTMaterialGen.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** How to get a GTFluidTube of any fluid by name **/
public static ItemStack getModdedTube(String name, int count) {
	FluidStack fluid = FluidRegistry.getFluidStack(name, 1000);
	ItemStack stack = new ItemStack(GTItems.testTube);
	IFluidHandlerItem handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
	handler.fill(fluid, true);
	stack.setCount(count);
	return stack;
}
 
Example #16
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToGetFluidFromCow(final ItemStack currentItemStack,
                                         final EntityPlayer entityPlayer) {
  boolean canGetFluid = false;

  if (!currentItemStack.isEmpty() && entityFluid != null) {
    ItemStack filledItemStack = ItemHandlerHelper.copyStackWithSize(currentItemStack, 1);
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(filledItemStack);
    if (fluidHandlerItem != null) {
      if (fluidHandlerItem.fill(
              new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), true) == Fluid.BUCKET_VOLUME) {

        filledItemStack = fluidHandlerItem.getContainer();

        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
              entityPlayer.inventory.currentItem,
              filledItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, filledItemStack.copy());
        }

        canGetFluid = true;
      }
    }
  }

  return canGetFluid;
}
 
Example #17
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToHealCowWithFluidContainer(final ItemStack currentItemStack,
                                                   final EntityPlayer entityPlayer) {
  boolean cowHealed = false;
  if (!currentItemStack.isEmpty() && entityFluid != null) {
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(currentItemStack);
    if (fluidHandlerItem != null) {
      FluidStack containedFluid = fluidHandlerItem
              .drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
      ItemStack emptyItemStack;
      if (containedFluid != null &&
              containedFluid.getFluid().getName().equalsIgnoreCase(entityFluid.getName())) {
        fluidHandlerItem.drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
        emptyItemStack = fluidHandlerItem.getContainer();
        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
                  entityPlayer.inventory.currentItem,
                  emptyItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, emptyItemStack.copy());
        }
        heal(4F);
        cowHealed = true;
      }
    }
  }
  return cowHealed;
}
 
Example #18
Source File: BlockCampfirePot.java    From Sakura_mod with MIT License 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) {
      if (worldIn.isRemote) {
          return true;
      }
ItemStack stack = playerIn.getHeldItem(hand);
TileEntity tile = worldIn.getTileEntity(pos);
if (hand == EnumHand.MAIN_HAND) {
    if (tile instanceof TileEntityCampfirePot) {
        TileEntityCampfirePot tileEntityCampfire = (TileEntityCampfirePot) tile;
        IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
        if (handler != null) {
            FluidUtil.interactWithFluidHandler(playerIn, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing));
            return true;
        }
        
        if (WorldUtil.isItemFuel(stack)) {
            tileEntityCampfire.setBurningTime(tileEntityCampfire.getBurningTime() + TileEntityFurnace.getItemBurnTime(stack));
            setState(true, worldIn, pos);
			if(stack.getItem().hasContainerItem(stack)) stack = stack.getItem().getContainerItem(stack);
				else stack.shrink(1);
            return true;
        }

        if (stack.getItem() == Items.FLINT_AND_STEEL) {
            tileEntityCampfire.setBurningTime(tileEntityCampfire.getBurningTime() + 10000);
            setState(true, worldIn, pos);
            stack.damageItem(1, playerIn);
            return true;
        }

        playerIn.openGui(SakuraMain.instance, SakuraGuiHandler.ID_CAMPFIREPOT, worldIn, pos.getX(), pos.getY(), pos.getZ());
        return true;
    }
}

return true;
  }
 
Example #19
Source File: PhantomFluidWidget.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private FluidStack drainFrom(Object ingredient) {
    if (ingredient instanceof ItemStack) {
        ItemStack itemStack = (ItemStack) ingredient;
        IFluidHandlerItem fluidHandler = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
        if (fluidHandler != null)
            return fluidHandler.drain(Integer.MAX_VALUE, false);
    }
    return null;
}
 
Example #20
Source File: MetaItem.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack itemStack, @Nullable World worldIn, List<String> lines, ITooltipFlag tooltipFlag) {
    T item = getItem(itemStack);
    if (item == null) return;
    String unlocalizedTooltip = "metaitem." + item.unlocalizedName + ".tooltip";
    if (I18n.hasKey(unlocalizedTooltip)) {
        lines.addAll(Arrays.asList(I18n.format(unlocalizedTooltip).split("/n")));
    }

    IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
    if (electricItem != null) {
        lines.add(I18n.format("metaitem.generic.electric_item.tooltip",
            electricItem.getCharge(),
            electricItem.getMaxCharge(),
            GTValues.VN[electricItem.getTier()]));
    }

    IFluidHandlerItem fluidHandler = ItemHandlerHelper.copyStackWithSize(itemStack, 1)
        .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    if (fluidHandler != null) {
        IFluidTankProperties fluidTankProperties = fluidHandler.getTankProperties()[0];
        FluidStack fluid = fluidTankProperties.getContents();
        if (fluid != null) {
            lines.add(I18n.format("metaitem.generic.fluid_container.tooltip",
                fluid.amount,
                fluidTankProperties.getCapacity(),
                fluid.getLocalizedName()));
        } else lines.add(I18n.format("metaitem.generic.fluid_container.tooltip_empty"));
    }

    for (IItemBehaviour behaviour : getBehaviours(itemStack)) {
        behaviour.addInformation(itemStack, lines);
    }
}
 
Example #21
Source File: MetaItem.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SideOnly(Side.CLIENT)
protected int getColorForItemStack(ItemStack stack, int tintIndex) {
    T metaValueItem = getItem(stack);
    if (metaValueItem != null && metaValueItem.getColorProvider() != null) {
        return metaValueItem.getColorProvider().getItemStackColor(stack, tintIndex);
    }
    IFluidHandlerItem fluidContainerItem = ItemHandlerHelper.copyStackWithSize(stack, 1)
        .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    if (tintIndex == 0 && fluidContainerItem != null) {
        FluidStack fluidStack = fluidContainerItem.drain(Integer.MAX_VALUE, false);
        return fluidStack == null ? 0x666666 : RenderUtil.getFluidColor(fluidStack);
    }
    return 0xFFFFFF;
}
 
Example #22
Source File: DefaultSubItemHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void addFluidContainerVariants(ItemStack itemStack, List<ItemStack> subItems) {
    for (Fluid fluid : FluidRegistry.getRegisteredFluids().values()) {
        ItemStack containerStack = itemStack.copy();
        IFluidHandlerItem fluidContainer = containerStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
        if (fluidContainer != null) {
            fluidContainer.fill(new FluidStack(fluid, Integer.MAX_VALUE), true);
            if (fluidContainer.drain(Integer.MAX_VALUE, false) == null)
                continue;
            subItems.add(fluidContainer.getContainer());
        }
    }
}
 
Example #23
Source File: DefaultSubItemHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getFluidContainerSubType(ItemStack itemStack) {
    IFluidHandlerItem fluidHandler = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    if (fluidHandler != null) {
        IFluidTankProperties fluidTankProperties = fluidHandler.getTankProperties()[0];
        FluidStack fluid = fluidTankProperties.getContents();
        return String.format("f=%s", fluid == null ? "empty" : fluid.getFluid().getName());
    }
    return "";
}
 
Example #24
Source File: FoamSprayerBehavior.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public double getDurabilityForDisplay(ItemStack itemStack) {
    IFluidHandlerItem fluidHandlerItem = itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    IFluidTankProperties fluidTankProperties = fluidHandlerItem.getTankProperties()[0];
    FluidStack fluidStack = fluidTankProperties.getContents();
    return fluidStack == null ? 1.0 : (1.0 - fluidStack.amount / (fluidTankProperties.getCapacity() * 1.0));
}
 
Example #25
Source File: EntityRocket.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
protected boolean interact(EntityPlayer player) {
	//Actual interact code needs to be moved to a packet receive on the server

	ItemStack heldItem = player.getHeldItem(EnumHand.MAIN_HAND);

	//Handle linkers and right-click with fuel
	if(heldItem != null) {
		float fuelMult;
		FluidStack fluidStack;

		if(heldItem.getItem() instanceof ItemLinker) {
			if(ItemLinker.isSet(heldItem)) {


				TileEntity tile = this.world.getTileEntity(ItemLinker.getMasterCoords(heldItem));

				if(tile instanceof IInfrastructure) {
					IInfrastructure infrastructure = (IInfrastructure)tile;
					if(this.getDistance(ItemLinker.getMasterX(heldItem), this.posY, ItemLinker.getMasterZ(heldItem)) < infrastructure.getMaxLinkDistance() + Math.max(storage.getSizeX(), storage.getSizeZ())) {
						if(!connectedInfrastructure.contains(tile)) {

							linkInfrastructure(infrastructure);
							if(!world.isRemote) {
								player.sendMessage(new TextComponentString("Linked Sucessfully"));
							}
							ItemLinker.resetPosition(heldItem);

							return true;
						}
						else if(!world.isRemote)
							player.sendMessage(new TextComponentString("Already linked!"));
					}
					else if(!world.isRemote)
						player.sendMessage(new TextComponentString("The object you are trying to link is too far away"));
				}
				else if(!world.isRemote)
					player.sendMessage(new TextComponentString("This cannot be linked to a rocket!"));
			}
			else if(!world.isRemote)
				player.sendMessage(new TextComponentString("Nothing to be linked"));
			return false;
		}

		else if((FluidUtils.containsFluid(heldItem) && (fluidStack = FluidUtils.getFluidForItem(heldItem)) != null && (fuelMult = FuelRegistry.instance.getMultiplier(FuelType.LIQUID, fluidStack.getFluid())) > 0 )) { 


			int amountToAdd = (int) (fuelMult*fluidStack.amount);
			this.addFuelAmount(amountToAdd);

			//if the player is not in creative then try to use the fluid container
			if(!player.capabilities.isCreativeMode) {
				heldItem = heldItem.copy();
				heldItem.setCount(1);
				IFluidHandlerItem handler = FluidUtils.getFluidHandler(heldItem);
				handler.drain(fluidStack.amount, true);
				ItemStack emptyStack = handler.getContainer();

				if(player.inventory.addItemStackToInventory(emptyStack)) {
					player.getHeldItem(EnumHand.MAIN_HAND).splitStack(1);
					if(player.getHeldItem(EnumHand.MAIN_HAND).isEmpty())
						player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY); 
				}

			}

			return true;
		}
	}

	//If player is holding shift open GUI
	if(player.isSneaking()) {
		openGui(player);
	}
	else if(stats.hasSeat()) { //If pilot seat is open mount entity there
		if(stats.hasSeat() && this.getPassengers().isEmpty()) {
			if(!world.isRemote)
				player.startRiding(this);
		}
		/*else if(stats.getNumPassengerSeats() > 0) { //If a passenger seat exists and one is empty, mount the player to it
			for(int i = 0; i < stats.getNumPassengerSeats(); i++) {
				if(this.mountedEntities[i] == null || this.mountedEntities[i].get() == null) {
					player.ridingEntity = this;
					this.mountedEntities[i] = new WeakReference<Entity>(player);
					break;
				}
			}
		}*/
	}
	return true;
}
 
Example #26
Source File: RecipeMapFluidCanner.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Nullable
public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs, int outputFluidTankCapacity) {
    Recipe recipe = super.findRecipe(voltage, inputs, fluidInputs, outputFluidTankCapacity);
    if (inputs.size() == 0 || inputs.get(0).isEmpty() || recipe != null)
        return recipe;

    // Fail early if input isn't a fluid container
    if (!inputs.get(0).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null))
        return null;

    // Make a copy to use for creating recipes
    ItemStack inputStack = inputs.get(0).copy();
    inputStack.setCount(1);

    // Make another copy to use for draining and filling
    ItemStack fluidHandlerItemStack = inputStack.copy();
    IFluidHandlerItem fluidHandlerItem = fluidHandlerItemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    if (fluidHandlerItem == null)
        return null;

    FluidStack containerFluid = fluidHandlerItem.drain(Integer.MAX_VALUE, true);
    if (containerFluid != null) {
        //if we actually drained something, then it's draining recipe
        return recipeBuilder()
            //we can reuse recipe as long as input container stack fully matches our one
            .inputs(new CountableIngredient(new NBTIngredient(inputStack), 1))
            .outputs(fluidHandlerItem.getContainer())
            .fluidOutputs(containerFluid)
            .duration(Math.max(16, containerFluid.amount / 64)).EUt(4)
            .build().getResult();
    }

    //if we didn't drain anything, try filling container
    if (!fluidInputs.isEmpty() && fluidInputs.get(0) != null) {
        FluidStack inputFluid = fluidInputs.get(0).copy();
        inputFluid.amount = fluidHandlerItem.fill(inputFluid, true);
        if (inputFluid.amount > 0) {
            return recipeBuilder()
                //we can reuse recipe as long as input container stack fully matches our one
                .inputs(new CountableIngredient(new NBTIngredient(inputStack), 1))
                .fluidInputs(inputFluid)
                .outputs(fluidHandlerItem.getContainer())
                .duration(Math.max(16, inputFluid.amount / 64)).EUt(4)
                .build().getResult();
        }
    }
    return null;
}
 
Example #27
Source File: FluidContainerSlotWidget.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean canPutStack(ItemStack stack) {
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(stack);
    return fluidHandlerItem != null && (!requireFilledContainer || fluidHandlerItem.getTankProperties()[0].getContents() != null);
}
 
Example #28
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
/**
 * @return the maximum amount of air allowed in this suit
 */
@Override
public int getMaxAir(ItemStack stack) {

	if(stack.hasTagCompound()) {
		EmbeddedInventory inv = new EmbeddedInventory(getNumSlots(stack));
		inv.readFromNBT(stack.getTagCompound());
		List<ItemStack> list = new LinkedList<ItemStack>();

		for(int i = 0; i < inv.getSizeInventory(); i++) {
			if(!inv.getStackInSlot(i).isEmpty()) {
				
				if( i < 2 && inv.getStackInSlot(i).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
					list.add(inv.getStackInSlot(i));
				}
				else if(inv.getStackInSlot(i).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
					
					FluidStack fstack = FluidUtils.getFluidForItem(inv.getStackInSlot(i));
					if(fstack != null && FluidUtils.areFluidsSameType(fstack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
						list.add(inv.getStackInSlot(i));
				}
				
			}
		}
		
		int maxAir = 0;
		for(ItemStack component : list) {
				IFluidHandlerItem fHandler = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
				FluidStack fluidStack = fHandler.getTankProperties()[0].getContents();

				if(fluidStack == null || fluidStack.getFluid() == null || FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
					maxAir += fHandler.getTankProperties()[0].getCapacity();
		}


		return maxAir;
	}

	return 0;

	//return Configuration.spaceSuitOxygenTime*1200; //30 minutes;
}
 
Example #29
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
/**
 * Increments air in the suit by amt
 * @param stack the item stack to operate on
 * @param amt amount of air by which to decrement
 * @return The amount of air inserted into the suit
 */
@Override
public int increment(ItemStack stack, int amt) {

	if(stack.hasTagCompound()) {
		EmbeddedInventory inv = new EmbeddedInventory(getNumSlots(stack));
		inv.readFromNBT(stack.getTagCompound());
		List<ItemStack> list = new LinkedList<ItemStack>();

		for(int i = 0; i < inv.getSizeInventory(); i++) {
			if(!inv.getStackInSlot(i).isEmpty()) {
				
				if( i < 2 && inv.getStackInSlot(i).hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
					list.add(inv.getStackInSlot(i));
				}
				else if(FluidUtils.containsFluid(inv.getStackInSlot(i))) {
					
					FluidStack fstack = FluidUtils.getFluidForItem(inv.getStackInSlot(i));
					if(fstack != null && FluidUtils.areFluidsSameType(fstack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
						list.add(inv.getStackInSlot(i));
				}
				
			}
		}


		int amtDrained = amt;
		//At this point the list contains ONLY capable items
		for(ItemStack component : list) {
				IFluidHandlerItem fHandler = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
				FluidStack fluidStack = fHandler.getTankProperties()[0].getContents();

				if(fluidStack == null || FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
					amtDrained -= fHandler.fill(new FluidStack(AdvancedRocketryFluids.fluidOxygen, amtDrained), true);

				if(amtDrained == 0)
					break;
			
		}

		saveEmbeddedInventory(stack, inv);

		return amt - amtDrained;
	}

	return 0;

	/*NBTTagCompound nbt;
	if(stack.hasTagCompound()) {
		nbt = stack.getTagCompound();
	}
	else {
		nbt = new NBTTagCompound();
	}

	int prevAmt = nbt.getInteger("air");
	int newAmt = Math.min(prevAmt + amt, getMaxAir());
	nbt.setInteger("air", newAmt);
	stack.setTagCompound(nbt);

	return newAmt - prevAmt;*/
}
 
Example #30
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
/**
 * Decrements air in the suit by amt
 * @param stack the item stack to operate on
 * @param amt amount of air by which to decrement
 * @return The amount of air extracted from the suit
 */
@Override
public int decrementAir(ItemStack stack, int amt) {

	if(stack.hasTagCompound()) {
		EmbeddedInventory inv = new EmbeddedInventory(getNumSlots(stack));
		inv.readFromNBT(stack.getTagCompound());
		List<ItemStack> list = new LinkedList<ItemStack>();

		for(int i = 0; i < inv.getSizeInventory(); i++) {
			if(!inv.getStackInSlot(i).isEmpty())
				list.add(inv.getStackInSlot(i));
		}
		int amtDrained = amt;
		for(ItemStack component : list) {
			if(component.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
				IFluidHandlerItem fluidItem = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
				FluidStack fluidStack = FluidUtils.getFluidForItem(component);

				FluidStack fluidDrained = null;

				if(fluidStack != null && FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
					fluidDrained = fluidItem.drain(amtDrained, true);

				if(fluidDrained != null)
					amtDrained -= fluidDrained.amount;

				if(amtDrained == 0)
					break;
			}
		}
		saveEmbeddedInventory(stack, inv);
		return amt - amtDrained;

	}
	return 0;

	/*NBTTagCompound nbt;
	if(stack.hasTagCompound()) {
		nbt = stack.getTagCompound();
	}
	else {
		nbt = new NBTTagCompound();
	}

	int prevAmt = nbt.getInteger("air");
	int newAmt = Math.max(prevAmt - amt,0);
	nbt.setInteger("air", newAmt);
	stack.setTagCompound(nbt);

	return prevAmt - newAmt;*/
}