diff --git a/Utilities/Collections/ItemBaseCollectionUtils.cs b/Utilities/Collections/ItemBaseCollectionUtils.cs
index 97faa1c..46f69a5 100644
--- a/Utilities/Collections/ItemBaseCollectionUtils.cs
+++ b/Utilities/Collections/ItemBaseCollectionUtils.cs
@@ -1,66 +1,330 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-
-namespace FortressCraft.Community.Utilities
-{
- ///
- /// Extension Methods and Helpers for the ItemBase Class
- ///
- public static partial class ItemBaseUtil
- {
- ///
- /// Gets the amount of items and cubes in any type of Enumerable ItemBase
- ///
- /// The list of items to get the total count from
- /// The amount of Items and Cubes
- public static Int32 GetItemCount(this IEnumerable items)
- {
- return items.Sum(itemBase => itemBase.GetAmount());
- }
-
- ///
- /// Gets the amount of items in any type of Enumerable ItemBase
- ///
- /// The list of items to get the total count from
- /// The unique id of the item to count
- /// The amount of Items
- public static Int32 GetItemCount(this IEnumerable items, Int32 itemId)
- {
- return items.Where(item => item.mnItemID == itemId).Sum(item => item.GetAmount());
- }
-
- ///
- /// Gets the amount of cubes in any type of Enumerable ItemBase
- ///
- ///
- ///
- ///
- /// The amount of Cubes
- public static Int32 GetItemCount(this IEnumerable items, UInt16 cubeId, UInt16 cubeValue)
- {
- return items.Where(item =>
- {
- var cube = item.As();
- if (cube == null)
- return false;
- return cube.mCubeType == cubeId && cube.mCubeValue == cubeValue;
- }).Sum(item => item.GetAmount());
- }
-
- ///
- /// Gets the amount of cubes OR items from any Enumerable ItemBase, based off of an ItemBase
- ///
- /// The list of items to get the total count from
- /// The ItemBase which to restrain the Count to
- /// The amount of Cubes or Items
- public static Int32 GetItemCount(this IEnumerable items, ItemBase restraints)
- {
- if (restraints.mType != ItemType.ItemCubeStack)
- return items.GetItemCount(restraints.mnItemID);
-
- var cube = restraints.As();
- return items.GetItemCount(cube.mCubeType, cube.mCubeValue);
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using UnityEngine;
+
+namespace FortressCraft.Community.Utilities
+{
+ ///
+ /// Extension Methods and Helpers for the ItemBase Class
+ ///
+ public static partial class ItemBaseUtil
+ {
+ ///
+ /// Gets the amount of items and cubes in any type of Enumerable ItemBase
+ ///
+ /// The list of items to get the total count from
+ /// The amount of Items and Cubes
+ public static Int32 GetItemCount(this IEnumerable items)
+ {
+ return items.Sum(itemBase => itemBase.GetAmount());
+ }
+
+ ///
+ /// Gets the amount of items in any type of Enumerable ItemBase
+ ///
+ /// The list of items to get the total count from
+ /// The unique id of the item to count
+ /// The amount of Items
+ public static Int32 GetItemCount(this IEnumerable items, Int32 itemId)
+ {
+ return items.Where(item => item.mnItemID == itemId).Sum(item => item.GetAmount());
+ }
+
+ ///
+ /// Gets the amount of cubes in any type of Enumerable ItemBase
+ ///
+ ///
+ ///
+ ///
+ /// The amount of Cubes
+ public static Int32 GetItemCount(this IEnumerable items, UInt16 cubeId, UInt16 cubeValue)
+ {
+ return items.Where(item =>
+ {
+ var cube = item.As();
+ if (cube == null)
+ return false;
+ return cube.mCubeType == cubeId && cube.mCubeValue == cubeValue;
+ }).Sum(item => item.GetAmount());
+ }
+
+ ///
+ /// Gets the amount of cubes OR items from any Enumerable ItemBase, based off of an ItemBase
+ ///
+ /// The list of items to get the total count from
+ /// The ItemBase which to restrain the Count to
+ /// The amount of Cubes or Items
+ public static Int32 GetItemCount(this IEnumerable items, ItemBase restraints)
+ {
+ if (restraints.mType != ItemType.ItemCubeStack)
+ return items.GetItemCount(restraints.mnItemID);
+
+ var cube = restraints.As();
+ return items.GetItemCount(cube.mCubeType, cube.mCubeValue);
+ }
+
+
+ ///
+ /// Adds an ItemBase to a list, consolidating stacks - obeys list storage capacity by returning excess items
+ ///
+ /// The item to add to the list
+ /// The list to transfer it to
+ /// If true returns partial stack when insufficient stack size found
+ /// The list's storage capacity
+ /// The remaining items that do not fit in the list
+ public static ItemBase AddListItem(this ItemBase item, ref List targetlist, bool returnpartialstack, int storagecapacity = int.MaxValue)
+ {
+ if (item == null)
+ {
+ Debug.LogError("AddListItem attempted to add a null item!");
+ return null;
+ }
+ List workingtarget = targetlist.ToList();
+ int remainder = 0;
+ int listcount = workingtarget.GetItemCount();
+ int itemcount = item.GetAmount();
+ int newtotal = listcount + itemcount;
+ ItemBase returnitem = null;
+ if (newtotal > storagecapacity)
+ {
+ remainder = newtotal - storagecapacity;
+ itemcount -= remainder;
+ item.SetAmount(itemcount);
+ returnitem = NewInstance(item);
+ returnitem.SetAmount(remainder);
+ }
+ int listlength = workingtarget.Count;
+ for (int index = 0; index <= listlength; index++)
+ {
+ if (index != listlength && item.IsStack())
+ {
+ if (workingtarget[index].Compare(item))
+ {
+ workingtarget[index].IncrementStack(itemcount);
+ item = null;
+ break;
+ }
+ }
+ else
+ {
+ workingtarget.Add(NewInstance(item));
+ item = null;
+ break;
+ }
+ }
+ targetlist = workingtarget;
+ return returnitem;
+ }
+
+ ///
+ /// Deduct an item from an item list by example
+ ///
+ /// The example item to attempt to remove from the list
+ /// The list to take the item from
+ /// If true returns partial stack when insufficient stack size found
+ /// The ItemBase object or null if unavailable
+ public static ItemBase RemoveListItem(this ItemBase item, ref List sourcelist, bool returnpartialstack)
+ {
+ if (item == null)
+ {
+ Debug.LogError("RemoveListItem attempted to remove a null item from a list");
+ return null;
+ }
+ List workingsource = sourcelist.ToList();
+ if (item.IsStack())
+ {
+ int itemcount = item.GetAmount();
+ int listitemcount;
+
+ for (int index = 0; index < workingsource.Count; index++)
+ {
+ ItemBase i = workingsource[index];
+ listitemcount = i.GetAmount();
+ if (i.Compare(item) && listitemcount > itemcount)
+ {
+ i.DecrementStack(itemcount);
+ sourcelist = workingsource;
+ return NewInstance(item);
+ }
+ else if (i.Compare(item) && listitemcount == itemcount)
+ {
+ workingsource.Remove(i);
+ sourcelist = workingsource;
+ return NewInstance(item);
+ }
+ else if (returnpartialstack)
+ {
+ item.SetAmount(listitemcount);
+ sourcelist = workingsource;
+ return NewInstance(item);
+ }
+ }
+ }
+ else
+ for (int index = 0; index < workingsource.Count; index++)
+ {
+ ItemBase i = workingsource[index];
+ if (i.Compare(item))
+ {
+ workingsource.Remove(i);
+ sourcelist = workingsource;
+ return i;
+ }
+ }
+ sourcelist = workingsource;
+ return null;
+ }
+
+ ///
+ /// Moves specified amount of any items from one list to another obeying target storage capacity.
+ ///
+ /// Source list of items
+ /// Target list of items
+ /// Quantity of items to move
+ /// Storage capacity of target item list
+ /// Moves only the first item found
+ /// A list of items to server as a whitelist or blacklist for transferring
+ /// True if whitelist otherwise treat as a blacklist
+ public static void MoveItems(ref List sourcelist, ref List targetlist, int amount = 1, int StorageCapacity = int.MaxValue, bool takefirstitem = false, IEnumerable whiteblacklist = null, bool iswhitelist = true)
+ {
+ int listcount;
+ int freespace;
+ List workingsource = sourcelist.ToList();
+ List workingtarget = targetlist.ToList();
+ List filter = new List();
+ if (whiteblacklist != null)
+ filter = whiteblacklist.ToList();
+ int filtercount = filter.Count;
+ bool matchfound = false;
+
+ if (amount <= 0)
+ {
+ return;
+ }
+ for (int index = 0; index < workingsource.Count; index++)
+ {
+ listcount = workingsource[index].GetAmount();
+ freespace = StorageCapacity - workingtarget.GetItemCount();
+ ItemBase i = workingsource[index];
+
+ if (filtercount != 0)
+ {
+ for (int index2 = 0; index2 < filtercount; index2++)
+ {
+ ItemBase j = filter[index2];
+ if (j == null)
+ {
+ Debug.LogError("MoveItems attempted to filter by null item");
+ return;
+ }
+ matchfound = (i.Compare(j));
+ if (matchfound)
+ break;
+ }
+ //XOR to skip the continue otherwise white/black list violation so go to next item
+ if (matchfound ^ iswhitelist)
+ continue;
+ }
+
+ if (i.IsStack())
+ {
+ if (listcount > amount)
+ {
+ if (amount > freespace)
+ {
+ AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false);
+ i.DecrementStack(freespace);
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ return;
+ }
+ else
+ {
+ AddListItem(NewInstance(i).SetAmount(amount), ref workingtarget, false);
+ i.DecrementStack(amount);
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ return;
+ }
+ }
+ else if (listcount == amount)
+ {
+ if (amount > freespace)
+ {
+ AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false);
+ workingsource.Remove(i);
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ return;
+ }
+ else
+ {
+ AddListItem(NewInstance(i).SetAmount(amount), ref workingtarget, false);
+ workingsource.Remove(i);
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ return;
+ }
+ }
+ else
+ {
+ if (listcount > freespace)
+ {
+ AddListItem(NewInstance(i).SetAmount(freespace), ref workingtarget, false);
+ workingsource.Remove(i);
+ amount -= listcount;
+ }
+ else
+ {
+ AddListItem(NewInstance(i).SetAmount(listcount), ref workingtarget, false);
+ workingsource.Remove(i);
+ amount -= listcount;
+ }
+ }
+ }
+ else
+ {
+ if (freespace > 0)
+ {
+ AddListItem(NewInstance(i), ref workingtarget, false);
+ workingsource.Remove(i);
+ amount--;
+ }
+ else
+ {
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ return;
+ }
+ }
+ if (takefirstitem)
+ break;
+ }
+ sourcelist = workingsource;
+ targetlist = workingtarget;
+ }
+
+ ///
+ /// Moves specified amount of any items from one list to another obeying target storage capacity.
+ ///
+ /// Source item to move
+ /// Target list of items
+ /// Quantity of items to move (for stacks)
+ /// Storage capacity of target item list
+ /// Moves only the first item found
+ /// A list of items to server as a whitelist or blacklist for transferring
+ /// True if whitelist otherwise treat as a blacklist
+ public static void MoveItems(this ItemBase sourceitem, ref List targetlist, int amount = 1, int StorageCapacity = int.MaxValue, bool takefirstitem = false, IEnumerable whiteblacklist = null, bool iswhitelist = true)
+ {
+ if (sourceitem == null)
+ {
+ Debug.LogError("MoveItems attempted to move a null item!");
+ return;
+ }
+ List sourcelist = new List();
+ sourcelist.Add(sourceitem);
+ MoveItems(ref sourcelist, ref targetlist, amount, StorageCapacity, takefirstitem, whiteblacklist, iswhitelist);
+ }
+ }
+}
diff --git a/Utilities/ItemBaseUtil.cs b/Utilities/ItemBaseUtil.cs
index 4b6f2bb..308af4a 100644
--- a/Utilities/ItemBaseUtil.cs
+++ b/Utilities/ItemBaseUtil.cs
@@ -183,14 +183,15 @@ public static void DecrementStack(this ItemBase item, Int32 amount = 1)
///
/// The Item Stack
/// The amount of Items
- public static void SetAmount(this ItemBase item, Int32 amount)
+ public static ItemBase SetAmount(this ItemBase item, Int32 amount)
{
if (!item.IsStack())
- return;
+ return item;
if (item.mType == ItemType.ItemCubeStack)
item.As().mnAmount = amount;
if (item.mType == ItemType.ItemStack)
item.As().mnAmount = amount;
+ return item;
}
///
diff --git a/Utilities/MachineInventory.cs b/Utilities/MachineInventory.cs
new file mode 100644
index 0000000..7d106ca
--- /dev/null
+++ b/Utilities/MachineInventory.cs
@@ -0,0 +1,409 @@
+using System.Collections.Generic;
+using System.IO;
+using UnityEngine;
+
+namespace FortressCraft.Community.Utilities
+{
+ ///
+ /// Generic machine inventory class with list inventory support
+ ///
+ /// Origianl code by steveman0
+ public class MachineInventory
+ {
+ ///
+ /// For associating the machine owner of the inventory
+ ///
+ public MachineEntity Machine;
+
+ ///
+ /// For associating the mob owner of the inventory
+ ///
+ public MobEntity Mob;
+
+ ///
+ /// The total item capacity of the storage
+ ///
+ public int StorageCapacity;
+
+ ///
+ /// The list of items in the inventory
+ ///
+ public List Inventory;
+
+ ///
+ /// Generic machine inventory class with list inventory support
+ ///
+ /// For associating the owner machine
+ /// The storage capacity of the inventory
+ public MachineInventory(MachineEntity machineentity, int storagecapacity)
+ {
+ this.Machine = machineentity;
+ this.StorageCapacity = storagecapacity;
+ this.Inventory = new List();
+ }
+
+ ///
+ /// Generic machine inventory class with list inventory support
+ ///
+ /// For associating the owner mob
+ /// The storage capacity of the inventory
+ public MachineInventory(MobEntity mobentity, int storagecapacity)
+ {
+ this.Mob = mobentity;
+ this.StorageCapacity = storagecapacity;
+ this.Inventory = new List();
+ }
+
+ ///
+ /// Add a single item type to the inventory
+ ///
+ /// The item to add
+ /// Amount of items added if given a stack
+ /// Returns the remainder that doesn't fit or null if successful
+ public ItemBase AddItem(ItemBase item, int amount = 1)
+ {
+ return ItemBaseUtil.AddListItem(item, ref this.Inventory, true, this.StorageCapacity);
+ }
+
+ ///
+ /// Add items from a source inventory or item list
+ ///
+ /// The source inventory or list of items
+ /// The number of items to transfer
+ public void AddItem(ref List items, int amount = 1)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false);
+ }
+
+ ///
+ /// Transfers items to machine inventory if they are on the provided whitelist
+ ///
+ /// Source inventory or list of items
+ /// List of items types allowed in the transfer
+ /// Number of items to add
+ public void AddWhiteList(ref List items, IEnumerable whitelist, int amount = 1)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, whitelist, true);
+ }
+
+ ///
+ /// Transfers items to machine inventory if they are on the provided whitelist
+ ///
+ /// Source inventory or list of items
+ /// Item type allowed in the transfer
+ /// Number of items to add
+ public void AddWhiteList(ref List items, ItemBase whitelist, int amount = 1)
+ {
+ ItemBase[] WhiteList = new[] { whitelist };
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, WhiteList, true);
+ }
+
+ ///
+ /// Transfers items to machine inventory if they are not on the provided blacklist
+ ///
+ /// Source inventory or list of items
+ /// List of items forbidden from transfer
+ /// Number of items to add
+ public void AddBlackList(ref List items, IEnumerable blacklist, int amount = 1)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, blacklist, false);
+ }
+
+
+
+ ///
+ /// Transfers items to machine inventory if they are not on the provided blacklist
+ ///
+ /// Source inventory or list of items
+ /// Item forbidden from transfer
+ /// Number of items to add
+ public void AddBlackList(ref List items, ItemBase blacklist, int amount = 1)
+ {
+ ItemBase[] BlackList = new[] { blacklist };
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, amount, this.StorageCapacity, false, BlackList, false);
+ }
+
+ ///
+ /// Fills the inventory to capacity with source items
+ ///
+ /// Source items to fill the inventory
+ public void Fill(ref List items)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity);
+ }
+
+ ///
+ /// Fills the inventory to capacity with source items
+ ///
+ /// Source items to fill the inventory
+ /// Item type allowed in the transfer
+ public void FillWhiteList(ref List items, IEnumerable whitelist)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, whitelist, true);
+ }
+
+ ///
+ /// Fills the inventory to capacity with source items
+ ///
+ /// Source items to fill the inventory
+ /// Item type allowed in the transfer
+ public void FillWhiteList(ref List items, ItemBase whitelist)
+ {
+ ItemBase[] WhiteList = new[] { whitelist };
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, WhiteList, true);
+ }
+
+ ///
+ /// Fills the inventory to capacity with source items
+ ///
+ /// Source items to fill the inventory
+ /// Item forbidden from transfer
+ public void FillBlackList(ref List items, IEnumerable blacklist)
+ {
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, blacklist, false);
+ }
+
+ ///
+ /// Fills the inventory to capacity with source items
+ ///
+ /// Source items to fill the inventory
+ /// Item forbidden from transfer
+ public void FillBlackList(ref List items, ItemBase blacklist)
+ {
+ ItemBase[] BlackList = new[] { blacklist };
+ ItemBaseUtil.MoveItems(ref items, ref this.Inventory, this.SpareCapacity(), this.StorageCapacity, false, BlackList, false);
+ }
+
+ ///
+ /// Empty the inventory of items
+ ///
+ /// Target inventory or list
+ /// Maximum number of items to take
+ public void Empty(ref List items, int amount)
+ {
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount);
+ }
+
+ ///
+ /// Return item from inventory by example (obeys stack size)
+ ///
+ /// Example item to find in inventory
+ /// Returns the item or null if unavailable or insufficient stack size
+ public ItemBase RemoveItem(ItemBase item)
+ {
+ return ItemBaseUtil.RemoveListItem(item, ref this.Inventory, false);
+ }
+
+ ///
+ /// Return item from inventory by example including partial item stack
+ ///
+ /// Example item to find in inventory
+ /// Returns the item or partial stack (null if item not found)
+ public ItemBase RemovePartialStack(ItemBase item)
+ {
+ return ItemBaseUtil.RemoveListItem(item, ref this.Inventory, true);
+ }
+
+ ///
+ /// Remove any single item type from the inventory
+ ///
+ /// Amount to remove (for stacks)
+ /// The ItemBase removed from inventory
+ public ItemBase RemoveAnySingle(int amount = 1)
+ {
+ List output = new List();
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref output, amount, amount, true);
+ if (output.Count == 0)
+ return null;
+ return output[0];
+ }
+
+ ///
+ /// Remove items from inventory if items are on the whitelist
+ ///
+ /// The target inventory or list to store the items
+ /// The list of items allowed to transfer
+ /// Storage capacity of target inventory
+ /// Amount of items to move in this transfer
+ public void RemoveWhiteList(ref List items, IEnumerable whitelist, int storagecapacity = int.MaxValue, int amount = 1)
+ {
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, whitelist, true);
+ }
+
+ ///
+ /// Remove items from inventory if items are on the whitelist
+ ///
+ /// The target inventory or list to store the items
+ /// Item allowed to transfer
+ /// Storage capacity of target inventory
+ /// Amount of items to move in this transfer
+ public void RemoveWhiteList(ref List items, ItemBase whitelist, int storagecapacity = int.MaxValue, int amount = 1)
+ {
+ ItemBase[] WhiteList = new [] { whitelist };
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, WhiteList, true);
+ }
+
+ ///
+ /// Remove items from inventory if items are not on the blacklist
+ ///
+ /// The target inventory or list to store the items
+ /// The list of items forbidden from transfer
+ /// Storage capacity of target inventory
+ /// Amount of items to move in this transfer
+ public void RemoveBlackList(ref List items, IEnumerable blacklist, int storagecapacity = int.MaxValue, int amount = 1)
+ {
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, blacklist, false);
+ }
+
+ ///
+ /// Remove items from inventory if items are not on the blacklist
+ ///
+ /// The target inventory or list to store the items
+ /// Item forbidden from transfer
+ /// Storage capacity of target inventory
+ /// Amount of items to move in this transfer
+ public void RemoveBlackList(ref List items, ItemBase blacklist, int storagecapacity = int.MaxValue, int amount = 1)
+ {
+ ItemBase[] BlackList = new[] { blacklist };
+ ItemBaseUtil.MoveItems(ref this.Inventory, ref items, amount, storagecapacity, false, BlackList, false);
+ }
+
+ ///
+ /// Returns the spare capacity of the inventory
+ ///
+ /// The spare capacity of the inventory
+ public int SpareCapacity()
+ {
+ return this.StorageCapacity - this.Inventory.GetItemCount();
+ }
+
+ ///
+ /// Returns the current number of items in the inventory
+ ///
+ /// Current number of items in inventory
+ public int ItemCount()
+ {
+ return this.Inventory.GetItemCount();
+ }
+
+ ///
+ /// Helper logic for checking if the inventory has space
+ ///
+ ///
+ public bool HasSpareCapcity()
+ {
+ return this.SpareCapacity() > 0;
+ }
+
+ ///
+ /// Helper logic for checking if the inventory is empty
+ ///
+ ///
+ public bool IsEmpty()
+ {
+ return this.ItemCount() == 0;
+ }
+
+ ///
+ /// Helper logic for checking if the inventory is full
+ ///
+ ///
+ public bool IsFull()
+ {
+ return this.ItemCount() >= this.StorageCapacity;
+ }
+
+ ///
+ /// Item drop code for emptying the inventory on the ground on machine delete
+ ///
+ public void DropOnDelete()
+ {
+ if (this.Machine == null)
+ {
+ Debug.LogWarning("Tried to drop machine inventory when not associated with a machine!");
+ return;
+ }
+ if (!WorldScript.mbIsServer)
+ return;
+ System.Random random = new System.Random();
+ for (int index = 0; index < this.Inventory.Count; ++index)
+ {
+ if (this.Inventory[index] != null)
+ {
+ Vector3 velocity = new Vector3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
+ ItemManager.instance.DropItem(this.Inventory[index], this.Machine.mnX, this.Machine.mnY, this.Machine.mnZ, velocity);
+ }
+ }
+ this.Inventory = null;
+ }
+
+ ///
+ /// Item drop code for emptying the inventory on the ground on mob delete
+ ///
+ public void DropOnMobDelete()
+ {
+ if (this.Mob == null)
+ {
+ Debug.LogWarning("Tried to drop mob inventory when not associated with a mob!");
+ return;
+ }
+ if (!WorldScript.mbIsServer)
+ return;
+ System.Random random = new System.Random();
+ for (int index = 0; index < this.Inventory.Count; ++index)
+ {
+ if (this.Inventory[index] != null)
+ {
+ Vector3 velocity = new Vector3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
+ ItemManager.instance.DropItem(this.Inventory[index], this.Mob.mnX, this.Mob.mnY, this.Mob.mnZ, velocity);
+ }
+ }
+ this.Inventory = null;
+ }
+
+ ///
+ /// Generic serialization function for writing the inventory to disk
+ ///
+ ///
+ public void WriteInventory(BinaryWriter writer)
+ {
+ int listcount = this.Inventory.Count;
+ int version = 0;
+
+ writer.Write(version);
+ writer.Write(listcount);
+ for (int index = 0; index < listcount; ++index)
+ {
+ ItemFile.SerialiseItem(this.Inventory[index], writer);
+ }
+ }
+
+ ///
+ /// Generic serialization function for reading the inventory from disk
+ ///
+ ///
+ public void ReadInventory(BinaryReader reader)
+ {
+ int listcount;
+ int version = reader.ReadInt32();
+
+ switch (version)
+ {
+ case 0:
+ listcount = reader.ReadInt32();
+ for (int index = 0; index < listcount; ++index)
+ {
+ ItemBase item = ItemFile.DeserialiseItem(reader);
+ if (item != null)
+ this.Inventory.Add(item);
+ else
+ Debug.LogError("Machine inventory tried to read in a null item! Corrupt save?");
+ }
+ break;
+ default:
+ Debug.LogError("Attempted to read Machine inventory version that does not exist!");
+ break;
+ }
+ }
+ }
+}