Stardew_Valley_Mods/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs

374 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Revitalize.Framework.Objects.Furniture;
using StardewModdingAPI;
using StardewValley;
namespace Revitalize.Framework.Objects
{
/// <summary>
/// Deals with handling all objects for the mod.
/// </summary>
public class ObjectManager
{
/// <summary>
/// All of the object managers id'd by a mod's or content pack's unique id.
/// </summary>
public static Dictionary<string, ObjectManager> ObjectPools;
/// <summary>
/// The name of this object manager.
/// </summary>
public string name;
/// <summary>
/// All of the chairs held by this object pool.
/// </summary>
public Dictionary<string, ChairMultiTiledObject> chairs;
/// <summary>
/// All of the tables held by this object pool.
/// </summary>
public Dictionary<string, TableMultiTiledObject> tables;
/// <summary>
/// All of the lamps held by this object pool.
/// </summary>
public Dictionary<string, LampMultiTiledObject> lamps;
/// <summary>
/// All of the rugs held by this object pool.
/// </summary>
public Dictionary<string, RugMultiTiledObject> rugs;
public Dictionary<string, StorageFurnitureOBJ> furnitureStorage;
public Dictionary<string, MultiTiledObject> generic;
/// <summary>
/// Misc. items for this mod.
/// </summary>
public Dictionary<string, MultiTiledObject> miscellaneous;
public ResourceManager resources;
public Dictionary<string, CustomObject> ItemsByName;
/// <summary>
/// Constructor.
/// </summary>
public ObjectManager()
{
this.initialize();
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="manifest"></param>
public ObjectManager(IManifest manifest)
{
this.name = manifest.UniqueID;
this.initialize();
}
/// <summary>
/// Initialize all objects used to manage this class.
/// </summary>
private void initialize()
{
this.chairs = new Dictionary<string, ChairMultiTiledObject>();
this.tables = new Dictionary<string, TableMultiTiledObject>();
this.lamps = new Dictionary<string, LampMultiTiledObject>();
this.rugs = new Dictionary<string, RugMultiTiledObject>();
this.furnitureStorage = new Dictionary<string, StorageFurnitureOBJ>();
this.generic = new Dictionary<string, MultiTiledObject>();
this.miscellaneous = new Dictionary<string, MultiTiledObject>();
this.resources = new ResourceManager();
this.ItemsByName = new Dictionary<string, CustomObject>();
ChairMultiTiledObject s = new ChairMultiTiledObject();
}
/// <summary>
/// Loads in the items for the object and resource managers.
/// </summary>
public void loadInItems()
{
this.resources.loadInItems();
}
/// <summary>
/// Gets a random object from the dictionary passed in.
/// </summary>
/// <param name="dictionary"></param>
/// <returns></returns>
public Item getRandomObject(Dictionary<string,CustomObject> dictionary)
{
if (dictionary.Count == 0) return null;
List<CustomObject> objs = new List<CustomObject>();
foreach(KeyValuePair<string,CustomObject> pair in dictionary)
{
objs.Add(pair.Value);
}
int rand = Game1.random.Next(0,objs.Count);
return objs[rand].getOne();
}
/// <summary>
/// Gets an object from the dictionary that is passed in.
/// </summary>
/// <param name="objectName"></param>
/// <param name="dictionary"></param>
/// <returns></returns>
public Item getObject(string objectName, Dictionary<string,CustomObject> dictionary)
{
if (dictionary.ContainsKey(objectName))
{
return dictionary[objectName].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
public Item getObject(string objectName, Dictionary<string, MultiTiledObject> dictionary)
{
if (dictionary.ContainsKey(objectName))
{
return dictionary[objectName].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
/// <summary>
/// Gets a chair from the object manager.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ChairMultiTiledObject getChair(string name)
{
if (this.chairs.ContainsKey(name))
{
return (ChairMultiTiledObject)this.chairs[name].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
/// <summary>
/// Gets a table from the object manager.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TableMultiTiledObject getTable(string name)
{
if (this.tables.ContainsKey(name))
{
return (TableMultiTiledObject)this.tables[name].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
/// <summary>
/// Gets a lamp from the object manager.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public LampMultiTiledObject getLamp(string name)
{
if (this.lamps.ContainsKey(name))
{
return (LampMultiTiledObject)this.lamps[name].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
/// <summary>
/// Gets storage furniture from the object manager.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public StorageFurnitureOBJ getStorageFuriture(string name)
{
if (this.furnitureStorage.ContainsKey(name))
{
return (StorageFurnitureOBJ)this.furnitureStorage[name].getOne();
}
else
{
throw new Exception("Object pool doesn't contain said object.");
}
}
/// <summary>
/// Adds in an item to be tracked by the mod's object manager.
/// </summary>
/// <param name="key"></param>
/// <param name="I"></param>
public void AddItem(string key, CustomObject I)
{
if (this.ItemsByName.ContainsKey(key))
{
throw new Exception("Item with the same key has already been added into the mod!");
}
else
{
this.ItemsByName.Add(key, I);
}
}
/// <summary>
/// Gets an item from the list of modded items.
/// </summary>
/// <param name="Key"></param>
/// <param name="Stack"></param>
/// <returns></returns>
public CustomObject GetItem(string Key,int Stack=1)
{
if (this.ItemsByName.ContainsKey(Key))
{
Item I= this.ItemsByName[Key].getOne();
I.Stack = Stack;
return (CustomObject)I;
}
else
{
return null;
}
}
/// <summary>
/// Adds a new object manager to the master pool of managers.
/// </summary>
/// <param name="Manifest"></param>
public static void AddObjectManager(IManifest Manifest)
{
if (ObjectPools == null) ObjectPools = new Dictionary<string, ObjectManager>();
ObjectPools.Add(Manifest.UniqueID, new ObjectManager(Manifest));
}
/// <summary>
/// Cleans up all stored information.
/// </summary>
public void returnToTitleCleanUp()
{
}
/// <summary>
/// Scans all mod items to try to find a match.
/// </summary>
/// <param name="ID"></param>
/// <param name="T"></param>
/// <returns></returns>
public Item getItemByIDAndType(string ID,Type T)
{
foreach(var v in this.chairs)
{
if(v.Value.GetType()==T && v.Value.info.id == ID)
{
Item I= v.Value.getOne();
return I;
}
}
foreach(var v in this.furnitureStorage)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.generic)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.ItemsByName)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.lamps)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.miscellaneous)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.rugs)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.tables)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.resources.ores)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
foreach(var v in this.resources.oreVeins)
{
if (v.Value.GetType() == T && v.Value.info.id == ID)
{
Item I = v.Value.getOne();
return I;
}
}
return null;
}
}
}