From 9a9b51a5134d8afd8f089d247c59668bd7da695d Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 18 Jan 2014 15:16:47 +0200 Subject: Basic command block implementation --- src/BlockEntities/CommandBlockEntity.cpp | 173 +++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/BlockEntities/CommandBlockEntity.cpp (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp new file mode 100644 index 000000000..6e371e0fe --- /dev/null +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -0,0 +1,173 @@ + +// CommandBlockEntity.cpp + +// Implements the cCommandBlockEntity class representing a single command block in the world + +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules +#include "json/json.h" +#include "CommandBlockEntity.h" +#include "../Entities/Player.h" + + + + + +cCommandBlockEntity::cCommandBlockEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) : + super(E_BLOCK_COMMAND_BLOCK, a_X, a_Y, a_Z, a_World), + m_ShouldExecute(false), + m_IsPowered(false) +{ + SetBlockEntity(this); // cBlockEntityWindowOwner +} + + + + + + +cCommandBlockEntity::~cCommandBlockEntity() +{ + // Tell window its owner is destroyed + cWindow * Window = GetWindow(); + if (Window != NULL) + { + Window->OwnerDestroyed(); + } +} + + + + + + +void cCommandBlockEntity::UsedBy(cPlayer * a_Player) +{ + cWindow * Window = GetWindow(); + if (Window == NULL) + { + //OpenWindow(new cDropSpenserWindow(m_PosX, m_PosY, m_PosZ, this)); + Window = GetWindow(); + } + + if (Window != NULL) + { + if (a_Player->GetWindow() != Window) + { + a_Player->OpenWindow(Window); + } + } +} + + + + + +void cCommandBlockEntity::SetCommand(const AString & a_Cmd) +{ + m_Command = a_Cmd;LOGD("Hrey %s", a_Cmd.c_str()); +} + + + + + +const AString & cCommandBlockEntity::GetCommand(void) const +{ + return m_Command; +} + + + + + +const AString & cCommandBlockEntity::GetLastOutput(void) const +{ + return m_LastOutput; +} + + + + + +void cCommandBlockEntity::Activate(void) +{ + m_ShouldExecute = true; +} + + + + + +void cCommandBlockEntity::SetRedstonePower(bool a_IsPowered) +{ + if (a_IsPowered && !m_IsPowered) + { + Activate(); + } + m_IsPowered = a_IsPowered; +} + + + + + +bool cCommandBlockEntity::Tick(float a_Dt, cChunk & a_Chunk) +{ + if (!m_ShouldExecute) + { + return false; + } + + m_ShouldExecute = false; + Execute(); + return true; +} + + + + + +void cCommandBlockEntity::SendTo(cClientHandle & a_Client) +{ + // Nothing needs to be sent + UNUSED(a_Client); +} + + + + + +bool cCommandBlockEntity::LoadFromJson(const Json::Value & a_Value) +{ + m_Command = a_Value.get("Command", "").asString(); + + m_LastOutput = a_Value.get("LastOutput", "").asString(); + + return true; +} + + + + + +void cCommandBlockEntity::SaveToJson(Json::Value & a_Value) +{ + a_Value["Command"] = m_Command; + + a_Value["LastOutput"] = m_LastOutput; +} + + + + + +void cCommandBlockEntity::Execute() +{ + // TODO: Parse arguments and dispatch command + LOGD("Command: %s", m_Command.c_str()); + m_LastOutput = ""; +} + + + + -- cgit v1.2.3 From be5299350a3b808ade39df7e9654eda47732e96e Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 18 Jan 2014 15:40:47 +0200 Subject: Command block (de)serialization --- src/BlockEntities/CommandBlockEntity.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp index 6e371e0fe..88e55dece 100644 --- a/src/BlockEntities/CommandBlockEntity.cpp +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -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; } -- cgit v1.2.3 From 1ad6469a180dfe6932c756fe3c99bcada3ee0b7e Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 18 Jan 2014 16:59:33 +0200 Subject: Command blocks: Execute() --- src/BlockEntities/CommandBlockEntity.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp index 88e55dece..e70e259aa 100644 --- a/src/BlockEntities/CommandBlockEntity.cpp +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -8,6 +8,10 @@ #include "CommandBlockEntity.h" #include "../Entities/Player.h" +#include "CommandOutput.h" +#include "Root.h" +#include "Server.h" // ExecuteConsoleCommand() + @@ -45,7 +49,8 @@ void cCommandBlockEntity::UsedBy(cPlayer * a_Player) cWindow * Window = GetWindow(); if (Window == NULL) { - //OpenWindow(new cDropSpenserWindow(m_PosX, m_PosY, m_PosZ, this)); FIXME + // TODO 2014-01-18 xdot: Open the appropriate window. + // OpenWindow(new cCommandBlockWindow(m_PosX, m_PosY, m_PosZ, this)); Window = GetWindow(); } @@ -64,7 +69,7 @@ void cCommandBlockEntity::UsedBy(cPlayer * a_Player) void cCommandBlockEntity::SetCommand(const AString & a_Cmd) { - m_Command = a_Cmd;LOGD("Hrey %s", a_Cmd.c_str()); + m_Command = a_Cmd; } @@ -190,11 +195,28 @@ void cCommandBlockEntity::SaveToJson(Json::Value & a_Value) void cCommandBlockEntity::Execute() { - // TODO: Parse arguments and dispatch command + class CommandBlockOutCb : + public cCommandOutputCallback + { + cCommandBlockEntity* m_CmdBlock; + + public: + CommandBlockOutCb(cCommandBlockEntity* a_CmdBlock) : m_CmdBlock(a_CmdBlock) {} + + virtual void Out(const AString & a_Text) + { + ASSERT(m_CmdBlock != NULL); + + // Overwrite field + m_CmdBlock->SetLastOutput(a_Text); + } + } CmdBlockOutCb(this); + + cServer* Server = cRoot::Get()->GetServer(); - LOGD("Command: %s", m_Command.c_str()); + Server->ExecuteConsoleCommand(m_Command, CmdBlockOutCb); - m_LastOutput = ""; + // TODO 2014-01-18 xdot: Update the signal strength. m_Result = 0; } -- cgit v1.2.3 From 02c9aa2b1e86974df4ef8ea152465719169bdac5 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 18 Jan 2014 19:58:46 +0200 Subject: Parse the MC|AdvCdm plugin message --- src/BlockEntities/CommandBlockEntity.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp index e70e259aa..c29e3dff8 100644 --- a/src/BlockEntities/CommandBlockEntity.cpp +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -212,6 +212,8 @@ void cCommandBlockEntity::Execute() } } CmdBlockOutCb(this); + LOGD("cCommandBlockEntity: Executing command %s", m_Command.c_str()); + cServer* Server = cRoot::Get()->GetServer(); Server->ExecuteConsoleCommand(m_Command, CmdBlockOutCb); -- cgit v1.2.3 From a037172465932de7d0d7d725c3c8ec5b2fa44029 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 18 Jan 2014 21:27:54 +0200 Subject: Command block fixes --- src/BlockEntities/CommandBlockEntity.cpp | 36 +++----------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp index c29e3dff8..543d47b7a 100644 --- a/src/BlockEntities/CommandBlockEntity.cpp +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -20,24 +20,7 @@ cCommandBlockEntity::cCommandBlockEntity(int a_X, int a_Y, int a_Z, cWorld * a_W super(E_BLOCK_COMMAND_BLOCK, a_X, a_Y, a_Z, a_World), m_ShouldExecute(false), m_IsPowered(false) -{ - SetBlockEntity(this); // cBlockEntityWindowOwner -} - - - - - - -cCommandBlockEntity::~cCommandBlockEntity() -{ - // Tell window its owner is destroyed - cWindow * Window = GetWindow(); - if (Window != NULL) - { - Window->OwnerDestroyed(); - } -} +{} @@ -46,21 +29,8 @@ cCommandBlockEntity::~cCommandBlockEntity() void cCommandBlockEntity::UsedBy(cPlayer * a_Player) { - cWindow * Window = GetWindow(); - if (Window == NULL) - { - // TODO 2014-01-18 xdot: Open the appropriate window. - // OpenWindow(new cCommandBlockWindow(m_PosX, m_PosY, m_PosZ, this)); - Window = GetWindow(); - } - - if (Window != NULL) - { - if (a_Player->GetWindow() != Window) - { - a_Player->OpenWindow(Window); - } - } + // Nothing to do + UNUSED(a_Player); } -- cgit v1.2.3 From 7153bc0578033c583e6ab6ddfc9d184388b90e22 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Sat, 18 Jan 2014 23:42:48 +0100 Subject: Fixed compiler error. --- src/BlockEntities/CommandBlockEntity.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/BlockEntities/CommandBlockEntity.cpp') diff --git a/src/BlockEntities/CommandBlockEntity.cpp b/src/BlockEntities/CommandBlockEntity.cpp index 543d47b7a..ca617b04d 100644 --- a/src/BlockEntities/CommandBlockEntity.cpp +++ b/src/BlockEntities/CommandBlockEntity.cpp @@ -8,9 +8,9 @@ #include "CommandBlockEntity.h" #include "../Entities/Player.h" -#include "CommandOutput.h" -#include "Root.h" -#include "Server.h" // ExecuteConsoleCommand() +#include "../CommandOutput.h" +#include "../Root.h" +#include "../Server.h" // ExecuteConsoleCommand() -- cgit v1.2.3