From 2023ae3cd11ca3182c288e760327086c81fadf85 Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Sun, 12 Feb 2023 21:04:04 -0500 Subject: [PATCH] Username command & fix commands a bit --- .../chipmunkmod/command/CommandManager.java | 4 +- .../chipmunkmod/commands/UsernameCommand.java | 67 +++++++++++++++++++ .../mixin/MinecraftClientAccessor.java | 16 +++++ src/main/resources/chipmunkmod.mixins.json | 3 +- 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java create mode 100644 src/main/java/land/chipmunk/chipmunkmod/mixin/MinecraftClientAccessor.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java index 1505c75..117b608 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java @@ -28,7 +28,8 @@ public class CommandManager { dispatcher.execute(command, commandSource); } catch (CommandSyntaxException e) { commandSource.sendError(Texts.toText(e.getRawMessage())); - commandSource.sendError(getContext(e)); + final Text context = getContext(e); + if (context != null) commandSource.sendError(context); } catch (CommandException e) { commandSource.sendError(e.getTextMessage()); } catch (RuntimeException e) { @@ -67,5 +68,6 @@ public class CommandManager { static { TestCommand.register(dispatcher); CoreCommand.register(dispatcher); + UsernameCommand.register(dispatcher); } } \ No newline at end of file diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java new file mode 100644 index 0000000..d9595cd --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/UsernameCommand.java @@ -0,0 +1,67 @@ +package land.chipmunk.chipmunkmod.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static land.chipmunk.chipmunkmod.command.CommandManager.literal; +import static land.chipmunk.chipmunkmod.command.CommandManager.argument; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ConnectScreen; +import net.minecraft.client.gui.screen.TitleScreen; +import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; +import net.minecraft.client.network.ServerInfo; +import net.minecraft.client.network.ServerAddress; +import net.minecraft.client.util.Session; +import net.minecraft.text.Text; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import java.util.Optional; +import land.chipmunk.chipmunkmod.mixin.MinecraftClientAccessor; + +public class UsernameCommand { + private static final Session ORIGINAL_SESSION = ((MinecraftClientAccessor) MinecraftClient.getInstance()).session(); + private static final SimpleCommandExceptionType USERNAME_TOO_LONG = new SimpleCommandExceptionType(Text.translatable("The specified username is longer than 16 characters")); + + public static void register (CommandDispatcher dispatcher) { + dispatcher.register( + literal("username") + .then( + literal("set") + .then( + argument("username", greedyString()) + .executes(c -> updateUsername(c)) + ) + ) + .then( + literal("revert") + .executes(c -> updateSession(c, ORIGINAL_SESSION)) + ) + ); + } + + public static int updateUsername (CommandContext context) throws CommandSyntaxException { + final String username = getString(context, "username"); + if (username.length() > 16) throw USERNAME_TOO_LONG.create(); + final Session session = new Session(username, "", "", Optional.empty(), Optional.empty(), Session.AccountType.MOJANG); + return updateSession(context, session); + } + + public static int updateSession (CommandContext context, Session session) throws CommandSyntaxException { + final FabricClientCommandSource source = context.getSource(); + + final MinecraftClient client = source.getClient(); + + ((MinecraftClientAccessor) client).session(session); + + // TODO: Put this in a separate class + final ServerInfo info = client.getCurrentServerEntry(); + client.world.disconnect(); + client.disconnect(); + ConnectScreen.connect(new MultiplayerScreen(new TitleScreen()), client, ServerAddress.parse(info.address), info); + + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/MinecraftClientAccessor.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/MinecraftClientAccessor.java new file mode 100644 index 0000000..9ea5de9 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/MinecraftClientAccessor.java @@ -0,0 +1,16 @@ +package land.chipmunk.chipmunkmod.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.gen.Accessor; +import net.minecraft.client.util.Session; + +@Mixin(net.minecraft.client.MinecraftClient.class) +public interface MinecraftClientAccessor { + @Accessor("session") + Session session (); + + @Mutable + @Accessor("session") + void session (Session session); +} diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index d3c7cf9..4156942 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -11,7 +11,8 @@ "ClientPlayNetworkHandlerAccessor", "ClientConnectionMixin", "ClientPlayerEntityMixin", - "ClientPlayNetworkHandlerMixin" + "ClientPlayNetworkHandlerMixin", + "MinecraftClientAccessor" ], "injectors": { "defaultRequire": 1