From 3c102870f600178b7cabf6714f83e06fc05b42fd Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 1 May 2014 00:47:57 +0200 Subject: Add anvil window and slot area. --- src/Blocks/BlockAnvil.h | 7 ++ src/Enchantments.cpp | 9 +++ src/Enchantments.h | 3 + src/Items/ItemArmor.h | 43 ++++++++++++ src/Items/ItemHandler.cpp | 19 +++++ src/Items/ItemHandler.h | 6 ++ src/Items/ItemPickaxe.h | 13 ++++ src/Items/ItemShovel.h | 14 ++++ src/Items/ItemSword.h | 13 ++++ src/UI/SlotArea.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++++++ src/UI/SlotArea.h | 29 ++++++++ src/UI/Window.cpp | 15 ++++ src/UI/Window.h | 12 ++++ 13 files changed, 356 insertions(+) (limited to 'src') diff --git a/src/Blocks/BlockAnvil.h b/src/Blocks/BlockAnvil.h index 93a796ef7..c9eec961d 100644 --- a/src/Blocks/BlockAnvil.h +++ b/src/Blocks/BlockAnvil.h @@ -23,6 +23,13 @@ public: { a_Pickups.push_back(cItem(E_BLOCK_ANVIL, 1, a_BlockMeta >> 2)); } + + + virtual void OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override + { + cWindow * Window = new cAnvilWindow(); + a_Player->OpenWindow(Window); + } virtual bool GetPlacementBlockTypeMeta( diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 64f89815b..bfcde9dac 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -83,6 +83,15 @@ void cEnchantments::AddFromString(const AString & a_StringSpec) +int cEnchantments::Size(void) +{ + return (int)m_Enchantments.size(); +} + + + + + AString cEnchantments::ToString(void) const { // Serialize all the enchantments into a string diff --git a/src/Enchantments.h b/src/Enchantments.h index ec42257c8..82eea53f9 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -84,6 +84,9 @@ public: /** Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it */ void AddFromString(const AString & a_StringSpec); + /** Get the count of enchantments */ + int Size(void); + /** Serializes all the enchantments into a string */ AString ToString(void) const; diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h index 08cddb1ad..f53100bb1 100644 --- a/src/Items/ItemArmor.h +++ b/src/Items/ItemArmor.h @@ -60,6 +60,49 @@ public: return true; } + virtual bool CanRepairWithItem(const cItem & a_Item) override + { + switch (m_ItemType) + { + case E_ITEM_CHAIN_BOOTS: + case E_ITEM_CHAIN_CHESTPLATE: + case E_ITEM_CHAIN_HELMET: + case E_ITEM_CHAIN_LEGGINGS: + { + return (a_Item.m_ItemType == E_ITEM_IRON); + } + case E_ITEM_DIAMOND_BOOTS: + case E_ITEM_DIAMOND_CHESTPLATE: + case E_ITEM_DIAMOND_HELMET: + case E_ITEM_DIAMOND_LEGGINGS: + { + return (a_Item.m_ItemType == E_ITEM_DIAMOND); + } + case E_ITEM_IRON_BOOTS: + case E_ITEM_IRON_CHESTPLATE: + case E_ITEM_IRON_HELMET: + case E_ITEM_IRON_LEGGINGS: + { + return (a_Item.m_ItemType == E_ITEM_IRON); + } + case E_ITEM_GOLD_BOOTS: + case E_ITEM_GOLD_CHESTPLATE: + case E_ITEM_GOLD_HELMET: + case E_ITEM_GOLD_LEGGINGS: + { + return (a_Item.m_ItemType == E_ITEM_GOLD); + } + case E_ITEM_LEATHER_BOOTS: + case E_ITEM_LEATHER_CAP: + case E_ITEM_LEATHER_PANTS: + case E_ITEM_LEATHER_TUNIC: + { + return (a_Item.m_ItemType == E_ITEM_LEATHER); + } + } + return false; + } + } ; diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index ce9593a70..105def2ce 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -511,6 +511,25 @@ bool cItemHandler::IsPlaceable(void) + +bool cItemHandler::CanRepairWithItem(const cItem & a_Item) +{ + return false; +} + + + + + +int cItemHandler::GetRepairCost(void) +{ + return 0; +} + + + + + bool cItemHandler::CanHarvestBlock(BLOCKTYPE a_BlockType) { UNUSED(a_BlockType); diff --git a/src/Items/ItemHandler.h b/src/Items/ItemHandler.h index 4993eac85..420eefcf1 100644 --- a/src/Items/ItemHandler.h +++ b/src/Items/ItemHandler.h @@ -85,6 +85,12 @@ public: /** Blocks simply get placed */ virtual bool IsPlaceable(void); + /** Can the anvil repair this item, when a_Item is the second input? */ + virtual bool CanRepairWithItem(const cItem & a_Item); + + /** Get the repair cost from the item, or 0 if the item hasn't repair cost. */ + virtual int GetRepairCost(void); + /** Called before a block is placed into a world. The handler should return true to allow placement, false to refuse. Also, the handler should set a_BlockType and a_BlockMeta to correct values for the newly placed block. diff --git a/src/Items/ItemPickaxe.h b/src/Items/ItemPickaxe.h index 2a8e40daa..46e68ec70 100644 --- a/src/Items/ItemPickaxe.h +++ b/src/Items/ItemPickaxe.h @@ -85,6 +85,19 @@ public: } return false; } + + virtual bool CanRepairWithItem(const cItem & a_Item) override + { + switch (m_ItemType) + { + case E_ITEM_WOODEN_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_PICKAXE: return (a_Item.m_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_PICKAXE: return (a_Item.m_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_PICKAXE: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + } + return false; + } } ; diff --git a/src/Items/ItemShovel.h b/src/Items/ItemShovel.h index 873d5ae25..7659ccc54 100644 --- a/src/Items/ItemShovel.h +++ b/src/Items/ItemShovel.h @@ -41,4 +41,18 @@ public: { return (a_BlockType == E_BLOCK_SNOW); } + + virtual bool CanRepairWithItem(const cItem & a_Item) override + { + switch (m_ItemType) + { + case E_ITEM_WOODEN_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_SHOVEL: return (a_Item.m_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_SHOVEL: return (a_Item.m_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_SHOVEL: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + } + return false; + } + }; diff --git a/src/Items/ItemSword.h b/src/Items/ItemSword.h index a7c1d2432..34656ff99 100644 --- a/src/Items/ItemSword.h +++ b/src/Items/ItemSword.h @@ -23,6 +23,19 @@ public: { return (a_BlockType == E_BLOCK_COBWEB); } + + virtual bool CanRepairWithItem(const cItem & a_Item) override + { + switch (m_ItemType) + { + case E_ITEM_WOODEN_SWORD: return (a_Item.m_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_SWORD: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_SWORD: return (a_Item.m_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_SWORD: return (a_Item.m_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_SWORD: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + } + return false; + } } ; diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 87b4032e0..eac8257ec 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -595,6 +595,179 @@ cCraftingRecipe & cSlotAreaCrafting::GetRecipeForPlayer(cPlayer & a_Player) +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cSlotAreaAnvil: + +cSlotAreaAnvil::cSlotAreaAnvil(cAnvilWindow & a_ParentWindow) : + cSlotAreaTemporary(3, a_ParentWindow), + m_MaximumCost(0), + m_RepairedItemName("") +{ +} + + + + + +void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) +{ + super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem); + UpdateResult(a_Player); +} + + + + + +void cSlotAreaAnvil::OnPlayerRemoved(cPlayer & a_Player) +{ + TossItems(a_Player, 0, 3); + super::OnPlayerRemoved(a_Player); +} + + + + + +void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) +{ + cItem Input(*GetSlot(0, a_Player)); + cItem SecondInput(*GetSlot(1, a_Player)); + cItem Output(*GetSlot(2, a_Player)); + + if (Input.IsEmpty() && !Output.IsEmpty()) + { + Output.Empty(); + SetSlot(2, a_Player, Output); + m_ParentWindow.SetProperty(0, 0, a_Player); + return; + } + + int RepairCost = cItemHandler::GetItemHandler(Input)->GetRepairCost(); + int NeedExp = 0; + if (!SecondInput.IsEmpty()) + { + RepairCost += cItemHandler::GetItemHandler(SecondInput)->GetRepairCost(); + + if (Input.IsDamageable() && cItemHandler::GetItemHandler(Input)->CanRepairWithItem(SecondInput)) + { + // Tool and armor repair with special item (iron / gold / diamond / ...) + int DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); + if (DamageDiff < 0) + { + // No enchantment + Output.Empty(); + SetSlot(2, a_Player, Output); + m_ParentWindow.SetProperty(0, 0, a_Player); + return; + } + + int x = 0; + while ((DamageDiff > 0) && (x < SecondInput.m_ItemCount)) + { + Input.m_ItemDamage -= DamageDiff; + NeedExp += std::max(1, DamageDiff / 100) + Input.m_Enchantments.Size(); + DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); + + ++x; + } + } + else + { + // Tool and armor repair with two tools / armors + if (!Input.IsSameType(SecondInput) || !Input.IsDamageable()) + { + // No enchantment + Output.Empty(); + SetSlot(2, a_Player, Output); + m_ParentWindow.SetProperty(0, 0, a_Player); + return; + } + + int FirstDamageDiff = Input.GetMaxDamage() - Input.m_ItemDamage; + int SecondDamageDiff = SecondInput.GetMaxDamage() - SecondInput.m_ItemDamage; + int Damage = SecondDamageDiff + Input.GetMaxDamage() * 12 / 100; + + int NewItemDamage = Input.GetMaxDamage() - (FirstDamageDiff + Damage); + if (NewItemDamage > 0) + { + NewItemDamage = 0; + } + + if (NewItemDamage < Input.m_ItemDamage) + { + Input.m_ItemDamage = NewItemDamage; + NeedExp += std::max(1, Damage / 100); + } + + // TODO: Add enchantments. + } + } + + int NameChangeExp = 0; + if (m_RepairedItemName.empty()) + { + // Remove custom name + if (!Input.m_CustomName.empty()) + { + NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5); + NeedExp += NameChangeExp; + Input.m_CustomName = ""; + } + } + else if (m_RepairedItemName != Input.m_CustomName) + { + // Change custom name + NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5); + NeedExp += NameChangeExp; + + if (!Input.m_CustomName.empty()) + { + RepairCost += NameChangeExp / 2; + } + + Input.m_CustomName = m_RepairedItemName; + } + + // TODO: Add enchantment exp cost. + + int MaximumCost = RepairCost + NeedExp; + + if (NeedExp < 0) + { + Input.Empty(); + } + + if (NameChangeExp == NeedExp && NameChangeExp > 0 && MaximumCost >= 40) + { + MaximumCost = 39; + } + if (MaximumCost >= 40 && !a_Player.IsGameModeCreative()) + { + Input.Empty(); + } + + /* TODO: Add repair cost to cItem and not ItemHandler. This is required for this function! + if (!Input.IsEmpty()) + { + RepairCost = max(cItemHandler::GetItemHandler(Input)->GetRepairCost(), cItemHandler::GetItemHandler(SecondInput)->GetRepairCost()); + if (!Input.m_CustomName.empty()) + { + RepairCost -= 9; + } + RepairCost = max(RepairCost, 0); + RepairCost += 2; + }*/ + + SetSlot(2, a_Player, Input); + m_ParentWindow.SetProperty(0, MaximumCost, a_Player); +} + + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cSlotAreaEnchanting: diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index 254722822..a316480c6 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -9,6 +9,7 @@ #pragma once #include "../Inventory.h" +#include "Window.h" @@ -259,6 +260,34 @@ protected: +class cSlotAreaAnvil : + public cSlotAreaTemporary +{ + typedef cSlotAreaTemporary super; + +public: + cSlotAreaAnvil(cAnvilWindow & a_ParentWindow); + + // cSlotArea overrides: + virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override; + + // cSlotAreaTemporary overrides: + virtual void OnPlayerRemoved(cPlayer & a_Player) override; + +protected: + /** Handles a click in the item slot. */ + void UpdateResult(cPlayer & a_Player); + + /** The maximum cost of repairing/renaming in the anvil. */ + int m_MaximumCost; + + AString m_RepairedItemName; +} ; + + + + + class cSlotAreaEnchanting : public cSlotAreaTemporary { diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index 0a78578fc..eb105f8ab 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -804,6 +804,21 @@ cCraftingWindow::cCraftingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) : +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cAnvilWindow: + +cAnvilWindow::cAnvilWindow() : + cWindow(wtAnvil, "Repair") +{ + m_SlotAreas.push_back(new cSlotAreaAnvil(*this)); + m_SlotAreas.push_back(new cSlotAreaInventory(*this)); + m_SlotAreas.push_back(new cSlotAreaHotBar(*this)); +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cEnchantingWindow: diff --git a/src/UI/Window.h b/src/UI/Window.h index 1ca67bfd8..c08b36f9b 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -231,6 +231,18 @@ public: +class cAnvilWindow : + public cWindow +{ + typedef cWindow super; +public: + cAnvilWindow(); +} ; + + + + + class cEnchantingWindow : public cWindow { -- cgit v1.2.3 From 6ac332cd0662f49e30f60ae0b5a3b7f85df8cfb3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 1 May 2014 01:25:04 +0200 Subject: Add MC|ItemName plugin message. --- src/ClientHandle.cpp | 32 ++++++++++++++++++++++++++++++++ src/ClientHandle.h | 3 +++ src/UI/SlotArea.cpp | 10 +++++----- src/UI/SlotArea.h | 2 -- src/UI/Window.cpp | 3 ++- src/UI/Window.h | 9 +++++++++ 6 files changed, 51 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index d414c3178..c0ddd1770 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -633,6 +633,10 @@ void cClientHandle::HandlePluginMessage(const AString & a_Channel, const AString // Client <-> Server branding exchange SendPluginMessage("MC|Brand", "MCServer"); } + else if (a_Channel == "MC|ItemName") + { + HandleAnvilItemName(a_Message.c_str(), a_Message.size()); + } else if (a_Channel == "REGISTER") { if (HasPluginChannel(a_Channel)) @@ -774,6 +778,34 @@ void cClientHandle::HandleCommandBlockMessage(const char * a_Data, size_t a_Leng +void cClientHandle::HandleAnvilItemName(const char * a_Data, unsigned int a_Length) +{ + if (a_Length < 1) + { + return; + } + + if ((m_Player->GetWindow() == NULL) || (m_Player->GetWindow()->GetWindowType() != cWindow::wtAnvil)) + { + return; + } + + cByteBuffer Buffer(a_Length); + Buffer.Write(a_Data, a_Length); + + AString Name; + Buffer.ReadAll(Name); + + if (Name.length() <= 30) + { + ((cAnvilWindow&)*m_Player->GetWindow()).SetRepairedItemName(Name); + } +} + + + + + void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, char a_Status) { LOGD("HandleLeftClick: {%i, %i, %i}; Face: %i; Stat: %i", diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 9fd17ac00..4dc6ab074 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -385,6 +385,9 @@ private: /** Handles the "MC|AdvCdm" plugin message */ void HandleCommandBlockMessage(const char * a_Data, size_t a_Length); + + /** Handles the "MC|ItemName" plugin message */ + void HandleAnvilItemName(const char * a_Data, size_t a_Length); // cSocketThreads::cCallback overrides: virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index eac8257ec..2941982a7 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -600,8 +600,7 @@ cCraftingRecipe & cSlotAreaCrafting::GetRecipeForPlayer(cPlayer & a_Player) cSlotAreaAnvil::cSlotAreaAnvil(cAnvilWindow & a_ParentWindow) : cSlotAreaTemporary(3, a_ParentWindow), - m_MaximumCost(0), - m_RepairedItemName("") + m_MaximumCost(0) { } @@ -705,7 +704,8 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) } int NameChangeExp = 0; - if (m_RepairedItemName.empty()) + const AString & RepairedItemName = ((cAnvilWindow*)&m_ParentWindow)->GetRepairedItemName(); + if (RepairedItemName.empty()) { // Remove custom name if (!Input.m_CustomName.empty()) @@ -715,7 +715,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) Input.m_CustomName = ""; } } - else if (m_RepairedItemName != Input.m_CustomName) + else if (RepairedItemName != Input.m_CustomName) { // Change custom name NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5); @@ -726,7 +726,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) RepairCost += NameChangeExp / 2; } - Input.m_CustomName = m_RepairedItemName; + Input.m_CustomName = RepairedItemName; } // TODO: Add enchantment exp cost. diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index a316480c6..01dcb88ab 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -280,8 +280,6 @@ protected: /** The maximum cost of repairing/renaming in the anvil. */ int m_MaximumCost; - - AString m_RepairedItemName; } ; diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index eb105f8ab..d6adbef8f 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -808,7 +808,8 @@ cCraftingWindow::cCraftingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) : // cAnvilWindow: cAnvilWindow::cAnvilWindow() : - cWindow(wtAnvil, "Repair") + cWindow(wtAnvil, "Repair"), + m_RepairedItemName("") { m_SlotAreas.push_back(new cSlotAreaAnvil(*this)); m_SlotAreas.push_back(new cSlotAreaInventory(*this)); diff --git a/src/UI/Window.h b/src/UI/Window.h index c08b36f9b..8f6f80a41 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -237,6 +237,15 @@ class cAnvilWindow : typedef cWindow super; public: cAnvilWindow(); + + /** Gets the repaired item name. */ + AString GetRepairedItemName(void) const { return m_RepairedItemName; } + + /** Set the repaired item name. */ + void SetRepairedItemName(const AString & a_Name) { m_RepairedItemName = a_Name; } + +protected: + AString m_RepairedItemName; } ; -- cgit v1.2.3 From 7fe6e40bf7d40347f7f452ea2b7c353a5bc8073f Mon Sep 17 00:00:00 2001 From: Howaner Date: Sat, 3 May 2014 23:42:26 +0200 Subject: Add clicks, exp subtraction, item check, ... --- src/Blocks/BlockAnvil.h | 2 +- src/ClientHandle.cpp | 2 +- src/Simulator/SandSimulator.cpp | 4 + src/UI/SlotArea.cpp | 164 +++++++++++++++++++++++++++++++++++++--- src/UI/SlotArea.h | 9 ++- src/UI/Window.cpp | 35 ++++++++- src/UI/Window.h | 12 ++- 7 files changed, 210 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/Blocks/BlockAnvil.h b/src/Blocks/BlockAnvil.h index c9eec961d..35a356678 100644 --- a/src/Blocks/BlockAnvil.h +++ b/src/Blocks/BlockAnvil.h @@ -27,7 +27,7 @@ public: virtual void OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override { - cWindow * Window = new cAnvilWindow(); + cWindow * Window = new cAnvilWindow(a_BlockX, a_BlockY, a_BlockZ); a_Player->OpenWindow(Window); } diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index c0ddd1770..dbc07a96d 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -798,7 +798,7 @@ void cClientHandle::HandleAnvilItemName(const char * a_Data, unsigned int a_Leng if (Name.length() <= 30) { - ((cAnvilWindow&)*m_Player->GetWindow()).SetRepairedItemName(Name); + ((cAnvilWindow&)*m_Player->GetWindow()).SetRepairedItemName(Name, m_Player); } } diff --git a/src/Simulator/SandSimulator.cpp b/src/Simulator/SandSimulator.cpp index f305ba61a..c4f57c86a 100644 --- a/src/Simulator/SandSimulator.cpp +++ b/src/Simulator/SandSimulator.cpp @@ -254,6 +254,10 @@ void cSandSimulator::FinishFalling( { // Rematerialize the material here: a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, a_FallingBlockType, a_FallingBlockMeta); + if (a_FallingBlockType == E_BLOCK_ANVIL) + { + a_World->BroadcastSoundParticleEffect(1022, a_BlockX, a_BlockY, a_BlockZ, 0); + } return; } diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 2941982a7..1e488de62 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -610,8 +610,151 @@ cSlotAreaAnvil::cSlotAreaAnvil(cAnvilWindow & a_ParentWindow) : void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) { - super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem); - UpdateResult(a_Player); + ASSERT((a_SlotNum >= 0) && (a_SlotNum < GetNumSlots())); + if (a_SlotNum != 2) + { + super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem); + UpdateResult(a_Player); + return; + } + + bool bAsync = false; + if (GetSlot(a_SlotNum, a_Player) == NULL) + { + LOGWARNING("GetSlot(%d) returned NULL! Ignoring click", a_SlotNum); + return; + } + + if (a_ClickAction == caDblClick) + { + return; + } + + cItem Slot(*GetSlot(a_SlotNum, a_Player)); + if (!Slot.IsSameType(a_ClickedItem)) + { + LOGWARNING("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots); + LOGWARNING("My item: %s", ItemToFullString(Slot).c_str()); + LOGWARNING("Their item: %s", ItemToFullString(a_ClickedItem).c_str()); + bAsync = true; + } + cItem & DraggingItem = a_Player.GetDraggingItem(); + + if (Slot.IsEmpty()) + { + return; + } + if (!DraggingItem.IsEmpty()) + { + if (!(DraggingItem.IsEqual(Slot) && ((DraggingItem.m_ItemCount + Slot.m_ItemCount) <= cItemHandler::GetItemHandler(Slot)->GetMaxStackSize()))) + { + return; + } + } + + if (!CanTakeResultItem(a_Player)) + { + return; + } + + cItem NewItem = cItem(Slot); + NewItem.m_ItemCount += DraggingItem.m_ItemCount; + + Slot.Empty(); + DraggingItem.Empty(); + SetSlot(a_SlotNum, a_Player, Slot); + + DraggingItem = NewItem; + OnTakeResult(a_Player, NewItem); + + if (bAsync) + { + m_ParentWindow.BroadcastWholeWindow(); + } +} + + + + + +void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player, cItem a_ResultItem) +{ + if (!a_Player.IsGameModeCreative()) + { + a_Player.DeltaExperience(cPlayer::XpForLevel(m_MaximumCost)); + } + SetSlot(0, a_Player, cItem()); + + if (m_StackSizeToBeUsedInRepair > 0) + { + const cItem * Item = GetSlot(1, a_Player); + if (!Item->IsEmpty() && (Item->m_ItemCount > m_StackSizeToBeUsedInRepair)) + { + cItem NewSecondItem(*Item); + NewSecondItem.m_ItemCount -= m_StackSizeToBeUsedInRepair; + SetSlot(1, a_Player, NewSecondItem); + } + else + { + SetSlot(1, a_Player, cItem()); + } + } + else + { + SetSlot(1, a_Player, cItem()); + } + m_ParentWindow.SetProperty(0, m_MaximumCost, a_Player); + + m_MaximumCost = 0; + ((cAnvilWindow*)&m_ParentWindow)->SetRepairedItemName("", false); + + int PosX, PosY, PosZ; + ((cAnvilWindow*)&m_ParentWindow)->GetBlockPos(PosX, PosY, PosZ); + + BLOCKTYPE Block; + NIBBLETYPE BlockMeta; + a_Player.GetWorld()->GetBlockTypeMeta(PosX, PosY, PosZ, Block, BlockMeta); + + cFastRandom Random; + if (!a_Player.IsGameModeCreative() && (Block == E_BLOCK_ANVIL) && (Random.NextFloat(1.0F) < 0.12F)) + { + NIBBLETYPE var4 = BlockMeta & 0x3; + NIBBLETYPE AnvilDamage = BlockMeta >> 2; + ++AnvilDamage; + + if (AnvilDamage > 2) + { + // Anvil will break + a_Player.GetWorld()->SetBlock(PosX, PosY, PosZ, E_BLOCK_AIR, (NIBBLETYPE)0); + a_Player.GetWorld()->BroadcastSoundParticleEffect(1020, PosX, PosY, PosZ, 0); + a_Player.CloseWindow(false); + } + else + { + a_Player.GetWorld()->SetBlockMeta(PosX, PosY, PosZ, var4 | AnvilDamage << 2); + a_Player.GetWorld()->BroadcastSoundParticleEffect(1021, PosX, PosY, PosZ, 0); + } + } + else + { + a_Player.GetWorld()->BroadcastSoundParticleEffect(1021, PosX, PosY, PosZ, 0); + } +} + + + + + +bool cSlotAreaAnvil::CanTakeResultItem(cPlayer & a_Player) +{ + return ( + ( + a_Player.IsGameModeCreative() + || a_Player.GetXpLevel() >= m_MaximumCost + ) + && !GetSlot(2, a_Player)->IsEmpty() + && m_MaximumCost > 0 + ); } @@ -620,7 +763,7 @@ void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_C void cSlotAreaAnvil::OnPlayerRemoved(cPlayer & a_Player) { - TossItems(a_Player, 0, 3); + TossItems(a_Player, 0, 2); super::OnPlayerRemoved(a_Player); } @@ -641,7 +784,9 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) m_ParentWindow.SetProperty(0, 0, a_Player); return; } - + + m_MaximumCost = 0; + m_StackSizeToBeUsedInRepair = 0; int RepairCost = cItemHandler::GetItemHandler(Input)->GetRepairCost(); int NeedExp = 0; if (!SecondInput.IsEmpty()) @@ -670,6 +815,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) ++x; } + m_StackSizeToBeUsedInRepair = x; } else { @@ -731,18 +877,18 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) // TODO: Add enchantment exp cost. - int MaximumCost = RepairCost + NeedExp; + m_MaximumCost = RepairCost + NeedExp; if (NeedExp < 0) { Input.Empty(); } - if (NameChangeExp == NeedExp && NameChangeExp > 0 && MaximumCost >= 40) + if (NameChangeExp == NeedExp && NameChangeExp > 0 && m_MaximumCost >= 40) { - MaximumCost = 39; + m_MaximumCost = 39; } - if (MaximumCost >= 40 && !a_Player.IsGameModeCreative()) + if (m_MaximumCost >= 40 && !a_Player.IsGameModeCreative()) { Input.Empty(); } @@ -760,7 +906,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) }*/ SetSlot(2, a_Player, Input); - m_ParentWindow.SetProperty(0, MaximumCost, a_Player); + m_ParentWindow.SetProperty(0, m_MaximumCost, a_Player); } diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index 01dcb88ab..4f273ec65 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -274,12 +274,19 @@ public: // cSlotAreaTemporary overrides: virtual void OnPlayerRemoved(cPlayer & a_Player) override; -protected: + bool CanTakeResultItem(cPlayer & a_Player); + + void OnTakeResult(cPlayer & a_Player, cItem a_ResultItem); + /** Handles a click in the item slot. */ void UpdateResult(cPlayer & a_Player); +protected: /** The maximum cost of repairing/renaming in the anvil. */ int m_MaximumCost; + + /** The stack size of the second item where was used for repair */ + char m_StackSizeToBeUsedInRepair; } ; diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index d6adbef8f..4991f0147 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -807,11 +807,15 @@ cCraftingWindow::cCraftingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) : /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cAnvilWindow: -cAnvilWindow::cAnvilWindow() : +cAnvilWindow::cAnvilWindow(int a_BlockX, int a_BlockY, int a_BlockZ) : cWindow(wtAnvil, "Repair"), - m_RepairedItemName("") + m_RepairedItemName(""), + m_BlockX(a_BlockX), + m_BlockY(a_BlockY), + m_BlockZ(a_BlockZ) { - m_SlotAreas.push_back(new cSlotAreaAnvil(*this)); + m_AnvilSlotArea = new cSlotAreaAnvil(*this); + m_SlotAreas.push_back(m_AnvilSlotArea); m_SlotAreas.push_back(new cSlotAreaInventory(*this)); m_SlotAreas.push_back(new cSlotAreaHotBar(*this)); } @@ -820,6 +824,31 @@ cAnvilWindow::cAnvilWindow() : +void cAnvilWindow::SetRepairedItemName(const AString & a_Name, cPlayer * a_Player) +{ + m_RepairedItemName = a_Name; + + if (a_Player != NULL) + { + m_AnvilSlotArea->UpdateResult(*a_Player); + } +} + + + + + +void cAnvilWindow::GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ) +{ + a_PosX = m_BlockX; + a_PosY = m_BlockY; + a_PosZ = m_BlockZ; +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cEnchantingWindow: diff --git a/src/UI/Window.h b/src/UI/Window.h index 8f6f80a41..c87d9b174 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -24,6 +24,7 @@ class cEnderChestEntity; class cFurnaceEntity; class cHopperEntity; class cSlotArea; +class cSlotAreaAnvil; class cWorld; typedef std::list cPlayerList; @@ -236,16 +237,21 @@ class cAnvilWindow : { typedef cWindow super; public: - cAnvilWindow(); + cAnvilWindow(int a_BlockX, int a_BlockY, int a_BlockZ); /** Gets the repaired item name. */ AString GetRepairedItemName(void) const { return m_RepairedItemName; } /** Set the repaired item name. */ - void SetRepairedItemName(const AString & a_Name) { m_RepairedItemName = a_Name; } + void SetRepairedItemName(const AString & a_Name, cPlayer * a_Player); + + /** Get the Position from the Enchantment Table */ + void GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ); protected: + cSlotAreaAnvil * m_AnvilSlotArea; AString m_RepairedItemName; + int m_BlockX, m_BlockY, m_BlockZ; } ; @@ -264,7 +270,7 @@ public: /** Return the Value of a Property */ int GetPropertyValue(int a_Property); - /** Set the Position Values to the Position of the Enchantment Table */ + /** Get the Position from the Enchantment Table */ void GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ); cSlotArea * m_SlotArea; -- cgit v1.2.3 From ca3c9ce84d50426631bf49124ad5e822c8ef9e94 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 4 May 2014 01:20:16 +0200 Subject: This isn't a enchantment table :D --- src/UI/Window.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/UI/Window.h b/src/UI/Window.h index c87d9b174..542dccb88 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -245,7 +245,7 @@ public: /** Set the repaired item name. */ void SetRepairedItemName(const AString & a_Name, cPlayer * a_Player); - /** Get the Position from the Enchantment Table */ + /** Gets the Position from the Anvil */ void GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ); protected: -- cgit v1.2.3 From ecc62dbb9c8d7fcce7e385285ff701a002462137 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 4 May 2014 11:11:07 +0200 Subject: Add anvil shift click. --- src/UI/SlotArea.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/UI/SlotArea.h | 4 ++- 2 files changed, 86 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 1e488de62..7b9f4e628 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -244,7 +244,7 @@ void cSlotArea::OnPlayerRemoved(cPlayer & a_Player) -void cSlotArea::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_Apply, bool a_KeepEmptySlots) +void cSlotArea::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) { for (int i = 0; i < m_NumSlots; i++) { @@ -264,7 +264,7 @@ void cSlotArea::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ { NumFit = a_ItemStack.m_ItemCount; } - if (a_Apply) + if (a_ShouldApply) { cItem NewSlot(a_ItemStack); NewSlot.m_ItemCount = Slot->m_ItemCount + NumFit; @@ -630,6 +630,12 @@ void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_C return; } + if ((a_ClickAction == caShiftLeftClick) || (a_ClickAction == caShiftRightClick)) + { + ShiftClicked(a_Player, a_SlotNum, a_ClickedItem); + return; + } + cItem Slot(*GetSlot(a_SlotNum, a_Player)); if (!Slot.IsSameType(a_ClickedItem)) { @@ -665,7 +671,7 @@ void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_C SetSlot(a_SlotNum, a_Player, Slot); DraggingItem = NewItem; - OnTakeResult(a_Player, NewItem); + OnTakeResult(a_Player); if (bAsync) { @@ -677,7 +683,80 @@ void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_C -void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player, cItem a_ResultItem) +void cSlotAreaAnvil::ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_ClickedItem) +{ + if (a_SlotNum != 2) + { + super::ShiftClicked(a_Player, a_SlotNum, a_ClickedItem); + UpdateResult(a_Player); + return; + } + + // Make a copy of the slot, distribute it among the other areas, then update the slot to contain the leftover: + cItem Slot(*GetSlot(a_SlotNum, a_Player)); + + if (Slot.IsEmpty() || !CanTakeResultItem(a_Player)) + { + return; + } + + m_ParentWindow.DistributeStack(Slot, a_Player, this, true); + if (Slot.IsEmpty()) + { + Slot.Empty(); + OnTakeResult(a_Player); + } + SetSlot(a_SlotNum, a_Player, Slot); + + // Some clients try to guess our actions and not always right (armor slots in 1.2.5), so we fix them: + m_ParentWindow.BroadcastWholeWindow(); +} + + + + + +void cSlotAreaAnvil::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) +{ + for (int i = 0; i < 2; i++) + { + const cItem * Slot = GetSlot(i, a_Player); + if (!Slot->IsEqual(a_ItemStack) && (!Slot->IsEmpty() || a_KeepEmptySlots)) + { + // Different items + continue; + } + int NumFit = ItemHandler(Slot->m_ItemType)->GetMaxStackSize() - Slot->m_ItemCount; + if (NumFit <= 0) + { + // Full stack already + continue; + } + if (NumFit > a_ItemStack.m_ItemCount) + { + NumFit = a_ItemStack.m_ItemCount; + } + if (a_ShouldApply) + { + cItem NewSlot(a_ItemStack); + NewSlot.m_ItemCount = Slot->m_ItemCount + NumFit; + SetSlot(i, a_Player, NewSlot); + } + a_ItemStack.m_ItemCount -= NumFit; + if (a_ItemStack.IsEmpty()) + { + UpdateResult(a_Player); + return; + } + } // for i - Slots + UpdateResult(a_Player); +} + + + + + +void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player) { if (!a_Player.IsGameModeCreative()) { diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index 4f273ec65..e7a2acd47 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -270,13 +270,15 @@ public: // cSlotArea overrides: virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override; + virtual void ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_ClickedItem) override; + virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) override; // cSlotAreaTemporary overrides: virtual void OnPlayerRemoved(cPlayer & a_Player) override; bool CanTakeResultItem(cPlayer & a_Player); - void OnTakeResult(cPlayer & a_Player, cItem a_ResultItem); + void OnTakeResult(cPlayer & a_Player); /** Handles a click in the item slot. */ void UpdateResult(cPlayer & a_Player); -- cgit v1.2.3 From 8a6119437db992a9d5650d8276a10547458607d4 Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 5 May 2014 15:42:41 +0200 Subject: Changed HandleAnvilItemName() length to size_t in ClientHandle.h --- src/ClientHandle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index dbc07a96d..b8e61a768 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -778,7 +778,7 @@ void cClientHandle::HandleCommandBlockMessage(const char * a_Data, size_t a_Leng -void cClientHandle::HandleAnvilItemName(const char * a_Data, unsigned int a_Length) +void cClientHandle::HandleAnvilItemName(const char * a_Data, size_t a_Length) { if (a_Length < 1) { -- cgit v1.2.3 From c162d69e7bb48221b9eacbd378b41129cfb35ebd Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 5 May 2014 15:45:03 +0200 Subject: Add doxycomments to cSlotAreaAnvil functions. --- src/UI/SlotArea.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index e7a2acd47..4da6a672f 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -276,8 +276,10 @@ public: // cSlotAreaTemporary overrides: virtual void OnPlayerRemoved(cPlayer & a_Player) override; + /** Can the player take the item from the slot? */ bool CanTakeResultItem(cPlayer & a_Player); + /** This function will call, when the player take the item from the slot. */ void OnTakeResult(cPlayer & a_Player); /** Handles a click in the item slot. */ -- cgit v1.2.3 From f2617d06830c649c5d13d1daf4f3e70e7ee487b0 Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 5 May 2014 15:49:56 +0200 Subject: Add comments to CanTakeResultItem() --- src/UI/SlotArea.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 7b9f4e628..0f5e292d0 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -828,11 +828,11 @@ bool cSlotAreaAnvil::CanTakeResultItem(cPlayer & a_Player) { return ( ( - a_Player.IsGameModeCreative() - || a_Player.GetXpLevel() >= m_MaximumCost - ) - && !GetSlot(2, a_Player)->IsEmpty() - && m_MaximumCost > 0 + a_Player.IsGameModeCreative() || // Is the player in gamemode? + (a_Player.GetXpLevel() >= m_MaximumCost) // or the player have enough exp? + ) && + (!GetSlot(2, a_Player)->IsEmpty()) && // Is a item in the result slot? + (m_MaximumCost > 0) // And: Is m_MaximumCost higher than 0? ); } -- cgit v1.2.3 From a138671e0e38cd452b001c87d42b7eccd1e447e9 Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 5 May 2014 17:36:22 +0200 Subject: Fix SetRepairedItemName() in SlotArea.cpp --- src/UI/SlotArea.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 0f5e292d0..0e3e293aa 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -785,7 +785,7 @@ void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player) m_ParentWindow.SetProperty(0, m_MaximumCost, a_Player); m_MaximumCost = 0; - ((cAnvilWindow*)&m_ParentWindow)->SetRepairedItemName("", false); + ((cAnvilWindow*)&m_ParentWindow)->SetRepairedItemName("", NULL); int PosX, PosY, PosZ; ((cAnvilWindow*)&m_ParentWindow)->GetBlockPos(PosX, PosY, PosZ); -- cgit v1.2.3 From dca3af1f0f4a047e99f19c4624c0dd0c86904734 Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 5 May 2014 22:11:48 +0200 Subject: Change int to size_t return. --- src/Enchantments.cpp | 4 ++-- src/Enchantments.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index bfcde9dac..58479d729 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -83,9 +83,9 @@ void cEnchantments::AddFromString(const AString & a_StringSpec) -int cEnchantments::Size(void) +size_t cEnchantments::Size(void) { - return (int)m_Enchantments.size(); + return m_Enchantments.size(); } diff --git a/src/Enchantments.h b/src/Enchantments.h index 82eea53f9..6793aca36 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -85,7 +85,7 @@ public: void AddFromString(const AString & a_StringSpec); /** Get the count of enchantments */ - int Size(void); + size_t Size(void); /** Serializes all the enchantments into a string */ AString ToString(void) const; -- cgit v1.2.3 From 27f95454a9114cad0f199552612ab67ddfe8b5ca Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 6 May 2014 17:31:02 +0200 Subject: Simplified the HandleAnvilItemName() code. --- src/ClientHandle.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index b8e61a768..94f031ed6 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -790,15 +790,10 @@ void cClientHandle::HandleAnvilItemName(const char * a_Data, size_t a_Length) return; } - cByteBuffer Buffer(a_Length); - Buffer.Write(a_Data, a_Length); - - AString Name; - Buffer.ReadAll(Name); - + AString Name(a_Data, a_Length); if (Name.length() <= 30) { - ((cAnvilWindow&)*m_Player->GetWindow()).SetRepairedItemName(Name, m_Player); + ((cAnvilWindow *)m_Player->GetWindow())->SetRepairedItemName(Name, m_Player); } } -- cgit v1.2.3 From 954b59d6f460106e93efab39c8666d4692db25b6 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 6 May 2014 19:38:09 +0200 Subject: Rename CanRepairWithItem to CanRepairWithRawMaterial and rename Size() to Count() --- src/Enchantments.cpp | 2 +- src/Enchantments.h | 2 +- src/Items/ItemArmor.h | 12 ++++++------ src/Items/ItemHandler.cpp | 2 +- src/Items/ItemHandler.h | 2 +- src/Items/ItemPickaxe.h | 12 ++++++------ src/Items/ItemShovel.h | 12 ++++++------ src/Items/ItemSword.h | 12 ++++++------ src/UI/SlotArea.cpp | 11 +++++------ 9 files changed, 33 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/Enchantments.cpp b/src/Enchantments.cpp index 58479d729..264878c22 100644 --- a/src/Enchantments.cpp +++ b/src/Enchantments.cpp @@ -83,7 +83,7 @@ void cEnchantments::AddFromString(const AString & a_StringSpec) -size_t cEnchantments::Size(void) +size_t cEnchantments::Count(void) { return m_Enchantments.size(); } diff --git a/src/Enchantments.h b/src/Enchantments.h index 6793aca36..85a316414 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -85,7 +85,7 @@ public: void AddFromString(const AString & a_StringSpec); /** Get the count of enchantments */ - size_t Size(void); + size_t Count(void); /** Serializes all the enchantments into a string */ AString ToString(void) const; diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h index f53100bb1..2436df5bd 100644 --- a/src/Items/ItemArmor.h +++ b/src/Items/ItemArmor.h @@ -60,7 +60,7 @@ public: return true; } - virtual bool CanRepairWithItem(const cItem & a_Item) override + virtual bool CanRepairWithRawMaterial(short a_ItemType) override { switch (m_ItemType) { @@ -69,35 +69,35 @@ public: case E_ITEM_CHAIN_HELMET: case E_ITEM_CHAIN_LEGGINGS: { - return (a_Item.m_ItemType == E_ITEM_IRON); + return (a_ItemType == E_ITEM_IRON); } case E_ITEM_DIAMOND_BOOTS: case E_ITEM_DIAMOND_CHESTPLATE: case E_ITEM_DIAMOND_HELMET: case E_ITEM_DIAMOND_LEGGINGS: { - return (a_Item.m_ItemType == E_ITEM_DIAMOND); + return (a_ItemType == E_ITEM_DIAMOND); } case E_ITEM_IRON_BOOTS: case E_ITEM_IRON_CHESTPLATE: case E_ITEM_IRON_HELMET: case E_ITEM_IRON_LEGGINGS: { - return (a_Item.m_ItemType == E_ITEM_IRON); + return (a_ItemType == E_ITEM_IRON); } case E_ITEM_GOLD_BOOTS: case E_ITEM_GOLD_CHESTPLATE: case E_ITEM_GOLD_HELMET: case E_ITEM_GOLD_LEGGINGS: { - return (a_Item.m_ItemType == E_ITEM_GOLD); + return (a_ItemType == E_ITEM_GOLD); } case E_ITEM_LEATHER_BOOTS: case E_ITEM_LEATHER_CAP: case E_ITEM_LEATHER_PANTS: case E_ITEM_LEATHER_TUNIC: { - return (a_Item.m_ItemType == E_ITEM_LEATHER); + return (a_ItemType == E_ITEM_LEATHER); } } return false; diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index 105def2ce..5cc5b66a0 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -512,7 +512,7 @@ bool cItemHandler::IsPlaceable(void) -bool cItemHandler::CanRepairWithItem(const cItem & a_Item) +bool cItemHandler::CanRepairWithRawMaterial(short a_ItemType) { return false; } diff --git a/src/Items/ItemHandler.h b/src/Items/ItemHandler.h index 420eefcf1..ca090eb29 100644 --- a/src/Items/ItemHandler.h +++ b/src/Items/ItemHandler.h @@ -86,7 +86,7 @@ public: virtual bool IsPlaceable(void); /** Can the anvil repair this item, when a_Item is the second input? */ - virtual bool CanRepairWithItem(const cItem & a_Item); + virtual bool CanRepairWithRawMaterial(short a_ItemType); /** Get the repair cost from the item, or 0 if the item hasn't repair cost. */ virtual int GetRepairCost(void); diff --git a/src/Items/ItemPickaxe.h b/src/Items/ItemPickaxe.h index 46e68ec70..ff2c2069b 100644 --- a/src/Items/ItemPickaxe.h +++ b/src/Items/ItemPickaxe.h @@ -86,15 +86,15 @@ public: return false; } - virtual bool CanRepairWithItem(const cItem & a_Item) override + virtual bool CanRepairWithRawMaterial(short a_ItemType) override { switch (m_ItemType) { - case E_ITEM_WOODEN_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_PLANKS); - case E_ITEM_STONE_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); - case E_ITEM_IRON_PICKAXE: return (a_Item.m_ItemType == E_ITEM_IRON); - case E_ITEM_GOLD_PICKAXE: return (a_Item.m_ItemType == E_ITEM_GOLD); - case E_ITEM_DIAMOND_PICKAXE: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + case E_ITEM_WOODEN_PICKAXE: return (a_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_PICKAXE: return (a_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_PICKAXE: return (a_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_PICKAXE: return (a_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_PICKAXE: return (a_ItemType == E_ITEM_DIAMOND); } return false; } diff --git a/src/Items/ItemShovel.h b/src/Items/ItemShovel.h index 7659ccc54..333ba46e8 100644 --- a/src/Items/ItemShovel.h +++ b/src/Items/ItemShovel.h @@ -42,15 +42,15 @@ public: return (a_BlockType == E_BLOCK_SNOW); } - virtual bool CanRepairWithItem(const cItem & a_Item) override + virtual bool CanRepairWithRawMaterial(short a_ItemType) override { switch (m_ItemType) { - case E_ITEM_WOODEN_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_PLANKS); - case E_ITEM_STONE_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); - case E_ITEM_IRON_SHOVEL: return (a_Item.m_ItemType == E_ITEM_IRON); - case E_ITEM_GOLD_SHOVEL: return (a_Item.m_ItemType == E_ITEM_GOLD); - case E_ITEM_DIAMOND_SHOVEL: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + case E_ITEM_WOODEN_SHOVEL: return (a_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_SHOVEL: return (a_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_SHOVEL: return (a_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_SHOVEL: return (a_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_SHOVEL: return (a_ItemType == E_ITEM_DIAMOND); } return false; } diff --git a/src/Items/ItemSword.h b/src/Items/ItemSword.h index 34656ff99..44feb2d83 100644 --- a/src/Items/ItemSword.h +++ b/src/Items/ItemSword.h @@ -24,15 +24,15 @@ public: return (a_BlockType == E_BLOCK_COBWEB); } - virtual bool CanRepairWithItem(const cItem & a_Item) override + virtual bool CanRepairWithRawMaterial(short a_ItemType) override { switch (m_ItemType) { - case E_ITEM_WOODEN_SWORD: return (a_Item.m_ItemType == E_BLOCK_PLANKS); - case E_ITEM_STONE_SWORD: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE); - case E_ITEM_IRON_SWORD: return (a_Item.m_ItemType == E_ITEM_IRON); - case E_ITEM_GOLD_SWORD: return (a_Item.m_ItemType == E_ITEM_GOLD); - case E_ITEM_DIAMOND_SWORD: return (a_Item.m_ItemType == E_ITEM_DIAMOND); + case E_ITEM_WOODEN_SWORD: return (a_ItemType == E_BLOCK_PLANKS); + case E_ITEM_STONE_SWORD: return (a_ItemType == E_BLOCK_COBBLESTONE); + case E_ITEM_IRON_SWORD: return (a_ItemType == E_ITEM_IRON); + case E_ITEM_GOLD_SWORD: return (a_ItemType == E_ITEM_GOLD); + case E_ITEM_DIAMOND_SWORD: return (a_ItemType == E_ITEM_DIAMOND); } return false; } diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 0e3e293aa..fcf5f6f6b 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -797,7 +797,7 @@ void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player) cFastRandom Random; if (!a_Player.IsGameModeCreative() && (Block == E_BLOCK_ANVIL) && (Random.NextFloat(1.0F) < 0.12F)) { - NIBBLETYPE var4 = BlockMeta & 0x3; + NIBBLETYPE Orientation = BlockMeta & 0x3; NIBBLETYPE AnvilDamage = BlockMeta >> 2; ++AnvilDamage; @@ -810,7 +810,7 @@ void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player) } else { - a_Player.GetWorld()->SetBlockMeta(PosX, PosY, PosZ, var4 | AnvilDamage << 2); + a_Player.GetWorld()->SetBlockMeta(PosX, PosY, PosZ, Orientation | (AnvilDamage << 2)); a_Player.GetWorld()->BroadcastSoundParticleEffect(1021, PosX, PosY, PosZ, 0); } } @@ -832,7 +832,7 @@ bool cSlotAreaAnvil::CanTakeResultItem(cPlayer & a_Player) (a_Player.GetXpLevel() >= m_MaximumCost) // or the player have enough exp? ) && (!GetSlot(2, a_Player)->IsEmpty()) && // Is a item in the result slot? - (m_MaximumCost > 0) // And: Is m_MaximumCost higher than 0? + (m_MaximumCost > 0) // When no maximum cost is set, the item isn't set from the UpdateResult() method and can't be a valid enchanting result. ); } @@ -871,8 +871,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) if (!SecondInput.IsEmpty()) { RepairCost += cItemHandler::GetItemHandler(SecondInput)->GetRepairCost(); - - if (Input.IsDamageable() && cItemHandler::GetItemHandler(Input)->CanRepairWithItem(SecondInput)) + if (Input.IsDamageable() && cItemHandler::GetItemHandler(Input)->CanRepairWithRawMaterial(SecondInput.m_ItemType)) { // Tool and armor repair with special item (iron / gold / diamond / ...) int DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); @@ -889,7 +888,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) while ((DamageDiff > 0) && (x < SecondInput.m_ItemCount)) { Input.m_ItemDamage -= DamageDiff; - NeedExp += std::max(1, DamageDiff / 100) + Input.m_Enchantments.Size(); + NeedExp += std::max(1, DamageDiff / 100) + Input.m_Enchantments.Count(); DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); ++x; -- cgit v1.2.3