2013-04-13 16:35:13 +02:00
|
|
|
package universalelectricity.core.item;
|
|
|
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some helper functions for electric items.
|
|
|
|
*
|
|
|
|
* @author Calclavia
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ElectricItemHelper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Recharges an electric item.
|
|
|
|
*
|
|
|
|
* @param joules - The joules being provided to the electric item
|
|
|
|
* @return The total amount of joules provided by the provider.
|
|
|
|
*/
|
2013-07-27 00:16:21 +02:00
|
|
|
public static float chargeItem(ItemStack itemStack, float joules)
|
2013-04-13 16:35:13 +02:00
|
|
|
{
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
if (itemStack.getItem() instanceof IItemElectric)
|
|
|
|
{
|
2013-07-27 00:16:21 +02:00
|
|
|
return ((IItemElectric) itemStack.getItem()).recharge(itemStack, Math.min(((IItemElectric) itemStack.getItem()).getTransfer(itemStack), joules), true);
|
2013-04-13 16:35:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-30 01:05:47 +02:00
|
|
|
* Decharges an electric item.
|
2013-04-13 16:35:13 +02:00
|
|
|
*
|
|
|
|
* @param joules - The joules being withdrawn from the electric item
|
|
|
|
* @return The total amount of joules the provider received.
|
|
|
|
*/
|
2013-07-27 00:16:21 +02:00
|
|
|
public static float dischargeItem(ItemStack itemStack, float joules)
|
2013-04-13 16:35:13 +02:00
|
|
|
{
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
if (itemStack.getItem() instanceof IItemElectric)
|
|
|
|
{
|
2013-07-27 00:16:21 +02:00
|
|
|
return ((IItemElectric) itemStack.getItem()).discharge(itemStack, Math.min(((IItemElectric) itemStack.getItem()).getMaxElectricityStored(itemStack), joules), true);
|
2013-04-13 16:35:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an uncharged version of the electric item. Use this if you want the crafting recipe
|
|
|
|
* to use a charged version of the electric item instead of an empty version of the electric
|
|
|
|
* item
|
|
|
|
*
|
|
|
|
* @return An electrical ItemStack with a specific charge.
|
|
|
|
*/
|
2013-07-27 00:16:21 +02:00
|
|
|
public static ItemStack getWithCharge(ItemStack itemStack, float joules)
|
2013-04-13 16:35:13 +02:00
|
|
|
{
|
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
if (itemStack.getItem() instanceof IItemElectric)
|
|
|
|
{
|
2013-07-27 00:16:21 +02:00
|
|
|
((IItemElectric) itemStack.getItem()).setElectricity(itemStack, joules);
|
2013-04-13 16:35:13 +02:00
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
2013-07-27 00:16:21 +02:00
|
|
|
public static ItemStack getWithCharge(Item item, float joules)
|
2013-04-13 16:35:13 +02:00
|
|
|
{
|
|
|
|
return getWithCharge(new ItemStack(item), joules);
|
|
|
|
}
|
|
|
|
|
2013-07-27 00:16:21 +02:00
|
|
|
public static ItemStack getCloneWithCharge(ItemStack itemStack, float joules)
|
2013-04-13 16:35:13 +02:00
|
|
|
{
|
|
|
|
return getWithCharge(itemStack.copy(), joules);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getUncharged(ItemStack itemStack)
|
|
|
|
{
|
|
|
|
return getWithCharge(itemStack, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getUncharged(Item item)
|
|
|
|
{
|
|
|
|
return getUncharged(new ItemStack(item));
|
|
|
|
}
|
|
|
|
}
|