69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
using ButterflyPins.BlockEntities;
|
|
using System;
|
|
using Vintagestory.API.Common;
|
|
using Vintagestory.API.MathTools;
|
|
|
|
namespace ButterflyPins.Blocks;
|
|
|
|
public class BlockButterflyPinBoard : Block
|
|
{
|
|
private const string ButterflyPinPrefix = "clothes-butterflypin-";
|
|
|
|
public override bool OnBlockInteractStart(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel)
|
|
{
|
|
if (blockSel == null)
|
|
return base.OnBlockInteractStart(world, byPlayer, blockSel);
|
|
|
|
if (world.BlockAccessor.GetBlockEntity(blockSel.Position) is not BlockEntityButterflyPinBoard board)
|
|
return base.OnBlockInteractStart(world, byPlayer, blockSel);
|
|
|
|
int slotIndex = board.GetSlotIndex(blockSel.HitPosition);
|
|
if (slotIndex < 0)
|
|
return base.OnBlockInteractStart(world, byPlayer, blockSel);
|
|
|
|
ItemSlot activeSlot = byPlayer.InventoryManager.ActiveHotbarSlot;
|
|
if (world.Side == EnumAppSide.Client)
|
|
return (!activeSlot.Empty && board.CanInsert(slotIndex, activeSlot.Itemstack)) || board.CanTake(slotIndex);
|
|
|
|
if (!activeSlot.Empty && board.TryInsert(byPlayer, slotIndex, activeSlot))
|
|
return true;
|
|
|
|
return board.TryTake(byPlayer, slotIndex) || base.OnBlockInteractStart(world, byPlayer, blockSel);
|
|
}
|
|
|
|
public override void OnBlockBroken(IWorldAccessor world, BlockPos pos, IPlayer byPlayer, float dropQuantityMultiplier = 1)
|
|
{
|
|
if (world.Side == EnumAppSide.Server && world.BlockAccessor.GetBlockEntity(pos) is BlockEntityButterflyPinBoard board)
|
|
board.DropContents();
|
|
|
|
base.OnBlockBroken(world, pos, byPlayer, dropQuantityMultiplier);
|
|
}
|
|
|
|
public override bool ConsumeCraftingIngredients(ItemSlot[] slots, ItemSlot outputSlot, GridRecipe matchingRecipe)
|
|
{
|
|
bool preservedPin = false;
|
|
|
|
foreach (ItemSlot slot in slots)
|
|
{
|
|
if (slot.Empty)
|
|
continue;
|
|
|
|
if (!preservedPin && IsButterflyPin(slot.Itemstack))
|
|
{
|
|
preservedPin = true;
|
|
continue;
|
|
}
|
|
|
|
slot.TakeOut(1);
|
|
slot.MarkDirty();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool IsButterflyPin(ItemStack stack)
|
|
{
|
|
AssetLocation? code = stack.Collectible?.Code;
|
|
return code != null && code.Path.StartsWith(ButterflyPinPrefix, StringComparison.Ordinal);
|
|
}
|
|
} |