Skip to content

Commit

Permalink
fix: generic handspindle
Browse files Browse the repository at this point in the history
  • Loading branch information
hoersamu committed Aug 30, 2023
1 parent 125bdd7 commit 8ac5b6a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 55 deletions.
8 changes: 4 additions & 4 deletions tailorsstory/assets/tailorsstory/itemtypes/handspindle.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"name": "metal",
"allowedVariants": [
"copper",
"brass",
"tinbronze",
"blackbronze",
"titanium",
"steel"
"iron",
"meteoriciron",
"steel",
"tinbronze"
]
},
"pattern": [
Expand Down
12 changes: 11 additions & 1 deletion tailorsstory/src/Attributes/SpinnableAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SpinnableAttributes(CollectibleObject collectible)
}
}

public JsonItemStack getJsonItemStack()
public JsonItemStack GetJsonItemStack()
{
if (!isSpinnable) return null;

Expand All @@ -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;
}
}
}
6 changes: 2 additions & 4 deletions tailorsstory/src/BlockEntity/BESpinnwheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
136 changes: 94 additions & 42 deletions tailorsstory/src/Item/ItemHandspindle.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
using System;
using System.Collections.Generic;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;

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);
Expand All @@ -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<int> 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;
}
}
}

0 comments on commit 8ac5b6a

Please sign in to comment.