From 8ac5b6a0e193e4d0a42cbc99694b01f9106f67b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6ra?= Date: Thu, 31 Aug 2023 00:24:38 +0200 Subject: [PATCH] fix: generic handspindle --- .../tailorsstory/itemtypes/handspindle.json | 8 +- .../recipes/smithing/needle4.json | 8 +- .../handspindle-use1.json} | 0 .../handspindle-use2.json} | 0 .../handspindle-use3.json} | 0 .../item/{ => handspindle}/handspindle.json | 0 .../src/Attributes/SpinnableAttributes.cs | 12 +- tailorsstory/src/BlockEntity/BESpinnwheel.cs | 6 +- tailorsstory/src/Item/ItemHandspindle.cs | 136 ++++++++++++------ 9 files changed, 115 insertions(+), 55 deletions(-) rename tailorsstory/assets/tailorsstory/shapes/item/{spindleUse1.json => handspindle/handspindle-use1.json} (100%) rename tailorsstory/assets/tailorsstory/shapes/item/{spindleUse2.json => handspindle/handspindle-use2.json} (100%) rename tailorsstory/assets/tailorsstory/shapes/item/{spindleUse3.json => handspindle/handspindle-use3.json} (100%) rename tailorsstory/assets/tailorsstory/shapes/item/{ => handspindle}/handspindle.json (100%) diff --git a/tailorsstory/assets/tailorsstory/itemtypes/handspindle.json b/tailorsstory/assets/tailorsstory/itemtypes/handspindle.json index 9c58a8d..8795226 100644 --- a/tailorsstory/assets/tailorsstory/itemtypes/handspindle.json +++ b/tailorsstory/assets/tailorsstory/itemtypes/handspindle.json @@ -29,16 +29,16 @@ } ], "shape": { - "base": "item/handspindle", + "base": "item/handspindle/handspindle", "alternates": [ { - "base": "item/spindleUse1" + "base": "item/handspindle/handspindle-use1" }, { - "base": "item/spindleUse2" + "base": "item/handspindle/handspindle-use2" }, { - "base": "item/spindleUse3" + "base": "item/handspindle/handspindle-use3" } ] }, diff --git a/tailorsstory/assets/tailorsstory/recipes/smithing/needle4.json b/tailorsstory/assets/tailorsstory/recipes/smithing/needle4.json index 7def267..b8787df 100644 --- a/tailorsstory/assets/tailorsstory/recipes/smithing/needle4.json +++ b/tailorsstory/assets/tailorsstory/recipes/smithing/needle4.json @@ -5,11 +5,11 @@ "name": "metal", "allowedVariants": [ "copper", - "brass", - "tinbronze", "blackbronze", - "titanium", - "steel" + "iron", + "meteoriciron", + "steel", + "tinbronze" ] }, "pattern": [ diff --git a/tailorsstory/assets/tailorsstory/shapes/item/spindleUse1.json b/tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use1.json similarity index 100% rename from tailorsstory/assets/tailorsstory/shapes/item/spindleUse1.json rename to tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use1.json diff --git a/tailorsstory/assets/tailorsstory/shapes/item/spindleUse2.json b/tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use2.json similarity index 100% rename from tailorsstory/assets/tailorsstory/shapes/item/spindleUse2.json rename to tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use2.json diff --git a/tailorsstory/assets/tailorsstory/shapes/item/spindleUse3.json b/tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use3.json similarity index 100% rename from tailorsstory/assets/tailorsstory/shapes/item/spindleUse3.json rename to tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle-use3.json diff --git a/tailorsstory/assets/tailorsstory/shapes/item/handspindle.json b/tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle.json similarity index 100% rename from tailorsstory/assets/tailorsstory/shapes/item/handspindle.json rename to tailorsstory/assets/tailorsstory/shapes/item/handspindle/handspindle.json diff --git a/tailorsstory/src/Attributes/SpinnableAttributes.cs b/tailorsstory/src/Attributes/SpinnableAttributes.cs index 5a7b08d..3eb3880 100644 --- a/tailorsstory/src/Attributes/SpinnableAttributes.cs +++ b/tailorsstory/src/Attributes/SpinnableAttributes.cs @@ -29,7 +29,7 @@ public SpinnableAttributes(CollectibleObject collectible) } } - public JsonItemStack getJsonItemStack() + public JsonItemStack GetJsonItemStack() { if (!isSpinnable) return null; @@ -40,5 +40,15 @@ public JsonItemStack getJsonItemStack() StackSize = outputStackSize }; } + + public ItemStack GetItemStack(IWorldAccessor resolver) + { + if (!isSpinnable) return null; + + JsonItemStack jsonItemStack = GetJsonItemStack(); + bool resolved = jsonItemStack.Resolve(resolver, "spinning"); + + return resolved ? jsonItemStack.ResolvedItemstack : null; + } } } diff --git a/tailorsstory/src/BlockEntity/BESpinnwheel.cs b/tailorsstory/src/BlockEntity/BESpinnwheel.cs index 6b40955..a4b3f56 100644 --- a/tailorsstory/src/BlockEntity/BESpinnwheel.cs +++ b/tailorsstory/src/BlockEntity/BESpinnwheel.cs @@ -157,11 +157,9 @@ private void spinInput() { int requiredMaterial = InputSpinnableAttributes.inputStackSize; if (InputSlot.Itemstack.StackSize < requiredMaterial) return; - JsonItemStack jsonItemStack = InputSpinnableAttributes.getJsonItemStack(); - bool resolve = jsonItemStack.Resolve(Api.World, "spinning"); - if (!resolve) return; - ItemStack spinnedStack = jsonItemStack.ResolvedItemstack; + ItemStack spinnedStack = InputSpinnableAttributes.GetItemStack(Api.World); + if (spinnedStack == null) return; if (OutputSlot.Itemstack == null) { diff --git a/tailorsstory/src/Item/ItemHandspindle.cs b/tailorsstory/src/Item/ItemHandspindle.cs index 4ff2b43..dbf6774 100644 --- a/tailorsstory/src/Item/ItemHandspindle.cs +++ b/tailorsstory/src/Item/ItemHandspindle.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Vintagestory.API.Common; using Vintagestory.API.MathTools; @@ -6,34 +7,23 @@ namespace tailorsstory { public class ItemHandspindle : Item { - private readonly float TIME_TO_SPIN = 3f; - - protected int getFlaxfibreCount(EntityAgent byPlayer) - { - int count = 0; - byPlayer.WalkInventory((invslot) => - { - // TODO: make generic - if (invslot.Itemstack != null && invslot.Itemstack.Collectible.Code.Path.StartsWith("flaxfibers")) - { - count += invslot.Itemstack.StackSize; - } - - return true; - }); - - return count; - } + private readonly int SPIN_TIME_IN_SECONDS = 3; + private CollectibleObject SpinnableObject; public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) { - if (getFlaxfibreCount(byEntity) > 4) handling = EnumHandHandling.Handled; + CollectibleObject spinnableObject = GetSpinnableObject(byEntity as EntityPlayer); + if (spinnableObject != null) + { + SpinnableObject = spinnableObject; + handling = EnumHandHandling.Handled; + } } public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) { - int renderVariant = GameMath.Clamp((int)Math.Ceiling(secondsUsed / TIME_TO_SPIN * 4), 0, 4); + int renderVariant = GameMath.Clamp((int)Math.Ceiling(secondsUsed / SPIN_TIME_IN_SECONDS * 4), 0, 4); int prevRenderVariant = slot.Itemstack.Attributes.GetInt("renderVariant", 0); slot.Itemstack.TempAttributes.SetInt("renderVariant", renderVariant); @@ -45,40 +35,102 @@ public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, Entity } - if (secondsUsed > TIME_TO_SPIN) + if (secondsUsed > SPIN_TIME_IN_SECONDS) { - if (getFlaxfibreCount(byEntity) < 4) return false; + SpinInput(byEntity as EntityPlayer); + return false; + } - IWorldAccessor world = byEntity.World; + return true; + } - IPlayer byPlayer = null; - if (byEntity is EntityPlayer) byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID); + public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel) + { + SpinnableObject = null; - int flaxfibersCost = 4; - byEntity.WalkInventory((invslot) => - { - if (invslot.Itemstack != null && invslot.Itemstack.Collectible.Code.Path.Equals("flaxfibers")) - { - int takeOutCount = Math.Min(flaxfibersCost, invslot.StackSize); - invslot.TakeOut(takeOutCount); - flaxfibersCost -= takeOutCount; + if (slot.Itemstack.Attributes.GetInt("renderVariant", 0) != 0) + { + slot.Itemstack.TempAttributes.SetInt("renderVariant", 0); + slot.Itemstack.Attributes.SetInt("renderVariant", 0); + (byEntity as EntityPlayer)?.Player?.InventoryManager.BroadcastHotbarSlot(); + } + } - if (flaxfibersCost <= 0) return false; - } - return true; - }); + private void SpinInput(EntityPlayer byPlayer) + { + if (SpinnableObject == null) return; - ItemStack stack = new ItemStack(world.GetItem(new AssetLocation("flaxtwine")), 4); + SpinnableAttributes spinnableAttributes = new SpinnableAttributes(SpinnableObject); - if (byPlayer?.InventoryManager.TryGiveItemstack(stack) == false) + int cost = spinnableAttributes.inputStackSize; + + byPlayer.WalkInventory((invslot) => + { + if (invslot.Itemstack != null && invslot.Itemstack.Id.Equals(SpinnableObject.Id)) { - byEntity.World.SpawnItemEntity(stack, byEntity.SidedPos.XYZ); + int takeOutCount = Math.Min(cost, invslot.StackSize); + invslot.TakeOut(takeOutCount); + cost -= takeOutCount; + + if (cost <= 0) return false; } + return true; + }); - return false; + ItemStack spunStack = spinnableAttributes.GetItemStack(byPlayer.World); + if (spunStack == null) return; + + if (byPlayer.TryGiveItemStack(spunStack) == false) + { + byPlayer.World.SpawnItemEntity(spunStack, byPlayer.SidedPos.XYZ); } + } - return true; + private static int GetItemCountForItemId(EntityPlayer byPlayer, int itemId) + { + int count = 0; + + byPlayer.WalkInventory((invslot) => + { + if (invslot.Itemstack != null && invslot.Itemstack.Id == itemId) + { + count += invslot.Itemstack.StackSize; + } + + return true; + }); + + return count; + } + + private static CollectibleObject GetSpinnableObject(EntityPlayer byPlayer) + { + HashSet alreadyCheckedItems = new(); + CollectibleObject spinnableObject = null; + + byPlayer.WalkInventory((invslot) => + { + if (invslot.Itemstack != null && !alreadyCheckedItems.Contains(invslot.Itemstack.Collectible.Id)) + { + SpinnableAttributes spinnableAttributes = new(invslot.Itemstack.Collectible); + + if (spinnableAttributes.isSpinnable) + { + int countInInventory = GetItemCountForItemId(byPlayer, invslot.Itemstack.Collectible.Id); + if (countInInventory >= spinnableAttributes.inputStackSize) + { + spinnableObject = invslot.Itemstack.Collectible; + return false; + } + } + + alreadyCheckedItems.Add(invslot.Itemstack.Collectible.Id); + } + + return true; + }); + + return spinnableObject; } } }