Cuberite
A lightweight, fast and extensible game server for Minecraft
CommandBlockEntity.cpp
Go to the documentation of this file.
1 
2 // CommandBlockEntity.cpp
3 
4 // Implements the cCommandBlockEntity class representing a single command block in the world
5 
6 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
7 #include "CommandBlockEntity.h"
8 
9 #include "../CommandOutput.h"
10 #include "../Root.h"
11 #include "../Server.h" // ExecuteConsoleCommand()
12 #include "../ChatColor.h"
13 #include "../World.h"
14 #include "../ClientHandle.h"
15 
16 
17 
18 
19 
20 cCommandBlockEntity::cCommandBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
21  Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
22  m_ShouldExecute(false),
23  m_Result(0)
24 {
25  ASSERT(a_BlockType == E_BLOCK_COMMAND_BLOCK);
26 }
27 
28 
29 
30 
31 
33 {
34  // Nothing to do
35  UNUSED(a_Player);
36  return true;
37 }
38 
39 
40 
41 
42 
44 {
45  m_Command = a_Cmd;
46 }
47 
48 
49 
50 
51 
53 {
54  m_LastOutput = a_LastOut;
55 }
56 
57 
58 
59 
60 
62 {
63  m_Result = a_Result;
64 }
65 
66 
67 
68 
69 
71 {
72  return m_Command;
73 }
74 
75 
76 
77 
78 
80 {
81  return m_LastOutput;
82 }
83 
84 
85 
86 
87 
89 {
90  return m_Result;
91 }
92 
93 
94 
95 
96 
98 {
99  m_ShouldExecute = true;
100 }
101 
102 
103 
104 
105 
107 {
108  Super::CopyFrom(a_Src);
109  auto & src = static_cast<const cCommandBlockEntity &>(a_Src);
110  m_Command = src.m_Command;
111  m_LastOutput = src.m_LastOutput;
112  m_Result = src.m_Result;
113  m_ShouldExecute = src.m_ShouldExecute;
114 }
115 
116 
117 
118 
119 
120 bool cCommandBlockEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
121 {
122  UNUSED(a_Dt);
123  UNUSED(a_Chunk);
124  if (!m_ShouldExecute)
125  {
126  return false;
127  }
128 
129  m_ShouldExecute = false;
130  Execute();
131  return true;
132 }
133 
134 
135 
136 
137 
139 {
140  a_Client.SendUpdateBlockEntity(*this);
141 }
142 
143 
144 
145 
146 
148 {
149  ASSERT(m_World != nullptr); // Execute should not be called before the command block is attached to a world
150 
152  {
153  return;
154  }
155 
156  if (m_Command.empty())
157  {
158  return;
159  }
160 
161  class CommandBlockOutCb :
163  {
164  cCommandBlockEntity * m_CmdBlock;
165 
166  public:
167  CommandBlockOutCb(cCommandBlockEntity * a_CmdBlock) : m_CmdBlock(a_CmdBlock) {}
168 
169  virtual void Out(const AString & a_Text) override
170  {
171  // Overwrite field
173  }
174  };
175 
176  AString RealCommand = m_Command;
177 
178  // Remove leading slash if it exists, since console commands don't use them
179  if (RealCommand[0] == '/')
180  {
181  RealCommand = RealCommand.substr(1, RealCommand.length());
182  }
183 
184  // Administrator commands are not executable by command blocks:
185  if (
186  (RealCommand != "stop") &&
187  (RealCommand != "restart") &&
188  (RealCommand != "kick") &&
189  (RealCommand != "ban") &&
190  (RealCommand != "ipban")
191  )
192  {
193  cServer * Server = cRoot::Get()->GetServer();
194  LOGD("cCommandBlockEntity: Executing command %s", m_Command.c_str());
195  Server->QueueExecuteConsoleCommand(RealCommand, *new CommandBlockOutCb(this));
196  }
197  else
198  {
199  SetLastOutput(cClientHandle::FormatChatPrefix(GetWorld()->ShouldUseChatPrefixes(), "FAILURE", cChatColor::Rose, cChatColor::White) + "Adminstration commands can not be executed");
200  LOGD("cCommandBlockEntity: Prevented execution of administration command %s", m_Command.c_str());
201  }
202 
203  // TODO 2014-01-18 xdot: Update the signal strength.
204  m_Result = 0;
205 }
206 
207 
208 
209 
@ E_BLOCK_COMMAND_BLOCK
Definition: BlockType.h:152
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
#define LOGD
Definition: LoggerSimple.h:83
std::string AString
Definition: StringUtils.h:11
cWorld * GetWorld() const
Definition: BlockEntity.h:99
virtual void CopyFrom(const cBlockEntity &a_Src)
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: BlockEntity.cpp:66
cWorld * m_World
Definition: BlockEntity.h:126
cCommandBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Creates a new empty command block entity.
const AString & GetLastOutput(void) const
Retrieves the last line of output generated by the command block.
void Activate(void)
Sets the command block to execute a command in the next tick.
void Execute()
Executes the associated command.
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
virtual void CopyFrom(const cBlockEntity &a_Src) override
Copies all properties of a_Src into this entity, except for its m_World and location.
virtual bool Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Ticks the entity; returns true if the chunk should be marked as dirty as a result of this ticking.
NIBBLETYPE GetResult(void) const
Retrieves the result (signal strength) of the last operation.
void SetLastOutput(const AString &a_LastOut)
void SetResult(const NIBBLETYPE a_Result)
void SetCommand(const AString &a_Cmd)
Sets the command.
const AString & GetCommand(void) const
Retrieves stored command.
virtual void SendTo(cClientHandle &a_Client) override
Sends the packet defining the block entity to the client specified.
static const char * White
Definition: ChatColor.h:32
static const char * Rose
Definition: ChatColor.h:29
static const char * Green
Definition: ChatColor.h:19
Definition: Chunk.h:36
static AString FormatChatPrefix(bool ShouldAppendChatPrefixes, const AString &a_ChatPrefixS, const AString &m_Color1, const AString &m_Color2)
void SendUpdateBlockEntity(cBlockEntity &a_BlockEntity)
Sends all command output to a log, line by line; deletes self when command finishes processing.
Definition: CommandOutput.h:94
Definition: Player.h:29
cServer * GetServer(void)
Definition: Root.h:71
static cRoot * Get()
Definition: Root.h:52
Definition: Server.h:56
void QueueExecuteConsoleCommand(const AString &a_Cmd, cCommandOutputCallback &a_Output)
Queues a console command for execution through the cServer class.
Definition: Server.cpp:445
Definition: World.h:53
bool ShouldUseChatPrefixes(void) const
Definition: World.h:714
bool AreCommandBlocksEnabled(void) const
Definition: World.h:705