Beginning work on inventory management
This commit is contained in:
parent
b4028d8fcb
commit
26393f8d72
4 changed files with 161 additions and 9 deletions
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
|
@ -8,6 +8,11 @@ import java.util.Set;
|
|||
|
||||
public class SetUtil
|
||||
{
|
||||
/**
|
||||
* Returns a new set with the reverse of the provided set's order.
|
||||
* @param set - set to perform the operation on
|
||||
* @return new set with the previous set's order reversed
|
||||
*/
|
||||
public static <V> Set<V> inverse(Set<V> set)
|
||||
{
|
||||
Set<V> toReturn = new HashSet<V>();
|
||||
|
@ -21,13 +26,19 @@ public class SetUtil
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the provided set, removing all objects after the defined cap index.
|
||||
* @param set - set to perform the operation on
|
||||
* @param cap - maximum amount of objects this set can store
|
||||
* @return new set with previous set's objects other than those after the cap
|
||||
*/
|
||||
public static <V> Set<V> cap(Set<V> set, int cap)
|
||||
{
|
||||
Set<V> toReturn = new HashSet<V>();
|
||||
|
||||
if(set.size() <= cap)
|
||||
{
|
||||
toReturn = set;
|
||||
toReturn = copy(set);
|
||||
}
|
||||
else {
|
||||
int count = 0;
|
||||
|
@ -48,6 +59,24 @@ public class SetUtil
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
public static <V> Set<V> copy(Set<V> set)
|
||||
{
|
||||
Set<V> toReturn = new HashSet<V>();
|
||||
|
||||
for(V obj : set)
|
||||
{
|
||||
toReturn.add(obj);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new set created by merging two others together.
|
||||
* @param setOne - a set to be used in the merge
|
||||
* @param setTwo - a set to be used in the merge
|
||||
* @return new merged set
|
||||
*/
|
||||
public static <V> Set<V> merge(Set<V> setOne, Set<V> setTwo)
|
||||
{
|
||||
Set<V> newSet = new HashSet<V>();
|
||||
|
@ -65,6 +94,12 @@ public class SetUtil
|
|||
return newSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a set into the defined amount of new sets, and returns them in an ArrayList.
|
||||
* @param set - set to split
|
||||
* @param divide - how many new sets should be created
|
||||
* @return sets split by the defined amount
|
||||
*/
|
||||
public static <V> ArrayList<Set<V>> split(Set<V> set, int divide)
|
||||
{
|
||||
int remain = set.size()%divide;
|
||||
|
@ -110,6 +145,11 @@ public class SetUtil
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of a Set, while maintaining all it's objects.
|
||||
* @param set - set to turn into a list
|
||||
* @return list with defined set's objects
|
||||
*/
|
||||
public static <V> List<V> asList(Set<V> set)
|
||||
{
|
||||
return (List<V>)Arrays.asList(set.toArray());
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import resonantinduction.api.IBattery;
|
||||
import resonantinduction.base.SetUtil;
|
||||
import resonantinduction.base.Vector3;
|
||||
|
||||
public class SynchronizedBatteryData
|
||||
|
@ -12,6 +14,8 @@ public class SynchronizedBatteryData
|
|||
|
||||
public Set<ItemStack> inventory = new HashSet<ItemStack>();
|
||||
|
||||
public ItemStack[] visibleInventory = new ItemStack[3];
|
||||
|
||||
public int length;
|
||||
|
||||
public int width;
|
||||
|
@ -32,6 +36,37 @@ public class SynchronizedBatteryData
|
|||
return getVolume()*BatteryManager.CELLS_PER_BATTERY;
|
||||
}
|
||||
|
||||
public void sortInventory()
|
||||
{
|
||||
ItemStack[] toSort = (ItemStack[])SetUtil.copy(inventory).toArray();
|
||||
|
||||
boolean cont = true;
|
||||
ItemStack temp;
|
||||
|
||||
while(cont)
|
||||
{
|
||||
cont = false;
|
||||
|
||||
for(int j = 0; j < toSort.length-1; j++)
|
||||
{
|
||||
if(((IBattery)toSort[j].getItem()).getEnergyStored(toSort[j]) < ((IBattery)toSort[j+1].getItem()).getEnergyStored(toSort[j+1]))
|
||||
{
|
||||
temp = toSort[j];
|
||||
toSort[j] = toSort[j+1];
|
||||
toSort[j+1] = temp;
|
||||
cont = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inventory = new HashSet<ItemStack>();
|
||||
|
||||
for(ItemStack itemStack : toSort)
|
||||
{
|
||||
inventory.add(itemStack);
|
||||
}
|
||||
}
|
||||
|
||||
public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, Set<ItemStack> inventory)
|
||||
{
|
||||
SynchronizedBatteryData structure = getBase(tileEntity);
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
@ -24,14 +25,15 @@ import com.google.common.io.ByteArrayDataInput;
|
|||
*
|
||||
* @author AidanBrady
|
||||
*/
|
||||
public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
||||
public class TileEntityBattery extends TileEntityBase implements IPacketReceiver, IInventory
|
||||
{
|
||||
public Set<ItemStack> cachedInventory = new HashSet<ItemStack>();
|
||||
|
||||
public SynchronizedBatteryData structure = SynchronizedBatteryData.getBase(this);
|
||||
|
||||
public SynchronizedBatteryData prevStructure;
|
||||
|
||||
public float clientEnergy;
|
||||
public int clientCells;
|
||||
|
||||
@Override
|
||||
public void updateEntity()
|
||||
{
|
||||
|
@ -46,6 +48,15 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
update();
|
||||
}
|
||||
|
||||
if(structure.visibleInventory[0] != null)
|
||||
{
|
||||
if(structure.inventory.size() < structure.getMaxCells())
|
||||
{
|
||||
structure.inventory.add(structure.visibleInventory[0]);
|
||||
structure.sortInventory();
|
||||
}
|
||||
}
|
||||
|
||||
if(prevStructure != structure)
|
||||
{
|
||||
for(EntityPlayer player : playersUsing)
|
||||
|
@ -71,13 +82,13 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
super.readFromNBT(nbtTags);
|
||||
|
||||
NBTTagList tagList = nbtTags.getTagList("Items");
|
||||
cachedInventory = new HashSet<ItemStack>();
|
||||
structure.inventory = new HashSet<ItemStack>();
|
||||
|
||||
for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
|
||||
{
|
||||
NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount);
|
||||
|
||||
cachedInventory.add(ItemStack.loadItemStackFromNBT(tagCompound));
|
||||
structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,7 +99,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
|
||||
for(ItemStack itemStack : cachedInventory)
|
||||
for(ItemStack itemStack : structure.inventory)
|
||||
{
|
||||
if(itemStack != null)
|
||||
{
|
||||
|
@ -137,7 +148,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
{
|
||||
float max = 0;
|
||||
|
||||
for (ItemStack itemStack : cachedInventory)
|
||||
for (ItemStack itemStack : structure.inventory)
|
||||
{
|
||||
if (itemStack != null)
|
||||
{
|
||||
|
@ -155,7 +166,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
{
|
||||
float energy = 0;
|
||||
|
||||
for (ItemStack itemStack : cachedInventory)
|
||||
for (ItemStack itemStack : structure.inventory)
|
||||
{
|
||||
if (itemStack != null)
|
||||
{
|
||||
|
@ -198,4 +209,70 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
|
|||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSizeInventory()
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int i)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int i, int j)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlotOnClosing(int i)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int i, ItemStack itemstack)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInvName()
|
||||
{
|
||||
return "Battery";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInvNameLocalized()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseableByPlayer(EntityPlayer entityplayer)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openChest() {}
|
||||
|
||||
@Override
|
||||
public void closeChest() {}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue