private static void Remove(Map map, Building_InfiniteStorage storage, Dictionary <Map, LinkedList <Building_InfiniteStorage> > storages)
 {
     if (map != null && storages.TryGetValue(map, out LinkedList <Building_InfiniteStorage> l))
     {
         l.Remove(storage);
         if (l.Count == 0)
         {
             ifStorages.Remove(map);
         }
     }
 }
 public static void Remove(Map map, Building_InfiniteStorage storage)
 {
     if (!storage.IncludeInWorldLookup)
     {
         Remove(map, storage, ifNonGlobalStorages);
     }
     else
     {
         Remove(map, storage, ifStorages);
     }
 }
        /*public static bool DropThing(Thing toDrop, Building_InfiniteStorage from, Map map, bool makeForbidden = false)
         * {
         *  if (toDrop.stackCount == 0)
         *  {
         *      Log.Warning("To Drop Thing " + toDrop.Label + " had stack count of 0");
         *      return false;
         *  }
         *
         *  try
         *  {
         *      from.AllowAdds = false;
         *      if (toDrop.stackCount <= toDrop.def.stackLimit)
         *      {
         *          return DropSingleThing(toDrop, from, map, makeForbidden);
         *      }
         *      return DropThing(toDrop, toDrop.stackCount, from, map, null, makeForbidden);
         *  }
         *  finally
         *  {
         *      from.AllowAdds = true;
         *  }
         * }
         *
         * public static bool DropThing(Thing toDrop, IntVec3 from, Map map)
         * {
         *  if (toDrop.stackCount == 0)
         *  {
         *      Log.Warning("To Drop Thing " + toDrop.Label + " had stack count of 0");
         *      return false;
         *  }
         *
         *  if (toDrop.stackCount <= toDrop.def.stackLimit)
         *  {
         *      return DropSingleThing(toDrop, from, map, out Thing result);
         *  }
         *  return DropThing(toDrop, toDrop.stackCount, from, map, null, out Thing result);
         * }*/

        public static bool DropThing(Thing toDrop, int amountToDrop, Building_InfiniteStorage from, Map map, List <Thing> droppedThings = null)
        {
            if (toDrop.stackCount == 0)
            {
                Log.Warning("To Drop Thing " + toDrop.Label + " had stack count of 0");
                return(false);
            }

            bool anyDropped = false;

            try
            {
                from.AllowAdds = false;

                Thing t;
                bool  done = false;
                while (!done)
                {
                    int toTake = toDrop.def.stackLimit;
                    if (toTake > amountToDrop)
                    {
                        toTake = amountToDrop;
                        done   = true;
                    }
                    if (toTake >= toDrop.stackCount)
                    {
                        if (amountToDrop > toTake)
                        {
                            Log.Error("        ThingStorage: Unable to drop " + (amountToDrop - toTake).ToString() + " of " + toDrop.def.label);
                        }
                        toTake = toDrop.stackCount;
                        done   = true;
                    }
                    if (toTake > 0)
                    {
                        amountToDrop -= toTake;
                        t             = toDrop.SplitOff(toTake);
                        if (DropSingleThing(t, from, map, out Thing result))
                        {
                            if (droppedThings != null)
                            {
                                droppedThings.Add(result);
                            }
                            anyDropped = true;
                        }
                    }
                }
            }
            finally
            {
                from.AllowAdds = true;
            }
            return(anyDropped);
        }
        private static void Add(Map map, Building_InfiniteStorage storage, Dictionary <Map, LinkedList <Building_InfiniteStorage> > storages)
        {
            if (!storages.TryGetValue(map, out LinkedList <Building_InfiniteStorage> l))
            {
                l = new LinkedList <Building_InfiniteStorage>();
                storages.Add(map, l);
            }

            if (!l.Contains(storage))
            {
                l.AddLast(storage);
            }
        }
 internal static void Postfix(Thing __result)
 {
     if (droppedAndStorage != null)
     {
         foreach (KeyValuePair <Thing, Building_InfiniteStorage> kv in droppedAndStorage)
         {
             if (kv.Key != __result)
             {
                 Building_InfiniteStorage storage = kv.Value;
                 storage.Add(kv.Key);
             }
         }
         droppedAndStorage.Clear();
     }
 }
        public static bool DropSingleThing(Thing toDrop, Building_InfiniteStorage from, Map map, out Thing result)
        {
            result = null;
            if (toDrop.stackCount == 0)
            {
                Log.Warning("To Drop Thing " + toDrop.Label + " had stack count of 0");
                return(false);
            }

            try
            {
                from.AllowAdds = false;
                return(DropSingleThing(toDrop, from.InteractionCell, map, out result));
            }
            finally
            {
                from.AllowAdds = true;
            }
        }
        public static void Add(Map map, Building_InfiniteStorage storage)
        {
            if (storage == null)
            {
                Log.Error("Tried to add a null storage");
                return;
            }

            if (map == null || storage.Map == null)
            {
                Log.Error("Tried to add " + storage.Label + " to a null map. Please let me know if this ever happens!");
                return;
            }

            if (!storage.IncludeInWorldLookup)
            {
                Add(map, storage, ifNonGlobalStorages);
            }
            else
            {
                Add(map, storage, ifStorages);
            }
        }
            internal static void Postfix(List <Thing> __result)
            {
                if (droppedAndStorage != null && __result != null)
                {
                    HashSet <Thing> results = new HashSet <Thing>();
                    foreach (Thing t in __result)
                    {
                        results.Add(t);
                    }

                    foreach (KeyValuePair <Thing, Building_InfiniteStorage> kv in droppedAndStorage)
                    {
                        if (!results.Contains(kv.Key))
                        {
                            Building_InfiniteStorage storage = kv.Value;
                            storage.Add(kv.Key);
                        }
                    }
                    results.Clear();
                    results = null;
                    droppedAndStorage.Clear();
                }
            }