Added commands

This commit is contained in:
Patrick
2026-06-21 00:39:17 +02:00
parent 4a00e8fa5d
commit 0f890186dd
10 changed files with 174 additions and 1 deletions
@@ -1,5 +1,6 @@
package de.winniepat.parrotmod; package de.winniepat.parrotmod;
import de.winniepat.parrotmod.command.CommandManager;
import de.winniepat.parrotmod.config.ConfigManager; import de.winniepat.parrotmod.config.ConfigManager;
import de.winniepat.parrotmod.discord.DiscordRPCManager; import de.winniepat.parrotmod.discord.DiscordRPCManager;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
@@ -19,6 +20,7 @@ public class Parrotmod implements ModInitializer {
public void onInitialize() { public void onInitialize() {
ConfigManager.load(); ConfigManager.load();
DiscordRPCManager.init(); DiscordRPCManager.init();
CommandManager.init();
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> DiscordRPCManager.shutdown()); ClientLifecycleEvents.CLIENT_STOPPING.register(client -> DiscordRPCManager.shutdown());
@@ -0,0 +1,9 @@
package de.winniepat.parrotmod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.commands.CommandBuildContext;
public interface Command {
void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandBuildContext registryAccess);
}
@@ -0,0 +1,19 @@
package de.winniepat.parrotmod.command;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import java.util.ArrayList;
import java.util.List;
public class CommandManager {
private static final List<Command> COMMANDS = new ArrayList<>();
public static void init() {
COMMANDS.add(new ParrotModCommand());
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
for (Command command : COMMANDS) {
command.register(dispatcher, registryAccess);
}
});
}
}
@@ -0,0 +1,39 @@
package de.winniepat.parrotmod.command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.winniepat.parrotmod.command.subcommands.InfoCommand;
import de.winniepat.parrotmod.command.subcommands.ServerCommand;
import de.winniepat.parrotmod.command.subcommands.SettingsCommand;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.network.chat.Component;
import java.util.ArrayList;
import java.util.List;
public class ParrotModCommand implements Command {
private final List<SubCommand> subCommands = new ArrayList<>();
public ParrotModCommand() {
subCommands.add(new SettingsCommand());
subCommands.add(new InfoCommand());
subCommands.add(new ServerCommand());
}
@Override
public void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandBuildContext registryAccess) {
LiteralArgumentBuilder<FabricClientCommandSource> builder = LiteralArgumentBuilder.literal("parrotmod");
for (SubCommand subCommand : subCommands) {
subCommand.build(builder, registryAccess);
}
builder.executes(context -> {
context.getSource().sendFeedback(Component.literal("ParrotMod is active! Use /parrotmod settings to open the configuration."));
return 1;
});
dispatcher.register(builder);
}
}
@@ -0,0 +1,9 @@
package de.winniepat.parrotmod.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.commands.CommandBuildContext;
public interface SubCommand {
void build(LiteralArgumentBuilder<FabricClientCommandSource> builder, CommandBuildContext registryAccess);
}
@@ -0,0 +1,19 @@
package de.winniepat.parrotmod.command.subcommands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.winniepat.parrotmod.command.SubCommand;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.network.chat.Component;
public class InfoCommand implements SubCommand {
@Override
public void build(LiteralArgumentBuilder<FabricClientCommandSource> builder, CommandBuildContext registryAccess) {
builder.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("info")
.executes(context -> {
context.getSource().sendFeedback(Component.literal("ParrotMod - A utility mod for Minecraft."));
return 1;
})
);
}
}
@@ -0,0 +1,56 @@
package de.winniepat.parrotmod.command.subcommands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.winniepat.parrotmod.command.SubCommand;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.resolver.ServerAddress;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.network.chat.Component;
import net.minecraft.server.ServerInfo;
import net.minecraft.world.Difficulty;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.attribute.EnvironmentAttribute;
import net.minecraft.world.attribute.EnvironmentAttributes;
import net.minecraft.world.level.dimension.DimensionType;
public class ServerCommand implements SubCommand {
@Override
public void build(LiteralArgumentBuilder<FabricClientCommandSource> builder, CommandBuildContext registryAccess) {
builder.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("server")
.executes(context -> {
Minecraft mc = Minecraft.getInstance();
ServerData serverData = mc.getCurrentServer();
if (serverData != null) {
context.getSource().sendFeedback(Component.literal("IP: " + serverData.ip));
context.getSource().sendFeedback(Component.literal("Port: " + getPort(serverData)));
context.getSource().sendFeedback(Component.literal("Motd:"));
context.getSource().sendFeedback(Component.literal(serverData.motd.getString()));
context.getSource().sendFeedback(Component.literal("Version: " + serverData.version.getString()));
context.getSource().sendFeedback(Component.literal("Protocol Version" + serverData.protocol));
String difficulty = mc.level.getDifficulty().getDisplayName().getString();
Difficulty diff = new DifficultyInstance(
mc.level.getDifficulty(),
mc.level.getGameTime(),
mc.level.getChunk(mc.player.blockPosition()).getInhabitedTime(),
DimensionType.MOON_BRIGHTNESS_PER_PHASE[mc.level.environmentAttributes().getValue(EnvironmentAttributes.MOON_PHASE, mc.player.blockPosition()).index()]
).getDifficulty();
context.getSource().sendFeedback(Component.literal("Difficulty: " + difficulty + " (Local: " + diff.getDisplayName().getString() + ")"));
context.getSource().sendFeedback(Component.literal("Day: " + mc.level.getGameTime() / 24000L));
}
return 1;
})
);
}
public int getPort(ServerData serverData) {
String rawAddress = serverData.ip;
ServerAddress address = ServerAddress.parseString(rawAddress);
return address.getPort();
}
}
@@ -0,0 +1,20 @@
package de.winniepat.parrotmod.command.subcommands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.winniepat.parrotmod.command.SubCommand;
import de.winniepat.parrotmod.ui.SettingsFragment;
import icyllis.modernui.mc.MuiModApi;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.commands.CommandBuildContext;
public class SettingsCommand implements SubCommand {
@Override
public void build(LiteralArgumentBuilder<FabricClientCommandSource> builder, CommandBuildContext registryAccess) {
builder.then(LiteralArgumentBuilder.<FabricClientCommandSource>literal("settings")
.executes(context -> {
MuiModApi.get().openScreen(new SettingsFragment());
return 1;
})
);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 659 KiB

+1 -1
View File
@@ -7,7 +7,7 @@
"authors": [], "authors": [],
"contact": {}, "contact": {},
"license": "All-Rights-Reserved", "license": "All-Rights-Reserved",
"icon": "assets/parrotmod/textures/gui/sprites/icon.png", "icon": "assets/parrotmod/icon.png",
"environment": "*", "environment": "*",
"entrypoints": { "entrypoints": {
"main": [ "main": [