Skip to content

Commit b8e76ea

Browse files
committed
Improve api structure
1 parent 9bd45e3 commit b8e76ea

File tree

16 files changed

+70
-32
lines changed

16 files changed

+70
-32
lines changed

demo/src/main/java/net/minestom/demo/PlayerInit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class A {
167167
player.getInventory().addItemStack(bundle);
168168

169169
player.getInventory().addItemStack(ItemStack.builder(Material.COMPASS)
170-
.set(ItemComponent.LODESTONE_TRACKER, new LodestoneTracker(player.getInstance().getDimensionType().value(), new Vec(10, 10, 10), true))
170+
.set(ItemComponent.LODESTONE_TRACKER, new LodestoneTracker(player.getInstance().getDimensionType(), new Vec(10, 10, 10), true))
171171
.build());
172172

173173
player.getInventory().addItemStack(ItemStack.builder(Material.STONE_SWORD)

src/main/java/net/minestom/server/entity/Player.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ public void setDeathLocation(@NotNull Pos position) {
12551255
setDeathLocation(getInstance().getDimensionName(), position);
12561256
}
12571257

1258-
public void setDeathLocation(@NotNull String dimension, @NotNull Pos position) {
1258+
public void setDeathLocation(@NotNull Key dimension, @NotNull Pos position) {
12591259
this.deathLocation = new WorldPos(dimension, position);
12601260
}
12611261

@@ -1673,7 +1673,7 @@ public boolean setGameMode(@NotNull GameMode gameMode) {
16731673
*
16741674
* @param dimensionType the new player dimension
16751675
*/
1676-
protected void sendDimension(@NotNull Key dimensionType, @NotNull String dimensionName) {
1676+
protected void sendDimension(@NotNull Key dimensionType, @NotNull Key dimensionName) {
16771677
Check.argCondition(instance.getDimensionName().equals(dimensionName),
16781678
"The dimension needs to be different than the current one!");
16791679
this.dimensionTypeId = DIMENSION_TYPE_REGISTRY.getId(dimensionType);

src/main/java/net/minestom/server/instance/Instance.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public abstract class Instance implements Block.Getter, Block.Setter,
7777

7878
private final Key dimensionType;
7979
private final DimensionType cachedDimensionType; // Cached to prevent self-destruction if the registry is changed, and to avoid the lookups.
80+
private final Key dimensionName;
8081

8182
// World border of the instance
8283
private WorldBorder worldBorder;
@@ -125,8 +126,8 @@ public abstract class Instance implements Block.Getter, Block.Setter,
125126
* @param uniqueId the {@link UUID} of the instance
126127
* @param dimensionType the {@link DimensionType} of the instance
127128
*/
128-
public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
129-
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType);
129+
public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType, Key dimensionName) {
130+
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, dimensionName);
130131
}
131132

132133
/**
@@ -135,8 +136,9 @@ public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
135136
* @param uniqueId the {@link UUID} of the instance
136137
* @param dimensionType the {@link DimensionType} of the instance
137138
*/
138-
public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @NotNull UUID uniqueId, @NotNull Key dimensionType) {
139+
public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @NotNull UUID uniqueId, @NotNull Key dimensionType, @NotNull Key dimensionName) {
139140
this.uniqueId = uniqueId;
141+
this.dimensionName = dimensionName;
140142
this.dimensionType = dimensionType;
141143
this.cachedDimensionType = dimensionTypeRegistry.get(dimensionType);
142144
Check.argCondition(cachedDimensionType == null, "The dimension " + dimensionType + " is not registered! Please add it to the registry (`MinecraftServer.getDimensionTypeRegistry().registry(dimensionType)`).");
@@ -157,6 +159,10 @@ public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @
157159
}
158160
}
159161

162+
public Key getDimensionName() {
163+
return dimensionName;
164+
}
165+
160166
/**
161167
* Schedules a task to be run during the next instance tick.
162168
*

src/main/java/net/minestom/server/instance/InstanceContainer.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,34 @@ public class InstanceContainer extends Instance {
9797
protected InstanceContainer srcInstance; // only present if this instance has been created using a copy
9898
private long lastBlockChangeTime; // Time at which the last block change happened (#setBlock)
9999

100+
/**
101+
* Creates a new instance container.
102+
* @param uniqueId the unique id of the instance
103+
* @param dimensionType the dimension type of the instance
104+
* @deprecated use {@link #InstanceContainer(DynamicRegistry, UUID, Key, Key, IChunkLoader)} instead. This constructor will be removed in future versions. It uses the dimension type as the dimension name that can cause with some errors.
105+
* @see #InstanceContainer(DynamicRegistry, UUID, Key, Key, IChunkLoader)
106+
*/
107+
@Deprecated
100108
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
101-
this(uniqueId, dimensionType, null);
109+
this(uniqueId, dimensionType, null, dimensionType);
110+
}
111+
112+
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @NotNull Key dimensionName) {
113+
this(uniqueId, dimensionType, null, dimensionName);
102114
}
103115

