From 74645fdfaa892e460c1b5444fd35eca11343e570 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 16 Jan 2014 19:00:49 +0000 Subject: Removed obsoleted functions --- src/BlockEntities/FurnaceEntity.cpp | 4 ++-- src/BlockEntities/HopperEntity.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/FurnaceEntity.cpp b/src/BlockEntities/FurnaceEntity.cpp index f15553968..c6643bcff 100644 --- a/src/BlockEntities/FurnaceEntity.cpp +++ b/src/BlockEntities/FurnaceEntity.cpp @@ -307,7 +307,7 @@ void cFurnaceEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) /// Updates the current recipe, based on the current input void cFurnaceEntity::UpdateInput(void) { - if (!m_Contents.GetSlot(fsInput).IsStackableWith(m_LastInput)) + if (!m_Contents.GetSlot(fsInput).IsEqual(m_LastInput)) { // The input is different from what we had before, reset the cooking time m_TimeCooked = 0; @@ -417,7 +417,7 @@ bool cFurnaceEntity::CanCookInputToOutput(void) const return true; } - if (!m_Contents.GetSlot(fsOutput).IsStackableWith(*m_CurrentRecipe->Out)) + if (!m_Contents.GetSlot(fsOutput).IsEqual(*m_CurrentRecipe->Out)) { // The output slot is blocked with something that cannot be stacked with the recipe's output return false; diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index eac59e74d..2255cad64 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -407,7 +407,7 @@ bool cHopperEntity::MoveItemsFromSlot(cBlockEntityWithItems & a_Entity, int a_Sl m_Contents.SetSlot(i, One); return true; } - else if (m_Contents.GetSlot(i).IsStackableWith(One)) + else if (m_Contents.GetSlot(i).IsEqual(One)) { if (cPluginManager::Get()->CallHookHopperPullingItem(*m_World, *this, i, a_Entity, a_SlotNum)) { @@ -544,7 +544,7 @@ bool cHopperEntity::MoveItemsToSlot(cBlockEntityWithItems & a_Entity, int a_DstS } for (int i = 0; i < ContentsWidth * ContentsHeight; i++) { - if (m_Contents.GetSlot(i).IsStackableWith(DestSlot)) + if (m_Contents.GetSlot(i).IsEqual(DestSlot)) { if (cPluginManager::Get()->CallHookHopperPushingItem(*m_World, *this, i, a_Entity, a_DstSlotNum)) { -- cgit v1.2.3 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/BlockEntity.cpp | 24 +++-- src/BlockEntities/CommandBlockEntity.cpp | 173 +++++++++++++++++++++++++++++++ src/BlockEntities/CommandBlockEntity.h | 85 +++++++++++++++ 3 files changed, 271 insertions(+), 11 deletions(-) create mode 100644 src/BlockEntities/CommandBlockEntity.cpp create mode 100644 src/BlockEntities/CommandBlockEntity.h (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BlockEntity.cpp b/src/BlockEntities/BlockEntity.cpp index 5d7e4a37a..97b5c1a66 100644 --- a/src/BlockEntities/BlockEntity.cpp +++ b/src/BlockEntities/BlockEntity.cpp @@ -6,6 +6,7 @@ #include "Globals.h" #include "BlockEntity.h" #include "ChestEntity.h" +#include "CommandBlockEntity.h" #include "DispenserEntity.h" #include "DropperEntity.h" #include "EnderChestEntity.h" @@ -23,17 +24,18 @@ cBlockEntity * cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE { 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_ENDER_CHEST: return new cEnderChestEntity (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); + case E_BLOCK_CHEST: return new cChestEntity (a_BlockX, a_BlockY, a_BlockZ, a_World); + case E_BLOCK_COMMAND_BLOCK: return new cCommandBlockEntity(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_ENDER_CHEST: return new cEnderChestEntity (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() 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 = ""; +} + + + + diff --git a/src/BlockEntities/CommandBlockEntity.h b/src/BlockEntities/CommandBlockEntity.h new file mode 100644 index 000000000..266a3c33d --- /dev/null +++ b/src/BlockEntities/CommandBlockEntity.h @@ -0,0 +1,85 @@ + +// CommandBlockEntity.h + +// Declares the cCommandBlockEntity class representing a single command block in the world + + + + + +#pragma once + +#include "BlockEntityWithItems.h" +#include "../UI/WindowOwner.h" + + + + + +namespace Json +{ + class Value; +} + + + + + +// tolua_begin + +class cCommandBlockEntity : + public cBlockEntity, + public cBlockEntityWindowOwner +{ + typedef cBlockEntity super; + +public: + + // tolua_end + + /// Creates a new empty command block entity + cCommandBlockEntity(int a_X, int a_Y, int a_Z, cWorld * a_World); + virtual ~cCommandBlockEntity(); + + bool LoadFromJson( const Json::Value& a_Value ); + virtual void SaveToJson(Json::Value& a_Value ) override; + + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void SendTo(cClientHandle & a_Client) override; + virtual void UsedBy(cPlayer * a_Player) override; + + // tolua_begin + + /// Sets the internal redstone power flag to "on" or "off", depending on the parameter. Calls Activate() if appropriate + void SetRedstonePower(bool a_IsPowered); + + /// Sets the command block to execute a command in the next tick + void Activate(void); + + /// Sets the command + void SetCommand(const AString & a_Cmd); + + /// Retrieves stored command + const AString & GetCommand(void) const; + + /// Retrieves the last line of output generated by the command block + const AString & GetLastOutput(void) const; + + // tolua_end + +private: + + /// Executes the associated command + void Execute(); + + bool m_ShouldExecute; + bool m_IsPowered; + + AString m_Command; + + AString m_LastOutput; +} ; // tolua_export + + + + -- 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 +++++++++++++++++++++++++++++++- src/BlockEntities/CommandBlockEntity.h | 13 +++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) (limited to 'src/BlockEntities') 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; } diff --git a/src/BlockEntities/CommandBlockEntity.h b/src/BlockEntities/CommandBlockEntity.h index 266a3c33d..c4529a1e6 100644 --- a/src/BlockEntities/CommandBlockEntity.h +++ b/src/BlockEntities/CommandBlockEntity.h @@ -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 @@ -64,6 +68,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 @@ -75,9 +82,11 @@ private: bool m_ShouldExecute; bool m_IsPowered; - AString m_Command; + AString m_Command; + + AString m_LastOutput; - AString m_LastOutput; + NIBBLETYPE m_Result; } ; // tolua_export -- 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') 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') 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 +++----------------------------- src/BlockEntities/CommandBlockEntity.h | 7 ++----- 2 files changed, 5 insertions(+), 38 deletions(-) (limited to 'src/BlockEntities') 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); } diff --git a/src/BlockEntities/CommandBlockEntity.h b/src/BlockEntities/CommandBlockEntity.h index c4529a1e6..12157670f 100644 --- a/src/BlockEntities/CommandBlockEntity.h +++ b/src/BlockEntities/CommandBlockEntity.h @@ -9,8 +9,7 @@ #pragma once -#include "BlockEntityWithItems.h" -#include "../UI/WindowOwner.h" +#include "BlockEntity.h" @@ -28,8 +27,7 @@ namespace Json // tolua_begin class cCommandBlockEntity : - public cBlockEntity, - public cBlockEntityWindowOwner + public cBlockEntity { typedef cBlockEntity super; @@ -39,7 +37,6 @@ public: /// Creates a new empty command block entity cCommandBlockEntity(int a_X, int a_Y, int a_Z, cWorld * a_World); - virtual ~cCommandBlockEntity(); bool LoadFromJson( const Json::Value& a_Value ); virtual void SaveToJson(Json::Value& a_Value ) override; -- 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') 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