From b3c470593faa82cac96cd917eb426cc129c4430d Mon Sep 17 00:00:00 2001 From: melchior Date: Fri, 12 Aug 2022 19:56:12 -0400 Subject: [PATCH] Mold/Ingot recovery A-OK Also, steam particles as intended --- .../MoldDestructionRecovererBehavior.cs | 42 ++++++++++++++++++- AnvilMetalRecovery/Helpers.cs | 8 ++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/AnvilMetalRecovery/BlockBehaviors/MoldDestructionRecovererBehavior.cs b/AnvilMetalRecovery/BlockBehaviors/MoldDestructionRecovererBehavior.cs index 3ecdabe..70f3c72 100644 --- a/AnvilMetalRecovery/BlockBehaviors/MoldDestructionRecovererBehavior.cs +++ b/AnvilMetalRecovery/BlockBehaviors/MoldDestructionRecovererBehavior.cs @@ -14,6 +14,10 @@ namespace AnvilMetalRecovery public class MoldDestructionRecovererBehavior : BlockBehavior { public static readonly string BehaviorClassName = @"MoldDestructionRecoverer"; + private readonly AssetLocation MetalBits_partial = new AssetLocation(GlobalConstants.DefaultDomain, @"metalbit"); + private const int shavingValue = 5; + + public MoldDestructionRecovererBehavior(Block block) : base(block) { @@ -41,17 +45,53 @@ namespace AnvilMetalRecovery #if DEBUG world.Api.Logger.VerboseDebug("{0} Ingot Mold(s) with L {1} Units, R {2} Units", ingotMold.quantityMolds, ingotMold.fillLevelLeft, ingotMold.fillLevelRight); #endif - } + if ( ingotMold.fillLevelLeft >= shavingValue && ingotMold.contentsLeft != null) + { + var ingotMetal = ingotMold.contentsLeft.Collectible.Variant[@"metal"]; + SpawnMetalBits(world, pos, ingotMold.fillLevelLeft, ingotMetal); + } + + if ( ingotMold.fillLevelRight >= shavingValue && ingotMold.contentsRight != null) + { + var ingotMetal = ingotMold.contentsLeft.Collectible.Variant[@"metal"]; + SpawnMetalBits(world, pos, ingotMold.fillLevelRight, ingotMetal); + } + + return; + } if (someBlockEntity is BlockEntityToolMold) { var toolMold = someBlockEntity as BlockEntityToolMold; #if DEBUG world.Api.Logger.VerboseDebug("Tool Mold with {0} Units", toolMold.fillLevel); #endif + if ( toolMold.fillLevel >= shavingValue && toolMold.metalContent != null) + { + var metalCode = toolMold.metalContent.Collectible.Variant.AnyKeys(@"metal", @"material"); + SpawnMetalBits(world, pos, toolMold.fillLevel, metalCode); + } } } + + internal void SpawnMetalBits(IWorldAccessor world, BlockPos pos, int unitQuantity, string baseMetalCode) + { + if (unitQuantity > 0) + { + int shavingQty = unitQuantity / shavingValue; + Item metalShavingsItem = world.Api.World.GetItem(MetalBits_partial.AppendPathVariant(baseMetalCode)); + + if (shavingQty >= 1 && metalShavingsItem != null) + { + var metalShavingsStack = new ItemStack(metalShavingsItem, shavingQty); + #if DEBUG + world.Api.Logger.VerboseDebug("Creating '{0}' @{1} *{2} Units",metalShavingsItem, pos, shavingQty); + #endif + world.Api.World.SpawnItemEntity(metalShavingsStack, pos.ToVec3d( ).Add(0.1d, 0, 0)); + } + } + } } } diff --git a/AnvilMetalRecovery/Helpers.cs b/AnvilMetalRecovery/Helpers.cs index 2b57b50..04f5e4b 100644 --- a/AnvilMetalRecovery/Helpers.cs +++ b/AnvilMetalRecovery/Helpers.cs @@ -100,6 +100,14 @@ namespace AnvilMetalRecovery { return parameters.All(parm => parm != null); } + + internal static string AnyKeys(this RelaxedReadOnlyDictionary source, params string[ ] keys) + { + foreach (var key in keys) { + if (source.ContainsKey(key)) return source[key]; + } + return String.Empty; + } } }