From 51b91befbd26b630cd2cecaec081edd03edfb5f3 Mon Sep 17 00:00:00 2001
From: Howaner
Date: Fri, 18 Jul 2014 23:11:59 +0200
Subject: Added RemoveItem() function to the player inventory.
---
MCServer/Plugins/Debuggers/Debuggers.lua | 40 ++++++++++++++++++++++++++++----
src/Inventory.cpp | 18 ++++++++++++++
src/Inventory.h | 3 +++
src/ItemGrid.cpp | 33 ++++++++++++++++++++++++++
src/ItemGrid.h | 3 +++
5 files changed, 93 insertions(+), 4 deletions(-)
diff --git a/MCServer/Plugins/Debuggers/Debuggers.lua b/MCServer/Plugins/Debuggers/Debuggers.lua
index 918204deb..95b36e854 100644
--- a/MCServer/Plugins/Debuggers/Debuggers.lua
+++ b/MCServer/Plugins/Debuggers/Debuggers.lua
@@ -60,9 +60,10 @@ function Initialize(Plugin)
PM:BindCommand("/ff", "debuggers", HandleFurnaceFuel, "- Shows how long the currently held item would burn in a furnace");
PM:BindCommand("/sched", "debuggers", HandleSched, "- Schedules a simple countdown using cWorld:ScheduleTask()");
PM:BindCommand("/cs", "debuggers", HandleChunkStay, "- Tests the ChunkStay Lua integration for the specified chunk coords");
- PM:BindCommand("/compo", "debuggers", HandleCompo, "- Tests the cCompositeChat bindings")
- PM:BindCommand("/sb", "debuggers", HandleSetBiome, "- Sets the biome around you to the specified one")
- PM:BindCommand("/wesel", "debuggers", HandleWESel, "- Expands the current WE selection by 1 block in X/Z")
+ PM:BindCommand("/compo", "debuggers", HandleCompo, "- Tests the cCompositeChat bindings");
+ PM:BindCommand("/sb", "debuggers", HandleSetBiome, "- Sets the biome around you to the specified one");
+ PM:BindCommand("/wesel", "debuggers", HandleWESel, "- Expands the current WE selection by 1 block in X/Z");
+ PM:BindCommand("/rmitem", "debuggers", HandleRMItem, "- Remove the specified item from the inventory.");
Plugin:AddWebTab("Debuggers", HandleRequest_Debuggers)
Plugin:AddWebTab("StressTest", HandleRequest_StressTest)
@@ -533,7 +534,7 @@ function OnTakeDamage(Receiver, TDI)
-- Receiver is cPawn
-- TDI is TakeDamageInfo
- LOG(Receiver:GetClass() .. " was dealt " .. DamageTypeToString(TDI.DamageType) .. " damage: Raw " .. TDI.RawDamage .. ", Final " .. TDI.FinalDamage .. " (" .. (TDI.RawDamage - TDI.FinalDamage) .. " covered by armor)");
+ -- LOG(Receiver:GetClass() .. " was dealt " .. DamageTypeToString(TDI.DamageType) .. " damage: Raw " .. TDI.RawDamage .. ", Final " .. TDI.FinalDamage .. " (" .. (TDI.RawDamage - TDI.FinalDamage) .. " covered by armor)");
return false;
end
@@ -1105,6 +1106,37 @@ end
+function HandleRMItem(a_Split, a_Player)
+ if ((#a_Split ~= 2) and (#a_Split ~= 3)) then
+ a_Player:SendMessage("Usage: /rmitem - [Count]")
+ return true
+ end
+
+ local Item = cItem()
+ if (not StringToItem(a_Split[2], Item)) then
+ a_Player:SendMessageFailure(a_Split[2] .. " isn't a valid item")
+ return true
+ end
+
+ if (#a_Split == 3) then
+ local Count = tonumber(a_Split[3])
+ if (Count == nil) then
+ a_Player:SendMessageFailure(a_Split[3] .. " isn't a valid number")
+ return true
+ end
+
+ Item.m_ItemCount = Count
+ end
+
+ local RemovedItems = a_Player:GetInventory():RemoveItem(Item)
+ a_Player:SendMessageSuccess("Removed " .. RemovedItems .. " Items!")
+ return true
+end
+
+
+
+
+
function HandleRequest_Debuggers(a_Request)
local FolderContents = cFile:GetFolderContents("./");
return "
The following objects have been returned by cFile:GetFolderContents():
- " .. table.concat(FolderContents, "
- ") .. "
";
diff --git a/src/Inventory.cpp b/src/Inventory.cpp
index 38d3c886d..5f7c24b60 100644
--- a/src/Inventory.cpp
+++ b/src/Inventory.cpp
@@ -151,6 +151,24 @@ int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a
+int cInventory::RemoveItem(const cItem & a_ItemStack)
+{
+ int RemovedItems = m_HotbarSlots.RemoveItem(a_ItemStack);
+
+ if (RemovedItems < a_ItemStack.m_ItemCount)
+ {
+ cItem Temp(a_ItemStack);
+ Temp.m_ItemCount -= RemovedItems;
+ RemovedItems += m_InventorySlots.RemoveItem(Temp);
+ }
+
+ return RemovedItems;
+}
+
+
+
+
+
bool cInventory::RemoveOneEquippedItem(void)
{
if (m_HotbarSlots.GetSlot(m_EquippedSlotNum).IsEmpty())
diff --git a/src/Inventory.h b/src/Inventory.h
index e25fc4a8a..6e32536b3 100644
--- a/src/Inventory.h
+++ b/src/Inventory.h
@@ -86,6 +86,9 @@ public:
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst);
+ /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */
+ int RemoveItem(const cItem & a_ItemStack);
+
/** Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed */
bool RemoveOneEquippedItem(void);
diff --git a/src/ItemGrid.cpp b/src/ItemGrid.cpp
index cd36b1f2a..38829cbf8 100644
--- a/src/ItemGrid.cpp
+++ b/src/ItemGrid.cpp
@@ -345,6 +345,39 @@ int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, int a_P
+int cItemGrid::RemoveItem(const cItem & a_ItemStack)
+{
+ int NumLeft = a_ItemStack.m_ItemCount;
+
+ for (int i = 0; i < m_NumSlots; i++)
+ {
+ if (NumLeft <= 0)
+ {
+ break;
+ }
+
+ if (m_Slots[i].IsEqual(a_ItemStack))
+ {
+ int NumToRemove = std::min(NumLeft, (int)m_Slots[i].m_ItemCount);
+ NumLeft -= NumToRemove;
+ m_Slots[i].m_ItemCount -= NumToRemove;
+
+ if (m_Slots[i].m_ItemCount <= 0)
+ {
+ m_Slots[i].Empty();
+ }
+
+ TriggerListeners(i);
+ }
+ }
+
+ return (a_ItemStack.m_ItemCount - NumLeft);
+}
+
+
+
+
+
int cItemGrid::ChangeSlotCount(int a_SlotNum, int a_AddToCount)
{
if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots))
diff --git a/src/ItemGrid.h b/src/ItemGrid.h
index c34d5e9e2..6731b0065 100644
--- a/src/ItemGrid.h
+++ b/src/ItemGrid.h
@@ -96,6 +96,9 @@ public:
Returns the total number of items that fit.
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
+
+ /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */
+ int RemoveItem(const cItem & a_ItemStack);
/** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
If the slot is empty, ignores the call.
--
cgit v1.2.3
From 13311c700ffbd94f28fe38654bda4bf026d2a8ce Mon Sep 17 00:00:00 2001
From: madmaxoft
Date: Fri, 18 Jul 2014 23:47:46 +0200
Subject: Clarified RemoveItem()'s comments.
---
src/Inventory.h | 3 ++-
src/ItemGrid.h | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/Inventory.h b/src/Inventory.h
index 6e32536b3..5175afd14 100644
--- a/src/Inventory.h
+++ b/src/Inventory.h
@@ -86,7 +86,8 @@ public:
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst);
- /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */
+ /** Removes the specified item from the inventory, as many as possible, up to a_ItemStack.m_ItemCount.
+ Returns the number of items that were removed. */
int RemoveItem(const cItem & a_ItemStack);
/** Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed */
diff --git a/src/ItemGrid.h b/src/ItemGrid.h
index 6731b0065..8d6544792 100644
--- a/src/ItemGrid.h
+++ b/src/ItemGrid.h
@@ -97,7 +97,8 @@ public:
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
- /** Remove the specified item with the specified amount from the inventory. Returns the amount from the items were was removed. */
+ /** Removes the specified item from the grid, as many as possible, up to a_ItemStack.m_ItemCount.
+ Returns the number of items that were removed. */
int RemoveItem(const cItem & a_ItemStack);
/** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
--
cgit v1.2.3
From abe8a6bd4588892f27f779fd92beb941853c5583 Mon Sep 17 00:00:00 2001
From: madmaxoft
Date: Fri, 18 Jul 2014 23:48:06 +0200
Subject: APIDump: Documented RemoveItem().
---
MCServer/Plugins/APIDump/APIDesc.lua | 2 ++
1 file changed, 2 insertions(+)
diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua
index 0e626c580..e65da1d16 100644
--- a/MCServer/Plugins/APIDump/APIDesc.lua
+++ b/MCServer/Plugins/APIDump/APIDesc.lua
@@ -1155,6 +1155,7 @@ These ItemGrids are available in the API and can be manipulated by the plugins,
HasItems = { Params = "{{cItem|cItem}}", Return = "bool", Notes = "Returns true if there are at least as many items of the specified type as in the parameter" },
HowManyCanFit = { Params = "{{cItem|cItem}}", Return = "number", Notes = "Returns the number of the specified items that can fit in the storage, including empty slots" },
HowManyItems = { Params = "{{cItem|cItem}}", Return = "number", Notes = "Returns the number of the specified items that are currently stored" },
+ RemoveItem = { Params = "{{cItem}}", Return = "number", Notes = "Removes the specified item from the inventory, as many as possible, up to the item's m_ItemCount. Returns the number of items that were removed." },
RemoveOneEquippedItem = { Params = "", Return = "", Notes = "Removes one item from the hotbar's currently selected slot" },
SetArmorSlot = { Params = "ArmorSlotNum, {{cItem|cItem}}", Return = "", Notes = "Sets the specified armor slot contents" },
SetEquippedSlotNum = { Params = "EquippedSlotNum", Return = "", Notes = "Sets the currently selected hotbar slot number" },
@@ -1384,6 +1385,7 @@ local Item5 = cItem(E_ITEM_DIAMOND_CHESTPLATE, 1, 0, "thorns=1;unbreaking=3");
{ Params = "SlotNum", Return = "bool", Notes = "Returns true if the specified slot is empty, or an invalid slot is specified" },
{ Params = "X, Y", Return = "bool", Notes = "Returns true if the specified slot is empty, or an invalid slot is specified" },
},
+ RemoveItem = { Params = "{{cItem}}", Return = "number", Notes = "Removes the specified item from the grid, as many as possible, up to the item's m_ItemCount. Returns the number of items that were removed." },
RemoveOneItem =
{
{ Params = "SlotNum", Return = "{{cItem|cItem}}", Notes = "Removes one item from the stack in the specified slot and returns it as a single cItem. Empty slots are skipped and an empty item is returned" },
--
cgit v1.2.3
From 0346ab15adaf73e4d7c4ee4cfb175d40e721adfe Mon Sep 17 00:00:00 2001
From: madmaxoft
Date: Fri, 18 Jul 2014 23:51:15 +0200
Subject: Debuggers: Optimized and commented the /rmitem handler.
---
MCServer/Plugins/Debuggers/Debuggers.lua | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/MCServer/Plugins/Debuggers/Debuggers.lua b/MCServer/Plugins/Debuggers/Debuggers.lua
index 95b36e854..b402c1867 100644
--- a/MCServer/Plugins/Debuggers/Debuggers.lua
+++ b/MCServer/Plugins/Debuggers/Debuggers.lua
@@ -1107,18 +1107,21 @@ end
function HandleRMItem(a_Split, a_Player)
- if ((#a_Split ~= 2) and (#a_Split ~= 3)) then
+ -- Check params:
+ if (a_Split[2] == nil) then
a_Player:SendMessage("Usage: /rmitem - [Count]")
return true
end
+ -- Parse the item type:
local Item = cItem()
if (not StringToItem(a_Split[2], Item)) then
a_Player:SendMessageFailure(a_Split[2] .. " isn't a valid item")
return true
end
- if (#a_Split == 3) then
+ -- Parse the optional item count
+ if (a_Split[3] ~= nil) then
local Count = tonumber(a_Split[3])
if (Count == nil) then
a_Player:SendMessageFailure(a_Split[3] .. " isn't a valid number")
@@ -1128,8 +1131,9 @@ function HandleRMItem(a_Split, a_Player)
Item.m_ItemCount = Count
end
- local RemovedItems = a_Player:GetInventory():RemoveItem(Item)
- a_Player:SendMessageSuccess("Removed " .. RemovedItems .. " Items!")
+ -- Remove the item:
+ local NumRemovedItems = a_Player:GetInventory():RemoveItem(Item)
+ a_Player:SendMessageSuccess("Removed " .. NumRemovedItems .. " Items!")
return true
end
--
cgit v1.2.3