104-
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @Nullable IChunkLoader loader) {
105-
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, loader);
116+
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @Nullable IChunkLoader loader, @NotNull Key dimensionName) {
117+
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, dimensionName, loader);
106118
}
107119

108120
public InstanceContainer(
109121
@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry,
110122
@NotNull UUID uniqueId,
111123
@NotNull Key dimensionType,
124+
@NotNull Key dimensionName,
112125
@Nullable IChunkLoader loader
113126
) {
114-
super(dimensionTypeRegistry, uniqueId, dimensionType);
127+
super(dimensionTypeRegistry, uniqueId, dimensionType, dimensionName);
115128
setChunkSupplier(DynamicChunk::new);
116129
setChunkLoader(Objects.requireNonNullElse(loader, DEFAULT_LOADER));
117130
this.chunkLoader.loadInstance(this);
@@ -524,7 +537,7 @@ protected void addSharedInstance(SharedInstance sharedInstance) {
524537
* @see #getSrcInstance() to retrieve the "creation source" of the copied instance
525538
*/
526539
public synchronized InstanceContainer copy() {
527-
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType());
540+
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType(), getDimensionName());
528541
copiedInstance.srcInstance = this;
529542
copiedInstance.tagHandler = this.tagHandler.copy();
530543
copiedInstance.lastBlockChangeTime = this.lastBlockChangeTime;

src/main/java/net/minestom/server/instance/InstanceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void registerInstance(@NotNull Instance instance) {
5353
* @return the created {@link InstanceContainer}
5454
*/
5555
public @NotNull InstanceContainer createInstanceContainer(@NotNull Key dimensionType, @Nullable IChunkLoader loader) {
56-
final InstanceContainer instanceContainer = new InstanceContainer(registries.dimensionType(), UUID.randomUUID(), dimensionType, loader);
56+
final InstanceContainer instanceContainer = new InstanceContainer(registries.dimensionType(), UUID.randomUUID(), dimensionType, dimensionType, loader);
5757
registerInstance(instanceContainer);
5858
return instanceContainer;
5959
}

src/main/java/net/minestom/server/instance/SharedInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SharedInstance extends Instance {
2222
private final InstanceContainer instanceContainer;
2323

2424
public SharedInstance(@NotNull UUID uniqueId, @NotNull InstanceContainer instanceContainer) {
25-
super(uniqueId, instanceContainer.getDimensionType());
25+
super(uniqueId, instanceContainer.getDimensionType(), instanceContainer.getDimensionName());
2626
this.instanceContainer = instanceContainer;
2727
}
2828

src/main/java/net/minestom/server/instance/anvil/AnvilLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ private void saveSectionData(@NotNull Chunk chunk, @NotNull CompoundBinaryTag.Bu
448448
if (x % 4 == 0 && sectionLocalY % 4 == 0 && z % 4 == 0) {
449449
int biomeIndex = (x / 4) + (sectionLocalY / 4) * 4 * 4 + (z / 4) * 4;
450450
final var biomeKey = chunk.getBiome(x, y, z);
451-
final BinaryTag biomeName = StringBinaryTag.stringBinaryTag(biomeKey.value());
451+
final BinaryTag biomeName = StringBinaryTag.stringBinaryTag(biomeKey.asString());
452452

453453
int biomePaletteIndex = biomePalette.indexOf(biomeName);
454454
if (biomePaletteIndex == -1) {

src/main/java/net/minestom/server/item/component/LodestoneTracker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.minestom.server.item.component;
22

3+
import net.kyori.adventure.key.Key;
34
import net.kyori.adventure.nbt.CompoundBinaryTag;
45
import net.minestom.server.coordinate.Point;
56
import net.minestom.server.network.NetworkBuffer;
@@ -36,7 +37,7 @@ public void write(@NotNull NetworkBuffer buffer, @NotNull LodestoneTracker value
3637
.build()
3738
);
3839

39-
public LodestoneTracker(@NotNull String dimension, @NotNull Point blockPosition, boolean tracked) {
40+
public LodestoneTracker(@NotNull Key dimension, @NotNull Point blockPosition, boolean tracked) {
4041
this(new WorldPos(dimension, blockPosition), tracked);
4142
}
4243

src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.minestom.server.network.packet.server.play;
22

3+
import net.kyori.adventure.key.Key;
34
import net.minestom.server.entity.GameMode;
45
import net.minestom.server.network.NetworkBuffer;
56
import net.minestom.server.network.packet.server.ServerPacket;
@@ -16,7 +17,7 @@ public record JoinGamePacket(
1617
int entityId, boolean isHardcore, List<String> worlds, int maxPlayers,
1718
int viewDistance, int simulationDistance, boolean reducedDebugInfo, boolean enableRespawnScreen,
1819
boolean doLimitedCrafting, int dimensionType,
19-
String world, long hashedSeed, GameMode gameMode, GameMode previousGameMode,
20+
Key dimensionName, long hashedSeed, GameMode gameMode, GameMode previousGameMode,
2021
boolean isDebug, boolean isFlat, @Nullable WorldPos deathLocation, int portalCooldown,
2122
boolean enforcesSecureChat
2223
) implements ServerPacket.Play {
@@ -38,7 +39,7 @@ public JoinGamePacket(@NotNull NetworkBuffer reader) {
3839
reader.read(BOOLEAN),
3940
reader.read(BOOLEAN),
4041
reader.read(VAR_INT),
41-
reader.read(STRING),
42+
Key.key(reader.read(STRING)),
4243
reader.read(LONG),
4344
GameMode.fromId(reader.read(BYTE)),
4445
getNullableGameMode(reader.read(BYTE)),
@@ -62,7 +63,7 @@ public void write(@NotNull NetworkBuffer writer) {
6263
writer.write(BOOLEAN, enableRespawnScreen);
6364
writer.write(BOOLEAN, doLimitedCrafting);
6465
writer.write(VAR_INT, dimensionType);
65-
writer.write(STRING, world);
66+
writer.write(STRING, dimensionName.asString());
6667
writer.write(LONG, hashedSeed);
6768
writer.write(BYTE, gameMode.id());
6869
if (previousGameMode != null) {

src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.minestom.server.network.packet.server.play;
22

3+
import net.kyori.adventure.key.Key;
34
import net.minestom.server.entity.GameMode;
45
import net.minestom.server.network.NetworkBuffer;
56
import net.minestom.server.network.packet.server.ServerPacket;
@@ -21,6 +22,18 @@ public record RespawnPacket(
2122
public static final int COPY_METADATA = 0x2;
2223
public static final int COPY_ALL = COPY_ATTRIBUTES | COPY_METADATA;
2324

25+
public RespawnPacket(
26+
int dimensionType, @NotNull Key dimensionName,
27+
long hashedSeed, @NotNull GameMode gameMode, @NotNull GameMode previousGameMode,
28+
boolean isDebug, boolean isFlat, @Nullable WorldPos deathLocation,
29+
int portalCooldown, int copyData
30+
) {
31+
this(dimensionType, dimensionName.asString(),
32+
hashedSeed, gameMode, previousGameMode,
33+
isDebug, isFlat, deathLocation,
34+
portalCooldown, copyData);
35+
}
36+
2437
public RespawnPacket(@NotNull NetworkBuffer reader) {
2538
this(reader.read(VAR_INT), reader.read(STRING),
2639
reader.read(LONG), GameMode.fromId(reader.read(BYTE)),

0 commit comments

Comments
 (0)