diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..bccc8a9 --- /dev/null +++ b/changelog.md @@ -0,0 +1,8 @@ +# Changelog + +## 0.1 +- Added `/gm ` for changing gamemode. +- Added `/gm{0|s|survival}` for changing gamemode to Survival. +- Added `/gm{1|c|creative}` for changing gamemode to Creative. +- Added `/gm{2|a|adventure}` for changing gamemode to Adventure. +- Added `/gm{3|sp|spectator}` for changing gamemode to Spectator. diff --git a/gradle.properties b/gradle.properties index d239f3f..65af146 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.3 # Mod Properties - mod_version = 1.0.0 - maven_group = com.example - archives_base_name = fabric-example-mod + mod_version = 0.1 + maven_group = com.nixinova + archives_base_name = morecmds # Dependencies - # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api (or https://fabricmc.net/versions.html) + # Info: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api (or https://fabricmc.net/versions.html) fabric_version=0.34.2+1.16 diff --git a/readme.md b/readme.md index 93ab3a6..5a47d91 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,10 @@ # More Commands Mod Adds various useful commands to the game. + +## Commands +- `/gm `: Change gamemode. +- `/gms` & `/gm0`: Change gamemode to Survival. +- `/gmc` & `/gm1`: Change gamemode to Creative. +- `/gma` & `/gm2`: Change gamemode to Adventure. +- `/gmsp` & `/gm3`: Change gamemode to Spectator. diff --git a/src/main/java/com/nixinova/morecmds/Main.java b/src/main/java/com/nixinova/morecmds/Main.java index b6cf35f..94c69f3 100644 --- a/src/main/java/com/nixinova/morecmds/Main.java +++ b/src/main/java/com/nixinova/morecmds/Main.java @@ -1,10 +1,18 @@ package com.nixinova.morecmds; +import com.nixinova.morecmds.commands.Gamemode; + import net.fabricmc.api.ModInitializer; public class Main implements ModInitializer { + @Override public void onInitialize() { - System.out.println("Initialised"); + new Gamemode().register(); } + + public static void debug(String msg) { + System.out.println("MoreCMDs> " + msg); + } + } diff --git a/src/main/java/com/nixinova/morecmds/commands/Gamemode.java b/src/main/java/com/nixinova/morecmds/commands/Gamemode.java new file mode 100644 index 0000000..bb54f20 --- /dev/null +++ b/src/main/java/com/nixinova/morecmds/commands/Gamemode.java @@ -0,0 +1,78 @@ +package com.nixinova.morecmds.commands; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; +import static com.mojang.brigadier.arguments.StringArgumentType.string; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.nixinova.morecmds.Main; + +import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.world.GameMode; + +public class Gamemode { + + public void register() { + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { + // Generic + dispatcher.register(literal("gm").then( + argument("mode", string()).executes(this::generic) + )); + // Survival + dispatcher.register(literal("gms").executes(this::survival)); + dispatcher.register(literal("gm0").executes(this::survival)); + // Creative + dispatcher.register(literal("gmc").executes(this::creative)); + dispatcher.register(literal("gm1").executes(this::creative)); + // Adventure + dispatcher.register(literal("gma").executes(this::adventure)); + dispatcher.register(literal("gm2").executes(this::adventure)); + // Spectator + dispatcher.register(literal("gmsp").executes(this::spectator)); + dispatcher.register(literal("gm3").executes(this::spectator)); + }); + } + + private int generic(CommandContext context) throws CommandSyntaxException { + Main.debug("Command 'gm' activated"); + String input = StringArgumentType.getString(context, "mode"); + GameMode mode = null; + switch (input.toLowerCase()) { + case "survival": case "s": case "0": mode = GameMode.SURVIVAL; break; + case "creative": case "c": case "1": mode = GameMode.CREATIVE; break; + case "adventure": case "a": case "2": mode = GameMode.ADVENTURE; break; + case "spectator": case "sp": case "3": mode = GameMode.SPECTATOR; break; + } + if (mode == null) return -1; + context.getSource().getPlayer().setGameMode(mode); + return 1; + } + + private int survival(CommandContext context) throws CommandSyntaxException { + Main.debug("Command 'gms' activated"); + context.getSource().getPlayer().setGameMode(GameMode.SURVIVAL); + return 1; + } + + private int creative(CommandContext context) throws CommandSyntaxException { + Main.debug("Command 'gmc' activated"); + context.getSource().getPlayer().setGameMode(GameMode.CREATIVE); + return 1; + } + + private int adventure(CommandContext context) throws CommandSyntaxException { + Main.debug("Command 'gma' activated"); + context.getSource().getPlayer().setGameMode(GameMode.ADVENTURE); + return 1; + } + + private int spectator(CommandContext context) throws CommandSyntaxException { + Main.debug("Command 'gmsp' activated"); + context.getSource().getPlayer().setGameMode(GameMode.SPECTATOR); + return 1; + } + +}