public static bool Prefix(object __instance, ref bool __result, IStorage input) { IReflectedProperty <SObject> instanceMachine = ModEntry.StaticHelper.Reflection.GetProperty <SObject>(__instance, "Machine"); if (instanceMachine.GetValue() is Processor processor) { IConsumable consumable = null; Recipe acceptingRecipe = processor.Recipes.FirstOrDefault(recipe => recipe.PossibleIngredients.Any(pair => input.TryGetIngredient(pair.Key, pair.Value, out consumable) ) ); if (acceptingRecipe == null) { __result = false; return(false); } processor.heldObject.Value = Processor.WithQuality(acceptingRecipe.Process)(consumable.Take() as SObject); processor.minutesUntilReady.Value = acceptingRecipe.Minutes; __result = true; return(false); } return(true); }
private bool IsBoolPropertyTrue(string propertyName, object tobeCheckedForProperty) { IReflectedProperty <bool> dinosaurAreaProperty = Helper.Reflection.GetProperty <bool>(tobeCheckedForProperty, propertyName); if (dinosaurAreaProperty != null) { return(dinosaurAreaProperty.GetValue()); } return(false); }
/// <summary> /// Replaces the recipes with the ones defined by the processor. /// </summary> public static bool Prefix(IMachine __instance, ref bool __result, IStorage input) { try { IReflectedProperty <SObject> instanceMachine = Util.Helper.Reflection.GetProperty <SObject>(__instance, "Machine"); if (instanceMachine.GetValue() is Processor processor) { IReflectedField <IRecipe[]> privateRecipes = Util.Helper.Reflection.GetField <IRecipe[]>(__instance, "Recipes"); IRecipe[] recipes = RecipeManager.GetRecipeAdaptorsFor(processor, privateRecipes?.GetValue()); IConsumable consumable = null; IRecipe acceptingRecipe = null; foreach (ITrackedStack item in input.GetItems()) { acceptingRecipe = recipes.FirstOrDefault(recipe => recipe.AcceptsInput(item)); if (acceptingRecipe != null) { input.TryGetIngredient(item.Sample.ParentSheetIndex, acceptingRecipe.InputCount, out consumable); break; } } if (acceptingRecipe != null && consumable != null) { processor.heldObject.Value = acceptingRecipe.Output(consumable.Take()); processor.MinutesUntilReady = acceptingRecipe.Minutes; __result = true; return(false); } __result = false; return(false); } return(true); } catch (Exception ex) { ModEntry.StaticMonitor.Log($"Failed overriding Automate.\n{ex}", LogLevel.Error); return(true); // run original code instead } }
/// <summary>Temporarily dismount and set up the player to interact with a tile, then return it to the previous state afterwards.</summary> /// <param name="action">The action to perform.</param> private void TemporarilyFakeInteraction(Action action) { // get references SFarmer player = Game1.player; IReflectedProperty <Horse> mountField = this.Reflection.GetProperty <Horse>(Game1.player, "mount"); // save current state Horse mount = mountField.GetValue(); Vector2 mountPosition = this.Position; WateringCan wateringCan = player.CurrentTool as WateringCan; int waterInCan = wateringCan?.WaterLeft ?? 0; float stamina = player.stamina; Vector2 position = player.Position; int facingDirection = player.FacingDirection; int currentToolIndex = player.CurrentToolIndex; bool canMove = Game1.player.canMove; // fix player frozen due to animations when performing an action // move mount out of the way mountField.SetValue(null); this.Position = new Vector2(-5, -5); // perform action try { action(); } finally { // move mount back this.Position = mountPosition; mountField.SetValue(mount); // restore previous state if (wateringCan != null) { wateringCan.WaterLeft = waterInCan; } player.stamina = stamina; player.Position = position; player.FacingDirection = facingDirection; player.CurrentToolIndex = currentToolIndex; Game1.player.canMove = canMove; } }