Skip to content

Commit 8aa8f50

Browse files
authored
refactor: replace Dimension::Type enum with DimensionId identifier (#371)
* refactor: replace Dimension::Type enum with DimensionId identifier Replace the fixed Dimension::Type enum (Overworld/Nether/TheEnd/Custom) with DimensionId (Identifier<Dimension>) to support custom dimensions with namespaced identifiers. Dimension no longer inherits from Registry — dimensions are accessed via Level::getDimension(DimensionId) instead of a global registry lookup. * fix: update type annotations for Dimension constants in __init__.pyi * test: add dimension tests and update existing tests for DimensionId Add test_dimension.py covering DimensionId constants, lookup, properties, and level back-reference. Update test_server.py and test_object.py to use Dimension.OVERWORLD/NETHER/THE_END instead of raw name strings. * refactor: remove Dimension::getName() in favor of getId() With DimensionId available via getId(), the separate getName() is redundant. All usages now use getId() which returns the namespaced identifier.
1 parent 666078f commit 8aa8f50

18 files changed

Lines changed: 167 additions & 175 deletions

File tree

endstone/level/__init__.pyi

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import enum
21
import typing
32

43
from endstone.actor import Actor, Item
@@ -35,9 +34,9 @@ class Level:
3534
Gets a list of all dimensions within this level.
3635
"""
3736
...
38-
def get_dimension(self, name: str) -> Dimension:
37+
def get_dimension(self, id: str) -> Dimension:
3938
"""
40-
Gets the dimension with the given name.
39+
Gets the dimension with the given id.
4140
"""
4241
...
4342
@property
@@ -51,30 +50,20 @@ class Dimension:
5150
"""
5251
Represents a dimension within a Level.
5352
"""
54-
class Type(enum.Enum):
55-
"""
56-
Represents various dimension types.
57-
"""
5853

59-
OVERWORLD = 0
60-
NETHER = 1
61-
THE_END = 2
62-
CUSTOM = 999
63-
64-
OVERWORLD = Type.OVERWORLD
65-
NETHER = Type.NETHER
66-
THE_END = Type.THE_END
67-
CUSTOM = Type.CUSTOM
54+
OVERWORLD = "minecraft:overworld"
55+
NETHER = "minecraft:nether"
56+
THE_END = "minecraft:the_end"
6857
@property
69-
def name(self) -> str:
58+
def id(self) -> str:
7059
"""
71-
Gets the name of this dimension
60+
Gets the identifier of this dimension
7261
"""
7362
...
7463
@property
75-
def type(self) -> Type:
64+
def translation_key(self) -> str:
7665
"""
77-
Gets the type of this dimension
66+
Gets the translation key for this dimension
7867
"""
7968
...
8069
@property

include/endstone/level/dimension.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,39 @@
2121
#include "endstone/actor/actor.h"
2222
#include "endstone/actor/item.h"
2323
#include "endstone/block/block.h"
24+
#include "endstone/identifier.h"
2425
#include "endstone/inventory/item_stack.h"
2526
#include "endstone/level/chunk.h"
2627

2728
namespace endstone {
2829

30+
class Dimension;
31+
using DimensionId = Identifier<Dimension>;
32+
2933
/**
3034
* @brief Represents a dimension within a Level.
3135
*/
3236
class Dimension {
3337
public:
34-
/**
35-
* @brief Represents various dimension types.
36-
*/
37-
enum class Type {
38-
Overworld = 0,
39-
Nether = 1,
40-
TheEnd = 2,
41-
Custom = 999
42-
};
38+
static constexpr auto Overworld = DimensionId::minecraft("overworld");
39+
static constexpr auto Nether = DimensionId::minecraft("nether");
40+
static constexpr auto TheEnd = DimensionId::minecraft("the_end");
4341

4442
virtual ~Dimension() = default;
4543

4644
/**
47-
* @brief Gets the name of this dimension
45+
* @brief Return the identifier of this dimension.
4846
*
49-
* @return Name of this dimension
47+
* @return this dimension's identifier
5048
*/
51-
[[nodiscard]] virtual std::string getName() const = 0;
49+
[[nodiscard]] virtual DimensionId getId() const = 0;
5250

5351
/**
54-
* @brief Gets the type of this dimension
52+
* @brief Get the translation key, suitable for use in a translation component.
5553
*
56-
* @return Type of this dimension
54+
* @return the translation key
5755
*/
58-
[[nodiscard]] virtual Type getType() const = 0;
56+
[[nodiscard]] virtual std::string getTranslationKey() const = 0;
5957

6058
/**
6159
* @brief Gets the level to which this dimension belongs
@@ -150,7 +148,7 @@ inline std::unique_ptr<Block> Location::getBlock() const
150148
inline float Location::distanceSquared(const Location &other) const
151149
{
152150
Preconditions::checkArgument(dimension_ == other.dimension_, "Cannot measure distance between {} and {}.",
153-
dimension_->getName(), other.dimension_->getName());
151+
dimension_->getId(), other.dimension_->getId());
154152
return ((x_ - other.x_) * (x_ - other.x_)) + ((y_ - other.y_) * (y_ - other.y_)) +
155153
((z_ - other.z_) * (z_ - other.z_));
156154
}
@@ -161,6 +159,6 @@ struct fmt::formatter<endstone::Dimension> : formatter<string_view> {
161159
template <typename FormatContext>
162160
auto format(const endstone::Dimension &self, FormatContext &ctx) const -> format_context::iterator
163161
{
164-
return fmt::format_to(ctx.out(), "Dimension(name={})", self.getName());
162+
return fmt::format_to(ctx.out(), "Dimension(id={})", self.getId());
165163
}
166164
};

include/endstone/level/level.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "endstone/actor/actor.h"
23+
#include "endstone/level/dimension.h"
2324

2425
namespace endstone {
2526

@@ -66,13 +67,13 @@ class Level {
6667
[[nodiscard]] virtual std::vector<Dimension *> getDimensions() const = 0;
6768

6869
/**
69-
* @brief Gets the dimension with the given name.
70+
* @brief Gets the dimension with the given id.
7071
*
71-
* @param name the name of the dimension to retrieve. For example, "overworld", "nether" or "the_end".
72+
* @param id the id of the dimension to retrieve.
7273
*
73-
* @return The Dimension with the given name, or nullptr if none exists
74+
* @return The Dimension with the given id, or nullptr if none exists
7475
*/
75-
[[nodiscard]] virtual Dimension *getDimension(std::string name) const = 0;
76+
[[nodiscard]] virtual Dimension *getDimension(DimensionId id) const = 0;
7677

7778
/**
7879
* @brief Gets the Seed for this level.

src/bedrock/world/level/dimension/dimension.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ bool Dimension::isBrightOutside() const
1919
return isNaturalDimension() && sky_darken_.value < 4;
2020
}
2121

22+
std::string Dimension::getLocalizationKey() const
23+
{
24+
return fmt::format("dimension.dimensionName{}", id_.runtime_id);
25+
}
26+
2227
Level &Dimension::getLevel() const
2328
{
2429
return *level_;

src/endstone/core/actor/actor.h

Lines changed: 23 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,15 @@ class EndstoneActorBase : public Interface {
9292
return ActorPermissibleBase::get().removeAttachment(attachment);
9393
}
9494

95-
void recalculatePermissions() override
96-
{
97-
ActorPermissibleBase::get().recalculatePermissions();
98-
}
95+
void recalculatePermissions() override { ActorPermissibleBase::get().recalculatePermissions(); }
9996

10097
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override
10198
{
10299
return ActorPermissibleBase::get().getEffectivePermissions();
103100
}
104101

105102
// Object
106-
[[nodiscard]] const std::type_info &getClassTypeId() const override
107-
{
108-
return typeid(Interface);
109-
}
103+
[[nodiscard]] const std::type_info &getClassTypeId() const override { return typeid(Interface); }
110104

111105
[[nodiscard]] bool isInstanceOf(const std::type_info &target) const override
112106
{
@@ -118,15 +112,9 @@ class EndstoneActorBase : public Interface {
118112

119113
void sendErrorMessage(const Message &message) const override {}
120114

121-
[[nodiscard]] Server &getServer() const override
122-
{
123-
return server_;
124-
}
115+
[[nodiscard]] Server &getServer() const override { return server_; }
125116

126-
[[nodiscard]] std::string getName() const override
127-
{
128-
return CommandUtils::getActorName(getHandle());
129-
}
117+
[[nodiscard]] std::string getName() const override { return CommandUtils::getActorName(getHandle()); }
130118

131119
// Actor
132120
[[nodiscard]] const ActorType &getType() const override
@@ -135,10 +123,7 @@ class EndstoneActorBase : public Interface {
135123
return server_.getRegistry<ActorType>().getOrThrow(ActorTypeId{canonical});
136124
}
137125

138-
[[nodiscard]] std::uint64_t getRuntimeId() const override
139-
{
140-
return getHandle().getRuntimeID().raw_id;
141-
}
126+
[[nodiscard]] std::uint64_t getRuntimeId() const override { return getHandle().getRuntimeID().raw_id; }
142127

143128
[[nodiscard]] Location getLocation() const override
144129
{
@@ -164,20 +149,11 @@ class EndstoneActorBase : public Interface {
164149
return {delta.x, delta.y, delta.z};
165150
}
166151

167-
[[nodiscard]] bool isOnGround() const override
168-
{
169-
return getHandle().isOnGround();
170-
}
152+
[[nodiscard]] bool isOnGround() const override { return getHandle().isOnGround(); }
171153

172-
[[nodiscard]] bool isInWater() const override
173-
{
174-
return getHandle().isInWater();
175-
}
154+
[[nodiscard]] bool isInWater() const override { return getHandle().isInWater(); }
176155

177-
[[nodiscard]] bool isInLava() const override
178-
{
179-
return getHandle().isInLava();
180-
}
156+
[[nodiscard]] bool isInLava() const override { return getHandle().isInLava(); }
181157

182158
[[nodiscard]] Level &getLevel() const override
183159
{
@@ -188,13 +164,10 @@ class EndstoneActorBase : public Interface {
188164

189165
[[nodiscard]] Dimension &getDimension() const override
190166
{
191-
return *getLevel().getDimension(getHandle().getDimension().getName());
167+
return *server_.getEndstoneLevel()->getDimension(getHandle().getDimension().getDimensionId());
192168
}
193169

194-
void setRotation(float yaw, float pitch) override
195-
{
196-
getHandle().setRotationWrapped({pitch, yaw});
197-
}
170+
void setRotation(float yaw, float pitch) override { getHandle().setRotationWrapped({pitch, yaw}); }
198171

199172
bool teleport(const Location &location) override
200173
{
@@ -215,25 +188,13 @@ class EndstoneActorBase : public Interface {
215188
return true;
216189
}
217190

218-
bool teleport(const Actor &target) override
219-
{
220-
return teleport(target.getLocation());
221-
}
191+
bool teleport(const Actor &target) override { return teleport(target.getLocation()); }
222192

223-
[[nodiscard]] std::int64_t getId() const override
224-
{
225-
return getHandle().getOrCreateUniqueID().raw_id;
226-
}
193+
[[nodiscard]] std::int64_t getId() const override { return getHandle().getOrCreateUniqueID().raw_id; }
227194

228-
void remove() override
229-
{
230-
getHandle().remove();
231-
}
195+
void remove() override { getHandle().remove(); }
232196

233-
[[nodiscard]] bool isDead() const override
234-
{
235-
return !getHandle().isAlive();
236-
}
197+
[[nodiscard]] bool isDead() const override { return !getHandle().isAlive(); }
237198

238199
[[nodiscard]] bool isValid() const override
239200
{
@@ -244,30 +205,15 @@ class EndstoneActorBase : public Interface {
244205
return handle->isAlive();
245206
}
246207

247-
[[nodiscard]] std::vector<std::string> getScoreboardTags() const override
248-
{
249-
return getHandle().getTags();
250-
}
208+
[[nodiscard]] std::vector<std::string> getScoreboardTags() const override { return getHandle().getTags(); }
251209

252-
[[nodiscard]] bool addScoreboardTag(std::string tag) const override
253-
{
254-
return getHandle().addTag(tag);
255-
}
210+
[[nodiscard]] bool addScoreboardTag(std::string tag) const override { return getHandle().addTag(tag); }
256211

257-
[[nodiscard]] bool removeScoreboardTag(std::string tag) const override
258-
{
259-
return getHandle().removeTag(tag);
260-
}
212+
[[nodiscard]] bool removeScoreboardTag(std::string tag) const override { return getHandle().removeTag(tag); }
261213

262-
[[nodiscard]] bool isNameTagVisible() const override
263-
{
264-
return getHandle().canShowNameTag();
265-
}
214+
[[nodiscard]] bool isNameTagVisible() const override { return getHandle().canShowNameTag(); }
266215

267-
void setNameTagVisible(bool visible) override
268-
{
269-
getHandle().setNameTagVisible(visible);
270-
}
216+
void setNameTagVisible(bool visible) override { getHandle().setNameTagVisible(visible); }
271217

272218
[[nodiscard]] bool isNameTagAlwaysVisible() const override
273219
{
@@ -281,25 +227,13 @@ class EndstoneActorBase : public Interface {
281227
static_cast<SynchedActorData::ID>(ActorDataIDs::NAMETAG_ALWAYS_SHOW), visible);
282228
}
283229

284-
[[nodiscard]] std::string getNameTag() const override
285-
{
286-
return getHandle().getNameTag();
287-
}
230+
[[nodiscard]] std::string getNameTag() const override { return getHandle().getNameTag(); }
288231

289-
void setNameTag(std::string name) override
290-
{
291-
getHandle().setNameTag(name);
292-
}
232+
void setNameTag(std::string name) override { getHandle().setNameTag(name); }
293233

294-
[[nodiscard]] std::string getScoreTag() const override
295-
{
296-
return getHandle().getScoreTag();
297-
}
234+
[[nodiscard]] std::string getScoreTag() const override { return getHandle().getScoreTag(); }
298235

299-
void setScoreTag(std::string score) override
300-
{
301-
getHandle().setScoreTag(score);
302-
}
236+
void setScoreTag(std::string score) override { getHandle().setScoreTag(score); }
303237

304238
Handle &getHandle() const
305239
{

src/endstone/core/command/defaults/status_command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool StatusCommand::execute(CommandSender &sender, const std::vector<std::string
102102
}
103103
}
104104
sender.sendMessage("- {}Dimension \"{}\": {}{}{} loaded chunks, {}{}{} entities", //
105-
ColorFormat::Gold, dimension->getName(), //
105+
ColorFormat::Gold, dimension->getId(), //
106106
ColorFormat::Red, dimension->getLoadedChunks().size(), ColorFormat::Green, //
107107
ColorFormat::Red, actor_count, ColorFormat::Green);
108108
}

0 commit comments

Comments
 (0)