1
0

Command block (de)serialization

This commit is contained in:
andrew 2014-01-18 15:40:47 +02:00
parent 9a9b51a513
commit be5299350a
6 changed files with 122 additions and 21 deletions

View File

@ -45,7 +45,7 @@ void cCommandBlockEntity::UsedBy(cPlayer * a_Player)
cWindow * Window = GetWindow();
if (Window == NULL)
{
//OpenWindow(new cDropSpenserWindow(m_PosX, m_PosY, m_PosZ, this));
//OpenWindow(new cDropSpenserWindow(m_PosX, m_PosY, m_PosZ, this)); FIXME
Window = GetWindow();
}
@ -71,6 +71,24 @@ void cCommandBlockEntity::SetCommand(const AString & a_Cmd)
void cCommandBlockEntity::SetLastOutput(const AString & a_LastOut)
{
m_LastOutput = a_LastOut;
}
void cCommandBlockEntity::SetResult(const NIBBLETYPE a_Result)
{
m_Result = a_Result;
}
const AString & cCommandBlockEntity::GetCommand(void) const
{
return m_Command;
@ -89,6 +107,15 @@ const AString & cCommandBlockEntity::GetLastOutput(void) const
NIBBLETYPE cCommandBlockEntity::GetResult(void) const
{
return m_Result;
}
void cCommandBlockEntity::Activate(void)
{
m_ShouldExecute = true;
@ -164,8 +191,11 @@ void cCommandBlockEntity::SaveToJson(Json::Value & a_Value)
void cCommandBlockEntity::Execute()
{
// TODO: Parse arguments and dispatch command
LOGD("Command: %s", m_Command.c_str());
m_LastOutput = "";
m_Result = 0;
}

View File

@ -48,6 +48,10 @@ public:
virtual void SendTo(cClientHandle & a_Client) override;
virtual void UsedBy(cPlayer * a_Player) override;
void SetLastOutput(const AString & a_LastOut);
void SetResult(const NIBBLETYPE a_Result);
// tolua_begin
/// Sets the internal redstone power flag to "on" or "off", depending on the parameter. Calls Activate() if appropriate
@ -65,6 +69,9 @@ public:
/// Retrieves the last line of output generated by the command block
const AString & GetLastOutput(void) const;
/// Retrieves the result (signal strength) of the last operation
NIBBLETYPE GetResult(void) const;
// tolua_end
private:
@ -78,6 +85,8 @@ private:
AString m_Command;
AString m_LastOutput;
NIBBLETYPE m_Result;
} ; // tolua_export

View File

@ -10,6 +10,7 @@
#include "FastNBT.h"
#include "../BlockEntities/ChestEntity.h"
#include "../BlockEntities/CommandBlockEntity.h"
#include "../BlockEntities/DispenserEntity.h"
#include "../BlockEntities/DropperEntity.h"
#include "../BlockEntities/FurnaceEntity.h"
@ -228,6 +229,21 @@ void cNBTChunkSerializer::AddNoteEntity(cNoteEntity * a_Note)
void cNBTChunkSerializer::AddCommandBlockEntity(cCommandBlockEntity * a_CmdBlock)
{
m_Writer.BeginCompound("");
AddBasicTileEntity(a_CmdBlock, "Control");
m_Writer.AddString("Command", a_CmdBlock->GetCommand());
m_Writer.AddInt ("SuccessCount", a_CmdBlock->GetResult());
m_Writer.AddString("LastOutput", a_CmdBlock->GetLastOutput());
m_Writer.AddByte ("TrackOutput", 1); // Unknown (?)
m_Writer.EndCompound();
}
void cNBTChunkSerializer::AddSignEntity(cSignEntity * a_Sign)
{
m_Writer.BeginCompound("");
@ -648,6 +664,7 @@ void cNBTChunkSerializer::BlockEntity(cBlockEntity * a_Entity)
case E_BLOCK_WALLSIGN: AddSignEntity ((cSignEntity *) a_Entity); break;
case E_BLOCK_NOTE_BLOCK: AddNoteEntity ((cNoteEntity *) a_Entity); break;
case E_BLOCK_JUKEBOX: AddJukeboxEntity ((cJukeboxEntity *) a_Entity); break;
case E_BLOCK_COMMAND_BLOCK: AddCommandBlockEntity((cCommandBlockEntity *) a_Entity); break;
default:
{
ASSERT(!"Unhandled block entity saved into Anvil");

View File

@ -21,6 +21,7 @@ class cEntity;
class cBlockEntity;
class cBoat;
class cChestEntity;
class cCommandBlockEntity;
class cDispenserEntity;
class cDropperEntity;
class cFurnaceEntity;
@ -92,6 +93,7 @@ protected:
void AddJukeboxEntity (cJukeboxEntity * a_Jukebox);
void AddNoteEntity (cNoteEntity * a_Note);
void AddSignEntity (cSignEntity * a_Sign);
void AddCommandBlockEntity(cCommandBlockEntity * a_CmdBlock);
// Entities:
void AddBasicEntity (cEntity * a_Entity, const AString & a_ClassName);

View File

@ -15,6 +15,7 @@
#include "../StringCompression.h"
#include "../BlockEntities/ChestEntity.h"
#include "../BlockEntities/CommandBlockEntity.h"
#include "../BlockEntities/DispenserEntity.h"
#include "../BlockEntities/DropperEntity.h"
#include "../BlockEntities/FurnaceEntity.h"
@ -567,6 +568,10 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntityList & a_BlockEntities, con
{
LoadChestFromNBT(a_BlockEntities, a_NBT, Child);
}
else if (strncmp(a_NBT.GetData(sID), "Control", a_NBT.GetDataLength(sID)) == 0)
{
LoadCommandBlockFromNBT(a_BlockEntities, a_NBT, Child);
}
else if (strncmp(a_NBT.GetData(sID), "Dropper", a_NBT.GetDataLength(sID)) == 0)
{
LoadDropperFromNBT(a_BlockEntities, a_NBT, Child);
@ -915,6 +920,43 @@ void cWSSAnvil::LoadSignFromNBT(cBlockEntityList & a_BlockEntities, const cParse
void cWSSAnvil::LoadCommandBlockFromNBT(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<cCommandBlockEntity> CmdBlock(new cCommandBlockEntity(x, y, z, m_World));
int currentLine = a_NBT.FindChildByName(a_TagIdx, "Command");
if (currentLine >= 0)
{
CmdBlock->SetCommand(a_NBT.GetString(currentLine));
}
currentLine = a_NBT.FindChildByName(a_TagIdx, "SuccessCount");
if (currentLine >= 0)
{
CmdBlock->SetResult(a_NBT.GetInt(currentLine));
}
currentLine = a_NBT.FindChildByName(a_TagIdx, "LastOutput");
if (currentLine >= 0)
{
CmdBlock->SetLastOutput(a_NBT.GetString(currentLine));
}
// FIXME: TrackOutput
a_BlockEntities.push_back(CmdBlock.release());
}
void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength)
{
if (strncmp(a_IDTag, "Boat", a_IDTagLength) == 0)

View File

@ -139,6 +139,7 @@ protected:
void LoadJukeboxFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadNoteFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadSignFromNBT (cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadCommandBlockFromNBT(cBlockEntityList & a_BlockEntities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength);