From 082573771f469cfaef03d22e4281f207beef36c8 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 14 Nov 2013 15:37:09 +0100 Subject: Added cSignEntity into API, added cChunkDesc:GetBlockEntity(). This fixes both #228 and #347. --- source/BlockEntities/BlockEntity.cpp | 44 ++++++++++++++++++++++++ source/BlockEntities/BlockEntity.h | 5 +++ source/BlockEntities/JukeboxEntity.cpp | 16 +++++++-- source/BlockEntities/JukeboxEntity.h | 5 +++ source/BlockEntities/NoteEntity.cpp | 13 ++++--- source/BlockEntities/NoteEntity.h | 6 +++- source/BlockEntities/SignEntity.cpp | 62 +++++++++++++++------------------- source/BlockEntities/SignEntity.h | 43 +++++++++++++++++++---- 8 files changed, 145 insertions(+), 49 deletions(-) create mode 100644 source/BlockEntities/BlockEntity.cpp (limited to 'source/BlockEntities') diff --git a/source/BlockEntities/BlockEntity.cpp b/source/BlockEntities/BlockEntity.cpp new file mode 100644 index 000000000..41a488717 --- /dev/null +++ b/source/BlockEntities/BlockEntity.cpp @@ -0,0 +1,44 @@ + +// BlockEntity.cpp + +// Implements the cBlockEntity class that is the common ancestor for all block entities + +#include "Globals.h" +#include "BlockEntity.h" +#include "ChestEntity.h" +#include "DispenserEntity.h" +#include "DropperEntity.h" +#include "FurnaceEntity.h" +#include "HopperEntity.h" +#include "JukeboxEntity.h" +#include "NoteEntity.h" +#include "SignEntity.h" + + + + + +cBlockEntity * cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) +{ + switch (a_BlockType) + { + case E_BLOCK_CHEST: return new cChestEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_DISPENSER: return new cDispenserEntity(a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_DROPPER: return new cDropperEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_LIT_FURNACE: return new cFurnaceEntity (a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, a_World); + case E_BLOCK_FURNACE: return new cFurnaceEntity (a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, a_World); + case E_BLOCK_HOPPER: return new cHopperEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_SIGN_POST: return new cSignEntity (a_BlockType, a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_WALLSIGN: return new cSignEntity (a_BlockType, a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_NOTE_BLOCK: return new cNoteEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_JUKEBOX: return new cJukeboxEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + } + LOGD("%s: Requesting creation of an unknown block entity - block type %d (%s)", + __FUNCTION__, a_BlockType, ItemTypeToString(a_BlockType).c_str() + ); + return NULL; +} + + + + diff --git a/source/BlockEntities/BlockEntity.h b/source/BlockEntities/BlockEntity.h index ab7d7f5dc..6a6ffb448 100644 --- a/source/BlockEntities/BlockEntity.h +++ b/source/BlockEntities/BlockEntity.h @@ -49,6 +49,11 @@ public: // tolua_begin + /// Creates a new block entity for the specified block type + /// If a_World is valid, then the entity is created bound to that world + /// Returns NULL for unknown block types + static cBlockEntity * CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World = NULL); + // Position, in absolute block coordinates: int GetPosX(void) const { return m_PosX; } int GetPosY(void) const { return m_PosY; } diff --git a/source/BlockEntities/JukeboxEntity.cpp b/source/BlockEntities/JukeboxEntity.cpp index 1288719f6..adf0f6af4 100644 --- a/source/BlockEntities/JukeboxEntity.cpp +++ b/source/BlockEntities/JukeboxEntity.cpp @@ -9,9 +9,19 @@ -cJukeboxEntity::cJukeboxEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) - : cBlockEntity(E_BLOCK_JUKEBOX, a_BlockX, a_BlockY, a_BlockZ, a_World) - , m_Record( 0 ) +cJukeboxEntity::cJukeboxEntity(int a_BlockX, int a_BlockY, int a_BlockZ) : + super(E_BLOCK_JUKEBOX, a_BlockX, a_BlockY, a_BlockZ, NULL), + m_Record(0) +{ +} + + + + + +cJukeboxEntity::cJukeboxEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : + super(E_BLOCK_JUKEBOX, a_BlockX, a_BlockY, a_BlockZ, a_World), + m_Record(0) { } diff --git a/source/BlockEntities/JukeboxEntity.h b/source/BlockEntities/JukeboxEntity.h index 38574c945..2dd61a403 100644 --- a/source/BlockEntities/JukeboxEntity.h +++ b/source/BlockEntities/JukeboxEntity.h @@ -20,7 +20,12 @@ namespace Json class cJukeboxEntity : public cBlockEntity { + typedef cBlockEntity super; public: + + /// Creates a new jukebox entity that is not assigned to a world + cJukeboxEntity(int a_BlockX, int a_BlockY, int a_BlockZ); + cJukeboxEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); virtual ~cJukeboxEntity(); diff --git a/source/BlockEntities/NoteEntity.cpp b/source/BlockEntities/NoteEntity.cpp index 6dc0e20a1..f06c90927 100644 --- a/source/BlockEntities/NoteEntity.cpp +++ b/source/BlockEntities/NoteEntity.cpp @@ -6,9 +6,12 @@ #include -cNoteEntity::cNoteEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) - : cBlockEntity(E_BLOCK_NOTE_BLOCK, a_BlockX, a_BlockY, a_BlockZ, a_World) - , m_Pitch( 0 ) + + + +cNoteEntity::cNoteEntity(int a_BlockX, int a_BlockY, int a_BlockZ) : + super(E_BLOCK_NOTE_BLOCK, a_BlockX, a_BlockY, a_BlockZ, NULL), + m_Pitch(0) { } @@ -16,7 +19,9 @@ cNoteEntity::cNoteEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_Wo -cNoteEntity::~cNoteEntity() +cNoteEntity::cNoteEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : + super(E_BLOCK_NOTE_BLOCK, a_BlockX, a_BlockY, a_BlockZ, a_World), + m_Pitch(0) { } diff --git a/source/BlockEntities/NoteEntity.h b/source/BlockEntities/NoteEntity.h index 385591df6..84c4972de 100644 --- a/source/BlockEntities/NoteEntity.h +++ b/source/BlockEntities/NoteEntity.h @@ -29,9 +29,13 @@ enum ENUM_NOTE_INSTRUMENTS class cNoteEntity : public cBlockEntity { + typedef cBlockEntity super; public: + + /// Creates a new note entity that is not assigned to a world + cNoteEntity(int a_BlockX, int a_BlockY, int a_BlockZ); + cNoteEntity(int a_X, int a_Y, int a_Z, cWorld * a_World); - virtual ~cNoteEntity(); bool LoadFromJson( const Json::Value& a_Value ); virtual void SaveToJson( Json::Value& a_Value ) override; diff --git a/source/BlockEntities/SignEntity.cpp b/source/BlockEntities/SignEntity.cpp index 2c160e603..8b335651d 100644 --- a/source/BlockEntities/SignEntity.cpp +++ b/source/BlockEntities/SignEntity.cpp @@ -1,21 +1,19 @@ -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules +// SignEntity.cpp -#include "SignEntity.h" - -#include "../Entities/Player.h" -// #include "ClientHandle.h" -// #include "World.h" -// #include "Root.h" +// Implements the cSignEntity class representing a single sign in the world +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include +#include "SignEntity.h" +#include "../Entities/Player.h" -cSignEntity::cSignEntity(BLOCKTYPE a_BlockType, int a_X, int a_Y, int a_Z, cWorld * a_World) - : cBlockEntity(a_BlockType, a_X, a_Y, a_Z, a_World) +cSignEntity::cSignEntity(BLOCKTYPE a_BlockType, int a_BlockX, int a_BlockY, int a_BlockZ) : + super(a_BlockType, a_BlockX, a_BlockY, a_BlockZ, NULL) { } @@ -23,7 +21,8 @@ cSignEntity::cSignEntity(BLOCKTYPE a_BlockType, int a_X, int a_Y, int a_Z, cWorl -cSignEntity::~cSignEntity() +cSignEntity::cSignEntity(BLOCKTYPE a_BlockType, int a_X, int a_Y, int a_Z, cWorld * a_World) : + super(a_BlockType, a_X, a_Y, a_Z, a_World) { } @@ -32,16 +31,16 @@ cSignEntity::~cSignEntity() // It don't do anything when 'used' -void cSignEntity::UsedBy( cPlayer * a_Player ) +void cSignEntity::UsedBy(cPlayer * a_Player) { - (void)a_Player; + UNUSED(a_Player); } -void cSignEntity::SetLines( const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4 ) +void cSignEntity::SetLines(const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4) { m_Line[0] = a_Line1; m_Line[1] = a_Line2; @@ -53,25 +52,28 @@ void cSignEntity::SetLines( const AString & a_Line1, const AString & a_Line2, co -void cSignEntity::SetLine( int a_Index, const AString & a_Line ) +void cSignEntity::SetLine(int a_Index, const AString & a_Line) { - if( a_Index < 4 && a_Index > -1 ) + if ((a_Index < 0) || (a_Index >= ARRAYCOUNT(m_Line))) { - m_Line[a_Index] = a_Line; + LOGWARNING("%s: setting a non-existent line %d (value \"%s\"", __FUNCTION__, a_Index, a_Line.c_str()); + return; } + m_Line[a_Index] = a_Line; } -AString cSignEntity::GetLine( int a_Index ) const +AString cSignEntity::GetLine(int a_Index) const { - if( a_Index < 4 && a_Index > -1 ) + if ((a_Index < 0) || (a_Index >= ARRAYCOUNT(m_Line))) { - return m_Line[a_Index]; + LOGWARNING("%s: requesting a non-existent line %d", __FUNCTION__, a_Index); + return ""; } - return ""; + return m_Line[a_Index]; } @@ -87,19 +89,7 @@ void cSignEntity::SendTo(cClientHandle & a_Client) -#define READ(File, Var) \ - if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \ - { \ - LOGERROR("ERROR READING cSignEntity %s FROM FILE (line %d)", #Var, __LINE__); \ - return false; \ - } - - - - - - -bool cSignEntity::LoadFromJson( const Json::Value & a_Value ) +bool cSignEntity::LoadFromJson(const Json::Value & a_Value) { m_PosX = a_Value.get("x", 0).asInt(); m_PosY = a_Value.get("y", 0).asInt(); @@ -113,7 +103,11 @@ bool cSignEntity::LoadFromJson( const Json::Value & a_Value ) return true; } -void cSignEntity::SaveToJson( Json::Value & a_Value ) + + + + +void cSignEntity::SaveToJson(Json::Value & a_Value) { a_Value["x"] = m_PosX; a_Value["y"] = m_PosY; diff --git a/source/BlockEntities/SignEntity.h b/source/BlockEntities/SignEntity.h index b4e7a141f..50706bdfe 100644 --- a/source/BlockEntities/SignEntity.h +++ b/source/BlockEntities/SignEntity.h @@ -1,4 +1,12 @@ +// SignEntity.h + +// Declares the cSignEntity class representing a single sign in the world + + + + + #pragma once #include "BlockEntity.h" @@ -13,28 +21,49 @@ namespace Json } + + + +// tolua_begin + class cSignEntity : public cBlockEntity { + typedef cBlockEntity super; + public: + + /// Creates a new empty sign entity at the specified block coords and block type (wall or standing) + /// Used mainly by plugins while generating chunks + cSignEntity(BLOCKTYPE a_BlockType, int a_BlockX, int a_BlockY, int a_BlockZ); + + // tolua_end + cSignEntity(BLOCKTYPE a_BlockType, int a_X, int a_Y, int a_Z, cWorld * a_World); - virtual ~cSignEntity(); bool LoadFromJson( const Json::Value& a_Value ); virtual void SaveToJson(Json::Value& a_Value ) override; - void SetLines( const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4 ); - void SetLine( int a_Index, const AString & a_Line ); - - AString GetLine( int a_Index ) const; + // tolua_begin + + /// Sets all the sign's lines + void SetLines(const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4); + + /// Sets individual line (zero-based index) + void SetLine(int a_Index, const AString & a_Line); - virtual void UsedBy( cPlayer * a_Player ) override; + /// Retrieves individual line (zero-based index) + AString GetLine(int a_Index) const; + + // tolua_end + + virtual void UsedBy(cPlayer * a_Player) override; virtual void SendTo(cClientHandle & a_Client) override; private: AString m_Line[4]; -}; +} ; // tolua_export -- cgit v1.2.3