First Machine Age's Mods (Combined repo.)
Revisión | b73dcb7b88af49474283ff49661e4be2ca152476 (tree) |
---|---|
Tiempo | 2022-08-21 06:54:35 |
Autor | melchior <melchior@user...> |
Commiter | melchior |
Final Split to Empty Voxel grid fix
@@ -12,8 +12,8 @@ namespace AnvilMetalRecovery | ||
12 | 12 | /// Metal Quantity (VOXELS) |
13 | 13 | /// </summary> |
14 | 14 | public uint Quantity; |
15 | - public float Melting_Duration; | |
16 | - public int Melting_Point; | |
15 | + public float Melting_Duration;//TODO: Replace with 1.17 MetalAlloy mappings | |
16 | + public int Melting_Point;//TODO: Replace with 1.17 MetalAlloy mappings | |
17 | 17 | |
18 | 18 | public RecoveryEntry(AssetLocation coll, AssetLocation ingot, uint qty, float dur, int point) |
19 | 19 | { |
@@ -26,7 +26,7 @@ namespace AnvilMetalRecovery.Patches | ||
26 | 26 | |
27 | 27 | if (original != null ) { |
28 | 28 | foreach(var patched in harmony.GetPatchedMethods()) |
29 | - { | |
29 | + { | |
30 | 30 | if (patched.Name == original.Name)return false; //SKIPS PATCHING, its already there |
31 | 31 | } |
32 | 32 | } |
@@ -39,24 +39,47 @@ namespace AnvilMetalRecovery.Patches | ||
39 | 39 | private static void Prefix_OnSplit(Vec3i voxelPos, BlockEntityAnvil __instance) |
40 | 40 | { |
41 | 41 | var anvil = new SmithAssist(__instance); |
42 | + #if DEBUG | |
43 | + anvil.Logger.VerboseDebug("Prefix_OnSplit"); | |
44 | + #endif | |
42 | 45 | |
43 | 46 | if (anvil.IsShavable && anvil.Voxel(voxelPos.X, voxelPos.Y, voxelPos.Z) == EnumVoxelMaterial.Metal) { |
47 | + #if DEBUG | |
48 | + anvil.Logger.VerboseDebug("{0}, Split into {1} @{2}, Total:{3}",anvil.OutputCode ,anvil.BaseMetal, voxelPos, anvil.SplitCount); | |
49 | + #endif | |
50 | + anvil.SplitCount++; | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + [HarmonyPostfix] | |
55 | + [HarmonyPatch(nameof(BlockEntityAnvil.OnSplit))] | |
56 | + private static void Postfix_OnSplit(Vec3i voxelPos, BlockEntityAnvil __instance) | |
57 | + { | |
58 | + var anvil = new SmithAssist(__instance); | |
59 | + if (anvil.World.Side.IsClient( )) return; | |
60 | + | |
44 | 61 | #if DEBUG |
45 | - anvil.Logger.VerboseDebug("{0}, Split into {1} @{2}, Total:{3}",anvil.OutputCode ,anvil.BaseMetal, voxelPos, anvil.SplitCount); | |
62 | + anvil.Logger.VerboseDebug("Postfix_OnSplit"); | |
46 | 63 | #endif |
47 | - anvil.SplitCount++; | |
48 | - } | |
49 | - | |
64 | + if (anvil.WorkEntirelySplit( )) | |
65 | + { | |
66 | + #if DEBUG | |
67 | + anvil.Logger.VerboseDebug("Work item entirely split up."); | |
68 | + #endif | |
69 | + anvil.IssueCancelStackEquivalent();//anvil.IssueShavings(null); | |
70 | + anvil.ClearWork(); | |
71 | + } | |
50 | 72 | } |
51 | 73 | |
74 | + //internal void OnUseOver --> | |
52 | 75 | [HarmonyPrefix] |
53 | 76 | [HarmonyPatch(nameof(BlockEntityAnvil.CheckIfFinished))] |
54 | 77 | private static void Prefix_CheckIfFinished(IPlayer byPlayer, BlockEntityAnvil __instance) |
55 | 78 | { |
56 | 79 | var anvil = new SmithAssist(__instance); |
57 | - if (anvil.WorkMatchesRecipe( )) { | |
58 | - anvil.IssueShavings(byPlayer); | |
59 | - } | |
80 | + if (anvil.WorkMatchesRecipe( )) { | |
81 | + anvil.IssueShavings(byPlayer); | |
82 | + } | |
60 | 83 | } |
61 | 84 | |
62 | 85 | [HarmonyPostfix] |
@@ -78,7 +101,7 @@ namespace AnvilMetalRecovery.Patches | ||
78 | 101 | */ |
79 | 102 | |
80 | 103 | |
81 | - } | |
104 | + } /************** HARMONY CLASS ENDS *******************/ | |
82 | 105 | |
83 | 106 | /// <summary> |
84 | 107 | /// Special tools for handling the Anvil's data/state, ect... |
@@ -90,6 +113,9 @@ namespace AnvilMetalRecovery.Patches | ||
90 | 113 | internal const string splitCountKey = @"splitCount"; |
91 | 114 | internal const int shavingValue = 5; |
92 | 115 | |
116 | + private AccessTools.FieldRef<BlockEntityAnvil, ItemStack> workItemStack_R = AccessTools.FieldRefAccess<BlockEntityAnvil, ItemStack>(@"workItemStack"); | |
117 | + private AccessTools.FieldRef<BlockEntityAnvil, ItemStack> returnOnCancelStack_R = AccessTools.FieldRefAccess<BlockEntityAnvil, ItemStack>(@"returnOnCancelStack"); | |
118 | + | |
93 | 119 | internal SmithAssist(BlockEntityAnvil a) |
94 | 120 | { |
95 | 121 | this.bea = a; |
@@ -102,6 +128,13 @@ namespace AnvilMetalRecovery.Patches | ||
102 | 128 | } |
103 | 129 | } |
104 | 130 | |
131 | + internal IWorldAccessor World { | |
132 | + get | |
133 | + { | |
134 | + return bea.Api.World; | |
135 | + } | |
136 | + } | |
137 | + | |
105 | 138 | public static AssetLocation MetalShavingsCode { |
106 | 139 | get |
107 | 140 | { |
@@ -196,23 +229,29 @@ namespace AnvilMetalRecovery.Patches | ||
196 | 229 | if (this.SplitCount > 0) { |
197 | 230 | int shavingQty = ShavingQuantity; |
198 | 231 | |
199 | - if (shavingQty > 0) | |
200 | - { | |
201 | - Item metalShavingsItem = bea.Api.World.GetItem(MetalShavingsCode.AppendPathVariant(BaseMetal)); | |
232 | + if (shavingQty > 0) | |
233 | + { | |
234 | + Item metalShavingsItem = World.GetItem(MetalShavingsCode.AppendPathVariant(BaseMetal)); | |
202 | 235 | |
203 | - if (metalShavingsItem != null) | |
236 | + if (metalShavingsItem != null) | |
204 | 237 | { |
205 | - ItemStack metalShavingsStack = new ItemStack(metalShavingsItem, shavingQty); | |
238 | + ItemStack metalShavingsStack = new ItemStack(metalShavingsItem, shavingQty); | |
206 | 239 | |
207 | - if (byPlayer != null) { | |
208 | - if (byPlayer.InventoryManager.TryGiveItemstack(metalShavingsStack, false) == false) { bea.Api.World.SpawnItemEntity(metalShavingsStack, bea.Pos.ToVec3d().Add(0.1d,0,0) ); } | |
240 | + if (byPlayer != null) | |
241 | + { | |
242 | + if (byPlayer.InventoryManager.TryGiveItemstack(metalShavingsStack, false) == false) | |
243 | + { World.SpawnItemEntity(metalShavingsStack, bea.Pos.ToVec3d( ).Add(0.1d, 0, 0)); } | |
244 | + } | |
245 | + else | |
246 | + { | |
247 | + //Just spew itemstack on top of anvil...Player ran off? | |
248 | + World.SpawnItemEntity(metalShavingsStack, bea.Pos.ToVec3d( ).Add(0.1d, 0, 0)); | |
249 | + } | |
209 | 250 | #if DEBUG |
210 | 251 | Logger.VerboseDebug("RecoveryAnvil: Smithing done - recover: {0} shavings of {1}", shavingQty, metalShavingsItem.Code); |
211 | 252 | #endif |
212 | - } | |
213 | 253 | } |
214 | - else | |
215 | - { | |
254 | + else { | |
216 | 255 | Logger.Warning("Missing or Invalid Item: {0} ", MetalShavingsCode.WithPathAppendix("-" + BaseMaterial)); |
217 | 256 | } |
218 | 257 | } |
@@ -242,6 +281,22 @@ namespace AnvilMetalRecovery.Patches | ||
242 | 281 | return true; |
243 | 282 | } |
244 | 283 | |
284 | + /// <summary> | |
285 | + /// If the Work voxels are _GONE_ and everything is split UP? | |
286 | + /// </summary> | |
287 | + /// <returns>If entirely split.</returns> | |
288 | + internal bool WorkEntirelySplit( ) | |
289 | + { | |
290 | + if ( SplitCount > 0 && bea.SelectedRecipe != null) { //Work-Item stack OK; for post split check... | |
291 | + foreach (EnumVoxelMaterial voxel in bea.Voxels) { | |
292 | + if (voxel == EnumVoxelMaterial.Metal || voxel == EnumVoxelMaterial.Slag) return false; | |
293 | + } | |
294 | + return true; | |
295 | + } | |
296 | + | |
297 | + return false; | |
298 | + } | |
299 | + | |
245 | 300 | internal bool PrefixMatcher(List<AssetLocation> nameList, AssetLocation target) |
246 | 301 | { |
247 | 302 | if (nameList == null || nameList.Count == 0 || target == null) return false; |
@@ -252,6 +307,46 @@ namespace AnvilMetalRecovery.Patches | ||
252 | 307 | |
253 | 308 | return false; |
254 | 309 | } |
310 | + | |
311 | + internal void IssueCancelStackEquivalent( ) | |
312 | + { | |
313 | + | |
314 | + if (bea.SelectedRecipeId > 0 && bea.SelectedRecipe != null ) | |
315 | + { | |
316 | + var itemToVoxelLookup = MetalRecoverySystem.GetCachedLookupTable(World); | |
317 | + if (itemToVoxelLookup.ContainsKey(bea.SelectedRecipe.Output.Code)) { | |
318 | + var result = itemToVoxelLookup[bea.SelectedRecipe.Output.Code]; | |
319 | + #if DEBUG | |
320 | + Logger.VerboseDebug("(old) Selected Recipe: {0} base-material: '{1}' worth {2} units; spawning", bea.SelectedRecipe.Output.Code, result.IngotCode, result.Quantity); | |
321 | + #endif | |
322 | + | |
323 | + int shavingQty = ( int )(result.Quantity / MetalRecoverySystem.IngotVoxelDefault); | |
324 | + | |
325 | + if (shavingQty > 0) { | |
326 | + Item metalShavingsItem = World.GetItem(MetalShavingsCode.AppendPathVariant(result.IngotCode.PathEnding())); | |
327 | + | |
328 | + if (metalShavingsItem != null) { | |
329 | + ItemStack metalShavingsStack = new ItemStack(metalShavingsItem, shavingQty); | |
330 | + World.SpawnItemEntity(metalShavingsStack, bea.Pos.ToVec3d( ).Add(0.1d, 0, 0)); | |
331 | + } | |
332 | + } | |
333 | + } | |
334 | + } | |
335 | + } | |
336 | + | |
337 | + internal void ClearWork( ) | |
338 | + { | |
339 | + #if DEBUG | |
340 | + Logger.Debug("Manually Clearing Work Item stack & Anvil state...the hard way."); | |
341 | + #endif | |
342 | + | |
343 | + workItemStack_R(bea) = null;//bea.workItemStack = null; | |
344 | + returnOnCancelStack_R(bea) = null; //Needed? | |
345 | + bea.Voxels = new byte[16, 6, 16]; | |
346 | + bea.rotation = 0; | |
347 | + bea.SelectedRecipeId = -1; | |
348 | + bea.MarkDirty(false, null); | |
349 | + } | |
255 | 350 | } |
256 | 351 | } |
257 | 352 |
@@ -89,6 +89,11 @@ namespace AnvilMetalRecovery | ||
89 | 89 | return checkCode.Valid && checkCode.Path.Split('-').First().Equals(term, StringComparison.OrdinalIgnoreCase); |
90 | 90 | } |
91 | 91 | |
92 | + internal static string PathEnding(this AssetLocation inputCode) | |
93 | + { | |
94 | + return inputCode.Path.Split('-').Last( ); | |
95 | + } | |
96 | + | |
92 | 97 | internal static AssetLocation AppendPathVariant(this AssetLocation toAppend, string append) |
93 | 98 | { |
94 | 99 | var appendedCode = toAppend.Clone( ); |
@@ -110,4 +115,3 @@ namespace AnvilMetalRecovery | ||
110 | 115 | } |
111 | 116 | } |
112 | 117 | } |
113 | - |
@@ -1,7 +1,7 @@ | ||
1 | 1 | { |
2 | 2 | "type": "code", |
3 | 3 | "name": "Anvil Metal Recovery+", |
4 | - "description" : "Get back smithing discards and broken tool scrap! Plus lots more.", | |
4 | + "description" : "Get back smithing discards, broken tool scrap, failed molds; and MORE!", | |
5 | 5 | "authors": ["Melchior"], |
6 | 6 | "ModID":"metalrecovery", |
7 | 7 | "version": "0.1.18-rc.1", |