Skip to content

Commit

Permalink
Add gamemode alias commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed May 30, 2021
1 parent 9ffed78 commit edcf646
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

## 0.1
- Added `/gm <gamemode>` 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.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# More Commands Mod

Adds various useful commands to the game.

## Commands
- `/gm <gamemode>`: 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.
10 changes: 9 additions & 1 deletion src/main/java/com/nixinova/morecmds/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
78 changes: 78 additions & 0 deletions src/main/java/com/nixinova/morecmds/commands/Gamemode.java
Original file line number Diff line number Diff line change
@@ -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<ServerCommandSource> 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<ServerCommandSource> context) throws CommandSyntaxException {
Main.debug("Command 'gms' activated");
context.getSource().getPlayer().setGameMode(GameMode.SURVIVAL);
return 1;
}

private int creative(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Main.debug("Command 'gmc' activated");
context.getSource().getPlayer().setGameMode(GameMode.CREATIVE);
return 1;
}

private int adventure(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Main.debug("Command 'gma' activated");
context.getSource().getPlayer().setGameMode(GameMode.ADVENTURE);
return 1;
}

private int spectator(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Main.debug("Command 'gmsp' activated");
context.getSource().getPlayer().setGameMode(GameMode.SPECTATOR);
return 1;
}

}

0 comments on commit edcf646

Please sign in to comment.