forked from ChomeNS/chipmunkmod
Username command & fix commands a bit
This commit is contained in:
parent
6b0014992c
commit
2023ae3cd1
4 changed files with 88 additions and 2 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<FabricClientCommandSource> 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<FabricClientCommandSource> 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<FabricClientCommandSource> 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -11,7 +11,8 @@
|
|||
"ClientPlayNetworkHandlerAccessor",
|
||||
"ClientConnectionMixin",
|
||||
"ClientPlayerEntityMixin",
|
||||
"ClientPlayNetworkHandlerMixin"
|
||||
"ClientPlayNetworkHandlerMixin",
|
||||
"MinecraftClientAccessor"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
|
Loading…
Reference in a new issue