From 411484a33f6fa407de332ec9eddf8ecb65ee078c Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 16 Mar 2026 21:07:06 +0100 Subject: feat(): support java 1.21.11 --- .../java/world/anhgelus/molehunt/Molehunt.java | 165 ++++++++++++--------- .../world/anhgelus/molehunt/config/Config.java | 22 +-- .../anhgelus/molehunt/config/ConfigPayload.java | 6 +- .../java/world/anhgelus/molehunt/game/Game.java | 18 ++- .../world/anhgelus/molehunt/game/GamePayload.java | 2 +- src/main/resources/fabric.mod.json | 2 +- 6 files changed, 117 insertions(+), 98 deletions(-) (limited to 'src/main') diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 7fa10a3..8df6e32 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -7,19 +7,23 @@ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; -import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; -import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; +import net.fabricmc.fabric.api.gamerule.v1.GameRuleBuilder; +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.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.GameRules; +import net.minecraft.world.rule.GameRule; +import net.minecraft.world.rule.GameRuleCategory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import world.anhgelus.molehunt.config.Config; @@ -29,6 +33,7 @@ import world.anhgelus.molehunt.game.Game; import world.anhgelus.molehunt.game.GamePayload; import java.util.HashMap; +import java.util.UUID; import static net.minecraft.server.command.CommandManager.literal; @@ -41,93 +46,100 @@ public class Molehunt implements ModInitializer { public static final SimpleConfig CONFIG_FILE = Config.configFile(MOD_ID); - public static final GameRules.Key GAME_DURATION = GameRuleRegistry.register( - MOD_ID +":gameDurationMinutes", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("game_duration", 90)) - ); - public static final GameRules.Key MOLE_PERCENTAGE = GameRuleRegistry.register( - MOD_ID +":molePercentage", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("mole_percentage", 25)) - ); - public static final GameRules.Key MOLE_COUNT = GameRuleRegistry.register( - MOD_ID +":moleCount", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("mole_count", -1)) - ); - public static final GameRules.Key SHOW_NAMETAGS = GameRuleRegistry.register( - MOD_ID +":showNametags", - GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_nametags", false), (server, val) -> { - if (CONFIG == null) return; - CONFIG.sendConfigPayload(); - }) - ); - public static final GameRules.Key SHOW_TAB = GameRuleRegistry.register( - MOD_ID +":showTab" - , GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_tab", false), (server, val) -> { - if (CONFIG == null) return; - CONFIG.sendConfigPayload(); - }) - ); - public static final GameRules.Key SHOW_SKINS = GameRuleRegistry.register( - MOD_ID +":showSkins", - GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_skins", false), (server, val) -> { - if (CONFIG == null) return; - CONFIG.sendConfigPayload(); - }) - ); - public static final GameRules.Key INITIAL_WORLD_SIZE = GameRuleRegistry.register( - MOD_ID +":initialWorldSize", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("initial_world_size", 600), 0) - ); - public static final GameRules.Key FINAL_WORLD_SIZE = GameRuleRegistry.register( - MOD_ID +":finalWorldSize", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("final_world_size", 100), 0) - ); - public static final GameRules.Key MOVING_STARTING_TIME_OFFSET = GameRuleRegistry.register( - MOD_ID +":borderMovingStartingTimeOffsetMinutes", - GameRules.Category.MISC, - GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("border_moving_starting_time_offset", 30), 0) - ); - public static final GameRules.Key ENABLE_PORTALS = GameRuleRegistry.register( - MOD_ID +":enablePortals", - GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("enable_portals", false)) - ); - public static final GameRules.Key FOOD_ON_START = GameRuleRegistry.register( - MOD_ID +":foodOnStart", - GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("food_on_start", true)) - ); + public static final GameRule GAME_DURATION = GameRuleBuilder + .forInteger(CONFIG_FILE.getOrDefault("game_duration", 90)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "gameDurationMinutes")); + + public static final GameRule MOLE_PERCENTAGE = GameRuleBuilder + .forInteger(CONFIG_FILE.getOrDefault("mole_percentage", 25)) + .range(0, 100) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "molePercentage")); + + public static final GameRule MOLE_COUNT = GameRuleBuilder + .forInteger(CONFIG_FILE.getOrDefault("mole_count", -1)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "moleCount")); + + public static final GameRule SHOW_NAMETAGS = GameRuleBuilder + .forBoolean(CONFIG_FILE.getOrDefault("show_nametags", false)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "showNametags")); + + public static final GameRule SHOW_TAB = GameRuleBuilder + .forBoolean(CONFIG_FILE.getOrDefault("show_tab", false)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "showTab")); + + public static final GameRule SHOW_SKINS = GameRuleBuilder + .forBoolean(CONFIG_FILE.getOrDefault("show_skins", false)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "showSkins")); + + public static final GameRule INITIAL_WORLD_SIZE = GameRuleBuilder + .forInteger(CONFIG_FILE.getOrDefault("initial_world_size", 600)) + .minValue(0) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "initialWorldSize")); + + public static final GameRule FINAL_WORLD_SIZE = GameRuleBuilder + .forInteger(CONFIG_FILE.getOrDefault("final_world_size", 100)) + .minValue(0) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "finalWorldSize")); + + public static final GameRule 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, "borderMovingStartingTimeOffsetMinutes")); + + public static final GameRule ENABLE_PORTALS = GameRuleBuilder + .forBoolean(CONFIG_FILE.getOrDefault("enable_portals", false)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "enablePortals")); + + public static final GameRule FOOD_ON_START = GameRuleBuilder + .forBoolean(CONFIG_FILE.getOrDefault("food_on_start", true)) + .category(GameRuleCategory.MISC) + .buildAndRegister(Identifier.of(MOD_ID, "foodOnStart")); public Game game; - public static HashMap timerVisibility = new HashMap<>(); + public static HashMap timerVisibility = new HashMap<>(); + + private static void sendConfigPayload(T v, MinecraftServer server) { + if (CONFIG == null) return; + CONFIG.sendConfigPayload(); + } + + static { + GameRuleEvents.changeCallback(SHOW_NAMETAGS).register(Molehunt::sendConfigPayload); + GameRuleEvents.changeCallback(SHOW_TAB).register(Molehunt::sendConfigPayload); + GameRuleEvents.changeCallback(SHOW_SKINS).register(Molehunt::sendConfigPayload); + } @Override public void onInitialize() { LOGGER.info("Initializing Molehunt"); final var command = literal("molehunt"); - command.then(literal("start").requires(source -> source.hasPermissionLevel(1)).executes(context -> { + command.then(literal("start") + .requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK)) + .executes(context -> { game = new Game(context.getSource().getServer()); game.start(); return Command.SINGLE_SUCCESS; })); command.then(literal("timer").requires(ServerCommandSource::isExecutedByPlayer).then( literal("show").executes(context -> { - timerVisibility.put(context.getSource().getPlayer(), true); - context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.show"), false); - var player = context.getSource().getPlayer(); assert player != null; + timerVisibility.put(player.getUuid(), true); + context.getSource().sendFeedback(() -> Text.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) @@ -140,7 +152,10 @@ public class Molehunt implements ModInitializer { }) ).then( literal("hide").executes(context -> { - timerVisibility.put(context.getSource().getPlayer(), false); + var player = context.getSource().getPlayer(); + assert player != null; + + timerVisibility.put(player.getUuid(), false); context.getSource().sendFeedback(() -> Text.translatable("commands.molehunt.timer.hide"), false); return Command.SINGLE_SUCCESS; }) @@ -174,7 +189,9 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; })); - command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { + 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(); } diff --git a/src/main/java/world/anhgelus/molehunt/config/Config.java b/src/main/java/world/anhgelus/molehunt/config/Config.java index e448f7d..08c3850 100644 --- a/src/main/java/world/anhgelus/molehunt/config/Config.java +++ b/src/main/java/world/anhgelus/molehunt/config/Config.java @@ -25,47 +25,47 @@ public class Config { } public int getGameDuration() { - return server.getGameRules().getInt(Molehunt.GAME_DURATION); + return server.getOverworld().getGameRules().getValue(Molehunt.GAME_DURATION); } public int getMolePercentage() { - return server.getGameRules().getInt(Molehunt.MOLE_PERCENTAGE); + return server.getOverworld().getGameRules().getValue(Molehunt.MOLE_PERCENTAGE); } public int getMoleCount() { - return server.getGameRules().getInt(Molehunt.MOLE_COUNT); + return server.getOverworld().getGameRules().getValue(Molehunt.MOLE_COUNT); } public boolean nametagsEnabled() { - return server.getGameRules().getBoolean(Molehunt.SHOW_NAMETAGS); + return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_NAMETAGS); } public boolean skinsEnabled() { - return server.getGameRules().getBoolean(Molehunt.SHOW_SKINS); + return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_SKINS); } public boolean tabEnabled() { - return server.getGameRules().getBoolean(Molehunt.SHOW_TAB); + return server.getOverworld().getGameRules().getValue(Molehunt.SHOW_TAB); } public int getInitialWorldSize() { - return server.getGameRules().getInt(Molehunt.INITIAL_WORLD_SIZE); + return server.getOverworld().getGameRules().getValue(Molehunt.INITIAL_WORLD_SIZE); } public int getFinalWorldSize() { - return server.getGameRules().getInt(Molehunt.FINAL_WORLD_SIZE); + return server.getOverworld().getGameRules().getValue(Molehunt.FINAL_WORLD_SIZE); } public int getBorderShrinkingStartingTimeOffset() { - return server.getGameRules().getInt(Molehunt.MOVING_STARTING_TIME_OFFSET); + return server.getOverworld().getGameRules().getValue(Molehunt.MOVING_STARTING_TIME_OFFSET); } public boolean portalsEnabled() { - return server.getGameRules().getBoolean(Molehunt.ENABLE_PORTALS); + return server.getOverworld().getGameRules().getValue(Molehunt.ENABLE_PORTALS); } public boolean foodOnStart() { - return server.getGameRules().getBoolean(Molehunt.FOOD_ON_START); + return server.getOverworld().getGameRules().getValue(Molehunt.FOOD_ON_START); } public static SimpleConfig configFile(String fileName) { diff --git a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java index 0123e74..b43bd1b 100644 --- a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java +++ b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java @@ -12,9 +12,9 @@ public record ConfigPayload(boolean showNametags, boolean showSkins, boolean sho public static final CustomPayload.Id ID = new CustomPayload.Id<>(CONFIG_PACKET_ID); public static final PacketCodec CODEC = PacketCodec.tuple( - PacketCodecs.BOOL, ConfigPayload::showNametags, - PacketCodecs.BOOL, ConfigPayload::showSkins, - PacketCodecs.BOOL, ConfigPayload::showTab, + PacketCodecs.BOOLEAN, ConfigPayload::showNametags, + PacketCodecs.BOOLEAN, ConfigPayload::showSkins, + PacketCodecs.BOOLEAN, ConfigPayload::showTab, ConfigPayload::new ); diff --git a/src/main/java/world/anhgelus/molehunt/game/Game.java b/src/main/java/world/anhgelus/molehunt/game/Game.java index 58ea4ee..117dbce 100644 --- a/src/main/java/world/anhgelus/molehunt/game/Game.java +++ b/src/main/java/world/anhgelus/molehunt/game/Game.java @@ -11,7 +11,7 @@ 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.GameRules; +import net.minecraft.world.rule.GameRules; import world.anhgelus.molehunt.Molehunt; import world.anhgelus.molehunt.utils.TimeUtils; @@ -53,12 +53,12 @@ public class Game { players.remove(r); } - final var gamerules = server.getGameRules(); + final var gamerules = server.getOverworld().getGameRules(); // immutable gamerules - gamerules.get(GameRules.SHOW_DEATH_MESSAGES).set(false, server); - gamerules.get(GameRules.ANNOUNCE_ADVANCEMENTS).set(false, server); + gamerules.setValue(GameRules.SHOW_DEATH_MESSAGES, false, server); + gamerules.setValue(GameRules.ANNOUNCE_ADVANCEMENTS, false, server); // gamerules for the start - gamerules.get(GameRules.DO_IMMEDIATE_RESPAWN).set(true, server); + gamerules.setValue(GameRules.DO_IMMEDIATE_RESPAWN, true, server); final var worldBorder = server.getOverworld().getWorldBorder(); worldBorder.setSize(Molehunt.CONFIG.getInitialWorldSize()); @@ -70,7 +70,9 @@ public class Game { worldBorder.interpolateSize( Molehunt.CONFIG.getInitialWorldSize(), Molehunt.CONFIG.getFinalWorldSize(), - (long) (Molehunt.CONFIG.getGameDuration() - Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset()) * 60 * 1000); + (long) (Molehunt.CONFIG.getGameDuration() - Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset()) * 60 * 1000, + 0L + ); } }, (long) Molehunt.CONFIG.getBorderShrinkingStartingTimeOffset() * 60 * 1000); } @@ -78,7 +80,7 @@ public class Game { final var title = new TitleS2CPacket(Text.translatable("molehunt.game.start.suspense")); playerManager.getPlayerList().forEach(p -> { p.getInventory().clear(); - p.kill(); + p.kill(p.getEntityWorld()); p.networkHandler.sendPacket(timing); p.networkHandler.sendPacket(title); p.changeGameMode(GameMode.SURVIVAL); @@ -105,7 +107,7 @@ public class Game { p.getHungerManager().setSaturationLevel(5.0f); }); // reset gamerules after the start - gamerules.get(GameRules.DO_IMMEDIATE_RESPAWN).set(false, server); + gamerules.setValue(GameRules.DO_IMMEDIATE_RESPAWN, false, server); // reset time and weather server.getOverworld().setTimeOfDay(0); server.getOverworld().resetWeather(); diff --git a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java index 4f7b8ce..66e3209 100644 --- a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java +++ b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java @@ -12,7 +12,7 @@ public record GamePayload(boolean gameLaunched) implements CustomPayload { public static final CustomPayload.Id ID = new CustomPayload.Id<>(GAME_PACKET_ID); public static final PacketCodec CODEC = PacketCodec.tuple( - PacketCodecs.BOOL, GamePayload::gameLaunched, + PacketCodecs.BOOLEAN, GamePayload::gameLaunched, GamePayload::new ); diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 81d7829..09a65fe 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -31,7 +31,7 @@ ], "depends": { "fabricloader": ">=${loader_version}", - "fabric": "*", + "fabric-api": "*", "minecraft": "${minecraft_version}" } } -- cgit v1.2.3