Cleaned up PartCharger, changed it to Extend PartInventoryPanel, Fixed dupe bug caused by failure to check for doReceive boolean, supports automation

This commit is contained in:
DarkGuardsman 2014-04-16 08:13:42 -04:00 committed by Robert S
parent 4fd8620f2f
commit 6c03ffa571

View file

@ -4,55 +4,28 @@ import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.core.prefab.part.PartFace;
import resonantinduction.core.prefab.part.PartInventoryPanel;
import resonantinduction.electrical.Electrical;
import universalelectricity.api.CompatibilityModule;
import universalelectricity.api.UniversalClass;
import universalelectricity.api.energy.IEnergyInterface;
import calclavia.lib.utility.WrenchUtility;
import calclavia.lib.utility.inventory.ExternalInventory;
import calclavia.lib.utility.inventory.IExternalInventory;
import calclavia.lib.utility.inventory.IExternalInventoryBox;
import calclavia.lib.utility.inventory.InventoryUtility;
import codechicken.lib.data.MCDataInput;
import codechicken.lib.data.MCDataOutput;
import codechicken.lib.vec.Vector3;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* TODO: This DOES NOT WORK when @UniversalClass is not annotated. Flat wires seem to only interact
* with TE multiparts.
/** Micro part machine designed to charge all items contained inside of it. Doesn't contain its own
* battery and only acts as an inventory. Items are charged each time doReceive is called by an
* energy supplier.
*
* @author Calclavia
*
*/
* @author Darkguardsman, converted to Part by Calclavia */
@UniversalClass
public class PartCharger extends PartFace implements IExternalInventory, ISidedInventory, IEnergyInterface
public class PartCharger extends PartInventoryPanel implements IEnergyInterface
{
private long lastPacket;
@Override
public void readDesc(MCDataInput packet)
{
super.readDesc(packet);
getInventory().load(packet.readNBTTagCompound());
}
@Override
public void writeDesc(MCDataOutput packet)
{
super.writeDesc(packet);
NBTTagCompound nbt = new NBTTagCompound();
getInventory().save(nbt);
packet.writeNBTTagCompound(nbt);
}
@Override
public boolean activate(EntityPlayer player, MovingObjectPosition part, ItemStack item)
{
@ -97,30 +70,29 @@ public class PartCharger extends PartFace implements IExternalInventory, ISidedI
@Override
public boolean canConnect(ForgeDirection direction, Object obj)
{
return obj instanceof IEnergyInterface;
return obj instanceof IEnergyInterface && placementSide != direction.getOpposite();
}
@Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
long energyUsed = 0;
long energyLeft = receive;
for (int i = 0; i < this.getSizeInventory(); i++)
if (receive > 0)
{
long input = CompatibilityModule.chargeItem(this.getStackInSlot(i), energyLeft, true);
energyUsed += input;
energyLeft -= input;
if (energyLeft <= 0)
long energyUsed = 0;
for (int slot = 0; slot < this.getSizeInventory(); slot++)
{
energyUsed += CompatibilityModule.chargeItem(this.getStackInSlot(slot), receive - energyUsed, doReceive);
if (energyUsed >= receive)
break;
}
if (!world().isRemote && energyUsed > 0 && System.currentTimeMillis() - this.lastPacket >= 50)
{
this.lastPacket = System.currentTimeMillis();
sendDescUpdate();
}
if (energyUsed > 0)
this.markedForUpdate = true;
return energyUsed;
}
return 0;
}
@Override
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
@ -163,166 +135,9 @@ public class PartCharger extends PartFace implements IExternalInventory, ISidedI
return drops;
}
/**
* Save and load
*/
@Override
public void load(NBTTagCompound nbt)
{
super.load(nbt);
this.getInventory().load(nbt);
}
@Override
public void save(NBTTagCompound nbt)
{
super.save(nbt);
this.getInventory().save(nbt);
}
/**
* Inventory Methods
*/
protected IExternalInventoryBox inventory;
protected int maxSlots = 1;
@Override
public IExternalInventoryBox getInventory()
{
if (inventory == null)
{
inventory = new ExternalInventory(null, this, this.maxSlots);
}
return inventory;
}
@Override
public int getSizeInventory()
{
return this.getInventory().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i)
{
return this.getInventory().getStackInSlot(i);
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return this.getInventory().decrStackSize(i, j);
}
public void incrStackSize(int slot, ItemStack itemStack)
{
if (this.getStackInSlot(slot) == null)
{
setInventorySlotContents(slot, itemStack.copy());
}
else if (this.getStackInSlot(slot).isItemEqual(itemStack))
{
getStackInSlot(slot).stackSize += itemStack.stackSize;
}
onInventoryChanged();
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return this.getInventory().getStackInSlotOnClosing(i);
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.getInventory().setInventorySlotContents(i, itemstack);
}
@Override
public String getInvName()
{
return "";
}
@Override
public boolean isInvNameLocalized()
{
return true;
}
@Override
public int getInventoryStackLimit()
{
return this.getInventory().getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.getInventory().isUseableByPlayer(entityplayer);
}
@Override
public void openChest()
{
this.getInventory().openChest();
}
@Override
public void closeChest()
{
this.getInventory().closeChest();
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return this.getInventory().isItemValidForSlot(i, itemstack);
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
return this.getInventory().getAccessibleSlotsFromSide(var1);
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canInsertItem(i, itemstack, j);
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canExtractItem(i, itemstack, j);
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{
return false;
return slot < this.getSizeInventory() && stack != null && CompatibilityModule.isHandler(stack.getItem());
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
if (slot >= this.getSizeInventory())
{
return false;
}
return true;
}
@Override
public void onInventoryChanged()
{
}
}