Added useForce to ForceConsumerUtils
This commit is contained in:
parent
85af07a40f
commit
2c6ae04a27
|
@ -14,4 +14,5 @@ public class Config {
|
|||
public static int damageLevel;
|
||||
public static int sturdyLevel;
|
||||
|
||||
public static float gemValue;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,382 @@
|
|||
package ley.modding.dartcraft.api.inventory;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class ItemInventory implements IInventory
|
||||
{
|
||||
public ItemStack origin;
|
||||
protected ItemStack[] contents;
|
||||
private Container handler;
|
||||
private String contentName;
|
||||
|
||||
public ItemInventory(int size)
|
||||
{
|
||||
this.contents = new ItemStack[size];
|
||||
this.contentName = "contents";
|
||||
}
|
||||
|
||||
public ItemInventory(int size, ItemStack stack)
|
||||
{
|
||||
this(size);
|
||||
this.origin = stack;
|
||||
|
||||
|
||||
readFromNBT(stack.getTagCompound());
|
||||
}
|
||||
|
||||
public ItemInventory(int size, ItemStack stack, String name)
|
||||
{
|
||||
this(size);
|
||||
this.origin = stack;
|
||||
|
||||
this.contentName = name;
|
||||
|
||||
if (this.contentName == null) {
|
||||
this.contentName = "contents";
|
||||
}
|
||||
readFromNBT(stack.getTagCompound());
|
||||
}
|
||||
|
||||
public void onGuiSaved(EntityPlayer player)
|
||||
{
|
||||
this.origin = findOrigin(player);
|
||||
if (this.origin != null) {
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ItemStack findOrigin(EntityPlayer player)
|
||||
{
|
||||
if (this.origin == null) {
|
||||
return null;
|
||||
}
|
||||
NBTTagCompound comp = this.origin.getTagCompound();
|
||||
if (comp == null) {
|
||||
return null;
|
||||
}
|
||||
int id = comp.getInteger("ID");
|
||||
|
||||
|
||||
for (int i = 0; i < player.inventory.getSizeInventory(); i++)
|
||||
{
|
||||
if (player.inventory.getStackInSlot(i) != null)
|
||||
{
|
||||
NBTTagCompound playerComp = player.inventory.getStackInSlot(i).getTagCompound();
|
||||
if (playerComp != null)
|
||||
{
|
||||
if (id == playerComp.getInteger("ID")) {
|
||||
return player.inventory.getStackInSlot(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (player.inventory.getItemStack() != null)
|
||||
{
|
||||
NBTTagCompound playerComp = player.inventory.getItemStack().getTagCompound();
|
||||
if ((playerComp != null) && (id == playerComp.getInteger("ID"))) {
|
||||
return player.inventory.getItemStack();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean matchesID(int secondID)
|
||||
{
|
||||
if (this.origin == null) {
|
||||
return false;
|
||||
}
|
||||
NBTTagCompound comp = this.origin.getTagCompound();
|
||||
if (comp == null) {
|
||||
return false;
|
||||
}
|
||||
int id = comp.getInteger("ID");
|
||||
return id == secondID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void save()
|
||||
{
|
||||
if (this.origin == null) {
|
||||
return;
|
||||
}
|
||||
NBTTagCompound comp = this.origin.getTagCompound();
|
||||
if (comp == null)
|
||||
comp = new NBTTagCompound();
|
||||
writeToNBT(comp);
|
||||
this.origin.setTagCompound(comp);
|
||||
}
|
||||
|
||||
|
||||
public void readFromNBT(NBTTagCompound comp)
|
||||
{
|
||||
if ((comp == null) || (this.contentName == null))
|
||||
return;
|
||||
if (comp.hasKey(this.contentName))
|
||||
{
|
||||
NBTTagList contentList = comp.getTagList(this.contentName, 10);
|
||||
this.contents = new ItemStack[getSizeInventory()];
|
||||
for (int i = 0; i < contentList.tagCount(); i++)
|
||||
{
|
||||
NBTTagCompound tempComp = contentList.getCompoundTagAt(i);
|
||||
byte slotByte = tempComp.getByte("Slot");
|
||||
|
||||
if ((slotByte >= 0) && (slotByte < this.contents.length))
|
||||
{
|
||||
this.contents[slotByte] = ItemStack.loadItemStackFromNBT(tempComp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void writeToNBT(NBTTagCompound comp)
|
||||
{
|
||||
if (this.contentName == null) {
|
||||
this.contentName = "contents";
|
||||
}
|
||||
NBTTagList contentList = new NBTTagList();
|
||||
for (int i = 0; i < this.contents.length; i++)
|
||||
{
|
||||
if (this.contents[i] != null)
|
||||
{
|
||||
NBTTagCompound tempComp = new NBTTagCompound();
|
||||
tempComp.setByte("Slot", (byte)i);
|
||||
|
||||
this.contents[i].writeToNBT(tempComp);
|
||||
contentList.appendTag(tempComp);
|
||||
}
|
||||
|
||||
comp.setTag(this.contentName, contentList);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setCraftingListener(Container container)
|
||||
{
|
||||
this.handler = container;
|
||||
}
|
||||
|
||||
public ItemStack decrStackSize(int i, int j)
|
||||
{
|
||||
if (this.contents[i] == null) {
|
||||
return null;
|
||||
}
|
||||
if (this.contents[i].stackSize <= j)
|
||||
{
|
||||
ItemStack product = this.contents[i];
|
||||
this.contents[i] = null;
|
||||
markDirty();
|
||||
return product;
|
||||
}
|
||||
|
||||
ItemStack product = this.contents[i].splitStack(j);
|
||||
|
||||
if (this.contents[i].stackSize == 0) {
|
||||
this.contents[i] = null;
|
||||
}
|
||||
markDirty();
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
public ItemStack getStackInSlotOnClosing(int slot)
|
||||
{
|
||||
if (this.contents[slot] == null)
|
||||
return null;
|
||||
ItemStack returnVal = this.contents[slot];
|
||||
this.contents[slot] = null;
|
||||
|
||||
markDirty();
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
|
||||
public void setInventorySlotContents(int index, ItemStack stack)
|
||||
{
|
||||
this.contents[index] = stack;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
|
||||
public int getFirstEmptyStack()
|
||||
{
|
||||
for (int i = 0; i < this.contents.length; i++) {
|
||||
if (this.contents[i] == null)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public int getFreeSlots()
|
||||
{
|
||||
int free = 0;
|
||||
|
||||
if ((this.contents != null) && (this.contents.length > 0)) {
|
||||
for (ItemStack checkStack : this.contents)
|
||||
if (checkStack == null)
|
||||
free++;
|
||||
}
|
||||
return free;
|
||||
}
|
||||
|
||||
|
||||
private int storePartialItemStack(ItemStack stack)
|
||||
{
|
||||
Item i = stack.getItem();
|
||||
int j = stack.stackSize;
|
||||
|
||||
|
||||
if (stack.getMaxStackSize() == 1)
|
||||
{
|
||||
int k = getFirstEmptyStack();
|
||||
|
||||
if (k < 0) {
|
||||
return j;
|
||||
}
|
||||
|
||||
if (this.contents[k] == null) {
|
||||
this.contents[k] = ItemStack.copyItemStack(stack);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int k = storeItemStack(stack);
|
||||
|
||||
if (k < 0) {
|
||||
k = getFirstEmptyStack();
|
||||
}
|
||||
if (k < 0) {
|
||||
return j;
|
||||
}
|
||||
|
||||
if (this.contents[k] == null)
|
||||
{
|
||||
this.contents[k] = new ItemStack(i, 0, stack.getItemDamage());
|
||||
|
||||
if (stack.hasTagCompound()) {
|
||||
this.contents[k].setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
|
||||
}
|
||||
}
|
||||
int l = j;
|
||||
|
||||
if (j > this.contents[k].getMaxStackSize() - this.contents[k].stackSize) {
|
||||
l = this.contents[k].getMaxStackSize() - this.contents[k].stackSize;
|
||||
}
|
||||
if (l > getInventoryStackLimit() - this.contents[k].stackSize) {
|
||||
l = getInventoryStackLimit() - this.contents[k].stackSize;
|
||||
}
|
||||
if (l == 0) {
|
||||
return j;
|
||||
}
|
||||
|
||||
j -= l;
|
||||
this.contents[k].stackSize += l;
|
||||
this.contents[k].animationsToGo = 5;
|
||||
return j;
|
||||
}
|
||||
|
||||
public int storeItemStack(ItemStack stack)
|
||||
{
|
||||
for (int i = 0; i < this.contents.length; i++) {
|
||||
if ((this.contents[i] != null) && (this.contents[i].getItem() == stack.getItem()) && (this.contents[i].isStackable()) && (this.contents[i].stackSize < this.contents[i].getMaxStackSize()) && (this.contents[i].stackSize < getInventoryStackLimit()) && ((!this.contents[i].getHasSubtypes()) || (this.contents[i].getItemDamage() == stack.getItemDamage())) && (ItemStack.areItemStackTagsEqual(this.contents[i], stack)))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public boolean addItemToInventory(ItemStack stack)
|
||||
{
|
||||
if (stack == null) {
|
||||
return false;
|
||||
}
|
||||
if (stack.isItemDamaged())
|
||||
{
|
||||
int i = getFirstEmptyStack();
|
||||
|
||||
if (i >= 0)
|
||||
{
|
||||
this.contents[i] = ItemStack.copyItemStack(stack);
|
||||
this.contents[i].animationsToGo = 5;
|
||||
stack.stackSize = 0;
|
||||
markDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int i;
|
||||
do
|
||||
{
|
||||
i = stack.stackSize;
|
||||
stack.stackSize = storePartialItemStack(stack);
|
||||
|
||||
}
|
||||
while ((stack.stackSize > 0) && (stack.stackSize < i));
|
||||
|
||||
return stack.stackSize < i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ItemStack getStackInSlot(int index)
|
||||
{
|
||||
return this.contents[index];
|
||||
}
|
||||
|
||||
public int getSizeInventory() { return this.contents.length; }
|
||||
|
||||
public String getInventoryName() {
|
||||
return "inventory.simulated";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomInventoryName() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInventoryStackLimit() { return 64; }
|
||||
|
||||
public void markDirty()
|
||||
{
|
||||
if (this.handler != null) {
|
||||
this.handler.onCraftMatrixChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUseableByPlayer(EntityPlayer player)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openInventory() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeInventory() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -5,8 +5,10 @@ import cpw.mods.fml.relauncher.SideOnly;
|
|||
import ley.modding.dartcraft.Dartcraft;
|
||||
import ley.modding.dartcraft.api.IBreakable;
|
||||
import ley.modding.dartcraft.api.IForceConsumer;
|
||||
import ley.modding.dartcraft.api.upgrades.IForceUpgradable;
|
||||
import ley.modding.dartcraft.item.DartItems;
|
||||
import ley.modding.dartcraft.util.ForceConsumerUtils;
|
||||
import ley.modding.dartcraft.util.ForceUpgradeManager;
|
||||
import ley.modding.dartcraft.util.Util;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
@ -18,7 +20,7 @@ import net.minecraft.util.IIcon;
|
|||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.EnumHelper;
|
||||
|
||||
public class ItemForcePickaxe extends ItemPickaxe implements IBreakable, IForceConsumer {
|
||||
public class ItemForcePickaxe extends ItemPickaxe implements IBreakable, IForceConsumer, IForceUpgradable {
|
||||
|
||||
private static int damage = 1;
|
||||
private static float efficiency = 5.0F;
|
||||
|
@ -108,11 +110,17 @@ public class ItemForcePickaxe extends ItemPickaxe implements IBreakable, IForceC
|
|||
|
||||
@Override
|
||||
public boolean useForce(ItemStack stack, int var2, boolean var3) {
|
||||
return false;
|
||||
return ForceConsumerUtils.useForce(stack, var2, var3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean attemptRepair(ItemStack stack) {
|
||||
return ForceConsumerUtils.attemptRepair(stack);
|
||||
}
|
||||
|
||||
public int[] validUpgrades()
|
||||
{
|
||||
return new int[] { ForceUpgradeManager.HEAT.getID(), ForceUpgradeManager.SPEED.getID(), ForceUpgradeManager.LUCK.getID(), ForceUpgradeManager.TOUCH.getID(), ForceUpgradeManager.STURDY.getID(), ForceUpgradeManager.GRINDING.getID(), ForceUpgradeManager.REPAIR.getID(), ForceUpgradeManager.IMPERVIOUS.getID() };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
package ley.modding.dartcraft.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import ley.modding.dartcraft.Dartcraft;
|
||||
import ley.modding.dartcraft.Config;
|
||||
import ley.modding.dartcraft.api.IForceConsumer;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import ley.modding.dartcraft.api.inventory.ItemInventory;
|
||||
import ley.modding.dartcraft.item.DartItems;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ForceConsumerUtils {
|
||||
|
||||
|
@ -49,4 +54,62 @@ public class ForceConsumerUtils {
|
|||
|
||||
return repaired;
|
||||
}
|
||||
|
||||
public static boolean useForce(ItemStack stack, int amount, boolean use)
|
||||
{
|
||||
if ((stack == null) || (!stack.hasTagCompound()) || (amount < 0) || (stack.getItem() == null) || (!(stack.getItem() instanceof IForceConsumer)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
boolean canUse = stack.getTagCompound().getInteger("storedForce") >= amount;
|
||||
|
||||
try
|
||||
{
|
||||
if (use)
|
||||
{
|
||||
stack.getTagCompound().setInteger("storedForce", stack.getTagCompound().getInteger("storedForce") - amount);
|
||||
|
||||
|
||||
ItemInventory inv = new ItemInventory(1, stack, "consumerContents");
|
||||
IForceConsumer consumer = (IForceConsumer)stack.getItem();
|
||||
ItemStack invStack = inv.getStackInSlot(0);
|
||||
|
||||
int defecit = consumer.getMaxStored(stack) - stack.getTagCompound().getInteger("storedForce");
|
||||
|
||||
|
||||
if ((defecit > 0) && (invStack != null) && (invStack.stackSize > 0))
|
||||
{
|
||||
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(new ItemStack(invStack.getItem(), 1, invStack.getItemDamage()));
|
||||
|
||||
|
||||
if ((liquid == null) && (invStack.getItem() == DartItems.forcegem)) {
|
||||
liquid = new FluidStack(FluidRegistry.getFluid("liquidforce"), (int)(1000.0F * Config.gemValue));
|
||||
}
|
||||
|
||||
if ((liquid != null) && (liquid.getFluid().getName().equalsIgnoreCase("liquidforce")))
|
||||
{
|
||||
|
||||
while ((defecit >= liquid.amount) && (invStack.stackSize > 0))
|
||||
{
|
||||
stack.getTagCompound().setInteger("storedForce", stack.getTagCompound().getInteger("storedForce") + liquid.amount);
|
||||
invStack.stackSize -= 1;
|
||||
if (invStack.stackSize <= 0) {
|
||||
inv.setInventorySlotContents(0, invStack.getItem().hasContainerItem() ? new ItemStack(invStack.getItem().getContainerItem()) : (ItemStack)null);
|
||||
}
|
||||
inv.save();
|
||||
defecit = consumer.getMaxStored(stack) - stack.getTagCompound().getInteger("storedForce");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.getLogger("DartCraft").info("There was a problem in IForceConsumer implementation.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return canUse;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue