aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/world/anhgelus/molehunt/Molehunt.java131
-rw-r--r--src/main/java/world/anhgelus/molehunt/config/Config.java26
-rw-r--r--src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java28
-rw-r--r--src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java15
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/Game.java175
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/GamePayload.java24
-rw-r--r--src/main/java/world/anhgelus/molehunt/mixin/NoJoinLeaveMessage.java16
-rw-r--r--src/main/java/world/anhgelus/molehunt/mixin/NoMsgCommand.java8
-rw-r--r--src/main/java/world/anhgelus/molehunt/mixin/NoPortals.java12
-rw-r--r--src/main/java/world/anhgelus/molehunt/mixin/WorldTimerAccess.java4
-rw-r--r--src/main/java/world/anhgelus/molehunt/timer/TimerAccess.java6
-rw-r--r--src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java35
12 files changed, 228 insertions, 252 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java
index d31be6b..f3153d5 100644
--- a/src/main/java/world/anhgelus/molehunt/Molehunt.java
+++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java
@@ -12,18 +12,17 @@ import net.fabricmc.fabric.api.gamerule.v1.GameRuleEvents;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
-import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
-import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket;
+import net.minecraft.ChatFormatting;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.commands.Commands;
+import net.minecraft.network.chat.Component;
+import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
+import net.minecraft.resources.Identifier;
import net.minecraft.server.MinecraftServer;
-import net.minecraft.server.command.CommandManager;
-import net.minecraft.server.command.ServerCommandSource;
-import net.minecraft.server.network.ServerPlayerEntity;
-import net.minecraft.text.Text;
-import net.minecraft.util.Formatting;
-import net.minecraft.util.Identifier;
-import net.minecraft.world.GameMode;
-import net.minecraft.world.rule.GameRule;
-import net.minecraft.world.rule.GameRuleCategory;
+import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.world.level.GameType;
+import net.minecraft.world.level.gamerules.GameRule;
+import net.minecraft.world.level.gamerules.GameRuleCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import world.anhgelus.molehunt.config.Config;
@@ -35,7 +34,7 @@ import world.anhgelus.molehunt.game.GamePayload;
import java.util.HashMap;
import java.util.UUID;
-import static net.minecraft.server.command.CommandManager.literal;
+import static net.minecraft.commands.Commands.literal;
public class Molehunt implements ModInitializer {
@@ -46,51 +45,51 @@ public class Molehunt implements ModInitializer {
public static final GameRule<Integer> GAME_DURATION = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("game_duration", 90))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "game_duration_minutes"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "game_duration_minutes"));
public static final GameRule<Integer> MOLE_PERCENTAGE = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("mole_percentage", 25))
.range(0, 100)
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "mole_percentage"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "mole_percentage"));
public static final GameRule<Integer> MOLE_COUNT = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("mole_count", -1))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "mole_count"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "mole_count"));
public static final GameRule<Boolean> SHOW_NAMETAGS = GameRuleBuilder
.forBoolean(CONFIG_FILE.getOrDefault("show_nametags", false))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "show_nametags"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "show_nametags"));
public static final GameRule<Boolean> SHOW_TAB = GameRuleBuilder
.forBoolean(CONFIG_FILE.getOrDefault("show_tab", false))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "show_tab"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "show_tab"));
public static final GameRule<Boolean> SHOW_SKINS = GameRuleBuilder
.forBoolean(CONFIG_FILE.getOrDefault("show_skins", false))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "show_skins"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "show_skins"));
public static final GameRule<Integer> INITIAL_WORLD_SIZE = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("initial_world_size", 600))
.minValue(0)
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "initial_world_size"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "initial_world_size"));
public static final GameRule<Integer> FINAL_WORLD_SIZE = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("final_world_size", 100))
.minValue(0)
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "final_world_size"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "final_world_size"));
public static final GameRule<Integer> MOVING_STARTING_TIME_OFFSET = GameRuleBuilder
.forInteger(CONFIG_FILE.getOrDefault("border_moving_starting_time_offset", 30))
.minValue(0)
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "border_moving_starting_time_offset_minutes"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "border_moving_starting_time_offset_minutes"));
public static final GameRule<Boolean> ENABLE_PORTALS = GameRuleBuilder
.forBoolean(CONFIG_FILE.getOrDefault("enable_portals", false))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "enable_portals"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "enable_portals"));
public static final GameRule<Boolean> FOOD_ON_START = GameRuleBuilder
.forBoolean(CONFIG_FILE.getOrDefault("food_on_start", true))
.category(GameRuleCategory.MISC)
- .buildAndRegister(Identifier.of(MOD_ID, "food_on_start"));
+ .buildAndRegister(Identifier.fromNamespaceAndPath(MOD_ID, "food_on_start"));
public static Config CONFIG;
public static HashMap<UUID, Boolean> timerVisibility = new HashMap<>();
@@ -113,26 +112,30 @@ public class Molehunt implements ModInitializer {
final var command = literal("molehunt");
command.then(literal("start")
- .requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
+ .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
.executes(context -> {
+ if (game != null && game.started())
+ throw (new SimpleCommandExceptionType(
+ Component.translatable("commands.molehunt.error.game_already_started")
+ )).create();
game = new Game(context.getSource().getServer());
game.start();
return Command.SINGLE_SUCCESS;
}));
- command.then(literal("timer").requires(ServerCommandSource::isExecutedByPlayer).then(
+ command.then(literal("timer").requires(CommandSourceStack::isPlayer).then(
literal("show").executes(context -> {
var player = context.getSource().getPlayer();
assert player != null;
- timerVisibility.put(player.getUuid(), true);
- context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.show"), false);
+ timerVisibility.put(player.getUUID(), true);
+ context.getSource().sendSuccess(() -> Component.translatable("commands.molehunt.timer.show"), false);
if (game == null || !game.started()) {
- player.networkHandler.sendPacket(new OverlayMessageS2CPacket(
- Text.translatable("commands.molehunt.error.game_not_started").formatted(Formatting.RED)
+ player.connection.send(new ClientboundSetActionBarTextPacket(
+ Component.translatable("commands.molehunt.error.game_not_started").withStyle(ChatFormatting.RED)
));
} else {
- player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(game.getRemainingText())));
+ player.connection.send(new ClientboundSetActionBarTextPacket(Component.translationArg(game.getRemainingText())));
}
return Command.SINGLE_SUCCESS;
@@ -142,48 +145,50 @@ public class Molehunt implements ModInitializer {
var player = context.getSource().getPlayer();
assert player != null;
- timerVisibility.put(player.getUuid(), false);
- context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.hide"), false);
+ timerVisibility.put(player.getUUID(), false);
+ context.getSource().sendSuccess(() -> Component.translatable("commands.molehunt.timer.hide"), false);
return Command.SINGLE_SUCCESS;
})
));
command.then(literal("role")
- .requires(ServerCommandSource::isExecutedByPlayer)
+ .requires(CommandSourceStack::isPlayer)
.executes(context -> {
- if (game == null || !game.started()) {
- throw (new SimpleCommandExceptionType(Text.translatable("commands.molehunt.error.game_not_started"))).create();
- }
+ if (game == null || !game.started())
+ throw (new SimpleCommandExceptionType(
+ Component.translatable("commands.molehunt.error.game_not_started")
+ )).create();
final var source = context.getSource();
final var player = source.getPlayer();
assert player != null;
if (game.isMole(player)) {
- source.sendFeedback(
- () -> Text.translatable("commands.molehunt.role.mole")
+ source.sendSuccess(
+ () -> Component.translatable("commands.molehunt.role.mole")
.append("\n\n")
- .append(Text.translatable("commands.molehunt.role.mole.list", game.getMolesAsString())),
+ .append(Component.translatable("commands.molehunt.role.mole.list", game.getMolesAsString())),
false);
} else if (player.isSpectator()) {
- source.sendFeedback(
- () -> Text.translatable("commands.molehunt.role.survivor.mole_count", game.getMolesCount()),
+ source.sendSuccess(
+ () -> Component.translatable("commands.molehunt.role.survivor.mole_count", game.getMoles().size()),
false);
} else {
- source.sendFeedback(
- () -> Text.translatable("commands.molehunt.role.survivor")
+ source.sendSuccess(
+ () -> Component.translatable("commands.molehunt.role.survivor")
.append("\n\n")
- .append(Text.translatable("commands.molehunt.role.survivor.mole_count", game.getMolesCount())),
+ .append(Component.translatable("commands.molehunt.role.survivor.mole_count", game.getMoles().size())),
false);
}
return Command.SINGLE_SUCCESS;
}));
command.then(literal("stop")
- .requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
- .executes(context -> {
- if (game == null || !game.started()) {
- throw (new SimpleCommandExceptionType(Text.translatable("commands.molehunt.error.game_not_started"))).create();
- }
+ .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
+ .executes(_ -> {
+ if (game == null || !game.started())
+ throw (new SimpleCommandExceptionType(
+ Component.translatable("commands.molehunt.error.game_not_started")
+ )).create();
game.stop();
@@ -192,34 +197,28 @@ public class Molehunt implements ModInitializer {
ServerLifecycleEvents.SERVER_STARTED.register(server -> CONFIG = new Config(server));
- CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command));
+ CommandRegistrationCallback.EVENT.register((dispatcher, _, _) -> dispatcher.register(command));
- ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> false);
+ ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((_, _, _) -> false);
- ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
- if (!(entity instanceof ServerPlayerEntity) || game == null) return;
+ ServerLivingEntityEvents.AFTER_DEATH.register((entity, _) -> {
+ if (!(entity instanceof ServerPlayer) || game == null) return;
if (!game.started()) return;
if (game.wonByMoles()) game.end();
});
- ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> {
+ ServerPlayerEvents.AFTER_RESPAWN.register((_, newPlayer, _) -> {
if (game == null) return;
if (!game.started()) return;
- newPlayer.changeGameMode(GameMode.SPECTATOR);
+ newPlayer.setGameMode(GameType.SPECTATOR);
});
- ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
- ServerPlayNetworking.send(
- handler.player,
- new ConfigPayload(CONFIG.nametagsEnabled(), CONFIG.skinsEnabled(), CONFIG.tabEnabled())
- );
- ServerPlayNetworking.send(
- handler.player,
- new GamePayload(game != null && game.started())
- );
+ ServerPlayConnectionEvents.JOIN.register((_, sender, _) -> {
+ sender.sendPacket(new ConfigPayload(CONFIG.nametagsEnabled(), CONFIG.skinsEnabled(), CONFIG.tabEnabled()));
+ sender.sendPacket(new GamePayload(game != null && game.started()));
});
- PayloadTypeRegistry.playS2C().register(ConfigPayload.ID, ConfigPayload.CODEC);
- PayloadTypeRegistry.playS2C().register(GamePayload.ID, GamePayload.CODEC);
+ PayloadTypeRegistry.clientboundPlay().register(ConfigPayload.ID, ConfigPayload.CODEC);
+ PayloadTypeRegistry.clientboundPlay().register(GamePayload.ID, GamePayload.CODEC);
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/config/Config.java b/src/main/java/world/anhgelus/molehunt/config/Config.java
index d65b569..ee46e4b 100644
--- a/src/main/java/world/anhgelus/molehunt/config/Config.java
+++ b/src/main/java/world/anhgelus/molehunt/config/Config.java
@@ -86,55 +86,55 @@ public class Config {
public void sendConfigPayload() {
final var payload = new ConfigPayload(nametagsEnabled(), skinsEnabled(), tabEnabled());
- server.getPlayerManager().getPlayerList().forEach(p -> ServerPlayNetworking.send(p, payload));
+ server.getPlayerList().getPlayers().forEach(p -> ServerPlayNetworking.send(p, payload));
}
public void sendConfigPayload(boolean showNametags, boolean showSkins, boolean showTab) {
final var payload = new ConfigPayload(showNametags, showSkins, showTab);
- server.getPlayerManager().getPlayerList().forEach(p -> ServerPlayNetworking.send(p, payload));
+ server.getPlayerList().getPlayers().forEach(p -> ServerPlayNetworking.send(p, payload));
}
public int getGameDuration() {
- return server.getOverworld().getGameRules().getValue(Molehunt.GAME_DURATION);
+ return server.overworld().getGameRules().get(Molehunt.GAME_DURATION);
}
public int getMolePercentage() {
- return server.getOverworld().getGameRules().getValue(Molehunt.MOLE_PERCENTAGE);
+ return server.overworld().getGameRules().get(Molehunt.MOLE_PERCENTAGE);
}
public int getMoleCount() {
- return server.getOverworld().getGameRules().getValue(Molehunt.MOLE_COUNT);
+ return server.overworld().getGameRules().get(Molehunt.MOLE_COUNT);
}
public boolean nametagsEnabled() {
- return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_NAMETAGS);
+ return server.overworld().getGameRules().get(Molehunt.SHOW_NAMETAGS);
}
public boolean skinsEnabled() {
- return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_SKINS);
+ return server.overworld().getGameRules().get(Molehunt.SHOW_SKINS);
}
public boolean tabEnabled() {
- return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_TAB);
+ return server.overworld().getGameRules().get(Molehunt.SHOW_TAB);
}
public int getInitialWorldSize() {
- return server.getOverworld().getGameRules().getValue(Molehunt.INITIAL_WORLD_SIZE);
+ return server.overworld().getGameRules().get(Molehunt.INITIAL_WORLD_SIZE);
}
public int getFinalWorldSize() {
- return server.getOverworld().getGameRules().getValue(Molehunt.FINAL_WORLD_SIZE);
+ return server.overworld().getGameRules().get(Molehunt.FINAL_WORLD_SIZE);
}
public int getBorderShrinkingStartingTimeOffset() {
- return server.getOverworld().getGameRules().getValue(Molehunt.MOVING_STARTING_TIME_OFFSET);
+ return server.overworld().getGameRules().get(Molehunt.MOVING_STARTING_TIME_OFFSET);
}
public boolean portalsEnabled() {
- return server.getOverworld().getGameRules().getValue(Molehunt.ENABLE_PORTALS);
+ return server.overworld().getGameRules().get(Molehunt.ENABLE_PORTALS);
}
public boolean foodOnStart() {
- return server.getOverworld().getGameRules().getValue(Molehunt.FOOD_ON_START);
+ return server.overworld().getGameRules().get(Molehunt.FOOD_ON_START);
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
index f33e2c5..950bec8 100644
--- a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
+++ b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
@@ -1,25 +1,27 @@
package world.anhgelus.molehunt.config;
-import net.minecraft.network.RegistryByteBuf;
-import net.minecraft.network.codec.PacketCodec;
-import net.minecraft.network.codec.PacketCodecs;
-import net.minecraft.network.packet.CustomPayload;
-import net.minecraft.util.Identifier;
+import net.minecraft.network.RegistryFriendlyByteBuf;
+import net.minecraft.network.codec.ByteBufCodecs;
+import net.minecraft.network.codec.StreamCodec;
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
+import net.minecraft.resources.Identifier;
+import org.jetbrains.annotations.NotNull;
import world.anhgelus.molehunt.Molehunt;
-public record ConfigPayload(boolean showNametags, boolean showSkins, boolean showTab) implements CustomPayload {
- public static final Identifier CONFIG_PACKET_ID = Identifier.of(Molehunt.MOD_ID, "config");
+public record ConfigPayload(boolean showNametags, boolean showSkins, boolean showTab) implements CustomPacketPayload {
+ public static final Identifier CONFIG_PACKET_ID = Identifier.fromNamespaceAndPath(Molehunt.MOD_ID, "config");
- public static final CustomPayload.Id<ConfigPayload> ID = new CustomPayload.Id<>(CONFIG_PACKET_ID);
- public static final PacketCodec<RegistryByteBuf, ConfigPayload> CODEC = PacketCodec.tuple(
- PacketCodecs.BOOLEAN, ConfigPayload::showNametags,
- PacketCodecs.BOOLEAN, ConfigPayload::showSkins,
- PacketCodecs.BOOLEAN, ConfigPayload::showTab,
+ public static final CustomPacketPayload.Type<ConfigPayload> ID = new CustomPacketPayload.Type<>(CONFIG_PACKET_ID);
+ public static final StreamCodec<RegistryFriendlyByteBuf, ConfigPayload> CODEC = StreamCodec.composite(
+ ByteBufCodecs.BOOL, ConfigPayload::showNametags,
+ ByteBufCodecs.BOOL, ConfigPayload::showSkins,
+ ByteBufCodecs.BOOL, ConfigPayload::showTab,
ConfigPayload::new
);
@Override
- public Id<? extends CustomPayload> getId() {
+ @NotNull
+ public Type<? extends CustomPacketPayload> type() {
return ID;
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java b/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
index 6b7060a..84e485b 100644
--- a/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
+++ b/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
@@ -84,9 +84,10 @@ public class SimpleConfig {
}
private void createConfig() throws IOException {
-
// try creating missing files
- request.file.getParentFile().mkdirs();
+ final var parent = request.file.getParentFile();
+ if (parent == null) throw new IOException("Cannot get parent file of the config");
+ parent.mkdirs();
Files.createFile(request.file.toPath());
// write default config data
@@ -114,15 +115,7 @@ public class SimpleConfig {
}
}
- /**
- * Queries a value from config, returns `null` if the
- * key does not exist.
- *
- * @return value corresponding to the given key
- * @see SimpleConfig#getOrDefault
- */
- @Deprecated
- public String get(String key) {
+ private String get(String key) {
return config.get(key);
}
diff --git a/src/main/java/world/anhgelus/molehunt/game/Game.java b/src/main/java/world/anhgelus/molehunt/game/Game.java
index 98784c3..9f44f77 100644
--- a/src/main/java/world/anhgelus/molehunt/game/Game.java
+++ b/src/main/java/world/anhgelus/molehunt/game/Game.java
@@ -1,19 +1,19 @@
package world.anhgelus.molehunt.game;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
-import net.minecraft.entity.Entity;
-import net.minecraft.entity.player.PlayerEntity;
-import net.minecraft.item.ItemStack;
-import net.minecraft.item.Items;
-import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket;
-import net.minecraft.network.packet.s2c.play.SubtitleS2CPacket;
-import net.minecraft.network.packet.s2c.play.TitleFadeS2CPacket;
-import net.minecraft.network.packet.s2c.play.TitleS2CPacket;
+import net.minecraft.network.chat.Component;
+import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
+import net.minecraft.network.protocol.game.ClientboundSetSubtitleTextPacket;
+import net.minecraft.network.protocol.game.ClientboundSetTitleTextPacket;
+import net.minecraft.network.protocol.game.ClientboundSetTitlesAnimationPacket;
import net.minecraft.server.MinecraftServer;
-import net.minecraft.server.network.ServerPlayerEntity;
-import net.minecraft.text.Text;
-import net.minecraft.world.GameMode;
-import net.minecraft.world.rule.GameRules;
+import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.player.Player;
+import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.item.Items;
+import net.minecraft.world.level.GameType;
+import net.minecraft.world.level.gamerules.GameRules;
import world.anhgelus.molehunt.Molehunt;
import world.anhgelus.molehunt.timer.TickTask;
import world.anhgelus.molehunt.timer.TimerAccess;
@@ -26,12 +26,12 @@ import java.util.stream.Stream;
public class Game {
- public final int defaultTime = Molehunt.CONFIG.getGameDuration() * 60;
+ public final int DEFAULT_TIME = Molehunt.CONFIG.getGameDuration() * 60;
private final MinecraftServer server;
private final List<UUID> moles = new ArrayList<>();
- private final TitleFadeS2CPacket timing = new TitleFadeS2CPacket(20, 40, 20);
- private int remaining = defaultTime;
+ private final ClientboundSetTitlesAnimationPacket timing = new ClientboundSetTitlesAnimationPacket(20, 40, 20);
private boolean started = false;
+ private int remaining = DEFAULT_TIME;
public Game(MinecraftServer server) {
this.server = server;
@@ -39,33 +39,32 @@ public class Game {
public void start() {
final int n = Molehunt.CONFIG.getMoleCount() < 0
- ? Math.floorDiv(server.getCurrentPlayerCount(), Math.floorDiv(100, Molehunt.CONFIG.getMolePercentage()))
+ ? Math.floorDiv(server.getPlayerCount(), Math.floorDiv(100, Molehunt.CONFIG.getMolePercentage()))
: Molehunt.CONFIG.getMoleCount();
- final var playerManager = server.getPlayerManager();
+ final var playerManager = server.getPlayerList();
- final var players = new ArrayList<>(playerManager.getPlayerList());
+ final var players = new ArrayList<>(playerManager.getPlayers());
for (int i = 0; i < n && !players.isEmpty(); i++) {
final var r = ThreadLocalRandom.current().nextInt(0, players.size());
final var mole = players.get(r);
- if (mole == null) throw new IllegalStateException("Mole is null!");
- moles.add(mole.getUuid());
+ moles.add(mole.getUUID());
players.remove(r);
}
- final var gamerules = server.getOverworld().getGameRules();
+ final var gamerules = server.overworld().getGameRules();
// immutable gamerules
- gamerules.setValue(GameRules.SHOW_DEATH_MESSAGES, false, server);
- gamerules.setValue(GameRules.ANNOUNCE_ADVANCEMENTS, false, server);
+ gamerules.set(GameRules.SHOW_DEATH_MESSAGES, false, server);
+ gamerules.set(GameRules.SHOW_ADVANCEMENT_MESSAGES, false, server);
// gamerules for the start
- gamerules.setValue(GameRules.DO_IMMEDIATE_RESPAWN, true, server);
+ gamerules.set(GameRules.IMMEDIATE_RESPAWN, true, server);
final var timer = TimerAccess.getTimerFromOverworld(server);
- final var worldBorder = server.getOverworld().getWorldBorder();
+ final var worldBorder = server.overworld().getWorldBorder();
worldBorder.setSize(Molehunt.CONFIG.getInitialWorldSize());
if (Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset() < Molehunt.CONFIG.getGameDuration()) {
- timer.dds_runTask(new TickTask(() -> worldBorder.interpolateSize(
+ timer.dds_runTask(new TickTask(() -> worldBorder.lerpSizeBetween(
Molehunt.CONFIG.getInitialWorldSize(),
Molehunt.CONFIG.getFinalWorldSize(),
(Molehunt.CONFIG.getGameDuration() - Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset()) * 60 * 20L,
@@ -73,54 +72,56 @@ public class Game {
), Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset() * 60 * 20L));
}
- final var title = new TitleS2CPacket(Text.translatable("molehunt.game.start.suspense"));
- playerManager.getPlayerList().forEach(p -> {
- p.getInventory().clear();
- p.kill(p.getEntityWorld());
- p.networkHandler.sendPacket(timing);
- p.networkHandler.sendPacket(title);
- p.changeGameMode(GameMode.SURVIVAL);
- if (Molehunt.CONFIG.foodOnStart()) p.giveItemStack(new ItemStack(Items.COOKED_BEEF, 64));
+ final var title = new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.start.suspense"));
+ playerManager.getPlayers().forEach(p -> {
+ p.getInventory().clearContent();
+ p.kill(p.level());
+ p.connection.send(timing);
+ p.connection.send(title);
+ p.setGameMode(GameType.SURVIVAL);
+ if (Molehunt.CONFIG.foodOnStart()) p.addItem(new ItemStack(Items.COOKED_BEEF, 64));
});
- server.setDefaultGameMode(GameMode.SPECTATOR);
+ server.setDefaultGameType(GameType.SPECTATOR);
timer.dds_runTask(new TickTask(() -> {
- playerManager.getPlayerList().forEach(p -> {
- p.networkHandler.sendPacket(timing);
- if (moles.contains(p.getUuid())) {
- p.networkHandler.sendPacket(new TitleS2CPacket(Text.translatable("molehunt.game.start.mole.title")));
- p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.translatable("molehunt.game.start.mole.subtitle")));
+ playerManager.getPlayers().forEach(p -> {
+ p.connection.send(timing);
+ if (moles.contains(p.getUUID())) {
+ p.connection.send(new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.start.mole.title")));
+ p.connection.send(new ClientboundSetSubtitleTextPacket(Component.translatable("molehunt.game.start.mole.subtitle")));
} else {
- p.networkHandler.sendPacket(new TitleS2CPacket(Text.translatable("molehunt.game.start.survivor.title")));
- p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.translatable("molehunt.game.start.survivor.subtitle")));
+ p.connection.send(new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.start.survivor.title")));
+ p.connection.send(new ClientboundSetSubtitleTextPacket(Component.translatable("molehunt.game.start.survivor.subtitle")));
}
// reset health and food level
p.setHealth(p.getMaxHealth());
- p.getHungerManager().setFoodLevel(20);
- p.getHungerManager().setSaturationLevel(5.0f);
+ p.getFoodData().setFoodLevel(20);
+ p.getFoodData().setSaturation(5.0f);
});
// reset gamerules after the start
- gamerules.setValue(GameRules.DO_IMMEDIATE_RESPAWN, false, server);
+ gamerules.set(GameRules.IMMEDIATE_RESPAWN, false, server);
// reset time and weather
- server.getOverworld().setTimeOfDay(0);
- server.getOverworld().resetWeather();
+ final var overworld = server.overworld();
+ server.clockManager()
+ .setTotalTicks(overworld.getLevel().dimensionType().defaultClock().orElseThrow(), 0);
+ overworld.resetWeatherCycle();
changeState(true);
timer.dds_runTask(new TickTask(() -> {
remaining--;
- playerManager.getPlayerList().forEach(player -> {
- if (Molehunt.timerVisibility.getOrDefault(player.getUuid(), true)) {
- player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(getRemainingText())));
+ playerManager.getPlayers().forEach(player -> {
+ if (Molehunt.timerVisibility.getOrDefault(player.getUUID(), true)) {
+ player.connection.send(new ClientboundSetActionBarTextPacket(Component.translationArg(getRemainingText())));
}
});
- playerManager.sendToAll(timing);
+ playerManager.broadcastAll(timing);
if (remaining == 0) end();
- }, 5 * 20, 20));
- }, 4 * 20));
+ }, 5 * 1000, 1000));
+ }, 4 * 1000));
}
public void stop() {
- server.getPlayerManager().broadcast(Text.translatable("commands.molehunt.stop.success"), false);
+ server.getPlayerList().broadcastSystemMessage(Component.translatable("commands.molehunt.stop.success"), false);
end();
}
@@ -128,41 +129,42 @@ public class Game {
final var timer = TimerAccess.getTimerFromOverworld(server);
timer.dds_cancel();
- final var worldBorder = server.getOverworld().getWorldBorder();
+ final var worldBorder = server.overworld().getWorldBorder();
// Stops the border shrinking.
worldBorder.setSize(worldBorder.getSize());
changeState(false);
- final var pm = server.getPlayerManager();
- final var winnerSuspense = new TitleS2CPacket(Text.translatable("molehunt.game.end.suspense.title"));
- pm.getPlayerList().forEach(p -> {
- p.networkHandler.sendPacket(timing);
- p.networkHandler.sendPacket(winnerSuspense);
- p.changeGameMode(GameMode.CREATIVE);
+ final var pm = server.getPlayerList();
+ final var winnerSuspense = new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.end.suspense.title"));
+ pm.getPlayers().forEach(p -> {
+ p.connection.send(timing);
+ p.connection.send(winnerSuspense);
+ p.setGameMode(GameType.CREATIVE);
});
+
timer.dds_runTask(new TickTask(() -> {
- TitleS2CPacket winner;
+ ClientboundSetTitleTextPacket winner;
if (wonByMoles()) {
- winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.moles.title"));
+ winner = new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.end.winners.moles.title"));
} else {
- winner = new TitleS2CPacket(Text.translatable("molehunt.game.end.winners.survivors.title"));
+ winner = new ClientboundSetTitleTextPacket(Component.translatable("molehunt.game.end.winners.survivors.title"));
}
- pm.sendToAll(new SubtitleS2CPacket(Text.translatable("molehunt.game.end.winners.subtitle", getMolesAsString())));
- pm.sendToAll(winner);
- pm.sendToAll(timing);
+ pm.broadcastAll(new ClientboundSetSubtitleTextPacket(Component.translatable("molehunt.game.end.winners.subtitle", getMolesAsString())));
+ pm.broadcastAll(winner);
+ pm.broadcastAll(timing);
moles.clear();
- }, 4 * 20));
+ }, 4 * 1000));
}
- public Text getRemainingText() {
- return Text.of("§c" + TimeUtils.generateShortString(remaining));
+ public Component getRemainingText() {
+ return Component.nullToEmpty("§c" + TimeUtils.toTime(remaining));
}
- private Stream<ServerPlayerEntity> getMoles() {
+ private Stream<ServerPlayer> getMoles() {
return moles.stream()
- .map(uuid -> server.getPlayerManager().getPlayer(uuid))
+ .map(uuid -> server.getPlayerList().getPlayer(uuid))
.filter(Objects::nonNull)
- .filter(p -> !p.isSpectator() && !p.isCreative());
+ .filter(p -> p.gameMode.isSurvival());
}
public int getMolesCount() {
@@ -170,35 +172,34 @@ public class Game {
}
public String getMolesAsString() {
- return getMoles().map(PlayerEntity::getDisplayName)
- .filter(Objects::nonNull)
+ return getMoles().map(Player::getDisplayName)
.map(Object::toString)
.collect(Collectors.joining(", "));
}
- public boolean isMole(ServerPlayerEntity player) {
- return moles.contains(player.getUuid());
+ public boolean isMole(ServerPlayer player) {
+ return moles.contains(player.getUUID());
}
public boolean wonByMoles() {
- final var moles = getMoles().map(PlayerEntity::getUuid).toList();
+ final var moles = getMoles().map(Player::getUUID).toList();
return !moles.isEmpty() && new HashSet<>(moles).containsAll(
- server.getPlayerManager()
- .getPlayerList()
+ server.getPlayerList()
+ .getPlayers()
.stream()
- .filter(p -> !p.isSpectator() && !p.isCreative())
- .map(Entity::getUuid)
+ .filter(p -> p.gameMode.isSurvival())
+ .map(Entity::getUUID)
.toList()
);
}
- public boolean started() {
- return started;
- }
-
private void changeState(boolean hasStarted) {
started = hasStarted;
final var payload = new GamePayload(hasStarted);
- server.getPlayerManager().sendToAll(ServerPlayNetworking.createS2CPacket(payload));
+ server.getPlayerList().getPlayers().forEach(p -> ServerPlayNetworking.send(p, payload));
+ }
+
+ public boolean started() {
+ return started;
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
index 1532fa9..f654115 100644
--- a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
+++ b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
@@ -1,23 +1,25 @@
package world.anhgelus.molehunt.game;
-import net.minecraft.network.RegistryByteBuf;
-import net.minecraft.network.codec.PacketCodec;
-import net.minecraft.network.codec.PacketCodecs;
-import net.minecraft.network.packet.CustomPayload;
-import net.minecraft.util.Identifier;
+import net.minecraft.network.RegistryFriendlyByteBuf;
+import net.minecraft.network.codec.ByteBufCodecs;
+import net.minecraft.network.codec.StreamCodec;
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
+import net.minecraft.resources.Identifier;
+import org.jetbrains.annotations.NotNull;
import world.anhgelus.molehunt.Molehunt;
-public record GamePayload(boolean gameLaunched) implements CustomPayload {
- public static final Identifier GAME_PACKET_ID = Identifier.of(Molehunt.MOD_ID, "game");
+public record GamePayload(boolean gameLaunched) implements CustomPacketPayload {
+ public static final Identifier GAME_PACKET_ID = Identifier.fromNamespaceAndPath(Molehunt.MOD_ID, "game");
- public static final CustomPayload.Id<GamePayload> ID = new CustomPayload.Id<>(GAME_PACKET_ID);
- public static final PacketCodec<RegistryByteBuf, GamePayload> CODEC = PacketCodec.tuple(
- PacketCodecs.BOOLEAN, GamePayload::gameLaunched,
+ public static final CustomPacketPayload.Type<GamePayload> ID = new CustomPacketPayload.Type<>(GAME_PACKET_ID);
+ public static final StreamCodec<RegistryFriendlyByteBuf, GamePayload> CODEC = StreamCodec.composite(
+ ByteBufCodecs.BOOL, GamePayload::gameLaunched,
GamePayload::new
);
@Override
- public Id<? extends CustomPayload> getId() {
+ @NotNull
+ public Type<? extends CustomPacketPayload> type() {
return ID;
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/mixin/NoJoinLeaveMessage.java b/src/main/java/world/anhgelus/molehunt/mixin/NoJoinLeaveMessage.java
index 0dfd624..e1992fe 100644
--- a/src/main/java/world/anhgelus/molehunt/mixin/NoJoinLeaveMessage.java
+++ b/src/main/java/world/anhgelus/molehunt/mixin/NoJoinLeaveMessage.java
@@ -1,18 +1,18 @@
package world.anhgelus.molehunt.mixin;
-import net.minecraft.server.PlayerManager;
-import net.minecraft.text.Text;
+import net.minecraft.network.chat.Component;
+import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
-@Mixin(PlayerManager.class)
+@Mixin(PlayerList.class)
public class NoJoinLeaveMessage {
- @Inject(at = @At("HEAD"), method = "broadcast*", cancellable = true)
- public void broadcastNoJoinLeaveMessage(Text message, boolean overlay, CallbackInfo ci) {
- final var content = message.getContent().toString();
- if (content.startsWith("translation{key='multiplayer.player.joined")) ci.cancel();
- else if (content.startsWith("translation{key='multiplayer.player.left")) ci.cancel();
+ @Inject(at = @At("HEAD"), method = "broadcastSystemMessage*", cancellable = true)
+ public void broadcastNoJoinLeaveMessage(final Component message, final boolean overlay, CallbackInfo ci) {
+ final var content = message.getContents().toString();
+ if (content.startsWith("translation{key='multiplayer.player.joined") ||
+ content.startsWith("translation{key='multiplayer.player.left")) ci.cancel();
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/mixin/NoMsgCommand.java b/src/main/java/world/anhgelus/molehunt/mixin/NoMsgCommand.java
index f698f1c..cef2f59 100644
--- a/src/main/java/world/anhgelus/molehunt/mixin/NoMsgCommand.java
+++ b/src/main/java/world/anhgelus/molehunt/mixin/NoMsgCommand.java
@@ -1,17 +1,17 @@
package world.anhgelus.molehunt.mixin;
import com.mojang.brigadier.CommandDispatcher;
-import net.minecraft.server.command.MessageCommand;
-import net.minecraft.server.command.ServerCommandSource;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.server.commands.MsgCommand;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
-@Mixin(MessageCommand.class)
+@Mixin(MsgCommand.class)
public class NoMsgCommand {
@Inject(at = @At("HEAD"), method = "register", cancellable = true)
- private static void register(CommandDispatcher<ServerCommandSource> dispatcher, CallbackInfo ci) {
+ private static void register(CommandDispatcher<CommandSourceStack> dispatcher, CallbackInfo ci) {
ci.cancel();
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/mixin/NoPortals.java b/src/main/java/world/anhgelus/molehunt/mixin/NoPortals.java
index 2648f25..acad2ba 100644
--- a/src/main/java/world/anhgelus/molehunt/mixin/NoPortals.java
+++ b/src/main/java/world/anhgelus/molehunt/mixin/NoPortals.java
@@ -1,18 +1,18 @@
package world.anhgelus.molehunt.mixin;
-import net.minecraft.entity.Entity;
-import net.minecraft.server.world.ServerWorld;
-import net.minecraft.world.dimension.PortalManager;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.PortalProcessor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import world.anhgelus.molehunt.Molehunt;
-@Mixin(PortalManager.class)
+@Mixin(PortalProcessor.class)
public class NoPortals {
- @Inject(at = @At("HEAD"), method = "tick", cancellable = true)
- public void disableTick(ServerWorld world, Entity entity, boolean canUsePortals, CallbackInfoReturnable<Boolean> cir) {
+ @Inject(at = @At("HEAD"), method = "processPortalTeleportation", cancellable = true)
+ public void disableTick(ServerLevel world, Entity entity, boolean canUsePortals, CallbackInfoReturnable<Boolean> cir) {
if (Molehunt.CONFIG == null || Molehunt.CONFIG.portalsEnabled()) return;
cir.setReturnValue(false);
}
diff --git a/src/main/java/world/anhgelus/molehunt/mixin/WorldTimerAccess.java b/src/main/java/world/anhgelus/molehunt/mixin/WorldTimerAccess.java
index 1e0ba12..a299d7f 100644
--- a/src/main/java/world/anhgelus/molehunt/mixin/WorldTimerAccess.java
+++ b/src/main/java/world/anhgelus/molehunt/mixin/WorldTimerAccess.java
@@ -1,6 +1,6 @@
package world.anhgelus.molehunt.mixin;
-import net.minecraft.server.world.ServerWorld;
+import net.minecraft.server.level.ServerLevel;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@@ -12,7 +12,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
-@Mixin(ServerWorld.class)
+@Mixin(ServerLevel.class)
public class WorldTimerAccess implements TimerAccess {
@Unique
private final List<TickTask> tasks = new ArrayList<>();
diff --git a/src/main/java/world/anhgelus/molehunt/timer/TimerAccess.java b/src/main/java/world/anhgelus/molehunt/timer/TimerAccess.java
index 32ee611..59453e3 100644
--- a/src/main/java/world/anhgelus/molehunt/timer/TimerAccess.java
+++ b/src/main/java/world/anhgelus/molehunt/timer/TimerAccess.java
@@ -1,7 +1,6 @@
package world.anhgelus.molehunt.timer;
import net.minecraft.server.MinecraftServer;
-import net.minecraft.world.World;
import java.util.List;
@@ -13,10 +12,7 @@ public interface TimerAccess {
* @return TimerAccess linked to the overworld
*/
static TimerAccess getTimerFromOverworld(MinecraftServer server) {
- final var timer = (TimerAccess) server.getWorld(World.OVERWORLD);
- if (timer == null)
- throw new NullPointerException("Impossible to get TimerAccess from the overworld (it is null)");
- return timer;
+ return (TimerAccess) server.overworld();
}
/**
diff --git a/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java b/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
index 952f2d2..4d69e0a 100644
--- a/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
+++ b/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
@@ -1,31 +1,9 @@
package world.anhgelus.molehunt.utils;
-public class TimeUtils {
-
- public static String generateString(long time) {
- final var pt = generateTime(time);
-
- StringBuilder sb = new StringBuilder();
- if (pt.hours != 0) {
- sb.append(pt.hours).append(" hours ");
- }
- if (pt.minutes != 0 || pt.hours != 0) {
- sb.append(pt.minutes).append(" minutes ");
- }
- sb.append(pt.seconds).append(" seconds");
-
- return sb.toString();
- }
-
- public static String generateShortString(long time) {
- final var pt = generateTime(time);
-
- return padLeft(pt.hours) + ":" +
- padLeft(pt.minutes) + ":" +
- padLeft(pt.seconds);
- }
+import org.jetbrains.annotations.NotNull;
- private static Time generateTime(long time) {
+public class TimeUtils {
+ public static Time toTime(long time) {
long hours = 0;
if (time > 3600) {
hours = Math.floorDiv(time, 3600);
@@ -47,6 +25,11 @@ public class TimeUtils {
return Long.toString(Math.round(n));
}
- private record Time(long hours, long minutes, long seconds) {
+ public record Time(long hours, long minutes, long seconds) {
+ public @NotNull String toString() {
+ return padLeft(hours) + ":" +
+ padLeft(minutes) + ":" +
+ padLeft(seconds);
+ }
}
}