From 89b1bbdc5fca5a51df1a5dd18ce91f27cb667c04 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 21:59:35 +0200 Subject: Added beacon. --- src/BlockEntities/BeaconEntity.cpp | 309 +++++++++++++++++++++++++++++++--- src/BlockEntities/BeaconEntity.h | 58 +++++-- src/BlockEntities/FlowerPotEntity.cpp | 2 +- src/Blocks/BlockHandler.cpp | 1 + src/Blocks/BlockPiston.h | 1 + src/ChunkMap.cpp | 1 + src/ClientHandle.cpp | 54 ++++++ src/ClientHandle.h | 5 +- src/Protocol/Protocol17x.cpp | 15 ++ src/UI/SlotArea.cpp | 195 +++++++++++++++++++++ src/UI/SlotArea.h | 29 ++++ src/UI/Window.cpp | 31 ++++ src/UI/Window.h | 21 +++ 13 files changed, 689 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 4b9662797..af6c124c0 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -3,33 +3,31 @@ #include "BeaconEntity.h" #include "../BlockArea.h" +#include "../Entities/Player.h" -cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : - super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, a_World) +cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) + : super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, 1, 1, a_World) + , m_IsActive(false) + , m_BeaconLevel(0) + , m_PrimaryPotion(cEntityEffect::effNoEffect) + , m_SecondaryPotion(cEntityEffect::effNoEffect) { + UpdateBeacon(); } -int cBeaconEntity::GetPyramidLevel(void) +char cBeaconEntity::CalculatePyramidLevel(void) { cBlockArea Area; - int MinY = GetPosY() - 4; - if (MinY < 0) - { - MinY = 0; - } - int MaxY = GetPosY() - 1; - if (MaxY < 0) - { - MaxY = 0; - } + int MinY = std::max(GetPosY() - 4, 0); + int MaxY = std::max(GetPosY() - 1, 0); Area.Read( m_World, @@ -42,7 +40,7 @@ int cBeaconEntity::GetPyramidLevel(void) int Layer = 1; int MiddleXZ = 4; - for (int Y = Area.GetSizeY() - 1; Y > 0; Y--) + for (int Y = (Area.GetSizeY() - 1); Y >= 0; Y--) { for (int X = MiddleXZ - Layer; X <= (MiddleXZ + Layer); X++) { @@ -50,14 +48,122 @@ int cBeaconEntity::GetPyramidLevel(void) { if (!IsMineralBlock(Area.GetRelBlockType(X, Y, Z))) { - return Layer - 1; + return (Layer - 1); } } } Layer++; } - return Layer - 1; + return (Layer - 1); +} + + + + + +bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel) +{ + if (a_Potion == cEntityEffect::effNoEffect) + { + return true; + } + + switch (a_BeaconLevel) + { + case 4: + { + // Beacon level 4 + if (a_Potion == cEntityEffect::effRegeneration) + { + return true; + } + } + case 3: + { + // Beacon level 3 + if (a_Potion == cEntityEffect::effStrength) + { + return true; + } + } + case 2: + { + // Beacon level 2 + switch (a_Potion) + { + case cEntityEffect::effResistance: + case cEntityEffect::effJumpBoost: + { + return true; + } + } + } + case 1: + { + // Beacon level 1 + switch (a_Potion) + { + case cEntityEffect::effSpeed: + case cEntityEffect::effHaste: + { + return true; + } + } + } + } + return false; +} + + + + + +bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) +{ + LOG("SelectPrimaryPotion!"); + if (!IsValidPotion(a_Potion, m_BeaconLevel)) + { + LOG("FALLSE!"); + return false; + } + + m_PrimaryPotion = a_Potion; + return true; +} + + + + + +bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) +{ + if (!IsValidPotion(a_Potion, m_BeaconLevel)) + { + return false; + } + + m_SecondaryPotion = a_Potion; + return true; +} + + + + + +bool cBeaconEntity::IsBeaconBlocked(void) +{ + bool IsBlocked = false; + for (int Y = m_PosY; Y < cChunkDef::Height; ++Y) + { + BLOCKTYPE Block = m_World->GetBlock(m_PosX, Y, m_PosZ); + if (!cBlockInfo::IsTransparent(Block)) + { + IsBlocked = true; + break; + } + } + return IsBlocked; } @@ -83,8 +189,102 @@ bool cBeaconEntity::IsMineralBlock(BLOCKTYPE a_BlockType) +void cBeaconEntity::UpdateBeacon(void) +{ + if (IsBeaconBlocked()) + { + m_IsActive = false; + m_BeaconLevel = 0; + } + else + { + m_BeaconLevel = CalculatePyramidLevel(); + m_IsActive = (m_BeaconLevel > 0); + } + + // TODO: Add achievement +} + + + + + +void cBeaconEntity::GiveEffects(void) +{ + if (!m_IsActive || (m_BeaconLevel < 0)) + { + return; + } + + int Radius = m_BeaconLevel * 10 + 10; + short EffectLevel = 0; + if ((m_BeaconLevel >= 4) && (m_PrimaryPotion == m_SecondaryPotion)) + { + EffectLevel = 1; + } + + cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; + if ((m_BeaconLevel >= 4) && (m_PrimaryPotion != m_SecondaryPotion) && (m_SecondaryPotion > 0)) + { + SecondaryPotion = m_SecondaryPotion; + } + + class cPlayerCallback : public cPlayerListCallback + { + int m_Radius; + int m_PosX, m_PosY, m_PosZ; + cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + short m_EffectLevel; + + virtual bool Item(cPlayer * a_Player) + { + Vector3d PlayerPosition = Vector3d(a_Player->GetPosition()); + if (PlayerPosition.y > (double)m_PosY) + { + PlayerPosition.y = (double)m_PosY; + } + + // TODO: Vanilla minecraft uses an AABB check instead of a radius one + Vector3d BeaconPosition = Vector3d(m_PosX, m_PosY, m_PosZ); + if ((PlayerPosition - BeaconPosition).Length() <= m_Radius) + { + a_Player->AddEntityEffect(m_PrimaryPotion, 180, m_EffectLevel); + + if (m_SecondaryPotion != cEntityEffect::effNoEffect) + { + a_Player->AddEntityEffect(m_SecondaryPotion, 180, 0); + } + } + return false; + } + + public: + cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryPotion, cEntityEffect::eType a_SecondaryPotion, short a_EffectLevel) + : m_Radius(a_Radius) + , m_PosX(a_PosX) + , m_PosY(a_PosY) + , m_PosZ(a_PosZ) + , m_PrimaryPotion(a_PrimaryPotion) + , m_SecondaryPotion(a_SecondaryPotion) + , m_EffectLevel(a_EffectLevel) + {}; + + } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryPotion, SecondaryPotion, EffectLevel); + GetWorld()->ForEachPlayer(PlayerCallback); +} + + + + + bool cBeaconEntity::Tick(float a_Dt, cChunk & a_Chunk) { + // Update the beacon every 4 seconds + if ((GetWorld()->GetWorldAge() % 80) == 0) + { + UpdateBeacon(); + GiveEffects(); + } return false; } @@ -92,23 +292,94 @@ bool cBeaconEntity::Tick(float a_Dt, cChunk & a_Chunk) -void cBeaconEntity::SaveToJson(Json::Value& a_Value) +void cBeaconEntity::UsedBy(cPlayer * a_Player) { + cWindow * Window = GetWindow(); + if (Window == NULL) + { + OpenWindow(new cBeaconWindow(m_PosX, m_PosY, m_PosZ, this)); + Window = GetWindow(); + } + + if (Window != NULL) + { + // if (a_Player->GetWindow() != Window) + // -> Because mojang doesn't send a 'close window' packet when you click the cancel button in the beacon inventory ... + { + a_Player->OpenWindow(Window); + } + } } -void cBeaconEntity::SendTo(cClientHandle & a_Client) + +bool cBeaconEntity::LoadFromJson(const Json::Value & a_Value) +{ + m_PosX = a_Value.get("x", 0).asInt(); + m_PosY = a_Value.get("y", 0).asInt(); + m_PosZ = a_Value.get("z", 0).asInt(); + + Json::Value AllSlots = a_Value.get("Slots", 0); + int SlotIdx = 0; + for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr) + { + cItem Item; + Item.FromJson(*itr); + SetSlot(SlotIdx, Item); + SlotIdx++; + } + + m_BeaconLevel = (char)a_Value.get("Level", 0).asInt(); + int PrimaryPotion = a_Value.get("PrimaryPotion", 0).asInt(); + int SecondaryPotion = a_Value.get("SecondaryPotion", 0).asInt(); + + if ((PrimaryPotion >= 0) && (PrimaryPotion <= (int)cEntityEffect::effSaturation)) + { + m_PrimaryPotion = (cEntityEffect::eType)PrimaryPotion; + } + + if ((SecondaryPotion >= 0) && (SecondaryPotion <= (int)cEntityEffect::effSaturation)) + { + m_SecondaryPotion = (cEntityEffect::eType)SecondaryPotion; + } + + return true; +} + + + + + +void cBeaconEntity::SaveToJson(Json::Value& a_Value) { + a_Value["x"] = m_PosX; + a_Value["y"] = m_PosY; + a_Value["z"] = m_PosZ; + + Json::Value AllSlots; + int NumSlots = m_Contents.GetNumSlots(); + for (int i = 0; i < NumSlots; i++) + { + Json::Value Slot; + m_Contents.GetSlot(i).GetJson(Slot); + AllSlots.append(Slot); + } + a_Value["Slots"] = AllSlots; + + a_Value["Level"] = m_BeaconLevel; + a_Value["PrimaryPotion"] = (int)m_PrimaryPotion; + a_Value["SecondaryPotion"] = (int)m_SecondaryPotion; } -void cBeaconEntity::UsedBy(cPlayer * a_Player) +void cBeaconEntity::SendTo(cClientHandle & a_Client) { + a_Client.SendUpdateBlockEntity(*this); } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index ee1eda391..52111e82a 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -1,7 +1,7 @@ #pragma once -#include "BlockEntity.h" +#include "BlockEntityWithItems.h" @@ -17,26 +17,60 @@ namespace Json class cBeaconEntity : - public cBlockEntity + public cBlockEntityWithItems { - typedef cBlockEntity super; + typedef cBlockEntityWithItems super; public: - - /** The initial constructor */ cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); - - /** Returns the amount of layers the pyramid below the beacon has. */ - int GetPyramidLevel(void); + + /** Is the beacon active? */ + bool IsActive(void) const { return m_IsActive; } + + /** Returns the beacon level. (0 - 4) */ + char GetBeaconLevel(void) const { return m_BeaconLevel; } + + char GetPrimaryPotion(void) const { return m_PrimaryPotion; } + char GetSecondaryPotion(void) const { return m_SecondaryPotion; } + + /** Select the primary potion. Returns false when the potion is invalid.*/ + bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); + + /** Select the secondary potion. Returns false when the potion is invalid. */ + bool SelectSecondaryPotion(cEntityEffect::eType a_Potion); + + /** Calculate the amount of layers the pyramid below the beacon has. */ + char CalculatePyramidLevel(void); + + /** Is the beacon blocked by non-transparent blocks that are higher than the beacon? */ + bool IsBeaconBlocked(void); /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ static bool IsMineralBlock(BLOCKTYPE a_BlockType); + + /** Returns true if the potion can be used. */ + static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + + /** Update the beacon. */ + void UpdateBeacon(void); + + /** Give the near-players the effects. */ + void GiveEffects(void); + + bool LoadFromJson(const Json::Value & a_Value); // cBlockEntity overrides: - virtual void SaveToJson(Json::Value& a_Value) override; - virtual void SendTo(cClientHandle & a_Client) override; - virtual void UsedBy(cPlayer * a_Player) override; - virtual bool Tick(float a_Dt, cChunk & /* a_Chunk */) override; + virtual void SaveToJson(Json::Value& a_Value) override; + virtual void SendTo(cClientHandle & a_Client) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void UsedBy(cPlayer * a_Player) override; + +protected: + bool m_IsActive; + char m_BeaconLevel; + + cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + } ; diff --git a/src/BlockEntities/FlowerPotEntity.cpp b/src/BlockEntities/FlowerPotEntity.cpp index 87bf8b921..e001634b8 100644 --- a/src/BlockEntities/FlowerPotEntity.cpp +++ b/src/BlockEntities/FlowerPotEntity.cpp @@ -1,7 +1,7 @@ // FlowerPotEntity.cpp -// Implements the cFlowerPotEntity class representing a single sign in the world +// Implements the cFlowerPotEntity class representing a single flower pot in the world #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "json/json.h" diff --git a/src/Blocks/BlockHandler.cpp b/src/Blocks/BlockHandler.cpp index ddb0186c9..52f7dd608 100644 --- a/src/Blocks/BlockHandler.cpp +++ b/src/Blocks/BlockHandler.cpp @@ -181,6 +181,7 @@ cBlockHandler * cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockType) case E_BLOCK_ACACIA_WOOD_STAIRS: return new cBlockStairsHandler (a_BlockType); case E_BLOCK_ACTIVATOR_RAIL: return new cBlockRailHandler (a_BlockType); case E_BLOCK_ANVIL: return new cBlockAnvilHandler (a_BlockType); + case E_BLOCK_BEACON: return new cBlockEntityHandler (a_BlockType); case E_BLOCK_BED: return new cBlockBedHandler (a_BlockType); case E_BLOCK_BIG_FLOWER: return new cBlockBigFlowerHandler (a_BlockType); case E_BLOCK_BIRCH_WOOD_STAIRS: return new cBlockStairsHandler (a_BlockType); diff --git a/src/Blocks/BlockPiston.h b/src/Blocks/BlockPiston.h index bbb8af75b..0bec603e3 100644 --- a/src/Blocks/BlockPiston.h +++ b/src/Blocks/BlockPiston.h @@ -94,6 +94,7 @@ private: switch (a_BlockType) { case E_BLOCK_ANVIL: + case E_BLOCK_BEACON: case E_BLOCK_BEDROCK: case E_BLOCK_BREWING_STAND: case E_BLOCK_CHEST: diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 05d219918..38e0cd82d 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -1839,6 +1839,7 @@ void cChunkMap::DoExplosionAt(double a_ExplosionSize, double a_BlockX, double a_ } case E_BLOCK_OBSIDIAN: + case E_BLOCK_BEACON: case E_BLOCK_BEDROCK: case E_BLOCK_WATER: case E_BLOCK_LAVA: diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 30ec737be..849de2ce1 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -31,6 +31,7 @@ #include "Items/ItemSword.h" #include "polarssl/md5.h" +#include "BlockEntities/BeaconEntity.h" @@ -659,6 +660,10 @@ void cClientHandle::HandlePluginMessage(const AString & a_Channel, const AString // Client <-> Server branding exchange SendPluginMessage("MC|Brand", "MCServer"); } + else if (a_Channel == "MC|Beacon") + { + HandleBeaconSelection(a_Message.c_str(), a_Message.size()); + } else if (a_Channel == "MC|ItemName") { HandleAnvilItemName(a_Message.c_str(), a_Message.size()); @@ -746,6 +751,55 @@ void cClientHandle::UnregisterPluginChannels(const AStringVector & a_ChannelList +void cClientHandle::HandleBeaconSelection(const char * a_Data, size_t a_Length) +{ + if (a_Length < 14) + { + SendChat("Failure setting beacon selection; bad request", mtFailure); + LOGD("Malformed MC|Beacon packet."); + return; + } + + cWindow * Window = m_Player->GetWindow(); + if ((Window == NULL) || (Window->GetWindowType() != cWindow::wtBeacon)) + { + return; + } + cBeaconWindow * BeaconWindow = (cBeaconWindow *) Window; + + if (Window->GetSlot(*m_Player, 0)->IsEmpty()) + { + return; + } + + cByteBuffer Buffer(a_Length); + Buffer.Write(a_Data, a_Length); + + int PrimaryPotionID, SecondaryPotionID; + Buffer.ReadBEInt(PrimaryPotionID); + Buffer.ReadBEInt(SecondaryPotionID); + + cEntityEffect::eType PrimaryPotion = cEntityEffect::effNoEffect; + if ((PrimaryPotionID >= 0) && (PrimaryPotionID <= (int)cEntityEffect::effSaturation)) + { + PrimaryPotion = (cEntityEffect::eType)PrimaryPotionID; + } + + cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; + if ((SecondaryPotionID >= 0) && (SecondaryPotionID <= (int)cEntityEffect::effSaturation)) + { + SecondaryPotion = (cEntityEffect::eType)SecondaryPotionID; + } + + Window->SetSlot(*m_Player, 0, cItem()); + BeaconWindow->GetBeaconEntity()->SelectPrimaryPotion(PrimaryPotion); + BeaconWindow->GetBeaconEntity()->SelectSecondaryPotion(SecondaryPotion); +} + + + + + void cClientHandle::HandleCommandBlockMessage(const char * a_Data, size_t a_Length) { if (a_Length < 14) diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 48eba4de1..f3f1da417 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -399,7 +399,10 @@ private: /** Removes all of the channels from the list of current plugin channels. Ignores channels that are not found. */ void UnregisterPluginChannels(const AStringVector & a_ChannelList); - + + /** Handles the "MC|Beacon" plugin message */ + void HandleBeaconSelection(const char * a_Data, size_t a_Length); + /** Handles the "MC|AdvCdm" plugin message */ void HandleCommandBlockMessage(const char * a_Data, size_t a_Length); diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 45d39e0e9..8a68edd1f 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -37,6 +37,7 @@ Implements the 1.7.x protocol classes: #include "../Mobs/IncludeAllMonsters.h" #include "../UI/Window.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/MobHeadEntity.h" #include "../BlockEntities/FlowerPotEntity.h" @@ -1328,6 +1329,7 @@ void cProtocol172::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) { case E_BLOCK_MOB_SPAWNER: Action = 1; break; // Update mob spawner spinny mob thing case E_BLOCK_COMMAND_BLOCK: Action = 2; break; // Update command block text + case E_BLOCK_BEACON: Action = 3; break; // Update beacon entity case E_BLOCK_HEAD: Action = 4; break; // Update Mobhead entity case E_BLOCK_FLOWER_POT: Action = 5; break; // Update flower pot default: ASSERT(!"Unhandled or unimplemented BlockEntity update request!"); break; @@ -2581,6 +2583,19 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt switch (a_BlockEntity.GetBlockType()) { + case E_BLOCK_BEACON: + { + cBeaconEntity & BeaconEntity = (cBeaconEntity &)a_BlockEntity; + + Writer.AddInt("x", BeaconEntity.GetPosX()); + Writer.AddInt("y", BeaconEntity.GetPosY()); + Writer.AddInt("z", BeaconEntity.GetPosZ()); + Writer.AddInt("Primary", BeaconEntity.GetPrimaryPotion()); + Writer.AddInt("Secondary", BeaconEntity.GetSecondaryPotion()); + Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); + Writer.AddString("id", "Beacon"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though + break; + } case E_BLOCK_COMMAND_BLOCK: { cCommandBlockEntity & CommandBlockEntity = (cCommandBlockEntity &)a_BlockEntity; diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index b5f84c24c..4199bbf56 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -5,6 +5,7 @@ #include "Globals.h" #include "SlotArea.h" #include "../Entities/Player.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/DropSpenserEntity.h" #include "../BlockEntities/EnderChestEntity.h" @@ -1190,6 +1191,200 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) +//////////////////////////////////////////////////////////////////////////////// +// cSlotAreaBeacon: + +cSlotAreaBeacon::cSlotAreaBeacon(cBeaconEntity * a_Beacon, cWindow & a_ParentWindow) : + cSlotArea(1, a_ParentWindow), + m_Beacon(a_Beacon) +{ + m_Beacon->GetContents().AddListener(*this); +} + + + + + +cSlotAreaBeacon::~cSlotAreaBeacon() +{ + m_Beacon->GetContents().RemoveListener(*this); +} + + + + +bool cSlotAreaBeacon::IsPlaceableItem(short a_ItemType) +{ + switch (a_ItemType) + { + case E_ITEM_EMERALD: + case E_ITEM_DIAMOND: + case E_ITEM_GOLD: + case E_ITEM_IRON: + { + return true; + } + } + return false; +} + + + + + +void cSlotAreaBeacon::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) +{ + ASSERT((a_SlotNum >= 0) && (a_SlotNum < GetNumSlots())); + + bool bAsync = false; + if (GetSlot(a_SlotNum, a_Player) == NULL) + { + LOGWARNING("GetSlot(%d) returned NULL! Ignoring click", a_SlotNum); + return; + } + + switch (a_ClickAction) + { + case caShiftLeftClick: + case caShiftRightClick: + { + ShiftClicked(a_Player, a_SlotNum, a_ClickedItem); + return; + } + case caMiddleClick: + { + MiddleClicked(a_Player, a_SlotNum); + return; + } + case caDropKey: + case caCtrlDropKey: + { + DropClicked(a_Player, a_SlotNum, false); + return; + } + case caNumber1: + case caNumber2: + case caNumber3: + case caNumber4: + case caNumber5: + case caNumber6: + case caNumber7: + case caNumber8: + case caNumber9: + { + NumberClicked(a_Player, a_SlotNum, a_ClickAction); + return; + } + default: + { + break; + } + } + + 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 (DraggingItem.IsEmpty()) + { + DraggingItem = Slot; + Slot.Empty(); + } + else if (Slot.IsEmpty()) + { + if (!IsPlaceableItem(DraggingItem.m_ItemType)) + { + return; + } + + Slot = DraggingItem.CopyOne(); + DraggingItem.m_ItemCount -= 1; + if (DraggingItem.m_ItemCount <= 0) + { + DraggingItem.Empty(); + } + } + else if (DraggingItem.m_ItemCount == 1) + { + if (!IsPlaceableItem(DraggingItem.m_ItemCount)) + { + return; + } + + // Switch contents + cItem tmp(DraggingItem); + DraggingItem = Slot; + Slot = tmp; + } + + SetSlot(a_SlotNum, a_Player, Slot); + if (bAsync) + { + m_ParentWindow.BroadcastWholeWindow(); + } +} + + + + + +void cSlotAreaBeacon::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) +{ + const cItem * Slot = GetSlot(0, a_Player); + if (!Slot->IsEmpty() || !IsPlaceableItem(a_ItemStack.m_ItemType) || (a_ItemStack.m_ItemCount != 1)) + { + return; + } + + if (a_ShouldApply) + { + SetSlot(0, a_Player, a_ItemStack.CopyOne()); + } + a_ItemStack.Empty(); +} + + + + + +const cItem * cSlotAreaBeacon::GetSlot(int a_SlotNum, cPlayer & a_Player) const +{ + UNUSED(a_Player); + return &(m_Beacon->GetSlot(a_SlotNum)); +} + + + + + +void cSlotAreaBeacon::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) +{ + UNUSED(a_Player); + m_Beacon->SetSlot(a_SlotNum, a_Item); +} + + + + + +void cSlotAreaBeacon::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) +{ + UNUSED(a_SlotNum); + // Something has changed in the window, broadcast the entire window to all clients + ASSERT(a_ItemGrid == &(m_Beacon->GetContents())); + + m_ParentWindow.BroadcastWholeWindow(); +} + + + + //////////////////////////////////////////////////////////////////////////////// // cSlotAreaEnchanting: diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index fa842bb81..9a96f2f3c 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -15,6 +15,7 @@ class cWindow; class cPlayer; +class cBeaconEntity; class cChestEntity; class cDropSpenserEntity; class cEnderChestEntity; @@ -314,6 +315,34 @@ protected: +class cSlotAreaBeacon : + public cSlotArea, + public cItemGrid::cListener +{ + typedef cSlotArea super; + +public: + cSlotAreaBeacon(cBeaconEntity * a_Beacon, cWindow & a_ParentWindow); + virtual ~cSlotAreaBeacon(); + + bool IsPlaceableItem(short a_ItemType); + + virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override; + virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) override; + virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override; + virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override; + +protected: + cBeaconEntity * m_Beacon; + + // cItemGrid::cListener overrides: + virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override; +} ; + + + + + class cSlotAreaEnchanting : public cSlotAreaTemporary { diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index 4731f282b..aa129bfe8 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -9,6 +9,7 @@ #include "../Entities/Pickup.h" #include "../Inventory.h" #include "../Items/ItemHandler.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/DropSpenserEntity.h" #include "../BlockEntities/EnderChestEntity.h" @@ -840,6 +841,36 @@ void cAnvilWindow::GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ) +//////////////////////////////////////////////////////////////////////////////// +// cBeaconWindow: + +cBeaconWindow::cBeaconWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconEntity * a_Beacon) : + cWindow(wtBeacon, "Beacon"), + m_Beacon(a_Beacon) +{ + m_ShouldDistributeToHotbarFirst = true; + m_SlotAreas.push_back(new cSlotAreaBeacon(m_Beacon, *this)); + m_SlotAreas.push_back(new cSlotAreaInventory(*this)); + m_SlotAreas.push_back(new cSlotAreaHotBar(*this)); +} + + + + + +void cBeaconWindow::OpenedByPlayer(cPlayer & a_Player) +{ + super::OpenedByPlayer(a_Player); + + a_Player.GetClientHandle()->SendWindowProperty(*this, 0, m_Beacon->GetBeaconLevel()); + a_Player.GetClientHandle()->SendWindowProperty(*this, 1, m_Beacon->GetPrimaryPotion()); + a_Player.GetClientHandle()->SendWindowProperty(*this, 2, m_Beacon->GetSecondaryPotion()); +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cEnchantingWindow: diff --git a/src/UI/Window.h b/src/UI/Window.h index 97db0ca88..9fb0e3b38 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -23,6 +23,7 @@ class cDropSpenserEntity; class cEnderChestEntity; class cFurnaceEntity; class cHopperEntity; +class cBeaconEntity; class cSlotArea; class cSlotAreaAnvil; class cWorld; @@ -258,6 +259,26 @@ protected: +class cBeaconWindow : + public cWindow +{ + typedef cWindow super; +public: + cBeaconWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconEntity * a_Beacon); + + cBeaconEntity * GetBeaconEntity(void) const { return m_Beacon; } + + // cWindow Overrides: + virtual void OpenedByPlayer(cPlayer & a_Player) override; + +protected: + cBeaconEntity * m_Beacon; +} ; + + + + + class cEnchantingWindow : public cWindow { -- cgit v1.2.3 From 81e095adda62e4067ab4c07b0e4c7ce0f3dbce39 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:19:51 +0200 Subject: Exported the beacon. --- src/Bindings/AllToLua.pkg | 1 + src/Bindings/CMakeLists.txt | 1 + src/Bindings/ManualBindings.cpp | 2 ++ src/BlockEntities/BeaconEntity.cpp | 2 -- src/BlockEntities/BeaconEntity.h | 42 ++++++++++++++++++++++++-------------- src/Chunk.cpp | 33 ++++++++++++++++++++++++++++++ src/Chunk.h | 5 +++++ src/ChunkMap.cpp | 18 ++++++++++++++++ src/ChunkMap.h | 5 +++++ src/World.cpp | 10 +++++++++ src/World.h | 5 +++++ 11 files changed, 107 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/Bindings/AllToLua.pkg b/src/Bindings/AllToLua.pkg index 1e5dfd2fe..621358662 100644 --- a/src/Bindings/AllToLua.pkg +++ b/src/Bindings/AllToLua.pkg @@ -47,6 +47,7 @@ $cfile "../Inventory.h" $cfile "../Enchantments.h" $cfile "../Item.h" $cfile "../ItemGrid.h" +$cfile "../BlockEntities/BeaconEntity.h" $cfile "../BlockEntities/BlockEntity.h" $cfile "../BlockEntities/BlockEntityWithItems.h" $cfile "../BlockEntities/ChestEntity.h" diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt index 48e7ce79c..a2b381a26 100644 --- a/src/Bindings/CMakeLists.txt +++ b/src/Bindings/CMakeLists.txt @@ -53,6 +53,7 @@ set(BINDING_DEPENDENCIES ../Bindings/WebPlugin.h ../BiomeDef.h ../BlockArea.h + ../BlockEntities/BeaconEntity.h ../BlockEntities/BlockEntity.h ../BlockEntities/BlockEntityWithItems.h ../BlockEntities/ChestEntity.h diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index df9687fc0..460d879d5 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -16,6 +16,7 @@ #include "../WebAdmin.h" #include "../ClientHandle.h" #include "../BlockArea.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/DispenserEntity.h" @@ -2996,6 +2997,7 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_beginmodule(tolua_S, "cWorld"); tolua_function(tolua_S, "ChunkStay", tolua_cWorld_ChunkStay); tolua_function(tolua_S, "DoWithBlockEntityAt", tolua_DoWithXYZ); + tolua_function(tolua_S, "DoWithBeaconAt", tolua_DoWithXYZ); tolua_function(tolua_S, "DoWithChestAt", tolua_DoWithXYZ); tolua_function(tolua_S, "DoWithDispenserAt", tolua_DoWithXYZ); tolua_function(tolua_S, "DoWithDropSpenserAt", tolua_DoWithXYZ); diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index af6c124c0..38b710181 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -121,10 +121,8 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) { - LOG("SelectPrimaryPotion!"); if (!IsValidPotion(a_Potion, m_BeaconLevel)) { - LOG("FALLSE!"); return false; } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 52111e82a..4710e91e0 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -1,3 +1,10 @@ +// BeaconEntity.h + +// Declares the cBeaconEntity class representing a single beacon in the world + + + + #pragma once @@ -16,14 +23,26 @@ namespace Json +// tolua_begin class cBeaconEntity : public cBlockEntityWithItems { typedef cBlockEntityWithItems super; public: + // tolua_end + cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); + bool LoadFromJson(const Json::Value & a_Value); + // cBlockEntity overrides: + virtual void SaveToJson(Json::Value& a_Value) override; + virtual void SendTo(cClientHandle & a_Client) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void UsedBy(cPlayer * a_Player) override; + + // tolua_begin + /** Is the beacon active? */ bool IsActive(void) const { return m_IsActive; } @@ -45,33 +64,26 @@ public: /** Is the beacon blocked by non-transparent blocks that are higher than the beacon? */ bool IsBeaconBlocked(void); - /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ - static bool IsMineralBlock(BLOCKTYPE a_BlockType); - - /** Returns true if the potion can be used. */ - static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); - /** Update the beacon. */ void UpdateBeacon(void); /** Give the near-players the effects. */ void GiveEffects(void); - bool LoadFromJson(const Json::Value & a_Value); - - // cBlockEntity overrides: - virtual void SaveToJson(Json::Value& a_Value) override; - virtual void SendTo(cClientHandle & a_Client) override; - virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; - virtual void UsedBy(cPlayer * a_Player) override; + /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ + static bool IsMineralBlock(BLOCKTYPE a_BlockType); + + /** Returns true if the potion can be used. */ + static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + + // tolua_end protected: bool m_IsActive; char m_BeaconLevel; cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; - -} ; +} ; // tolua_export diff --git a/src/Chunk.cpp b/src/Chunk.cpp index b83036b6d..cd7dc698c 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -11,6 +11,7 @@ #include "Server.h" #include "zlib/zlib.h" #include "Defines.h" +#include "BlockEntities/BeaconEntity.h" #include "BlockEntities/ChestEntity.h" #include "BlockEntities/DispenserEntity.h" #include "BlockEntities/DropperEntity.h" @@ -2126,6 +2127,38 @@ bool cChunk::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBloc +bool cChunk::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback) +{ + // The blockentity list is locked by the parent chunkmap's CS + for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), itr2 = itr; itr != m_BlockEntities.end(); itr = itr2) + { + ++itr2; + if (((*itr)->GetPosX() != a_BlockX) || ((*itr)->GetPosY() != a_BlockY) || ((*itr)->GetPosZ() != a_BlockZ)) + { + continue; + } + if ((*itr)->GetBlockType() != E_BLOCK_BEACON) + { + // There is a block entity here, but of different type. No other block entity can be here, so we can safely bail out + return false; + } + + // The correct block entity is here + if (a_Callback.Item((cBeaconEntity *)*itr)) + { + return false; + } + return true; + } // for itr - m_BlockEntitites[] + + // Not found: + return false; +} + + + + + bool cChunk::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback) { // The blockentity list is locked by the parent chunkmap's CS diff --git a/src/Chunk.h b/src/Chunk.h index 7eee3999c..5cde3f08f 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -28,6 +28,7 @@ class cServer; class MTRand; class cPlayer; class cChunkMap; +class cBeaconEntity; class cChestEntity; class cDispenserEntity; class cFurnaceEntity; @@ -45,6 +46,7 @@ class cMobSpawner; typedef std::list cClientHandleList; typedef cItemCallback cEntityCallback; +typedef cItemCallback cBeaconCallback; typedef cItemCallback cChestCallback; typedef cItemCallback cDispenserCallback; typedef cItemCallback cFurnaceCallback; @@ -236,6 +238,9 @@ public: /** Calls the callback for the block entity at the specified coords; returns false if there's no block entity at those coords, true if found */ bool DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback); // Lua-acessible + /** Calls the callback for the beacon at the specified coords; returns false if there's no beacon at those coords, true if found */ + bool DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback); // Lua-acessible + /** Calls the callback for the chest at the specified coords; returns false if there's no chest at those coords, true if found */ bool DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback); // Lua-acessible diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 38e0cd82d..dd8be0631 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -2116,6 +2116,24 @@ bool cChunkMap::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cB +bool cChunkMap::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback) +{ + int ChunkX, ChunkZ; + int BlockX = a_BlockX, BlockY = a_BlockY, BlockZ = a_BlockZ; + cChunkDef::AbsoluteToRelative(BlockX, BlockY, BlockZ, ChunkX, ChunkZ); + cCSLock Lock(m_CSLayers); + cChunkPtr Chunk = GetChunkNoGen(ChunkX, ZERO_CHUNK_Y, ChunkZ); + if ((Chunk == NULL) || !Chunk->IsValid()) + { + return false; + } + return Chunk->DoWithBeaconAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); +} + + + + + bool cChunkMap::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback) { int ChunkX, ChunkZ; diff --git a/src/ChunkMap.h b/src/ChunkMap.h index e33d9f894..1e9a0f982 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -19,6 +19,7 @@ class MTRand; class cChunkStay; class cChunk; class cPlayer; +class cBeaconEntity; class cChestEntity; class cDispenserEntity; class cDropperEntity; @@ -40,6 +41,7 @@ typedef std::list cClientHandleList; typedef cChunk * cChunkPtr; typedef cItemCallback cEntityCallback; typedef cItemCallback cBlockEntityCallback; +typedef cItemCallback cBeaconCallback; typedef cItemCallback cChestCallback; typedef cItemCallback cDispenserCallback; typedef cItemCallback cDropperCallback; @@ -234,6 +236,9 @@ public: /** Calls the callback for the block entity at the specified coords; returns false if there's no block entity at those coords, true if found */ bool DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback); // Lua-acessible + /** Calls the callback for the beacon at the specified coords; returns false if there's no beacon at those coords, true if found */ + bool DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback); // Lua-acessible + /** Calls the callback for the chest at the specified coords; returns false if there's no chest at those coords, true if found */ bool DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback); // Lua-acessible diff --git a/src/World.cpp b/src/World.cpp index 348498693..b3c4b1de0 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -25,6 +25,7 @@ #include "Entities/TNTEntity.h" #include "BlockEntities/CommandBlockEntity.h" +#include "BlockEntities/BeaconEntity.h" // Simulators: #include "Simulator/SimulatorManager.h" @@ -1232,6 +1233,15 @@ bool cWorld::DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBloc +bool cWorld::DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback& a_Callback) +{ + return m_ChunkMap->DoWithBeaconAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); +} + + + + + bool cWorld::DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback) { return m_ChunkMap->DoWithChestAt(a_BlockX, a_BlockY, a_BlockZ, a_Callback); diff --git a/src/World.h b/src/World.h index 4d0ccf2dd..90b798e8e 100644 --- a/src/World.h +++ b/src/World.h @@ -41,6 +41,7 @@ class cEntity; class cBlockEntity; class cWorldGenerator; // The generator that actually generates the chunks for a single world class cChunkGenerator; // The thread responsible for generating chunks +class cBeaconEntity; class cChestEntity; class cDispenserEntity; class cFlowerPotEntity; @@ -59,6 +60,7 @@ typedef std::vector cSetChunkDataPtrs; typedef cItemCallback cPlayerListCallback; typedef cItemCallback cEntityCallback; +typedef cItemCallback cBeaconCallback; typedef cItemCallback cChestCallback; typedef cItemCallback cDispenserCallback; typedef cItemCallback cFurnaceCallback; @@ -523,6 +525,9 @@ public: /** Calls the callback for the block entity at the specified coords; returns false if there's no block entity at those coords, true if found */ virtual bool DoWithBlockEntityAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBlockEntityCallback & a_Callback) override; // Exported in ManualBindings.cpp + /** Calls the callback for the beacon at the specified coords; returns false if there's no beacon at those coords, true if found */ + bool DoWithBeaconAt(int a_BlockX, int a_BlockY, int a_BlockZ, cBeaconCallback & a_Callback); // Exported in ManualBindings.cpp + /** Calls the callback for the chest at the specified coords; returns false if there's no chest at those coords, true if found */ bool DoWithChestAt(int a_BlockX, int a_BlockY, int a_BlockZ, cChestCallback & a_Callback); // Exported in ManualBindings.cpp -- cgit v1.2.3 From ad4fa6eba823de90f528e199c9f7e61bdb3f55f3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:22:06 +0200 Subject: Changed return type from GetPrimaryPotion() and GetSecondaryPotion() --- src/BlockEntities/BeaconEntity.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 4710e91e0..07b2b85dc 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -49,8 +49,8 @@ public: /** Returns the beacon level. (0 - 4) */ char GetBeaconLevel(void) const { return m_BeaconLevel; } - char GetPrimaryPotion(void) const { return m_PrimaryPotion; } - char GetSecondaryPotion(void) const { return m_SecondaryPotion; } + cEntityEffect::eType GetPrimaryPotion(void) const { return m_PrimaryPotion; } + cEntityEffect::eType GetSecondaryPotion(void) const { return m_SecondaryPotion; } /** Select the primary potion. Returns false when the potion is invalid.*/ bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); -- cgit v1.2.3 From dcd226d9040409dec41c8f1f8909262946308ab0 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:50:34 +0200 Subject: Added beacon load/save. --- src/BlockEntities/BeaconEntity.h | 3 ++ src/WorldStorage/NBTChunkSerializer.cpp | 19 +++++++++++++ src/WorldStorage/NBTChunkSerializer.h | 2 ++ src/WorldStorage/WSSAnvil.cpp | 50 ++++++++++++++++++++++++++++++++- src/WorldStorage/WSSAnvil.h | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 07b2b85dc..59a4bdbd9 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -41,6 +41,9 @@ public: virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; virtual void UsedBy(cPlayer * a_Player) override; + /** Modify the beacon level. (It is needed to load the beacon corectly) */ + void SetBeaconLevel(char a_Level) { m_BeaconLevel = a_Level; } + // tolua_begin /** Is the beacon active? */ diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 4857da1b6..63387c33d 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -10,6 +10,7 @@ #include "../StringCompression.h" #include "FastNBT.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/DispenserEntity.h" @@ -176,6 +177,23 @@ void cNBTChunkSerializer::AddBasicTileEntity(cBlockEntity * a_Entity, const char +void cNBTChunkSerializer::AddBeaconEntity(cBeaconEntity * a_Entity) +{ + m_Writer.BeginCompound(""); + AddBasicTileEntity(a_Entity, "Beacon"); + m_Writer.AddInt("Levels", a_Entity->GetBeaconLevel()); + m_Writer.AddInt("Primary", (int)a_Entity->GetPrimaryPotion()); + m_Writer.AddInt("Secondary", (int)a_Entity->GetSecondaryPotion()); + m_Writer.BeginList("Items", TAG_Compound); + AddItemGrid(a_Entity->GetContents()); + m_Writer.EndList(); + m_Writer.EndCompound(); +} + + + + + void cNBTChunkSerializer::AddChestEntity(cChestEntity * a_Entity, BLOCKTYPE a_ChestType) { m_Writer.BeginCompound(""); @@ -825,6 +843,7 @@ void cNBTChunkSerializer::BlockEntity(cBlockEntity * a_Entity) // Add tile-entity into NBT: switch (a_Entity->GetBlockType()) { + case E_BLOCK_BEACON: AddBeaconEntity ((cBeaconEntity *) a_Entity); break; case E_BLOCK_CHEST: AddChestEntity ((cChestEntity *) a_Entity, a_Entity->GetBlockType()); break; case E_BLOCK_COMMAND_BLOCK: AddCommandBlockEntity((cCommandBlockEntity *)a_Entity); break; case E_BLOCK_DISPENSER: AddDispenserEntity ((cDispenserEntity *) a_Entity); break; diff --git a/src/WorldStorage/NBTChunkSerializer.h b/src/WorldStorage/NBTChunkSerializer.h index 710a06a70..4c229a65c 100644 --- a/src/WorldStorage/NBTChunkSerializer.h +++ b/src/WorldStorage/NBTChunkSerializer.h @@ -20,6 +20,7 @@ class cFastNBTWriter; class cEntity; class cBlockEntity; class cBoat; +class cBeaconEntity; class cChestEntity; class cCommandBlockEntity; class cDispenserEntity; @@ -93,6 +94,7 @@ protected: // Block entities: void AddBasicTileEntity(cBlockEntity * a_Entity, const char * a_EntityTypeID); + void AddBeaconEntity (cBeaconEntity * a_Entity); void AddChestEntity (cChestEntity * a_Entity, BLOCKTYPE a_ChestType); void AddDispenserEntity(cDispenserEntity * a_Entity); void AddDropperEntity (cDropperEntity * a_Entity); diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 71ff3ef99..5a1972fd4 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -16,6 +16,7 @@ #include "../StringCompression.h" #include "../SetChunkData.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/DispenserEntity.h" @@ -582,7 +583,11 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con { continue; } - if (strncmp(a_NBT.GetData(sID), "Chest", a_NBT.GetDataLength(sID)) == 0) + if (strncmp(a_NBT.GetData(sID), "Beacon", a_NBT.GetDataLength(sID)) == 0) + { + LoadBeaconFromNBT(a_BlockEntities, a_NBT, Child); + } + else if (strncmp(a_NBT.GetData(sID), "Chest", a_NBT.GetDataLength(sID)) == 0) { LoadChestFromNBT(a_BlockEntities, a_NBT, Child, E_BLOCK_CHEST); } @@ -746,6 +751,49 @@ void cWSSAnvil::LoadItemGridFromNBT(cItemGrid & a_ItemGrid, const cParsedNBT & a +void cWSSAnvil::LoadBeaconFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx) +{ + ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound); + int x, y, z; + if (!GetBlockEntityNBTPos(a_NBT, a_TagIdx, x, y, z)) + { + return; + } + + std::auto_ptr Beacon(new cBeaconEntity(x, y, z, m_World)); + + int CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Levels"); + if (CurrentLine >= 0) + { + Beacon->SetBeaconLevel((char)a_NBT.GetInt(CurrentLine)); + } + + CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Primary"); + if (CurrentLine >= 0) + { + Beacon->SelectPrimaryPotion((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + } + + CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Secondary"); + if (CurrentLine >= 0) + { + Beacon->SelectSecondaryPotion((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + } + + // We are better than mojang, we load/save the beacon inventory! + int Items = a_NBT.FindChildByName(a_TagIdx, "Items"); + if ((Items >= 0) && (a_NBT.GetType(Items) == TAG_List)) + { + LoadItemGridFromNBT(Beacon->GetContents(), a_NBT, Items); + } + + a_BlockEntities.push_back(Beacon.release()); +} + + + + + void cWSSAnvil::LoadChestFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_ChestType) { ASSERT(a_NBT.GetType(a_TagIdx) == TAG_Compound); diff --git a/src/WorldStorage/WSSAnvil.h b/src/WorldStorage/WSSAnvil.h index 9f8714404..f8eeb8247 100644 --- a/src/WorldStorage/WSSAnvil.h +++ b/src/WorldStorage/WSSAnvil.h @@ -133,6 +133,7 @@ protected: */ void LoadItemGridFromNBT(cItemGrid & a_ItemGrid, const cParsedNBT & a_NBT, int a_ItemsTagIdx, int s_SlotOffset = 0); + void LoadBeaconFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadChestFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_ChestType); void LoadDispenserFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); void LoadDropperFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx); -- cgit v1.2.3 From e6ca5a5ece61d8a4dd69b877a91f567edf2a3f63 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:54:19 +0200 Subject: Added window update. --- src/BlockEntities/BeaconEntity.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 38b710181..30d927663 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -127,6 +127,12 @@ bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) } m_PrimaryPotion = a_Potion; + + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(1, m_PrimaryPotion); + } return true; } @@ -142,6 +148,12 @@ bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) } m_SecondaryPotion = a_Potion; + + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(2, m_SecondaryPotion); + } return true; } @@ -189,6 +201,8 @@ bool cBeaconEntity::IsMineralBlock(BLOCKTYPE a_BlockType) void cBeaconEntity::UpdateBeacon(void) { + int OldBeaconLevel = m_BeaconLevel; + if (IsBeaconBlocked()) { m_IsActive = false; @@ -200,6 +214,15 @@ void cBeaconEntity::UpdateBeacon(void) m_IsActive = (m_BeaconLevel > 0); } + if (m_BeaconLevel != OldBeaconLevel) + { + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(0, m_BeaconLevel); + } + } + // TODO: Add achievement } -- cgit v1.2.3 From 556fc908aedcc36388e9d859487b140045e5e33e Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 12:13:11 +0200 Subject: Renamed functions and added beacon json saving. --- src/BlockEntities/BeaconEntity.cpp | 78 ++++++++++++++++----------------- src/BlockEntities/BeaconEntity.h | 12 ++--- src/ClientHandle.cpp | 24 +++++----- src/Protocol/Protocol17x.cpp | 4 +- src/UI/Window.cpp | 4 +- src/WorldStorage/NBTChunkSerializer.cpp | 4 +- src/WorldStorage/WSSAnvil.cpp | 4 +- src/WorldStorage/WSSCompact.cpp | 20 +++++++++ 8 files changed, 84 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 30d927663..c94783ba8 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -13,8 +13,8 @@ cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * : super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, 1, 1, a_World) , m_IsActive(false) , m_BeaconLevel(0) - , m_PrimaryPotion(cEntityEffect::effNoEffect) - , m_SecondaryPotion(cEntityEffect::effNoEffect) + , m_PrimaryEffect(cEntityEffect::effNoEffect) + , m_SecondaryEffect(cEntityEffect::effNoEffect) { UpdateBeacon(); } @@ -62,9 +62,9 @@ char cBeaconEntity::CalculatePyramidLevel(void) -bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel) +bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel) { - if (a_Potion == cEntityEffect::effNoEffect) + if (a_Effect == cEntityEffect::effNoEffect) { return true; } @@ -74,7 +74,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 4: { // Beacon level 4 - if (a_Potion == cEntityEffect::effRegeneration) + if (a_Effect == cEntityEffect::effRegeneration) { return true; } @@ -82,7 +82,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 3: { // Beacon level 3 - if (a_Potion == cEntityEffect::effStrength) + if (a_Effect == cEntityEffect::effStrength) { return true; } @@ -90,7 +90,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 2: { // Beacon level 2 - switch (a_Potion) + switch (a_Effect) { case cEntityEffect::effResistance: case cEntityEffect::effJumpBoost: @@ -102,7 +102,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 1: { // Beacon level 1 - switch (a_Potion) + switch (a_Effect) { case cEntityEffect::effSpeed: case cEntityEffect::effHaste: @@ -119,19 +119,19 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe -bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) +bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) { - if (!IsValidPotion(a_Potion, m_BeaconLevel)) + if (!IsValidEffect(a_Effect, m_BeaconLevel)) { return false; } - m_PrimaryPotion = a_Potion; + m_PrimaryEffect = a_Effect; // Send window update: if (GetWindow() != NULL) { - GetWindow()->SetProperty(1, m_PrimaryPotion); + GetWindow()->SetProperty(1, m_PrimaryEffect); } return true; } @@ -140,19 +140,19 @@ bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) -bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) +bool cBeaconEntity::SelectSecondaryEffect(cEntityEffect::eType a_Effect) { - if (!IsValidPotion(a_Potion, m_BeaconLevel)) + if (!IsValidEffect(a_Effect, m_BeaconLevel)) { return false; } - m_SecondaryPotion = a_Potion; + m_SecondaryEffect = a_Effect; // Send window update: if (GetWindow() != NULL) { - GetWindow()->SetProperty(2, m_SecondaryPotion); + GetWindow()->SetProperty(2, m_SecondaryEffect); } return true; } @@ -163,17 +163,15 @@ bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) bool cBeaconEntity::IsBeaconBlocked(void) { - bool IsBlocked = false; for (int Y = m_PosY; Y < cChunkDef::Height; ++Y) { BLOCKTYPE Block = m_World->GetBlock(m_PosX, Y, m_PosZ); if (!cBlockInfo::IsTransparent(Block)) { - IsBlocked = true; - break; + return true; } } - return IsBlocked; + return false; } @@ -239,22 +237,22 @@ void cBeaconEntity::GiveEffects(void) int Radius = m_BeaconLevel * 10 + 10; short EffectLevel = 0; - if ((m_BeaconLevel >= 4) && (m_PrimaryPotion == m_SecondaryPotion)) + if ((m_BeaconLevel >= 4) && (m_PrimaryEffect == m_SecondaryEffect)) { EffectLevel = 1; } - cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; - if ((m_BeaconLevel >= 4) && (m_PrimaryPotion != m_SecondaryPotion) && (m_SecondaryPotion > 0)) + cEntityEffect::eType SecondaryEffect = cEntityEffect::effNoEffect; + if ((m_BeaconLevel >= 4) && (m_PrimaryEffect != m_SecondaryEffect) && (m_SecondaryEffect > 0)) { - SecondaryPotion = m_SecondaryPotion; + SecondaryEffect = m_SecondaryEffect; } class cPlayerCallback : public cPlayerListCallback { int m_Radius; int m_PosX, m_PosY, m_PosZ; - cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + cEntityEffect::eType m_PrimaryEffect, m_SecondaryEffect; short m_EffectLevel; virtual bool Item(cPlayer * a_Player) @@ -269,28 +267,28 @@ void cBeaconEntity::GiveEffects(void) Vector3d BeaconPosition = Vector3d(m_PosX, m_PosY, m_PosZ); if ((PlayerPosition - BeaconPosition).Length() <= m_Radius) { - a_Player->AddEntityEffect(m_PrimaryPotion, 180, m_EffectLevel); + a_Player->AddEntityEffect(m_PrimaryEffect, 180, m_EffectLevel); - if (m_SecondaryPotion != cEntityEffect::effNoEffect) + if (m_SecondaryEffect != cEntityEffect::effNoEffect) { - a_Player->AddEntityEffect(m_SecondaryPotion, 180, 0); + a_Player->AddEntityEffect(m_SecondaryEffect, 180, 0); } } return false; } public: - cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryPotion, cEntityEffect::eType a_SecondaryPotion, short a_EffectLevel) + cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryEffect, cEntityEffect::eType a_SecondaryEffect, short a_EffectLevel) : m_Radius(a_Radius) , m_PosX(a_PosX) , m_PosY(a_PosY) , m_PosZ(a_PosZ) - , m_PrimaryPotion(a_PrimaryPotion) - , m_SecondaryPotion(a_SecondaryPotion) + , m_PrimaryEffect(a_PrimaryEffect) + , m_SecondaryEffect(a_SecondaryEffect) , m_EffectLevel(a_EffectLevel) {}; - } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryPotion, SecondaryPotion, EffectLevel); + } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryEffect, SecondaryEffect, EffectLevel); GetWorld()->ForEachPlayer(PlayerCallback); } @@ -353,17 +351,17 @@ bool cBeaconEntity::LoadFromJson(const Json::Value & a_Value) } m_BeaconLevel = (char)a_Value.get("Level", 0).asInt(); - int PrimaryPotion = a_Value.get("PrimaryPotion", 0).asInt(); - int SecondaryPotion = a_Value.get("SecondaryPotion", 0).asInt(); + int PrimaryEffect = a_Value.get("PrimaryEffect", 0).asInt(); + int SecondaryEffect = a_Value.get("SecondaryEffect", 0).asInt(); - if ((PrimaryPotion >= 0) && (PrimaryPotion <= (int)cEntityEffect::effSaturation)) + if ((PrimaryEffect >= 0) && (PrimaryEffect <= (int)cEntityEffect::effSaturation)) { - m_PrimaryPotion = (cEntityEffect::eType)PrimaryPotion; + m_PrimaryEffect = (cEntityEffect::eType)PrimaryEffect; } - if ((SecondaryPotion >= 0) && (SecondaryPotion <= (int)cEntityEffect::effSaturation)) + if ((SecondaryEffect >= 0) && (SecondaryEffect <= (int)cEntityEffect::effSaturation)) { - m_SecondaryPotion = (cEntityEffect::eType)SecondaryPotion; + m_SecondaryEffect = (cEntityEffect::eType)SecondaryEffect; } return true; @@ -390,8 +388,8 @@ void cBeaconEntity::SaveToJson(Json::Value& a_Value) a_Value["Slots"] = AllSlots; a_Value["Level"] = m_BeaconLevel; - a_Value["PrimaryPotion"] = (int)m_PrimaryPotion; - a_Value["SecondaryPotion"] = (int)m_SecondaryPotion; + a_Value["PrimaryEffect"] = (int)m_PrimaryEffect; + a_Value["SecondaryEffect"] = (int)m_SecondaryEffect; } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 59a4bdbd9..5cf8da24e 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -52,14 +52,14 @@ public: /** Returns the beacon level. (0 - 4) */ char GetBeaconLevel(void) const { return m_BeaconLevel; } - cEntityEffect::eType GetPrimaryPotion(void) const { return m_PrimaryPotion; } - cEntityEffect::eType GetSecondaryPotion(void) const { return m_SecondaryPotion; } + cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } + cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } /** Select the primary potion. Returns false when the potion is invalid.*/ - bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); + bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); /** Select the secondary potion. Returns false when the potion is invalid. */ - bool SelectSecondaryPotion(cEntityEffect::eType a_Potion); + bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ char CalculatePyramidLevel(void); @@ -77,7 +77,7 @@ public: static bool IsMineralBlock(BLOCKTYPE a_BlockType); /** Returns true if the potion can be used. */ - static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + static bool IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel); // tolua_end @@ -85,7 +85,7 @@ protected: bool m_IsActive; char m_BeaconLevel; - cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + cEntityEffect::eType m_PrimaryEffect, m_SecondaryEffect; } ; // tolua_export diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 849de2ce1..e833f338a 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -7,6 +7,7 @@ #include "Bindings/PluginManager.h" #include "Entities/Player.h" #include "Inventory.h" +#include "BlockEntities/BeaconEntity.h" #include "BlockEntities/ChestEntity.h" #include "BlockEntities/CommandBlockEntity.h" #include "BlockEntities/SignEntity.h" @@ -31,7 +32,6 @@ #include "Items/ItemSword.h" #include "polarssl/md5.h" -#include "BlockEntities/BeaconEntity.h" @@ -775,25 +775,25 @@ void cClientHandle::HandleBeaconSelection(const char * a_Data, size_t a_Length) cByteBuffer Buffer(a_Length); Buffer.Write(a_Data, a_Length); - int PrimaryPotionID, SecondaryPotionID; - Buffer.ReadBEInt(PrimaryPotionID); - Buffer.ReadBEInt(SecondaryPotionID); + int PrimaryEffectID, SecondaryEffectID; + Buffer.ReadBEInt(PrimaryEffectID); + Buffer.ReadBEInt(SecondaryEffectID); - cEntityEffect::eType PrimaryPotion = cEntityEffect::effNoEffect; - if ((PrimaryPotionID >= 0) && (PrimaryPotionID <= (int)cEntityEffect::effSaturation)) + cEntityEffect::eType PrimaryEffect = cEntityEffect::effNoEffect; + if ((PrimaryEffectID >= 0) && (PrimaryEffectID <= (int)cEntityEffect::effSaturation)) { - PrimaryPotion = (cEntityEffect::eType)PrimaryPotionID; + PrimaryEffect = (cEntityEffect::eType)PrimaryEffectID; } - cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; - if ((SecondaryPotionID >= 0) && (SecondaryPotionID <= (int)cEntityEffect::effSaturation)) + cEntityEffect::eType SecondaryEffect = cEntityEffect::effNoEffect; + if ((SecondaryEffectID >= 0) && (SecondaryEffectID <= (int)cEntityEffect::effSaturation)) { - SecondaryPotion = (cEntityEffect::eType)SecondaryPotionID; + SecondaryEffect = (cEntityEffect::eType)SecondaryEffectID; } Window->SetSlot(*m_Player, 0, cItem()); - BeaconWindow->GetBeaconEntity()->SelectPrimaryPotion(PrimaryPotion); - BeaconWindow->GetBeaconEntity()->SelectSecondaryPotion(SecondaryPotion); + BeaconWindow->GetBeaconEntity()->SelectPrimaryEffect(PrimaryEffect); + BeaconWindow->GetBeaconEntity()->SelectSecondaryEffect(SecondaryEffect); } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 8a68edd1f..8e31e211c 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -2590,8 +2590,8 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt Writer.AddInt("x", BeaconEntity.GetPosX()); Writer.AddInt("y", BeaconEntity.GetPosY()); Writer.AddInt("z", BeaconEntity.GetPosZ()); - Writer.AddInt("Primary", BeaconEntity.GetPrimaryPotion()); - Writer.AddInt("Secondary", BeaconEntity.GetSecondaryPotion()); + Writer.AddInt("Primary", BeaconEntity.GetPrimaryEffect()); + Writer.AddInt("Secondary", BeaconEntity.GetSecondaryEffect()); Writer.AddInt("Levels", BeaconEntity.GetBeaconLevel()); Writer.AddString("id", "Beacon"); // "Tile Entity ID" - MC wiki; vanilla server always seems to send this though break; diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index aa129bfe8..8f4913030 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -863,8 +863,8 @@ void cBeaconWindow::OpenedByPlayer(cPlayer & a_Player) super::OpenedByPlayer(a_Player); a_Player.GetClientHandle()->SendWindowProperty(*this, 0, m_Beacon->GetBeaconLevel()); - a_Player.GetClientHandle()->SendWindowProperty(*this, 1, m_Beacon->GetPrimaryPotion()); - a_Player.GetClientHandle()->SendWindowProperty(*this, 2, m_Beacon->GetSecondaryPotion()); + a_Player.GetClientHandle()->SendWindowProperty(*this, 1, m_Beacon->GetPrimaryEffect()); + a_Player.GetClientHandle()->SendWindowProperty(*this, 2, m_Beacon->GetSecondaryEffect()); } diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 63387c33d..601cd8833 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -182,8 +182,8 @@ void cNBTChunkSerializer::AddBeaconEntity(cBeaconEntity * a_Entity) m_Writer.BeginCompound(""); AddBasicTileEntity(a_Entity, "Beacon"); m_Writer.AddInt("Levels", a_Entity->GetBeaconLevel()); - m_Writer.AddInt("Primary", (int)a_Entity->GetPrimaryPotion()); - m_Writer.AddInt("Secondary", (int)a_Entity->GetSecondaryPotion()); + m_Writer.AddInt("Primary", (int)a_Entity->GetPrimaryEffect()); + m_Writer.AddInt("Secondary", (int)a_Entity->GetSecondaryEffect()); m_Writer.BeginList("Items", TAG_Compound); AddItemGrid(a_Entity->GetContents()); m_Writer.EndList(); diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 5a1972fd4..555ef410d 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -771,13 +771,13 @@ void cWSSAnvil::LoadBeaconFromNBT(cBlockEntityList & a_BlockEntities, const cPar CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Primary"); if (CurrentLine >= 0) { - Beacon->SelectPrimaryPotion((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + Beacon->SelectPrimaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); } CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Secondary"); if (CurrentLine >= 0) { - Beacon->SelectSecondaryPotion((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + Beacon->SelectSecondaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); } // We are better than mojang, we load/save the beacon inventory! diff --git a/src/WorldStorage/WSSCompact.cpp b/src/WorldStorage/WSSCompact.cpp index ee47047a0..58f9e3cab 100644 --- a/src/WorldStorage/WSSCompact.cpp +++ b/src/WorldStorage/WSSCompact.cpp @@ -9,6 +9,7 @@ #include "zlib/zlib.h" #include "json/json.h" #include "../StringCompression.h" +#include "../BlockEntities/BeaconEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/DispenserEntity.h" @@ -75,6 +76,7 @@ void cJsonChunkSerializer::BlockEntity(cBlockEntity * a_BlockEntity) const char * SaveInto = NULL; switch (a_BlockEntity->GetBlockType()) { + case E_BLOCK_BEACON: SaveInto = "Beacons"; break; case E_BLOCK_CHEST: SaveInto = "Chests"; break; case E_BLOCK_DISPENSER: SaveInto = "Dispensers"; break; case E_BLOCK_DROPPER: SaveInto = "Droppers"; break; @@ -268,6 +270,24 @@ bool cWSSCompact::EraseChunkData(const cChunkCoords & a_Chunk) void cWSSCompact::LoadEntitiesFromJson(Json::Value & a_Value, cEntityList & a_Entities, cBlockEntityList & a_BlockEntities, cWorld * a_World) { + // Load beacon: + Json::Value AllBeacons = a_Value.get("Beacons", Json::nullValue); + if (!AllBeacons.empty()) + { + for (Json::Value::iterator itr = AllBeacons.begin(); itr != AllBeacons.end(); ++itr) + { + std::auto_ptr BeaconEntity(new cBeaconEntity(0, 0, 0, a_World)); + if (!BeaconEntity->LoadFromJson(*itr)) + { + LOGWARNING("ERROR READING BEACON FROM JSON!"); + } + else + { + a_BlockEntities.push_back(BeaconEntity.release()); + } + } // for itr - AllBeacons[] + } + // Load chests: Json::Value AllChests = a_Value.get("Chests", Json::nullValue); if (!AllChests.empty()) -- cgit v1.2.3 From c49d4fd215170da29b5c285cc6a344ec102764c6 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 12:15:18 +0200 Subject: Updated documentation. --- src/BlockEntities/BeaconEntity.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 5cf8da24e..cc8ee8ad2 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -55,10 +55,10 @@ public: cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } - /** Select the primary potion. Returns false when the potion is invalid.*/ + /** Select the primary effect. Returns false when the effect is invalid.*/ bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); - /** Select the secondary potion. Returns false when the potion is invalid. */ + /** Select the secondary effect. Returns false when the effect is invalid. */ bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ @@ -76,7 +76,7 @@ public: /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ static bool IsMineralBlock(BLOCKTYPE a_BlockType); - /** Returns true if the potion can be used. */ + /** Returns true if the effect can be used. */ static bool IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel); // tolua_end -- cgit v1.2.3 From 6b1f7e7a45ebc04f39bb54edc85fbe9c9d0659e7 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 18:15:39 +0200 Subject: Renamed "select..." methods to "set..." and better IsValidEffect() function. --- src/BlockEntities/BeaconEntity.cpp | 58 ++++++++------------------------------ src/BlockEntities/BeaconEntity.h | 8 +++--- src/ClientHandle.cpp | 4 +-- src/WorldStorage/WSSAnvil.cpp | 4 +-- 4 files changed, 19 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index c94783ba8..55c5ccb7f 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -64,54 +64,18 @@ char cBeaconEntity::CalculatePyramidLevel(void) bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel) { - if (a_Effect == cEntityEffect::effNoEffect) + switch (a_Effect) { - return true; + case cEntityEffect::effRegeneration: return (a_BeaconLevel >= 4); + case cEntityEffect::effStrength: return (a_BeaconLevel >= 3); + case cEntityEffect::effResistance: return (a_BeaconLevel >= 2); + case cEntityEffect::effJumpBoost: return (a_BeaconLevel >= 2); + case cEntityEffect::effSpeed: return (a_BeaconLevel >= 1); + case cEntityEffect::effHaste: return (a_BeaconLevel >= 1); + case cEntityEffect::effNoEffect: return true; } - switch (a_BeaconLevel) - { - case 4: - { - // Beacon level 4 - if (a_Effect == cEntityEffect::effRegeneration) - { - return true; - } - } - case 3: - { - // Beacon level 3 - if (a_Effect == cEntityEffect::effStrength) - { - return true; - } - } - case 2: - { - // Beacon level 2 - switch (a_Effect) - { - case cEntityEffect::effResistance: - case cEntityEffect::effJumpBoost: - { - return true; - } - } - } - case 1: - { - // Beacon level 1 - switch (a_Effect) - { - case cEntityEffect::effSpeed: - case cEntityEffect::effHaste: - { - return true; - } - } - } - } + LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); return false; } @@ -119,7 +83,7 @@ bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLe -bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) +bool cBeaconEntity::SetPrimaryEffect(cEntityEffect::eType a_Effect) { if (!IsValidEffect(a_Effect, m_BeaconLevel)) { @@ -140,7 +104,7 @@ bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) -bool cBeaconEntity::SelectSecondaryEffect(cEntityEffect::eType a_Effect) +bool cBeaconEntity::SetSecondaryEffect(cEntityEffect::eType a_Effect) { if (!IsValidEffect(a_Effect, m_BeaconLevel)) { diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index cc8ee8ad2..0d7150aef 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -55,11 +55,11 @@ public: cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } - /** Select the primary effect. Returns false when the effect is invalid.*/ - bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); + /** Sets the primary effect. Returns false when the effect is invalid. */ + bool SetPrimaryEffect(cEntityEffect::eType a_Effect); - /** Select the secondary effect. Returns false when the effect is invalid. */ - bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); + /** Sets the secondary effect. Returns false when the effect is invalid. */ + bool SetSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ char CalculatePyramidLevel(void); diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index e833f338a..3ce506e1e 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -792,8 +792,8 @@ void cClientHandle::HandleBeaconSelection(const char * a_Data, size_t a_Length) } Window->SetSlot(*m_Player, 0, cItem()); - BeaconWindow->GetBeaconEntity()->SelectPrimaryEffect(PrimaryEffect); - BeaconWindow->GetBeaconEntity()->SelectSecondaryEffect(SecondaryEffect); + BeaconWindow->GetBeaconEntity()->SetPrimaryEffect(PrimaryEffect); + BeaconWindow->GetBeaconEntity()->SetSecondaryEffect(SecondaryEffect); } diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 555ef410d..d3a156ee1 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -771,13 +771,13 @@ void cWSSAnvil::LoadBeaconFromNBT(cBlockEntityList & a_BlockEntities, const cPar CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Primary"); if (CurrentLine >= 0) { - Beacon->SelectPrimaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + Beacon->SetPrimaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); } CurrentLine = a_NBT.FindChildByName(a_TagIdx, "Secondary"); if (CurrentLine >= 0) { - Beacon->SelectSecondaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); + Beacon->SetSecondaryEffect((cEntityEffect::eType)a_NBT.GetInt(CurrentLine)); } // We are better than mojang, we load/save the beacon inventory! -- cgit v1.2.3 From 09b63565bcdd5977988d390f33de47b17cc7b7b7 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 23:19:05 +0200 Subject: Use "default:" in switch. --- src/BlockEntities/BeaconEntity.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 55c5ccb7f..805e5e61f 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -73,10 +73,13 @@ bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLe case cEntityEffect::effSpeed: return (a_BeaconLevel >= 1); case cEntityEffect::effHaste: return (a_BeaconLevel >= 1); case cEntityEffect::effNoEffect: return true; - } - LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); - return false; + default: + { + LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); + return false; + } + } } -- cgit v1.2.3