Skip to content

Commit

Permalink
Improve api structure
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMeinerLP committed Sep 30, 2024
1 parent 42bbcc3 commit c9988be
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion demo/src/main/java/net/minestom/demo/PlayerInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class A {
player.getInventory().addItemStack(bundle);

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

player.getInventory().addItemStack(ItemStack.builder(Material.STONE_SWORD)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/minestom/server/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ public void setDeathLocation(@NotNull Pos position) {
setDeathLocation(getInstance().getDimensionName(), position);
}

public void setDeathLocation(@NotNull String dimension, @NotNull Pos position) {
public void setDeathLocation(@NotNull Key dimension, @NotNull Pos position) {
this.deathLocation = new WorldPos(dimension, position);
}

Expand Down Expand Up @@ -1654,7 +1654,7 @@ public boolean setGameMode(@NotNull GameMode gameMode) {
*
* @param dimensionType the new player dimension
*/
protected void sendDimension(@NotNull Key dimensionType, @NotNull String dimensionName) {
protected void sendDimension(@NotNull Key dimensionType, @NotNull Key dimensionName) {
Check.argCondition(instance.getDimensionName().equals(dimensionName),
"The dimension needs to be different than the current one!");
this.dimensionTypeId = DIMENSION_TYPE_REGISTRY.getId(dimensionType);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/minestom/server/instance/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public abstract class Instance implements Block.Getter, Block.Setter,

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

// World border of the instance
private WorldBorder worldBorder;
Expand Down Expand Up @@ -125,8 +126,8 @@ public abstract class Instance implements Block.Getter, Block.Setter,
* @param uniqueId the {@link UUID} of the instance
* @param dimensionType the {@link DimensionType} of the instance
*/
public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType);
public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType, Key dimensionName) {
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, dimensionName);
}

/**
Expand All @@ -135,8 +136,9 @@ public Instance(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
* @param uniqueId the {@link UUID} of the instance
* @param dimensionType the {@link DimensionType} of the instance
*/
public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @NotNull UUID uniqueId, @NotNull Key dimensionType) {
public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @NotNull UUID uniqueId, @NotNull Key dimensionType, @NotNull Key dimensionName) {
this.uniqueId = uniqueId;
this.dimensionName = dimensionName;
this.dimensionType = dimensionType;
this.cachedDimensionType = dimensionTypeRegistry.get(dimensionType);
Check.argCondition(cachedDimensionType == null, "The dimension " + dimensionType + " is not registered! Please add it to the registry (`MinecraftServer.getDimensionTypeRegistry().registry(dimensionType)`).");
Expand All @@ -157,6 +159,10 @@ public Instance(@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry, @
}
}

public Key getDimensionName() {
return dimensionName;
}

/**
* Schedules a task to be run during the next instance tick.
*
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/net/minestom/server/instance/InstanceContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,34 @@ public class InstanceContainer extends Instance {
protected InstanceContainer srcInstance; // only present if this instance has been created using a copy
private long lastBlockChangeTime; // Time at which the last block change happened (#setBlock)

/**
* Creates a new instance container.
* @param uniqueId the unique id of the instance
* @param dimensionType the dimension type of the instance
* @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.
* @see #InstanceContainer(DynamicRegistry, UUID, Key, Key, IChunkLoader)
*/
@Deprecated
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType) {
this(uniqueId, dimensionType, null);
this(uniqueId, dimensionType, null, dimensionType);
}

public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @NotNull Key dimensionName) {
this(uniqueId, dimensionType, null, dimensionName);
}

public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @Nullable IChunkLoader loader) {
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, loader);
public InstanceContainer(@NotNull UUID uniqueId, @NotNull Key dimensionType, @Nullable IChunkLoader loader, @NotNull Key dimensionName) {
this(MinecraftServer.getDimensionTypeRegistry(), uniqueId, dimensionType, dimensionName, loader);
}

