Exemplo n.º 1
0
        public override bool checkForAction(Farmer who, bool justCheckingForActivity = false)
        {
            if (justCheckingForActivity)
            {
                return(true);
            }
            List <Chest> nearby_chests = new List <Chest>();

            Vector2[] neighbor_tiles = new Vector2[8]
            {
                new Vector2(-1f, 1f),
                new Vector2(0f, 1f),
                new Vector2(1f, 1f),
                new Vector2(-1f, 0f),
                new Vector2(1f, 0f),
                new Vector2(-1f, -1f),
                new Vector2(0f, -1f),
                new Vector2(1f, -1f)
            };
            for (int i = 0; i < neighbor_tiles.Length; i++)
            {
                if (who.currentLocation is FarmHouse && who.currentLocation.getTileIndexAt((int)(tileLocation.X + neighbor_tiles[i].X), (int)(tileLocation.Y + neighbor_tiles[i].Y), "Buildings") == 173)
                {
                    nearby_chests.Add((who.currentLocation as FarmHouse).fridge.Value);
                    continue;
                }
                if (who.currentLocation is IslandFarmHouse && who.currentLocation.getTileIndexAt((int)(tileLocation.X + neighbor_tiles[i].X), (int)(tileLocation.Y + neighbor_tiles[i].Y), "Buildings") == 173)
                {
                    nearby_chests.Add((who.currentLocation as IslandFarmHouse).fridge.Value);
                    continue;
                }
                Vector2 tile_location   = new Vector2((int)(tileLocation.X + neighbor_tiles[i].X), (int)(tileLocation.Y + neighbor_tiles[i].Y));
                Object  neighbor_object = null;
                if (who.currentLocation.objects.ContainsKey(tile_location))
                {
                    neighbor_object = who.currentLocation.objects[tile_location];
                }
                if (neighbor_object != null && neighbor_object is Chest && (neighbor_object as Chest).SpecialChestType == Chest.SpecialChestTypes.None)
                {
                    nearby_chests.Add(neighbor_object as Chest);
                }
            }
            List <NetMutex> muticies = new List <NetMutex>();

            foreach (Chest chest in nearby_chests)
            {
                muticies.Add(chest.mutex);
            }
            if (!mutex.IsLocked())
            {
                MultipleMutexRequest multipleMutexRequest = null;
                multipleMutexRequest = new MultipleMutexRequest(muticies, delegate
                {
                    mutex.RequestLock(delegate
                    {
                        Vector2 topLeftPositionForCenteringOnScreen = Utility.getTopLeftPositionForCenteringOnScreen(800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2);
                        Game1.activeClickableMenu = new CraftingPage((int)topLeftPositionForCenteringOnScreen.X, (int)topLeftPositionForCenteringOnScreen.Y, 800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2, cooking: false, standalone_menu: true, nearby_chests);
                        Game1.activeClickableMenu.exitFunction = delegate
                        {
                            mutex.ReleaseLock();
                            multipleMutexRequest.ReleaseLocks();
                        };
                    }, delegate
                    {
                        multipleMutexRequest.ReleaseLocks();
                    });
                }, delegate
                {
                    Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:Workbench_Chest_Warning"));
                });
            }
            return(true);
        }
        /// <summary>Opens the kitchen menu using any available containers. Imitates <see cref="GameLocation.ActivateKitchen"/> but avoids issues with a null "fridge" parameter.</summary>
        /// <param name="location">The location of the kitchen.</param>
        /// <returns>True if the menu successfully opened. False otherwise, e.g. if no storage containers exist or one is currently in use.</returns>
        private static void ActivateKitchen(GameLocation location)
        {
            //get the current location's static fridge if applicable
            NetRef <Chest> fridge = null;

            if (location is FarmHouse farmhouse)
            {
                fridge = farmhouse.fridge;
            }
            else if (location is IslandFarmHouse islandfarmhouse)
            {
                fridge = islandfarmhouse.fridge;
            }

            //imitate GameLocation.ActivateKitchen except where otherwise noted
            List <NetMutex> muticies     = new List <NetMutex>();
            List <Chest>    mini_fridges = new List <Chest>();

            foreach (StardewValley.Object item in location.objects.Values)
            {
                if (item != null && item is Chest chest)
                {
                    if ((chest.bigCraftable.Value && chest.ParentSheetIndex == 216) || chest.fridge.Value) //if this chest is a Mini-Fridge OR has "fridge" set to true (the original method only checks for Mini-Fridge)
                    {
                        mini_fridges.Add(chest);
                        muticies.Add(chest.mutex);
                    }
                }
            }
            if (fridge != null && fridge.Value.mutex.IsLocked())
            {
                Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:Kitchen_InUse"));
                return;
            }
            MultipleMutexRequest multiple_mutex_request = null;

            multiple_mutex_request = new MultipleMutexRequest(muticies, delegate
            {
                if (fridge != null) //if this location has a fridge, use the original code
                {
                    fridge.Value.mutex.RequestLock(delegate
                    {
                        List <Chest> list = new List <Chest>();
                        //skip redundant null check on the fridge
                        list.Add(fridge);
                        list.AddRange(mini_fridges);
                        Vector2 topLeftPositionForCenteringOnScreen = Utility.getTopLeftPositionForCenteringOnScreen(800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2);
                        Game1.activeClickableMenu = new CraftingPage((int)topLeftPositionForCenteringOnScreen.X, (int)topLeftPositionForCenteringOnScreen.Y, 800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2, cooking: true, standalone_menu: true, list);
                        Game1.activeClickableMenu.exitFunction = delegate
                        {
                            fridge.Value.mutex.ReleaseLock();
                            multiple_mutex_request.ReleaseLocks();
                        };
                    }, delegate
                    {
                        Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:Kitchen_InUse"));
                        multiple_mutex_request.ReleaseLocks();
                    });
                }
                else //if this location does NOT have a fridge, only use mini-fridges
                {
                    //don't RequestLock the fridge mutex
                    List <Chest> list = new List <Chest>();
                    //don't add the fridge to this list
                    list.AddRange(mini_fridges);
                    Vector2 topLeftPositionForCenteringOnScreen = Utility.getTopLeftPositionForCenteringOnScreen(800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2);
                    Game1.activeClickableMenu = new CraftingPage((int)topLeftPositionForCenteringOnScreen.X, (int)topLeftPositionForCenteringOnScreen.Y, 800 + IClickableMenu.borderWidth * 2, 600 + IClickableMenu.borderWidth * 2, cooking: true, standalone_menu: true, mini_fridges);
                    Game1.activeClickableMenu.exitFunction = delegate
                    {
                        //don't ReleaseLock the fridge mutex
                        multiple_mutex_request.ReleaseLocks();
                    };
                }
            }, delegate
            {
                Game1.showRedMessage(Game1.content.LoadString("Strings\\UI:Kitchen_InUse"));
            });
        }