Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow liquid interaction with ground storage #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Block/BlockGroundStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor
var beg = world.BlockAccessor.GetBlockEntity(selection.Position) as BlockEntityGroundStorage;
if (beg?.StorageProps != null)
{
WorldInteraction[] liquidInteractions = Array.Empty<WorldInteraction>();
ItemSlot slotLiquidContainer = beg.Inventory.FirstOrDefault(slot => !slot.Empty && slot.Itemstack.Collectible is BlockLiquidContainerBase);
if (slotLiquidContainer != null)
{
liquidInteractions = (slotLiquidContainer.Itemstack.Collectible as BlockLiquidContainerBase).interactions;
}

int bulkquantity = beg.StorageProps.BulkTransferQuantity;

if (beg.StorageProps.Layout == EnumGroundStorageLayout.Stacking && !beg.Inventory.Empty)
Expand Down Expand Up @@ -432,7 +439,7 @@ public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor
MouseButton = EnumMouseButton.Right
}

}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer));
}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer)).Append(liquidInteractions);
}

if (beg.StorageProps.Layout == EnumGroundStorageLayout.SingleCenter)
Expand All @@ -445,7 +452,7 @@ public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor
MouseButton = EnumMouseButton.Right
},

}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer));
}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer)).Append(liquidInteractions);
}

if (beg.StorageProps.Layout == EnumGroundStorageLayout.Halves || beg.StorageProps.Layout == EnumGroundStorageLayout.Quadrants)
Expand All @@ -466,9 +473,8 @@ public override WorldInteraction[] GetPlacedBlockInteractionHelp(IWorldAccessor
HotKeyCode = null
}

}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer));
}.Append(base.GetPlacedBlockInteractionHelp(world, selection, forPlayer)).Append(liquidInteractions);
}

}

return base.GetPlacedBlockInteractionHelp(world, selection, forPlayer);
Expand Down
55 changes: 55 additions & 0 deletions BlockEntity/BEGroundStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,61 @@ public bool putOrGetItemSingle(ItemSlot ourSlot, IPlayer player, BlockSelection
}

ItemSlot hotbarSlot = player.InventoryManager.ActiveHotbarSlot;

if (ourSlot?.Itemstack?.Collectible is ILiquidInterface liquidCnt1 && hotbarSlot?.Itemstack?.Collectible is ILiquidInterface liquidCnt2)
{
BlockLiquidContainerBase heldLiquidContainer = hotbarSlot.Itemstack.Collectible as BlockLiquidContainerBase;

CollectibleObject obj = hotbarSlot.Itemstack.Collectible;
bool singleTake = player.WorldData.EntityControls.ShiftKey;
bool singlePut = player.WorldData.EntityControls.CtrlKey;


if (obj is ILiquidSource liquidSource && liquidSource.AllowHeldLiquidTransfer && !singleTake)
{
ItemStack contentStackToMove = liquidSource.GetContent(hotbarSlot.Itemstack);

int moved = heldLiquidContainer.TryPutLiquid(
containerStack: ourSlot.Itemstack,
liquidStack: contentStackToMove,
desiredLitres: singlePut ? liquidSource.TransferSizeLitres : liquidSource.CapacityLitres);

if (moved > 0)
{
heldLiquidContainer.SplitStackAndPerformAction(player.Entity, hotbarSlot, delegate (ItemStack stack)
{
liquidSource.TryTakeContent(stack, moved);
return moved;
});
heldLiquidContainer.DoLiquidMovedEffects(player, contentStackToMove, moved, BlockLiquidContainerBase.EnumLiquidDirection.Pour);

BlockGroundStorage.IsUsingContainedBlock = true;
isUsingSlot = ourSlot;
return true;
}
}

if (obj is ILiquidSink liquidSink && liquidSink.AllowHeldLiquidTransfer && !singlePut)
{
ItemStack owncontentStack = heldLiquidContainer.GetContent(ourSlot.Itemstack);
if (owncontentStack != null)
{
ItemStack liquidStackForParticles = owncontentStack.Clone();
float litres = (singleTake ? liquidSink.TransferSizeLitres : liquidSink.CapacityLitres);
int moved = heldLiquidContainer.SplitStackAndPerformAction(player.Entity, hotbarSlot, (ItemStack stack) => liquidSink.TryPutLiquid(stack, owncontentStack, litres));
if (moved > 0)
{
heldLiquidContainer.TryTakeContent(ourSlot.Itemstack, moved);
heldLiquidContainer.DoLiquidMovedEffects(player, liquidStackForParticles, moved, BlockLiquidContainerBase.EnumLiquidDirection.Fill);

BlockGroundStorage.IsUsingContainedBlock = true;
isUsingSlot = ourSlot;
return true;
}
}
}
}

if (!hotbarSlot.Empty && !inventory.Empty)
{
bool layoutEqual = StorageProps.Layout == hotbarSlot.Itemstack.Collectible.GetBehavior<CollectibleBehaviorGroundStorable>()?.StorageProps.Layout;
Expand Down
11 changes: 10 additions & 1 deletion Systems/Liquid/BlockLiquidContainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public virtual int GetContainerSlotId(ItemStack containerStack)
}

#region Interaction help
protected WorldInteraction[] interactions;
public WorldInteraction[] interactions { get; private set; }

public override void OnLoaded(ICoreAPI api)
{
Expand Down Expand Up @@ -642,6 +642,15 @@ protected override void tryEatBegin(ItemSlot slot, EntityAgent byEntity, ref Enu

public override void OnHeldInteractStart(ItemSlot itemslot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handHandling)
{
if (blockSel != null && api.World.BlockAccessor.GetBlockEntity(blockSel.Position) is BlockEntityGroundStorage begs)
{
ItemSlot gslot = begs.GetSlotAt(blockSel);
if (!gslot.Empty && gslot.Itemstack.Collectible is ILiquidInterface)
{
return;
}
}

if (blockSel == null || byEntity.Controls.ShiftKey)
{
if (byEntity.Controls.ShiftKey) base.OnHeldInteractStart(itemslot, byEntity, blockSel, entitySel, firstEvent, ref handHandling);
Expand Down