Files
ButterflyPinBoard/ButterflyPins/Blocks/BlockButterflyPinBoard.cs

38 lines
1.6 KiB
C#

using ButterflyPins.BlockEntities;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
namespace ButterflyPins.Blocks;
public class BlockButterflyPinBoard : Block
{
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);
}
}