|
@@ -97,7 +97,8 @@ void InventoryInteraction::transaction(Inventory* zSource, Inventory* zTarget, I
|
|
while (sourceSlot && (sourceSlot->getNumberOfItems() == 0 || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
|
|
while (sourceSlot && (sourceSlot->getNumberOfItems() == 0 || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
|
|
sourceSlot++;
|
|
sourceSlot++;
|
|
if (!sourceSlot) break;
|
|
if (!sourceSlot) break;
|
|
- // TODO: use target cache ot get list of slots that already contains the source item
|
|
|
|
|
|
+ // TODO: use target cache ot get list of slots that already contains the source ite
|
|
|
|
+ bool needNext = 1;
|
|
for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot; )
|
|
for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot; )
|
|
{
|
|
{
|
|
while (targetSlot && (targetSlot->isFull() || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
|
|
while (targetSlot && (targetSlot->isFull() || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
|
|
@@ -124,7 +125,8 @@ void InventoryInteraction::transaction(Inventory* zSource, Inventory* zTarget, I
|
|
stack->release();
|
|
stack->release();
|
|
count -= number;
|
|
count -= number;
|
|
if (count == 0)
|
|
if (count == 0)
|
|
- break;
|
|
|
|
|
|
+ return;
|
|
|
|
+ needNext = 0;
|
|
}
|
|
}
|
|
else
|
|
else
|
|
targetSlot++;
|
|
targetSlot++;
|
|
@@ -158,6 +160,7 @@ void InventoryInteraction::transaction(Inventory* zSource, Inventory* zTarget, I
|
|
count -= number;
|
|
count -= number;
|
|
if (count == 0)
|
|
if (count == 0)
|
|
return;
|
|
return;
|
|
|
|
+ needNext = 0;
|
|
}
|
|
}
|
|
else
|
|
else
|
|
targetSlot++;
|
|
targetSlot++;
|
|
@@ -171,6 +174,8 @@ void InventoryInteraction::transaction(Inventory* zSource, Inventory* zTarget, I
|
|
if (sourceSlot->getNumberOfItems() == 0)
|
|
if (sourceSlot->getNumberOfItems() == 0)
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
+ if (needNext)
|
|
|
|
+ sourceSlot++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -305,6 +310,8 @@ void Inventory::afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem
|
|
*(int*)(message + 5) = zSlot->getNumberOfItems();
|
|
*(int*)(message + 5) = zSlot->getNumberOfItems();
|
|
msg.setMessage(message, 9, 0);
|
|
msg.setMessage(message, 9, 0);
|
|
notyObservers(msg);
|
|
notyObservers(msg);
|
|
|
|
+ for (auto call : afterPullStackCalls)
|
|
|
|
+ call(zSlot, dir, zItem, count);
|
|
}
|
|
}
|
|
|
|
|
|
void Inventory::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
|
|
void Inventory::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
|
|
@@ -335,6 +342,8 @@ void Inventory::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem
|
|
msg.setMessage(message, 29, 0);
|
|
msg.setMessage(message, 29, 0);
|
|
notyObservers(msg);
|
|
notyObservers(msg);
|
|
}
|
|
}
|
|
|
|
+ for (auto call : afterPushStackCalls)
|
|
|
|
+ call(zSlot, dir, zItem, count);
|
|
}
|
|
}
|
|
|
|
|
|
void Inventory::loadInventory(Framework::StreamReader* zReader)
|
|
void Inventory::loadInventory(Framework::StreamReader* zReader)
|
|
@@ -418,6 +427,23 @@ void Inventory::removeObserver(Entity* zSource, Framework::Text id)
|
|
cs.unlock();
|
|
cs.unlock();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Inventory::addObserver(Entity* zSource, Framework::Text id)
|
|
|
|
+{
|
|
|
|
+ cs.lock();
|
|
|
|
+ for (auto observer : observers)
|
|
|
|
+ {
|
|
|
|
+ if (observer.getFirst() == zSource->getId() && observer.getSecond().istGleich(id))
|
|
|
|
+ {
|
|
|
|
+ cs.unlock();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ observers.add(ImmutablePair<int, Text>(zSource->getId(), id));
|
|
|
|
+ cs.unlock();
|
|
|
|
+ for (auto call : observerAddedCalls)
|
|
|
|
+ call(zSource, id);
|
|
|
|
+}
|
|
|
|
+
|
|
const ItemSlot* Inventory::zSlot(int id) const
|
|
const ItemSlot* Inventory::zSlot(int id) const
|
|
{
|
|
{
|
|
if (itemCache)
|
|
if (itemCache)
|
|
@@ -527,9 +553,9 @@ void Inventory::localTransaction(Array< ItemSlot* >* zSourceSlots, Array< ItemSl
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void Inventory::addItems(ItemStack* items, Direction dir)
|
|
|
|
|
|
+void Inventory::addItems(ItemStack* zItems, Direction dir)
|
|
{
|
|
{
|
|
- if (itemCache && items && items->getSize() > 0)
|
|
|
|
|
|
+ if (itemCache && zItems && zItems->getSize() > 0)
|
|
{
|
|
{
|
|
cs.lock();
|
|
cs.lock();
|
|
for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
|
|
for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
|
|
@@ -538,32 +564,35 @@ void Inventory::addItems(ItemStack* items, Direction dir)
|
|
{
|
|
{
|
|
if (targetSlot->zStack())
|
|
if (targetSlot->zStack())
|
|
{
|
|
{
|
|
- int number = MIN(targetSlot->numberOfAddableItems(items, dir), items->getSize());
|
|
|
|
- int tmp = number;
|
|
|
|
- if (number > 0 && allowPushStack(targetSlot, dir, items->zItem(), tmp))
|
|
|
|
|
|
+ if (targetSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
|
|
{
|
|
{
|
|
- number = MIN(number, tmp);
|
|
|
|
- ItemStack* stack = items->split(number);
|
|
|
|
- if (stack)
|
|
|
|
|
|
+ int number = MIN(targetSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
|
|
|
|
+ int tmp = number;
|
|
|
|
+ if (number > 0 && allowPushStack(targetSlot, dir, zItems->zItem(), tmp))
|
|
{
|
|
{
|
|
- targetSlot->addItems(stack, dir);
|
|
|
|
- afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
|
|
|
|
- if (stack->getSize())
|
|
|
|
- throw stack;
|
|
|
|
- stack->release();
|
|
|
|
- if (!items->getSize())
|
|
|
|
- break;
|
|
|
|
|
|
+ number = MIN(number, tmp);
|
|
|
|
+ ItemStack* stack = zItems->split(number);
|
|
|
|
+ if (stack)
|
|
|
|
+ {
|
|
|
|
+ targetSlot->addItems(stack, dir);
|
|
|
|
+ afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
|
|
|
|
+ if (stack->getSize())
|
|
|
|
+ throw stack;
|
|
|
|
+ stack->release();
|
|
|
|
+ if (!zItems->getSize())
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- int number = MIN(targetSlot->numberOfAddableItems(items, dir), items->getSize());
|
|
|
|
|
|
+ int number = MIN(targetSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
|
|
int tmp = number;
|
|
int tmp = number;
|
|
- if (number > 0 && allowPushStack(targetSlot, dir, items->zItem(), tmp))
|
|
|
|
|
|
+ if (number > 0 && allowPushStack(targetSlot, dir, zItems->zItem(), tmp))
|
|
{
|
|
{
|
|
number = MIN(number, tmp);
|
|
number = MIN(number, tmp);
|
|
- ItemStack* stack = items->split(number);
|
|
|
|
|
|
+ ItemStack* stack = zItems->split(number);
|
|
if (stack)
|
|
if (stack)
|
|
{
|
|
{
|
|
targetSlot->addItems(stack, dir);
|
|
targetSlot->addItems(stack, dir);
|
|
@@ -572,7 +601,7 @@ void Inventory::addItems(ItemStack* items, Direction dir)
|
|
if (stack->getSize())
|
|
if (stack->getSize())
|
|
throw stack;
|
|
throw stack;
|
|
stack->release();
|
|
stack->release();
|
|
- if (!items->getSize())
|
|
|
|
|
|
+ if (!zItems->getSize())
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -583,6 +612,30 @@ void Inventory::addItems(ItemStack* items, Direction dir)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
|
|
|
|
+{
|
|
|
|
+ if (zSlot->zStack() && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
|
|
|
|
+ return;
|
|
|
|
+ bool needUpdate = !zSlot->zStack();
|
|
|
|
+ int number = MIN(zSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
|
|
|
|
+ int tmp = number;
|
|
|
|
+ if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
|
|
|
|
+ {
|
|
|
|
+ number = MIN(number, tmp);
|
|
|
|
+ ItemStack* stack = zItems->split(number);
|
|
|
|
+ if (stack)
|
|
|
|
+ {
|
|
|
|
+ zSlot->addItems(stack, dir);
|
|
|
|
+ if (needUpdate)
|
|
|
|
+ updateCache(zSlot, -1);
|
|
|
|
+ afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
|
|
|
|
+ if (stack->getSize())
|
|
|
|
+ throw stack;
|
|
|
|
+ stack->release();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
|
|
ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
|
|
{
|
|
{
|
|
if (allowPullStack(zSlot, dir))
|
|
if (allowPullStack(zSlot, dir))
|
|
@@ -646,7 +699,7 @@ void Inventory::inventoryApi(Framework::StreamReader* zRequest, NetworkMessage*
|
|
zRequest->lese(id, idLen);
|
|
zRequest->lese(id, idLen);
|
|
id[(int)idLen] = 0;
|
|
id[(int)idLen] = 0;
|
|
zResponse->adressGui(id);
|
|
zResponse->adressGui(id);
|
|
- observers.add(ImmutablePair<int, Text>(zSource->getId(), id));
|
|
|
|
|
|
+ addObserver(zSource, id);
|
|
delete[] id;
|
|
delete[] id;
|
|
char filterLen;
|
|
char filterLen;
|
|
zRequest->lese(&filterLen, 1);
|
|
zRequest->lese(&filterLen, 1);
|
|
@@ -697,4 +750,19 @@ void Inventory::inventoryApi(Framework::StreamReader* zRequest, NetworkMessage*
|
|
delete[] id;
|
|
delete[] id;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Inventory::registerAfterPullStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
|
|
|
|
+{
|
|
|
|
+ afterPullStackCalls.add(call);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Inventory::registerAfterPushStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
|
|
|
|
+{
|
|
|
|
+ afterPushStackCalls.add(call);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Inventory::registerObserverAddedCall(std::function<void(Entity* zSource, Framework::Text id)> call)
|
|
|
|
+{
|
|
|
|
+ observerAddedCalls.add(call);
|
|
}
|
|
}
|