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 BlockState;
16 
17 
18 
19 
20 
23 class BlockInfo
24 {
25 public:
26 
29  using HintCallback = std::function<AString(const AString & aTypeName, const BlockState & aBlockState)>;
30 
31 
34  BlockInfo(
35  const AString & aPluginName,
36  const AString & aBlockTypeName,
37  std::shared_ptr<cBlockHandler> aHandler,
38  const std::map<AString, AString> & aHints = std::map<AString, AString>(),
39  const std::map<AString, HintCallback> & aHintCallbacks = std::map<AString, HintCallback>()
40  );
41 
42 
47  const AString & aHintName,
48  const BlockState & aBlockState
49  );
50 
51  // Simple getters:
52  const AString & pluginName() const { return mPluginName; }
53  const AString & blockTypeName() const { return mBlockTypeName; }
54  std::shared_ptr<cBlockHandler> handler() const { return mHandler; }
55 
59  void setHint(const AString & aHintKey, const AString & aHintValue);
60 
63  void removeHint(const AString & aHintKey);
64 
65 
66 private:
67 
70 
73 
75  std::shared_ptr<cBlockHandler> mHandler;
76 
79  std::map<AString, AString> mHints;
80 
83  std::map<AString, HintCallback> mHintCallbacks;
84 };
85 
86 
87 
88 
89 
97 {
98 public:
99  // fwd:
102 
103 
105  BlockTypeRegistry() = default;
106 
110  void registerBlockType(
111  const AString & aPluginName,
112  const AString & aBlockTypeName,
113  std::shared_ptr<cBlockHandler> aHandler,
114  const std::map<AString, AString> & aHints = std::map<AString, AString>(),
115  const std::map<AString, BlockInfo::HintCallback> & aHintCallbacks = std::map<AString, BlockInfo::HintCallback>()
116  );
117 
120  std::shared_ptr<BlockInfo> blockInfo(const AString & aBlockTypeName);
121 
123  void removeAllByPlugin(const AString & aPluginName);
124 
127  void setBlockTypeHint(
128  const AString & aBlockTypeName,
129  const AString & aHintKey,
130  const AString & aHintValue
131  );
132 
136  void removeBlockTypeHint(
137  const AString & aBlockTypeName,
138  const AString & aHintKey
139  );
140 
141 
142 private:
143 
146  std::map<AString, std::shared_ptr<BlockInfo>> mRegistry;
147 
150 };
151 
152 
153 
154 
155 
157 class BlockTypeRegistry::AlreadyRegisteredException: public std::runtime_error
158 {
159  using Super = std::runtime_error;
160 
161 public:
162 
166  std::shared_ptr<BlockInfo> aPreviousRegistration,
167  std::shared_ptr<BlockInfo> aNewRegistration
168  );
169 
170  // Simple getters:
171  std::shared_ptr<BlockInfo> previousRegistration() const { return mPreviousRegistration; }
172  std::shared_ptr<BlockInfo> newRegistration() const { return mNewRegistration; }
173 
174 
175 private:
176 
177  std::shared_ptr<BlockInfo> mPreviousRegistration;
178  std::shared_ptr<BlockInfo> mNewRegistration;
179 
180 
183  static AString message(
184  std::shared_ptr<BlockInfo> aPreviousRegistration,
185  std::shared_ptr<BlockInfo> aNewRegistration
186  );
187 };
188 
189 
190 
191 
192 
194 class BlockTypeRegistry::NotRegisteredException: public std::runtime_error
195 {
196  using Super = std::runtime_error;
197 
198 public:
199 
203  const AString & aBlockTypeName,
204  const AString & aHintKey,
205  const AString & aHintValue
206  );
207 
208  // Simple getters:
209  const AString & blockTypeName() const { return mBlockTypeName; }
210 
211 
212 private:
213 
215 };
The exception thrown from BlockTypeRegistry::setBlockTypeHint() if the block type has not been regist...
Stores information on all known block types.
std::shared_ptr< cBlockHandler > handler() const
void setHint(const AString &aHintKey, const AString &aHintValue)
Sets (creates or updates) a static hint.
AString mPluginName
The name of the plugin that registered the block.
std::map< AString, std::shared_ptr< BlockInfo > > mRegistry
The actual block type registry.
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::map< AString, AString > mHints
Optional static hints for any subsystem to use, such as "IsSnowable" -> "1".
AString hintValue(const AString &aHintName, const BlockState &aBlockState)
Retrieves the value associated with the specified hint for this specific BlockTypeName and BlockState...
void removeHint(const AString &aHintKey)
Removes a hint.
cCriticalSection mCSRegistry
The CS that protects mRegistry against multithreaded access.
Complete information about a single block type.
std::string AString
Definition: StringUtils.h:13
const AString & blockTypeName() const
AString mBlockTypeName
The name of the block type, such as "minecraft:redstone_lamp".
const AString & pluginName() const
std::function< AString(const AString &aTypeName, const BlockState &aBlockState)> HintCallback
Callback is used to query block hints dynamically, based on the current BlockState.
std::shared_ptr< BlockInfo > newRegistration() const
std::shared_ptr< BlockInfo > previousRegistration() const
std::shared_ptr< BlockInfo > mPreviousRegistration
The exception thrown from BlockTypeRegistry::registerBlockType() if the same block type is being regi...
std::map< AString, HintCallback > mHintCallbacks
The callbacks for dynamic evaluation of hints, such as "LightValue" -> function(BlockTypeName, BlockState).
std::shared_ptr< cBlockHandler > mHandler
The callbacks to call for various interaction.
Represents the state of a single block (previously known as "block meta").
Definition: BlockState.h:19