Get commands to work
This commit is contained in:
parent
593ff01e3b
commit
e0eae0c3d1
5 changed files with 117 additions and 17 deletions
|
@ -0,0 +1,70 @@
|
||||||
|
package land.chipmunk.chipmunkmod.command;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.arguments.ArgumentType;
|
||||||
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import net.minecraft.command.CommandException;
|
||||||
|
import net.minecraft.text.ClickEvent;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.Texts;
|
||||||
|
import net.minecraft.text.MutableText;
|
||||||
|
import net.minecraft.util.Formatting;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||||
|
import land.chipmunk.chipmunkmod.commands.*;
|
||||||
|
|
||||||
|
public class CommandManager {
|
||||||
|
public static CommandDispatcher<FabricClientCommandSource> dispatcher = new CommandDispatcher();
|
||||||
|
public static String prefix = ".";
|
||||||
|
|
||||||
|
public static void executeCommand (String command) {
|
||||||
|
final MinecraftClient client = MinecraftClient.getInstance();
|
||||||
|
|
||||||
|
final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource();
|
||||||
|
|
||||||
|
try {
|
||||||
|
dispatcher.execute(command, commandSource);
|
||||||
|
} catch (CommandSyntaxException e) {
|
||||||
|
commandSource.sendError(Texts.toText(e.getRawMessage()));
|
||||||
|
commandSource.sendError(getContext(e));
|
||||||
|
} catch (CommandException e) {
|
||||||
|
commandSource.sendError(e.getTextMessage());
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
commandSource.sendError(Text.of(e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Text getContext (CommandSyntaxException exception) {
|
||||||
|
final int _cursor = exception.getCursor();
|
||||||
|
final String input = exception.getInput();
|
||||||
|
|
||||||
|
if (input == null || _cursor < 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final MutableText text = Text.literal("")
|
||||||
|
.formatted(Formatting.GRAY);
|
||||||
|
text.setStyle(text.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, prefix + input)));
|
||||||
|
|
||||||
|
final int cursor = Math.min(input.length(), _cursor);
|
||||||
|
|
||||||
|
if (cursor > CommandSyntaxException.CONTEXT_AMOUNT) {
|
||||||
|
text.append(Text.literal("..."));
|
||||||
|
}
|
||||||
|
|
||||||
|
text
|
||||||
|
.append(Text.literal(input.substring(Math.max(0, cursor - CommandSyntaxException.CONTEXT_AMOUNT), cursor)))
|
||||||
|
.append(Text.literal(input.substring(cursor)).formatted(Formatting.RED, Formatting.UNDERLINE))
|
||||||
|
.append(Text.translatable("command.context.here").formatted(Formatting.RED, Formatting.ITALIC));
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LiteralArgumentBuilder<FabricClientCommandSource> literal (String name) { return LiteralArgumentBuilder.<FabricClientCommandSource>literal(name); }
|
||||||
|
public static <T> RequiredArgumentBuilder<FabricClientCommandSource, T> argument (String name, ArgumentType<T> type) { return RequiredArgumentBuilder.<FabricClientCommandSource, T>argument(name, type); }
|
||||||
|
|
||||||
|
static {
|
||||||
|
TestCommand.register(dispatcher);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package land.chipmunk.chipmunkmod.commands;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.Command;
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
import static land.chipmunk.chipmunkmod.command.CommandManager.literal;
|
||||||
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
public class TestCommand {
|
||||||
|
public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher) {
|
||||||
|
dispatcher.register(
|
||||||
|
literal("test")
|
||||||
|
.executes(c -> helloWorld(c))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int helloWorld (CommandContext<FabricClientCommandSource> context) {
|
||||||
|
final FabricClientCommandSource source = context.getSource();
|
||||||
|
source.sendFeedback(Text.literal("Hello, world!"));
|
||||||
|
|
||||||
|
return Command.SINGLE_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package land.chipmunk.chipmunkmod.mixin;
|
||||||
|
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import land.chipmunk.chipmunkmod.command.CommandManager;
|
||||||
|
|
||||||
|
@Mixin(net.minecraft.client.gui.screen.ChatScreen.class)
|
||||||
|
public class ChatScreenMixin {
|
||||||
|
@Inject(at = @At("HEAD"), method = "sendMessage", cancellable = true)
|
||||||
|
public void sendMessage(String chatText, boolean addToHistory, CallbackInfoReturnable<Boolean> cir) {
|
||||||
|
if (chatText.startsWith(CommandManager.prefix)) {
|
||||||
|
CommandManager.executeCommand(chatText.substring(CommandManager.prefix.length()));
|
||||||
|
|
||||||
|
if (addToHistory) MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(chatText);
|
||||||
|
|
||||||
|
cir.setReturnValue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +0,0 @@
|
||||||
package land.chipmunk.chipmunkmod.mixin;
|
|
||||||
|
|
||||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
|
||||||
import net.minecraft.client.gui.screen.TitleScreen;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
@Mixin(TitleScreen.class)
|
|
||||||
public class ExampleMixin {
|
|
||||||
@Inject(at = @At("HEAD"), method = "init()V")
|
|
||||||
private void init(CallbackInfo info) {
|
|
||||||
ChipmunkMod.LOGGER.info("This line is printed by an example mod mixin!");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@
|
||||||
"mixins": [
|
"mixins": [
|
||||||
],
|
],
|
||||||
"client": [
|
"client": [
|
||||||
"ExampleMixin"
|
"ChatScreenMixin"
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": 1
|
"defaultRequire": 1
|
||||||
|
|
Loading…
Reference in a new issue