-
Notifications
You must be signed in to change notification settings - Fork 29
/
CaskOverrides.cs
147 lines (139 loc) · 6.58 KB
/
CaskOverrides.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Threading.Tasks;
using HarmonyLib;
using Microsoft.Xna.Framework;
using StardewValley;
using StardewValley.GameData.BigCraftables;
using StardewValley.GameData.Machines;
using StardewValley.Objects;
using SObject = StardewValley.Object;
namespace CustomCaskMod
{
internal class CaskOverrides
{
public static bool IsValidCaskLocation(ref Cask __instance, ref bool __result)
{
if ((IsVanillaCask(__instance) && DataLoader.ModConfig.EnableCasksAnywhere)
|| (AgerController.GetAger(__instance.QualifiedItemId) is CustomAger ager && (ager.EnableAgingAnywhere || DataLoader.ModConfig.EnableCustomAgersAnywhere)))
{
__result = true;
return false;
}
else
{
return true;
}
}
public static bool checkForMaturity(ref Cask __instance)
{
if ((IsVanillaCask(__instance) && DataLoader.ModConfig.EnableMoreThanOneQualityIncrementPerDay)
|| (AgerController.GetAger(__instance.QualifiedItemId) is CustomAger ager && ager.EnableMoreThanOneQualityIncrementPerDay))
{
if ((float)__instance.daysToMature.Value <= 0f)
{
__instance.MinutesUntilReady = 1;
__instance.heldObject.Value.Quality = 4;
}
else if ((float)__instance.daysToMature.Value <= 28f)
{
__instance.heldObject.Value.Quality = 2;
}
else if ((float)__instance.daysToMature.Value <= 42f)
{
__instance.heldObject.Value.Quality = 1;
}
return false;
}
return true;
}
public static bool placementAction(ref SObject __instance, GameLocation location, int x, int y, ref bool __result)
{
if (__instance.bigCraftable.Value && AgerController.HasAger(__instance.QualifiedItemId))
{
Vector2 placementTile = new(x / 64, y / 64);
Cask cask = new(placementTile)
{
ParentSheetIndex = __instance.ParentSheetIndex
};
Game1.bigCraftableData.TryGetValue(__instance.ItemId, out BigCraftableData objectInformation);
if (objectInformation != null)
{
cask.ItemId = __instance.ItemId;
cask.name = objectInformation.Name;
cask.Price = objectInformation.Price;
cask.setOutdoors.Value = objectInformation.CanBePlacedOutdoors;
cask.setIndoors.Value = objectInformation.CanBePlacedIndoors;
cask.Fragility = objectInformation.Fragility;
cask.isLamp.Value = objectInformation.IsLamp;
}
cask.initializeLightSource(cask.TileLocation);
cask.boundingBox.Value = new Microsoft.Xna.Framework.Rectangle((int)cask.TileLocation.X * 64, (int)cask.TileLocation.Y * 64, 64, 64);
location.objects.Add(placementTile, cask);
location.playSound("hammer");
__result = true;
return false;
}
return true;
}
public static IEnumerable<CodeInstruction> performToolAction_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
LinkedList<CodeInstruction> newInstructions = new(instructions);
CodeInstruction codeInstructionWoodWhack = newInstructions.FirstOrDefault(c => c.opcode == OpCodes.Ldstr && c.operand?.ToString() == "woodWhack");
LinkedListNode<CodeInstruction> linkedListNode = newInstructions.Find(codeInstructionWoodWhack);
if (linkedListNode != null && codeInstructionWoodWhack != null && linkedListNode.Next is { Value: { } codeInstructionAfterWoodWhack })
{
Label endLabelWoodWack = generator.DefineLabel();
codeInstructionWoodWhack.labels.Add(endLabelWoodWack);
Label endLabelBeforePlaySound = generator.DefineLabel();
codeInstructionAfterWoodWhack.labels.Add(endLabelBeforePlaySound);
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Ldarg_0, null));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Callvirt, AccessTools.Property(typeof(Item), "Name").GetGetMethod()));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Ldstr, "Cask"));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(String), "op_Equality", new Type[] { typeof(string), typeof(string) })));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Brtrue_S, endLabelWoodWack));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Ldstr, "hammer"));
newInstructions.AddBefore(linkedListNode, new CodeInstruction(OpCodes.Br_S, endLabelBeforePlaySound));
}
return newInstructions;
}
internal static bool TryApplyFairyDust(SObject __instance, bool probe, ref bool __result)
{
if (__instance.GetMachineData() != null) return true;
if (__instance.MinutesUntilReady <= 0) return true;
if (!AgerController.HasAger(__instance.QualifiedItemId)) return true;
if (!probe)
{
Utility.addSprinklesToLocation(__instance.Location, (int)__instance.TileLocation.X,
(int)__instance.TileLocation.Y, 1, 2, 400, 40, Color.White);
Game1.playSound("yoba");
__instance.MinutesUntilReady = 0;
__instance.minutesElapsed(0);
}
__result = true;
return false;
}
private static bool IsVanillaCask(SObject cask)
{
return cask.Name == "Cask";
}
private static bool IsColoredObjectAtParentSheetIndex(Item item, int index)
{
if (item == null)
{
return false;
}
if (item.GetType() != typeof(ColoredObject))
{
return false;
}
if ((item as ColoredObject)?.bigCraftable.Value == true)
{
return false;
}
return item.ParentSheetIndex == index;
}
}
}