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/UI/SlotArea.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/UI/SlotArea.h | 29 +++++++++ src/UI/Window.cpp | 15 +++++ src/UI/Window.h | 12 ++++ 4 files changed, 229 insertions(+) (limited to 'src/UI') 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/UI/SlotArea.cpp | 10 +++++----- src/UI/SlotArea.h | 2 -- src/UI/Window.cpp | 3 ++- src/UI/Window.h | 9 +++++++++ 4 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/UI') 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/UI/SlotArea.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/UI/SlotArea.h | 9 ++- src/UI/Window.cpp | 35 ++++++++++- src/UI/Window.h | 12 +++- 4 files changed, 204 insertions(+), 16 deletions(-) (limited to 'src/UI') 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/UI') 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/UI') 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 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/UI') 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/UI') 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/UI') 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 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/UI/SlotArea.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/UI') 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 From d6cb6e0423fe38478c69f5d249882a01d727f0ed Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 7 May 2014 12:30:30 +0200 Subject: Add repair cost to cItem, add custom name to NBTChunkSerializer and fix anvil bugs. --- src/UI/SlotArea.cpp | 60 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) (limited to 'src/UI') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index fcf5f6f6b..c4c95c7b1 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -866,16 +866,19 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) m_MaximumCost = 0; m_StackSizeToBeUsedInRepair = 0; - int RepairCost = cItemHandler::GetItemHandler(Input)->GetRepairCost(); + UInt16 RepairCost = Input.m_RepairCost; int NeedExp = 0; + bool IsEnchantBook = false; if (!SecondInput.IsEmpty()) { - RepairCost += cItemHandler::GetItemHandler(SecondInput)->GetRepairCost(); + IsEnchantBook = (SecondInput.m_ItemType == E_ITEM_ENCHANTED_BOOK); + + RepairCost += SecondInput.m_RepairCost; 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); - if (DamageDiff < 0) + if (DamageDiff <= 0) { // No enchantment Output.Empty(); @@ -898,7 +901,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) else { // Tool and armor repair with two tools / armors - if (!Input.IsSameType(SecondInput) || !Input.IsDamageable()) + if (!IsEnchantBook && (!Input.IsSameType(SecondInput) || !Input.IsDamageable())) { // No enchantment Output.Empty(); @@ -907,20 +910,23 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & 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) + if ((Input.GetMaxDamage() > 0) && !IsEnchantBook) { - NewItemDamage = 0; - } + int FirstDamageDiff = Input.GetMaxDamage() - Input.m_ItemDamage; + int SecondDamageDiff = SecondInput.GetMaxDamage() - SecondInput.m_ItemDamage; + int Damage = SecondDamageDiff + Input.GetMaxDamage() * 12 / 100; - if (NewItemDamage < Input.m_ItemDamage) - { - Input.m_ItemDamage = NewItemDamage; - NeedExp += std::max(1, Damage / 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. @@ -934,7 +940,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) // Remove custom name if (!Input.m_CustomName.empty()) { - NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5); + NameChangeExp = (Input.IsDamageable()) ? 7 : (Input.m_ItemCount * 5); NeedExp += NameChangeExp; Input.m_CustomName = ""; } @@ -942,7 +948,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) else if (RepairedItemName != Input.m_CustomName) { // Change custom name - NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5); + NameChangeExp = (Input.IsDamageable()) ? 7 : (Input.m_ItemCount * 5); NeedExp += NameChangeExp; if (!Input.m_CustomName.empty()) @@ -962,7 +968,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) Input.Empty(); } - if (NameChangeExp == NeedExp && NameChangeExp > 0 && m_MaximumCost >= 40) + if ((NameChangeExp == NeedExp) && (NameChangeExp > 0) && (m_MaximumCost >= 40)) { m_MaximumCost = 39; } @@ -971,17 +977,23 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) 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()); + RepairCost = std::max(Input.m_RepairCost, SecondInput.m_RepairCost); if (!Input.m_CustomName.empty()) { - RepairCost -= 9; + if (RepairCost < 9) + { + RepairCost = 0; + } + else + { + RepairCost -= 9; + } } - RepairCost = max(RepairCost, 0); RepairCost += 2; - }*/ + Input.m_RepairCost = RepairCost; + } SetSlot(2, a_Player, Input); m_ParentWindow.SetProperty(0, m_MaximumCost, a_Player); -- cgit v1.2.3 From fbb6404cc8793f5caef374e1eda243fa2d159eb5 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 7 May 2014 20:43:37 +0200 Subject: Change m_RepairCost to int. --- src/UI/SlotArea.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/UI') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index c4c95c7b1..13a9f9b92 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -866,7 +866,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) m_MaximumCost = 0; m_StackSizeToBeUsedInRepair = 0; - UInt16 RepairCost = Input.m_RepairCost; + int RepairCost = Input.m_RepairCost; int NeedExp = 0; bool IsEnchantBook = false; if (!SecondInput.IsEmpty()) @@ -982,15 +982,9 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) RepairCost = std::max(Input.m_RepairCost, SecondInput.m_RepairCost); if (!Input.m_CustomName.empty()) { - if (RepairCost < 9) - { - RepairCost = 0; - } - else - { - RepairCost -= 9; - } + RepairCost -= 9; } + RepairCost = std::max(RepairCost, 0); RepairCost += 2; Input.m_RepairCost = RepairCost; } -- cgit v1.2.3 From fb58ef55beddc73500029ae6c0fe08400de550d2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 8 May 2014 20:16:35 +0200 Subject: Fixed MSVC 64-bit build warnings. --- src/UI/SlotArea.cpp | 2 +- src/UI/Window.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/UI') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 13a9f9b92..788974f9c 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -891,7 +891,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.Count(); + NeedExp += std::max(1, DamageDiff / 100) + (int)Input.m_Enchantments.Count(); DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); ++x; diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index 4991f0147..46885390b 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -591,7 +591,7 @@ void cWindow::OnLeftPaintEnd(cPlayer & a_Player) const cSlotNums & SlotNums = a_Player.GetInventoryPaintSlots(); cItem ToDistribute(a_Player.GetDraggingItem()); - int ToEachSlot = (int)ToDistribute.m_ItemCount / SlotNums.size(); + int ToEachSlot = (int)ToDistribute.m_ItemCount / (int)SlotNums.size(); int NumDistributed = DistributeItemToSlots(a_Player, ToDistribute, ToEachSlot, SlotNums); -- cgit v1.2.3