Skip to content

Commit

Permalink
Run callback in IDropAction, refactor function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker authored and nerzhul committed Apr 2, 2018
1 parent f6eff57 commit 9fa547b
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 263 deletions.
95 changes: 50 additions & 45 deletions src/inventorymanager.cpp
Expand Up @@ -266,23 +266,22 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::DETACHED &&
from_inv.name == to_inv.name) {
src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowMove(
from_inv.name, from_list, from_i,
to_list, to_i, try_take_count, player);
*this, try_take_count, player);
dst_can_put_count = src_can_take_count;
} else {
// Destination is detached
if (to_inv.type == InventoryLocation::DETACHED) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
dst_can_put_count = PLAYER_TO_SA(player)->detached_inventory_AllowPut(
to_inv.name, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is detached
if (from_inv.type == InventoryLocation::DETACHED) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
from_inv.name, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand All @@ -294,23 +293,22 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::NODEMETA &&
from_inv.p == to_inv.p) {
src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowMove(
from_inv.p, from_list, from_i,
to_list, to_i, try_take_count, player);
*this, try_take_count, player);
dst_can_put_count = src_can_take_count;
} else {
// Destination is nodemeta
if (to_inv.type == InventoryLocation::NODEMETA) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
dst_can_put_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowPut(
to_inv.p, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is nodemeta
if (from_inv.type == InventoryLocation::NODEMETA) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
from_inv.p, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand All @@ -321,23 +319,22 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::PLAYER &&
from_inv.name == to_inv.name) {
src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowMove(
from_inv, from_list, from_i,
to_list, to_i, try_take_count, player);
*this, try_take_count, player);
dst_can_put_count = src_can_take_count;
} else {
// Destination is a player
if (to_inv.type == InventoryLocation::PLAYER) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
dst_can_put_count = PLAYER_TO_SA(player)->player_inventory_AllowPut(
to_inv, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is a player
if (from_inv.type == InventoryLocation::PLAYER) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
from_inv, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand Down Expand Up @@ -478,18 +475,17 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::DETACHED &&
from_inv.name == to_inv.name) {
PLAYER_TO_SA(player)->detached_inventory_OnMove(
from_inv.name, from_list, from_i,
to_list, to_i, count, player);
*this, count, player);
} else {
// Destination is detached
if (to_inv.type == InventoryLocation::DETACHED) {
PLAYER_TO_SA(player)->detached_inventory_OnPut(
to_inv.name, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is detached
if (from_inv.type == InventoryLocation::DETACHED) {
PLAYER_TO_SA(player)->detached_inventory_OnTake(
from_inv.name, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand All @@ -500,18 +496,17 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::NODEMETA &&
from_inv.p == to_inv.p) {
PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
from_inv.p, from_list, from_i,
to_list, to_i, count, player);
*this, count, player);
} else {
// Destination is nodemeta
if (to_inv.type == InventoryLocation::NODEMETA) {
PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
to_inv.p, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is nodemeta
if (from_inv.type == InventoryLocation::NODEMETA) {
PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
from_inv.p, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand All @@ -522,18 +517,17 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
to_inv.type == InventoryLocation::PLAYER &&
from_inv.name == to_inv.name) {
PLAYER_TO_SA(player)->player_inventory_OnMove(
from_inv, from_list, from_i,
to_list, to_i, count, player);
*this, count, player);
} else {
// Destination is player inventory
if (to_inv.type == InventoryLocation::PLAYER) {
PLAYER_TO_SA(player)->player_inventory_OnPut(
to_inv, to_list, to_i, src_item, player);
*this, src_item, player);
}
// Source is player inventory
if (from_inv.type == InventoryLocation::PLAYER) {
PLAYER_TO_SA(player)->player_inventory_OnTake(
from_inv, from_list, from_i, src_item, player);
*this, src_item, player);
}
}

Expand Down Expand Up @@ -635,28 +629,34 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
take_count = count;
int src_can_take_count = take_count;

// Source is detached
if (from_inv.type == InventoryLocation::DETACHED) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = take_count;
src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
from_inv.name, from_list, from_i, src_item, player);
}
ItemStack src_item = list_from->getItem(from_i);
src_item.count = take_count;

// Source is nodemeta
if (from_inv.type == InventoryLocation::NODEMETA) {
ItemStack src_item = list_from->getItem(from_i);
src_item.count = take_count;
// Run callbacks depending on source inventory
switch (from_inv.type) {
case InventoryLocation::DETACHED:
src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
*this, src_item, player);
break;
case InventoryLocation::NODEMETA:
src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
from_inv.p, from_list, from_i, src_item, player);
*this, src_item, player);
break;
case InventoryLocation::PLAYER:
src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
*this, src_item, player);
break;
default:
break;
}

if (src_can_take_count != -1 && src_can_take_count < take_count)
take_count = src_can_take_count;

int actually_dropped_count = 0;

ItemStack src_item = list_from->getItem(from_i);
// Update item due executed callbacks
src_item = list_from->getItem(from_i);

// Drop the item
ItemStack item1 = list_from->getItem(from_i);
Expand Down Expand Up @@ -694,16 +694,21 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
Report drop to endpoints
*/

// Source is detached
if (from_inv.type == InventoryLocation::DETACHED) {
switch (from_inv.type) {
case InventoryLocation::DETACHED:
PLAYER_TO_SA(player)->detached_inventory_OnTake(
from_inv.name, from_list, from_i, src_item, player);
}

// Source is nodemeta
if (from_inv.type == InventoryLocation::NODEMETA) {
*this, src_item, player);
break;
case InventoryLocation::NODEMETA:
PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
from_inv.p, from_list, from_i, src_item, player);
*this, src_item, player);
break;
case InventoryLocation::PLAYER:
PLAYER_TO_SA(player)->player_inventory_OnTake(
*this, src_item, player);
break;
default:
break;
}

/*
Expand Down
15 changes: 8 additions & 7 deletions src/inventorymanager.h
Expand Up @@ -134,16 +134,20 @@ struct InventoryAction
virtual ~InventoryAction() = default;;
};

struct IMoveAction : public InventoryAction
struct MoveAction
{
// count=0 means "everything"
u16 count = 0;
InventoryLocation from_inv;
std::string from_list;
s16 from_i = -1;
InventoryLocation to_inv;
std::string to_list;
s16 to_i = -1;
};

struct IMoveAction : public InventoryAction, public MoveAction
{
// count=0 means "everything"
u16 count = 0;
bool move_somewhere = false;

// treat these as private
Expand Down Expand Up @@ -181,13 +185,10 @@ struct IMoveAction : public InventoryAction
void clientApply(InventoryManager *mgr, IGameDef *gamedef);
};

struct IDropAction : public InventoryAction
struct IDropAction : public InventoryAction, public MoveAction
{
// count=0 means "everything"
u16 count = 0;
InventoryLocation from_inv;
std::string from_list;
s16 from_i = -1;

IDropAction() = default;

Expand Down

0 comments on commit 9fa547b

Please sign in to comment.