mirror of https://github.com/Minestom/Minestom.git
GameMode cleanup
This commit is contained in:
parent
b43c404ed2
commit
7c874bb588
|
@ -1,6 +1,6 @@
|
|||
package net.minestom.server.entity;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Represents the game mode of a player.
|
||||
|
@ -8,11 +8,12 @@ import org.jetbrains.annotations.Nullable;
|
|||
* Can be set with {@link Player#setGameMode(GameMode)}.
|
||||
*/
|
||||
public enum GameMode {
|
||||
|
||||
SURVIVAL((byte) 0, true), CREATIVE((byte) 1, false), ADVENTURE((byte) 2, true), SPECTATOR((byte) 3, false);
|
||||
SURVIVAL((byte) 0, true),
|
||||
CREATIVE((byte) 1, false),
|
||||
ADVENTURE((byte) 2, true),
|
||||
SPECTATOR((byte) 3, false);
|
||||
|
||||
private final byte id;
|
||||
private boolean hardcore;
|
||||
private final boolean canTakeDamage;
|
||||
|
||||
GameMode(byte id, boolean canTakeDamage) {
|
||||
|
@ -20,29 +21,21 @@ public enum GameMode {
|
|||
this.canTakeDamage = canTakeDamage;
|
||||
}
|
||||
|
||||
public void setHardcore(boolean hardcore) {
|
||||
this.hardcore = hardcore;
|
||||
}
|
||||
|
||||
public byte getId() {
|
||||
public byte id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isHardcore() {
|
||||
return hardcore;
|
||||
}
|
||||
|
||||
public boolean canTakeDamage() {
|
||||
return canTakeDamage;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static GameMode fromId(byte id) {
|
||||
for (GameMode gameMode : values()) {
|
||||
if (gameMode.id == id) {
|
||||
return gameMode;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
public static @NotNull GameMode fromId(int id) {
|
||||
return switch (id) {
|
||||
case 0 -> SURVIVAL;
|
||||
case 1 -> CREATIVE;
|
||||
case 2 -> ADVENTURE;
|
||||
case 3 -> SPECTATOR;
|
||||
default -> throw new IllegalArgumentException("Unknown game mode id: " + id);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -243,7 +243,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
NBTCompound nbt = NBT.Compound(Map.of(
|
||||
"minecraft:dimension_type", MinecraftServer.getDimensionTypeManager().toNBT(),
|
||||
"minecraft:worldgen/biome", MinecraftServer.getBiomeManager().toNBT()));
|
||||
final JoinGamePacket joinGamePacket = new JoinGamePacket(getEntityId(), gameMode.isHardcore(), gameMode, null,
|
||||
final JoinGamePacket joinGamePacket = new JoinGamePacket(getEntityId(), false, gameMode, null,
|
||||
List.of("minestom:world"), nbt, dimensionType.toNBT(), dimensionType.getName().asString(),
|
||||
0, 0, MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(),
|
||||
false, true, false, levelFlat);
|
||||
|
@ -1230,7 +1230,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
this.gameMode = gameMode;
|
||||
// Condition to prevent sending the packets before spawning the player
|
||||
if (isActive()) {
|
||||
sendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.getId()));
|
||||
sendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.id()));
|
||||
PacketUtils.broadcastPacket(new PlayerInfoPacket(PlayerInfoPacket.Action.UPDATE_GAMEMODE,
|
||||
new PlayerInfoPacket.UpdateGameMode(getUuid(), gameMode)));
|
||||
}
|
||||
|
|
|
@ -26,9 +26,9 @@ public record JoinGamePacket(int entityId, boolean isHardcore, GameMode gameMode
|
|||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeInt(entityId);
|
||||
writer.writeBoolean(isHardcore);
|
||||
writer.writeByte(gameMode.getId());
|
||||
writer.writeByte(gameMode.id());
|
||||
if (previousGameMode != null) {
|
||||
writer.writeByte(previousGameMode.getId());
|
||||
writer.writeByte(previousGameMode.id());
|
||||
} else {
|
||||
writer.writeByte((byte) -1);
|
||||
}
|
||||
|
|
|
@ -144,7 +144,7 @@ public record PlayerInfoPacket(@NotNull Action action,
|
|||
public void write(BinaryWriter writer) {
|
||||
writer.writeSizedString(name);
|
||||
writer.writeVarIntList(properties, BinaryWriter::write);
|
||||
writer.writeVarInt(gameMode.getId());
|
||||
writer.writeVarInt(gameMode.id());
|
||||
writer.writeVarInt(ping);
|
||||
writer.writeBoolean(displayName != null);
|
||||
if (displayName != null) writer.writeComponent(displayName);
|
||||
|
@ -189,7 +189,7 @@ public record PlayerInfoPacket(@NotNull Action action,
|
|||
|
||||
@Override
|
||||
public void write(BinaryWriter writer) {
|
||||
writer.writeVarInt(gameMode.getId());
|
||||
writer.writeVarInt(gameMode.id());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@ public record RespawnPacket(DimensionType dimensionType, String worldName,
|
|||
writer.writeNBT("", dimensionType.toNBT());
|
||||
writer.writeSizedString(worldName);
|
||||
writer.writeLong(hashedSeed);
|
||||
writer.writeByte(gameMode.getId());
|
||||
writer.writeByte(previousGameMode.getId()); // Hardcore flag not included
|
||||
writer.writeByte(gameMode.id());
|
||||
writer.writeByte(previousGameMode.id());
|
||||
writer.writeBoolean(isDebug);
|
||||
writer.writeBoolean(isFlat);
|
||||
writer.writeBoolean(copyMeta);
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package net.minestom.server.entity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class GameModeTest {
|
||||
|
||||
@Test
|
||||
public void toId() {
|
||||
assertEquals(GameMode.SURVIVAL.id(), 0);
|
||||
assertEquals(GameMode.CREATIVE.id(), 1);
|
||||
assertEquals(GameMode.ADVENTURE.id(), 2);
|
||||
assertEquals(GameMode.SPECTATOR.id(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromId() {
|
||||
assertEquals(GameMode.SURVIVAL, GameMode.fromId(0));
|
||||
assertEquals(GameMode.CREATIVE, GameMode.fromId(1));
|
||||
assertEquals(GameMode.ADVENTURE, GameMode.fromId(2));
|
||||
assertEquals(GameMode.SPECTATOR, GameMode.fromId(3));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue