Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockTypeRegistry.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 
6 
7 #include <map>
8 #include <functional>
9 
10 
11 
12 
13 
14 // fwd:
15 class cBlockHandler;
16 class BlockState;
17 
18 
19 
20 
21 
24 class BlockInfo
25 {
26 public:
27 
30  using HintCallback = std::function<AString(const AString & aTypeName, const BlockState & aBlockState)>;
31 
32 
35  BlockInfo(
36  const AString & aPluginName,
37  const AString & aBlockTypeName,
38  std::shared_ptr<cBlockHandler> aHandler,
39  const std::map<AString, AString> & aHints = std::map<AString, AString>(),
40  const std::map<AString, HintCallback> & aHintCallbacks = std::map<AString, HintCallback>()
41  );
42 
43 
48  const AString & aHintName,
49  const BlockState & aBlockState
50  );
51 
52  // Simple getters:
53  const AString & pluginName() const { return m_PluginName; }
54  const AString & blockTypeName() const { return m_BlockTypeName; }
55  std::shared_ptr<cBlockHandler> handler() const { return m_Handler; }
56 
60  void setHint(const AString & aHintKey, const AString & aHintValue);
61 
64  void removeHint(const AString & aHintKey);
65 
66 
67 private:
68 
71 
74 
76  std::shared_ptr<cBlockHandler> m_Handler;
77 
80  std::map<AString, AString> m_Hints;
81 
84  std::map<AString, HintCallback> m_HintCallbacks;
85 };
86 
87 
88 
89 
90 
98 {
99 public:
100  // fwd:
103 
104 
106  BlockTypeRegistry() = default;
107 
111  void registerBlockType(
112  const AString & aPluginName,
113  const AString & aBlockTypeName,
114  std::shared_ptr<cBlockHandler> aHandler,
115  const std::map<AString, AString> & aHints = std::map<AString, AString>(),
116  const std::map<AString, BlockInfo::HintCallback> & aHintCallbacks = std::map<AString, BlockInfo::HintCallback>()
117  );
118 
121  std::shared_ptr<BlockInfo> blockInfo(const AString & aBlockTypeName);
122 
124  void removeAllByPlugin(const AString & aPluginName);
125 
128  void setBlockTypeHint(
129  const AString & aBlockTypeName,
130  const AString & aHintKey,
131  const AString & aHintValue
132  );
133 
137  void removeBlockTypeHint(
138  const AString & aBlockTypeName,
139  const AString & aHintKey
140  );
141 
142 
143 private:
144 
147  std::map<AString, std::shared_ptr<BlockInfo>> m_Registry;
148 
151 };
152 
153 
154 
155 
156 
158 class BlockTypeRegistry::AlreadyRegisteredException: public std::runtime_error
159 {
160  using Super = std::runtime_error;
161 
162 public:
163 
167  const std::shared_ptr<BlockInfo> & aPreviousRegistration,
168  const std::shared_ptr<BlockInfo> & aNewRegistration
169  );
170 
171  // Simple getters:
172  std::shared_ptr<BlockInfo> previousRegistration() const { return m_PreviousRegistration; }
173  std::shared_ptr<BlockInfo> newRegistration() const { return m_NewRegistration; }
174 
175 
176 private:
177 
178  std::shared_ptr<BlockInfo> m_PreviousRegistration;
179  std::shared_ptr<BlockInfo> m_NewRegistration;
180 
181 
184  static AString message(
185  const std::shared_ptr<BlockInfo> & aPreviousRegistration,
186  const std::shared_ptr<BlockInfo> & aNewRegistration
187  );
188 };
189 
190 
191 
192 
193 
195 class BlockTypeRegistry::NotRegisteredException: public std::runtime_error
196 {
197  using Super = std::runtime_error;
198 
199 public:
200 
204  const AString & aBlockTypeName,
205  const AString & aHintKey,
206  const AString & aHintValue
207  );
208 
209  // Simple getters:
210  const AString & blockTypeName() const { return m_BlockTypeName; }
211 
212 
213 private:
214 
216 };
std::string AString
Definition: StringUtils.h:11
Represents the state of a single block (previously known as "block meta").
Definition: BlockState.h:20
Complete information about a single block type.
AString hintValue(const AString &aHintName, const BlockState &aBlockState)
Retrieves the value associated with the specified hint for this specific BlockTypeName and BlockState...
std::function< AString(const AString &aTypeName, const BlockState &aBlockState)> HintCallback
Callback is used to query block hints dynamically, based on the current BlockState.
const AString & blockTypeName() const
std::shared_ptr< cBlockHandler > handler() const
BlockInfo(const AString &aPluginName, const AString &aBlockTypeName, std::shared_ptr< cBlockHandler > aHandler, const std::map< AString, AString > &aHints=std::map< AString, AString >(), const std::map< AString, HintCallback > &aHintCallbacks=std::map< AString, HintCallback >())
Creates a new instance with the specified BlockTypeName and handler / hints / callbacks.
std::shared_ptr< cBlockHandler > m_Handler
The callbacks to call for various interaction.
void setHint(const AString &aHintKey, const AString &aHintValue)
Sets (creates or updates) a static hint.
AString m_PluginName
The name of the plugin that registered the block.
std::map< AString, AString > m_Hints
Optional static hints for any subsystem to use, such as "IsSnowable" -> "1".
std::map< AString, HintCallback > m_HintCallbacks
The callbacks for dynamic evaluation of hints, such as "LightValue" -> function(BlockTypeName,...
AString m_BlockTypeName
The name of the block type, such as "minecraft:redstone_lamp".
const AString & pluginName() const
void removeHint(const AString &aHintKey)
Removes a hint.
Stores information on all known block types.
void setBlockTypeHint(const AString &aBlockTypeName, const AString &aHintKey, const AString &aHintValue)
Sets (adds or overwrites) a single Hint value for a BlockType.
BlockTypeRegistry()=default
Creates an empty new instance of the block type registry.
cCriticalSection m_CSRegistry
The CS that protects m_Registry against multithreaded access.
void removeAllByPlugin(const AString &aPluginName)
Removes all registrations done by the specified plugin.
std::map< AString, std::shared_ptr< BlockInfo > > m_Registry
The actual block type registry.
std::shared_ptr< BlockInfo > blockInfo(const AString &aBlockTypeName)
Returns the registration information for the specified BlockTypeName.
void registerBlockType(const AString &aPluginName, const AString &aBlockTypeName, std::shared_ptr< cBlockHandler > aHandler, const std::map< AString, AString > &aHints=std::map< AString, AString >(), const std::map< AString, BlockInfo::HintCallback > &aHintCallbacks=std::map< AString, BlockInfo::HintCallback >())
Registers the specified block type.
void removeBlockTypeHint(const AString &aBlockTypeName, const AString &aHintKey)
Removes a previously registered single Hint value for a BlockType.
The exception thrown from BlockTypeRegistry::registerBlockType() if the same block type is being regi...
AlreadyRegisteredException(const std::shared_ptr< BlockInfo > &aPreviousRegistration, const std::shared_ptr< BlockInfo > &aNewRegistration)
Creates a new instance of the exception that provides info on both the original registration and the ...
std::shared_ptr< BlockInfo > previousRegistration() const
std::shared_ptr< BlockInfo > newRegistration() const
std::shared_ptr< BlockInfo > m_NewRegistration
static AString message(const std::shared_ptr< BlockInfo > &aPreviousRegistration, const std::shared_ptr< BlockInfo > &aNewRegistration)
Returns the general exception message formatted by the two registrations.
std::shared_ptr< BlockInfo > m_PreviousRegistration
The exception thrown from BlockTypeRegistry::setBlockTypeHint() if the block type has not been regist...
NotRegisteredException(const AString &aBlockTypeName, const AString &aHintKey, const AString &aHintValue)
Creates a new instance of the exception that provides info on both the original registration and the ...