public InstanceContainer(
@NotNull DynamicRegistry<DimensionType> dimensionTypeRegistry,
@NotNull UUID uniqueId,
@NotNull Key dimensionType,
@NotNull Key dimensionName,
@Nullable IChunkLoader loader
) {
super(dimensionTypeRegistry, uniqueId, dimensionType);
super(dimensionTypeRegistry, uniqueId, dimensionType, dimensionName);
setChunkSupplier(DynamicChunk::new);
setChunkLoader(Objects.requireNonNullElse(loader, DEFAULT_LOADER));
this.chunkLoader.loadInstance(this);
Expand Down Expand Up @@ -524,7 +537,7 @@ protected void addSharedInstance(SharedInstance sharedInstance) {
* @see #getSrcInstance() to retrieve the "creation source" of the copied instance
*/
public synchronized InstanceContainer copy() {
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType());
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType(), getDimensionName());
copiedInstance.srcInstance = this;
copiedInstance.tagHandler = this.tagHandler.copy();
copiedInstance.lastBlockChangeTime = this.lastBlockChangeTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void registerInstance(@NotNull Instance instance) {
* @return the created {@link InstanceContainer}
*/
public @NotNull InstanceContainer createInstanceContainer(@NotNull Key dimensionType, @Nullable IChunkLoader loader) {
final InstanceContainer instanceContainer = new InstanceContainer(registries.dimensionType(), UUID.randomUUID(), dimensionType, loader);
final InstanceContainer instanceContainer = new InstanceContainer(registries.dimensionType(), UUID.randomUUID(), dimensionType, dimensionType, loader);
registerInstance(instanceContainer);
return instanceContainer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SharedInstance extends Instance {
private final InstanceContainer instanceContainer;

public SharedInstance(@NotNull UUID uniqueId, @NotNull InstanceContainer instanceContainer) {
super(uniqueId, instanceContainer.getDimensionType());
super(uniqueId, instanceContainer.getDimensionType(), instanceContainer.getDimensionName());
this.instanceContainer = instanceContainer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private void saveSectionData(@NotNull Chunk chunk, @NotNull CompoundBinaryTag.Bu
if (x % 4 == 0 && sectionLocalY % 4 == 0 && z % 4 == 0) {
int biomeIndex = (x / 4) + (sectionLocalY / 4) * 4 * 4 + (z / 4) * 4;
final var biomeKey = chunk.getBiome(x, y, z);
final BinaryTag biomeName = StringBinaryTag.stringBinaryTag(biomeKey.value());
final BinaryTag biomeName = StringBinaryTag.stringBinaryTag(biomeKey.asString());

int biomePaletteIndex = biomePalette.indexOf(biomeName);
if (biomePaletteIndex == -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.item.component;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.coordinate.Point;
import net.minestom.server.network.NetworkBuffer;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void write(@NotNull NetworkBuffer buffer, @NotNull LodestoneTracker value
.build()
);

public LodestoneTracker(@NotNull String dimension, @NotNull Point blockPosition, boolean tracked) {
public LodestoneTracker(@NotNull Key dimension, @NotNull Point blockPosition, boolean tracked) {
this(new WorldPos(dimension, blockPosition), tracked);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play;

import net.kyori.adventure.key.Key;
import net.minestom.server.entity.GameMode;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.packet.server.ServerPacket;
Expand All @@ -16,7 +17,7 @@ public record JoinGamePacket(
int entityId, boolean isHardcore, List<String> worlds, int maxPlayers,
int viewDistance, int simulationDistance, boolean reducedDebugInfo, boolean enableRespawnScreen,
boolean doLimitedCrafting, int dimensionType,
String world, long hashedSeed, GameMode gameMode, GameMode previousGameMode,
Key dimensionName, long hashedSeed, GameMode gameMode, GameMode previousGameMode,
boolean isDebug, boolean isFlat, @Nullable WorldPos deathLocation, int portalCooldown,
boolean enforcesSecureChat
) implements ServerPacket.Play {
Expand All @@ -38,7 +39,7 @@ public JoinGamePacket(@NotNull NetworkBuffer reader) {
reader.read(BOOLEAN),
reader.read(BOOLEAN),
reader.read(VAR_INT),
reader.read(STRING),
Key.key(reader.read(STRING)),
reader.read(LONG),
GameMode.fromId(reader.read(BYTE)),
getNullableGameMode(reader.read(BYTE)),
Expand All @@ -62,7 +63,7 @@ public void write(@NotNull NetworkBuffer writer) {
writer.write(BOOLEAN, enableRespawnScreen);
writer.write(BOOLEAN, doLimitedCrafting);
writer.write(VAR_INT, dimensionType);
writer.write(STRING, world);
writer.write(STRING, dimensionName.asString());
writer.write(LONG, hashedSeed);
writer.write(BYTE, gameMode.id());
if (previousGameMode != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play;

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

public RespawnPacket(
int dimensionType, @NotNull Key dimensionName,
long hashedSeed, @NotNull GameMode gameMode, @NotNull GameMode previousGameMode,
boolean isDebug, boolean isFlat, @Nullable WorldPos deathLocation,
int portalCooldown, int copyData
) {
this(dimensionType, dimensionName.asString(),
hashedSeed, gameMode, previousGameMode,
isDebug, isFlat, deathLocation,
portalCooldown, copyData);
}

public RespawnPacket(@NotNull NetworkBuffer reader) {
this(reader.read(VAR_INT), reader.read(STRING),
reader.read(LONG), GameMode.fromId(reader.read(BYTE)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play.data;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.coordinate.Point;
import net.minestom.server.network.NetworkBuffer;
Expand All @@ -9,32 +10,33 @@
import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION;
import static net.minestom.server.network.NetworkBuffer.STRING;

public record WorldPos(@NotNull String dimension, @NotNull Point blockPosition) implements NetworkBuffer.Writer {
public record WorldPos(@NotNull Key dimension, @NotNull Point blockPosition) implements NetworkBuffer.Writer {

public static final NetworkBuffer.Type<WorldPos> NETWORK_TYPE = new NetworkBuffer.Type<WorldPos>() {
@Override
public void write(@NotNull NetworkBuffer buffer, WorldPos value) {
buffer.write(NetworkBuffer.STRING, value.dimension);
buffer.write(NetworkBuffer.STRING, value.dimension.asString());
buffer.write(NetworkBuffer.BLOCK_POSITION, value.blockPosition);
}

@Override
public WorldPos read(@NotNull NetworkBuffer buffer) {
return new WorldPos(buffer.read(NetworkBuffer.STRING), buffer.read(NetworkBuffer.BLOCK_POSITION));
return new WorldPos(Key.key(buffer.read(NetworkBuffer.STRING)), buffer.read(NetworkBuffer.BLOCK_POSITION));
}
};
public static final BinaryTagSerializer<WorldPos> NBT_TYPE = BinaryTagSerializer.COMPOUND.map(
tag -> new WorldPos(tag.getString("dimension"), BinaryTagSerializer.BLOCK_POSITION.read(tag.get("pos"))),
tag -> new WorldPos(Key.key(tag.getString("dimension")), BinaryTagSerializer.BLOCK_POSITION.read(tag.get("pos"))),
pos -> CompoundBinaryTag.builder()
.putString("dimension", pos.dimension)
.putString("dimension", pos.dimension.asString())
.put("pos", BinaryTagSerializer.BLOCK_POSITION.write(pos.blockPosition))
.build()
);

public WorldPos(@NotNull NetworkBuffer reader) {
this(reader.read(STRING), reader.read(BLOCK_POSITION));
this(Key.key(reader.read(STRING)), reader.read(BLOCK_POSITION));
}

public @NotNull WorldPos withDimension(@NotNull String dimension) {
public @NotNull WorldPos withDimension(@NotNull Key dimension) {
return new WorldPos(dimension, blockPosition);
}

Expand All @@ -44,7 +46,7 @@ public WorldPos(@NotNull NetworkBuffer reader) {

@Override
public void write(@NotNull NetworkBuffer writer) {
writer.write(STRING, dimension);
writer.write(STRING, dimension.asString());
writer.write(BLOCK_POSITION, blockPosition);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.registry;

import net.kyori.adventure.key.Key;
import net.minestom.server.entity.Player;
import net.minestom.server.gamedata.DataPack;
import net.minestom.server.network.packet.server.SendablePacket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private BinaryTagSerializer<T> serializer() {
return new BinaryTagSerializer<>() {
@Override
public @NotNull BinaryTag write(@NotNull Context context, Key value) {
return stringBinaryTag(value.value());
return stringBinaryTag(value.asString());
}

@Override
Expand Down Expand Up @@ -477,7 +477,7 @@ interface Function5<P1, P2, P3, P4, P5, R> {
if (!(result instanceof CompoundBinaryTag resultCompound))
throw new IllegalArgumentException("Expected compound tag for tagged union");

return CompoundBinaryTag.builder().put(resultCompound).putString(key, type.value()).build();
return CompoundBinaryTag.builder().put(resultCompound).putString(key, type.asString()).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public AmbientSound(Key sound) {

public @NotNull CompoundBinaryTag toNbt() {
var builder = CompoundBinaryTag.builder()
.putString("sound_id", sound.value());
.putString("sound_id", sound.asString());

if (range != null) builder.putFloat("range", range);
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void deathLocationTest(Env env) {
player.damage(DamageType.OUT_OF_WORLD, 30);

assertNotNull(player.getDeathLocation());
assertEquals(dimensionNamespace, player.getDeathLocation().dimension());
assertEquals(dimensionNamespace, player.getDeathLocation().dimension().asString());
assertEquals(5, player.getDeathLocation().blockPosition().x());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.minestom.server.item.component;

import net.kyori.adventure.key.Key;
import net.minestom.server.component.DataComponent;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.item.ItemComponent;
Expand All @@ -18,8 +19,8 @@ public class LodestoneTrackerTest extends AbstractItemComponentTest<LodestoneTra
@Override
protected @NotNull List<Map.Entry<String, LodestoneTracker>> directReadWriteEntries() {
return List.of(
Map.entry("tracked", new LodestoneTracker("minecraft:overworld", Vec.ZERO, true)),
Map.entry("not tracked", new LodestoneTracker("minecraft:overworld", new Vec(1, 2, 3), false))
Map.entry("tracked", new LodestoneTracker(Key.key("minecraft:overworld"), Vec.ZERO, true)),
Map.entry("not tracked", new LodestoneTracker(Key.key("minecraft:overworld"), new Vec(1, 2, 3), false))
);
}

Expand Down

0 comments on commit c9988be

Please sign in to comment.