Skip to content
This repository has been archived by the owner on Oct 22, 2022. It is now read-only.

Commit

Permalink
Fix some right click actions being cancelled
Browse files Browse the repository at this point in the history
Such as interacting with a trader, and
eating food while looking at the sky.

- CanInteract now takes bool requireEmptyHands
- Bump version to 0.4.2
  • Loading branch information
copygirl committed Jun 11, 2019
1 parent 37e1787 commit aad2287
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CarryCapacity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<AssemblyTitle>CarryCapacity</AssemblyTitle>
<Authors>copygirl</Authors>
<Version>0.4.1</Version>
<Version>0.4.2</Version>

<Description>Vintage Story mod which adds the capability to carry various things</Description>
<RepositoryUrl>https://github.com/copygirl/CarryCapacity</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion resources/modinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "code",
"name": "CarryCapacity",
"modid": "carrycapacity",
"version": "0.4.1",
"version": "0.4.2",

"description" : "Adds the capability to carry various things",
"website": "https://github.com/copygirl/CarryCapacity",
Expand Down
41 changes: 24 additions & 17 deletions src/Common/CarryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,36 +107,43 @@ public void OnEntityAction(EnumEntityAction action, ref EnumHandling handled)
if (!isInteract || (_timeHeld > 0.0F)) return;

if (holdingAny != null) {
// If something's being carried in-hand or on shoulder and aiming at block, try to place it.
// If something's being held and aiming at block, try to place it.
if (selection != null) {
if (!CanInteract(player.Entity, CurrentAction.PlaceDown)) return;
// If carrying something in-hand, don't require empty hands.
// This shouldn't occur since nothing is supposed to go into
// an active slot while something is carried there. This is
// just in case, so a carried block can still be placed down.
if (!CanInteract(player.Entity, (carriedHands != null))) return;
// Make sure it's put on a solid top face of a block.
if (!CanPlace(world, selection, holdingAny)) return;
_action = CurrentAction.PlaceDown;
_targetSlot = holdingAny.Slot;
_selectedBlock = GetPlacedPosition(world, selection, holdingAny.Block);
}
// If something's being carried in-hand and aiming at nothing, try to put held block on back.
else if ((carriedBack == null) && (holdingAny.Behavior.Slots[CarrySlot.Back] != null)) {
if (!CanInteract(player.Entity, CurrentAction.SwapBack)) return;
_action = CurrentAction.SwapBack;
_targetSlot = holdingAny.Slot;
// If something's being held and aiming at nothing, try to put held block on back.
else {
if (!CanInteract(player.Entity, true)) return;
if ((carriedBack == null) && (holdingAny.Behavior.Slots[CarrySlot.Back] != null)) {
_action = CurrentAction.SwapBack;
_targetSlot = holdingAny.Slot;
}
}
}
// If nothing's being carried in-hand and aiming at carryable block, try to pick it up.
// If nothing's being held and aiming at carryable block, try to pick it up.
else if (selection != null) {
if (!CanInteract(player.Entity, CurrentAction.PickUp)) return;
if (!CanInteract(player.Entity, true)) return;
if ((_targetSlot = FindActionSlot(slot => block.IsCarryable(slot))) != null) {
_action = CurrentAction.PickUp;
_selectedBlock = selection.Position;
}
}
// If nothing's being carried in-hand and aiming at nothing or non-carryable block, try to grab block on back.
// If nothing's being held and aiming at nothing or non-carryable block, try to grab block on back.
else if (carriedBack != null) {
if (!CanInteract(player.Entity, CurrentAction.SwapBack)) return;
if (!CanInteract(player.Entity, true)) return;
if ((_targetSlot = FindActionSlot(slot => (carriedBack.Behavior.Slots[slot] != null))) != null)
_action = CurrentAction.SwapBack;
}
else return;

// Run this once to for validation. May reset action to None.
_timeHeld = 0.0F;
Expand All @@ -157,7 +164,7 @@ public void OnGameTick(float deltaTime)
// TODO: Only allow close blocks to be picked up.
// TODO: Don't allow the block underneath to change?

if (!CanInteract(player.Entity, _action))
if (!CanInteract(player.Entity, (_action != CurrentAction.PlaceDown) || (_targetSlot != CarrySlot.Hands)))
{ CancelInteraction(); return; }

var carriedTarget = _targetSlot.HasValue ? player.Entity.GetCarried(_targetSlot.Value) : null;
Expand Down Expand Up @@ -262,7 +269,7 @@ public void OnPickUpMessage(IServerPlayer player, PickUpMessage message)

var carried = player.Entity.GetCarried(message.Slot);
if ((message.Slot == CarrySlot.Back) || (carried != null) ||
!CanInteract(player.Entity, CurrentAction.PickUp) ||
!CanInteract(player.Entity, true) ||
!player.Entity.World.Claims.TryAccess(player, message.Position, EnumBlockAccessFlags.BuildOrBreak) ||
!player.Entity.Carry(message.Position, message.Slot))
InvalidCarry(player, message.Position);
Expand All @@ -274,7 +281,7 @@ public void OnPlaceDownMessage(IServerPlayer player, PlaceDownMessage message)

var carried = player.Entity.GetCarried(message.Slot);
if ((message.Slot == CarrySlot.Back) ||
!CanInteract(player.Entity, CurrentAction.PlaceDown) ||
!CanInteract(player.Entity, (message.Slot != CarrySlot.Hands)) ||
(carried == null) || !PlaceDown(player, carried, message.Selection, out var placedAt))
InvalidCarry(player, message.PlacedAt);
// If succeeded, but by chance the client's projected placement isn't
Expand All @@ -286,7 +293,7 @@ public void OnPlaceDownMessage(IServerPlayer player, PlaceDownMessage message)
public void OnSwapSlotsMessage(IServerPlayer player, SwapSlotsMessage message)
{
if ((message.First == message.Second) || (message.First != CarrySlot.Back) ||
!CanInteract(player.Entity, CurrentAction.SwapBack) ||
!CanInteract(player.Entity, true) ||
!player.Entity.Swap(message.First, message.Second))
player.Entity.WatchedAttributes.MarkPathDirty(CarriedBlock.ATTRIBUTE_ID);
}
Expand All @@ -296,11 +303,11 @@ public void OnSwapSlotsMessage(IServerPlayer player, SwapSlotsMessage message)
/// Returns whether the specified entity has the required prerequisites
/// to interact using CarryCapacity: Must be sneaking with an empty hand.
/// </summary>
private static bool CanInteract(EntityAgent entity, CurrentAction action)
private static bool CanInteract(EntityAgent entity, bool requireEmptyHanded)
{
var isSneaking = entity.Controls.Sneak;
var isEmptyHanded = entity.RightHandItemSlot.Empty && entity.LeftHandItemSlot.Empty;
return (isSneaking && ((action == CurrentAction.PlaceDown) || isEmptyHanded));
return (isSneaking && (!requireEmptyHanded || isEmptyHanded));
}

public static bool CanPlace(IWorldAccessor world, BlockSelection selection,
Expand Down

0 comments on commit aad2287

Please sign in to comment.