From 9cebad9146ab7753ed70561b2caefb37f3aa3e06 Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Thu, 4 Jul 2024 10:01:20 +0200 Subject: [PATCH] feat: Add /balm dev command to quickly change game rules to not be annoying during dev --- .../mods/balm/common/command/BalmCommand.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java b/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java index b72af985..02a84f4a 100644 --- a/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java +++ b/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java @@ -8,13 +8,42 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; +import net.minecraft.world.Difficulty; +import net.minecraft.world.level.GameRules; import java.io.File; +import java.util.Objects; public class BalmCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("balm") + .then(Commands.literal("dev").requires(BalmCommand::mayEnableDevMode).executes(context -> { + final var source = context.getSource(); + final var server = source.getServer(); + final var gameRules = server.getGameRules(); + gameRules.getRule(GameRules.RULE_DAYLIGHT).set(false, server); + source.sendSuccess(() -> Component.literal("Daylight cycle disabled"), true); + gameRules.getRule(GameRules.RULE_WEATHER_CYCLE).set(false, server); + source.sendSuccess(() -> Component.literal("Weather cycle disabled"), true); + gameRules.getRule(GameRules.RULE_KEEPINVENTORY).set(true, server); + source.sendSuccess(() -> Component.literal("Keep Inventory enabled"), true); + gameRules.getRule(GameRules.RULE_DOINSOMNIA).set(false, server); + source.sendSuccess(() -> Component.literal("Insomnia disabled"), true); + gameRules.getRule(GameRules.RULE_MOBGRIEFING).set(false, server); + source.sendSuccess(() -> Component.literal("Mob Griefing disabled"), true); + gameRules.getRule(GameRules.RULE_DO_TRADER_SPAWNING).set(false, server); + source.sendSuccess(() -> Component.literal("Trader Spawning disabled"), true); + server.setDifficulty(Difficulty.PEACEFUL, true); + source.sendSuccess(() -> Component.literal("Difficulty set to Peaceful"), true); + server.overworld().setWeatherParameters(99999, 0, false, false); + source.sendSuccess(() -> Component.literal("Weather cleared"), true); + for (final var level : server.getAllLevels()) { + level.setDayTime(1000); + } + source.sendSuccess(() -> Component.literal("Set the time to Daytime"), true); + return 0; + })) .then(Commands.literal("export") .then(Commands.literal("config").then(Commands.argument("class", StringArgumentType.greedyString()).executes(context -> { final var className = context.getArgument("class", String.class); @@ -48,4 +77,19 @@ public static void register(CommandDispatcher dispatcher) { }))))); } + private static boolean mayEnableDevMode(CommandSourceStack it) { + if (it.hasPermission(2)) { + return true; + } + + final var server = it.getServer(); + if (server.isSingleplayer()) { + final var player = it.getPlayer(); + if (player != null) { + return Objects.equals(player.getGameProfile(), server.getSingleplayerProfile()); + } + } + + return false; + } }