From 7074f35e4ba831e358117842b99ee35b87f85ae5 Mon Sep 17 00:00:00 2001 From: void_17 Date: Mon, 2 Mar 2026 15:58:20 +0700 Subject: shared_ptr -> std::shared_ptr This is one of the first commits in a plan to remove all `using namespace std;` lines in the entire codebase as it is considered anti-pattern today. --- Minecraft.World/Inventory.cpp | 78 +++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'Minecraft.World/Inventory.cpp') diff --git a/Minecraft.World/Inventory.cpp b/Minecraft.World/Inventory.cpp index 8ef3f085..7218e14c 100644 --- a/Minecraft.World/Inventory.cpp +++ b/Minecraft.World/Inventory.cpp @@ -14,7 +14,7 @@ const int Inventory::INVENTORY_SIZE = 4 * 9; const int Inventory::SELECTION_SIZE = 9; // 4J Stu - The Pllayer is managed by shared_ptrs elsewhere, but it owns us so we don't want to also -// keep a shared_ptr of it. If we pass it on we should use shared_from_this() though +// keep a std::shared_ptr of it. If we pass it on we should use shared_from_this() though Inventory::Inventory(Player *player) { items = ItemInstanceArray( INVENTORY_SIZE ); @@ -35,7 +35,7 @@ Inventory::~Inventory() delete [] armor.data; } -shared_ptr Inventory::getSelected() +std::shared_ptr Inventory::getSelected() { // sanity checking to prevent exploits if (selected < SELECTION_SIZE && selected >= 0) @@ -83,11 +83,11 @@ int Inventory::getSlot(int tileId, int data) return -1; } -int Inventory::getSlotWithRemainingSpace(shared_ptr item) +int Inventory::getSlotWithRemainingSpace(std::shared_ptr item) { for (unsigned int i = 0; i < items.length; i++) { - if (items[i] != NULL && items[i]->id == item->id && items[i]->isStackable() + if (items[i] != NULL && items[i]->id == item->id && items[i]->isStackable() && items[i]->count < items[i]->getMaxStackSize() && items[i]->count < getMaxStackSize() && (!items[i]->isStackedByData() || items[i]->getAuxValue() == item->getAuxValue()) && ItemInstance::tagMatches(items[i], item)) @@ -182,12 +182,12 @@ void Inventory::replaceSlot(Item *item, int data) { return; } - items[selected] = shared_ptr(new ItemInstance(Item::items[item->id], 1, data)); + items[selected] = std::shared_ptr(new ItemInstance(Item::items[item->id], 1, data)); } } -int Inventory::addResource(shared_ptr itemInstance) +int Inventory::addResource(std::shared_ptr itemInstance) { int type = itemInstance->id; @@ -200,7 +200,7 @@ int Inventory::addResource(shared_ptr itemInstance) if (slot < 0) return count; if (items[slot] == NULL) { - items[slot] = ItemInstance::clone(itemInstance); + items[slot] = ItemInstance::clone(itemInstance); player->handleCollectItem(itemInstance); } return 0; @@ -211,7 +211,7 @@ int Inventory::addResource(shared_ptr itemInstance) if (slot < 0) return count; if (items[slot] == NULL) { - items[slot] = shared_ptr( new ItemInstance(type, 0, itemInstance->getAuxValue()) ); + items[slot] = std::shared_ptr( new ItemInstance(type, 0, itemInstance->getAuxValue()) ); // 4J Stu - Brought forward from 1.2 if (itemInstance->hasTag()) { @@ -269,16 +269,16 @@ bool Inventory::removeResource(int type,int iAuxVal) return true; } -void Inventory::removeResources(shared_ptr item) +void Inventory::removeResources(std::shared_ptr item) { - if(item == NULL) return; + if(item == NULL) return; int countToRemove = item->count; for (unsigned int i = 0; i < items.length; i++) { if (items[i] != NULL && items[i]->sameItemWithTags(item)) { - int slotCount = items[i]->count; + int slotCount = items[i]->count; items[i]->count -= countToRemove; if(slotCount < countToRemove) { @@ -293,14 +293,14 @@ void Inventory::removeResources(shared_ptr item) } } -shared_ptr Inventory::getResourceItem(int type) +std::shared_ptr Inventory::getResourceItem(int type) { int slot = getSlot(type); if (slot < 0) return nullptr; return getItem( slot ); } -shared_ptr Inventory::getResourceItem(int type,int iAuxVal) +std::shared_ptr Inventory::getResourceItem(int type,int iAuxVal) { int slot = getSlot(type,iAuxVal); if (slot < 0) return nullptr; @@ -317,12 +317,12 @@ bool Inventory::hasResource(int type) void Inventory::swapSlots(int from, int to) { - shared_ptr tmp = items[to]; + std::shared_ptr tmp = items[to]; items[to] = items[from]; items[from] = tmp; } -bool Inventory::add(shared_ptr item) +bool Inventory::add(std::shared_ptr item) { // 4J Stu - Fix for duplication glitch if(item->count <= 0) return true; @@ -359,7 +359,7 @@ bool Inventory::add(shared_ptr item) { player->handleCollectItem(item); - player->awardStat( + player->awardStat( GenericStats::itemsCollected(item->id, item->getAuxValue()), GenericStats::param_itemsCollected(item->id, item->getAuxValue(), item->GetCount())); @@ -377,7 +377,7 @@ bool Inventory::add(shared_ptr item) return false; } -shared_ptr Inventory::removeItem(unsigned int slot, int count) +std::shared_ptr Inventory::removeItem(unsigned int slot, int count) { ItemInstanceArray pile = items; @@ -391,13 +391,13 @@ shared_ptr Inventory::removeItem(unsigned int slot, int count) { if (pile[slot]->count <= count) { - shared_ptr item = pile[slot]; + std::shared_ptr item = pile[slot]; pile[slot] = nullptr; return item; } else { - shared_ptr i = pile[slot]->remove(count); + std::shared_ptr i = pile[slot]->remove(count); if (pile[slot]->count == 0) pile[slot] = nullptr; return i; } @@ -405,7 +405,7 @@ shared_ptr Inventory::removeItem(unsigned int slot, int count) return nullptr; } -shared_ptr Inventory::removeItemNoUpdate(int slot) +std::shared_ptr Inventory::removeItemNoUpdate(int slot) { ItemInstanceArray pile = items; if (slot >= items.length) @@ -416,14 +416,14 @@ shared_ptr Inventory::removeItemNoUpdate(int slot) if (pile[slot] != NULL) { - shared_ptr item = pile[slot]; + std::shared_ptr item = pile[slot]; pile[slot] = nullptr; return item; } return nullptr; } -void Inventory::setItem(unsigned int slot, shared_ptr item) +void Inventory::setItem(unsigned int slot, std::shared_ptr item) { #ifdef _DEBUG if(item!=NULL) @@ -447,7 +447,7 @@ void Inventory::setItem(unsigned int slot, shared_ptr item) else { items[slot] = item; - } + } player->handleCollectItem(item); /* ItemInstanceArray& pile = items; @@ -512,7 +512,7 @@ void Inventory::load(ListTag *inventoryList) { CompoundTag *tag = inventoryList->get(i); unsigned int slot = tag->getByte(L"Slot") & 0xff; - shared_ptr item = shared_ptr( ItemInstance::fromTag(tag) ); + std::shared_ptr item = std::shared_ptr( ItemInstance::fromTag(tag) ); if (item != NULL) { if (slot >= 0 && slot < items.length) items[slot] = item; @@ -526,7 +526,7 @@ unsigned int Inventory::getContainerSize() return items.length + 4; } -shared_ptr Inventory::getItem(unsigned int slot) +std::shared_ptr Inventory::getItem(unsigned int slot) { // 4J Stu - Changed this a little from the Java so it's less funny if( slot >= items.length ) @@ -559,9 +559,9 @@ int Inventory::getMaxStackSize() return MAX_INVENTORY_STACK_SIZE; } -int Inventory::getAttackDamage(shared_ptr entity) +int Inventory::getAttackDamage(std::shared_ptr entity) { - shared_ptr item = getItem(selected); + std::shared_ptr item = getItem(selected); if (item != NULL) return item->getAttackDamage(entity); return 1; } @@ -570,12 +570,12 @@ bool Inventory::canDestroy(Tile *tile) { if (tile->material->isAlwaysDestroyable()) return true; - shared_ptr item = getItem(selected); + std::shared_ptr item = getItem(selected); if (item != NULL) return item->canDestroySpecial(tile); return false; } -shared_ptr Inventory::getArmor(int layer) +std::shared_ptr Inventory::getArmor(int layer) { return armor[layer]; } @@ -640,7 +640,7 @@ void Inventory::setChanged() changed = true; } -bool Inventory::isSame(shared_ptr copy) +bool Inventory::isSame(std::shared_ptr copy) { for (unsigned int i = 0; i < items.length; i++) { @@ -654,7 +654,7 @@ bool Inventory::isSame(shared_ptr copy) } -bool Inventory::isSame(shared_ptr a, shared_ptr b) +bool Inventory::isSame(std::shared_ptr a, std::shared_ptr b) { if (a == NULL && b == NULL) return true; if (a == NULL || b == NULL) return false; @@ -663,9 +663,9 @@ bool Inventory::isSame(shared_ptr a, shared_ptr b) } -shared_ptr Inventory::copy() +std::shared_ptr Inventory::copy() { - shared_ptr copy = shared_ptr( new Inventory(NULL) ); + std::shared_ptr copy = std::shared_ptr( new Inventory(NULL) ); for (unsigned int i = 0; i < items.length; i++) { copy->items[i] = items[i] != NULL ? items[i]->copy() : nullptr; @@ -677,25 +677,25 @@ shared_ptr Inventory::copy() return copy; } -void Inventory::setCarried(shared_ptr carried) +void Inventory::setCarried(std::shared_ptr carried) { this->carried = carried; player->handleCollectItem(carried); } -shared_ptr Inventory::getCarried() +std::shared_ptr Inventory::getCarried() { return carried; } -bool Inventory::stillValid(shared_ptr player) +bool Inventory::stillValid(std::shared_ptr player) { if (this->player->removed) return false; if (player->distanceToSqr(this->player->shared_from_this()) > 8 * 8) return false; return true; } -bool Inventory::contains(shared_ptr itemInstance) +bool Inventory::contains(std::shared_ptr itemInstance) { for (unsigned int i = 0; i < armor.length; i++) { @@ -718,7 +718,7 @@ void Inventory::stopOpen() // TODO Auto-generated method stub } -void Inventory::replaceWith(shared_ptr other) +void Inventory::replaceWith(std::shared_ptr other) { for (int i = 0; i < items.length; i++) { @@ -730,7 +730,7 @@ void Inventory::replaceWith(shared_ptr other) } } -int Inventory::countMatches(shared_ptr itemInstance) +int Inventory::countMatches(std::shared_ptr itemInstance) { if(itemInstance == NULL) return 0; int count = 0; -- cgit v1.2.3