#24w33a - register command without fabric api (server-side)

62 messages · Page 1 of 1 (latest)

tardy ginkgo
#

How could I register a command in the new snapshot 24w33a, since fabric API doesnt exist for that version, i tried doing

package app.qwertz;

import app.qwertz.commands.CommandRegistrationEvent;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import com.mojang.brigadier.CommandDispatcher;

public class QWERTZpixelCore implements ModInitializer {
    @Override
    public void onInitialize() {
        System.out.println("QWERTZpixelCore is initializing!");
        registerCommands();
    }

    private void registerCommands() {
        CommandRegistrationEvent.register((dispatcher, environment) -> {
            dispatcher.register(CommandManager.literal("mods").executes(context -> {
                ServerCommandSource source = context.getSource();
                StringBuilder modList = new StringBuilder("Server-side mods:\n");

FabricLoader.getInstance().getAllMods().forEach(mod -> {
                    modList.append("- ")
                            .append(mod.getMetadata().getName())
                            .append(" (")
                            .append(mod.getMetadata().getVersion().getFriendlyString())
                            .append(")\n");
                });

                source.sendFeedback(() -> Text.literal(modList.toString()).formatted(Formatting.GREEN), false);
                return 1;
            }));
        });
    }
}```

Server started up with no errors, but command wasnt there,  (server-side-only mod), this is my CommandRegistrationEvent class (took a little inspiration from https://github.com/FabricMC/fabric/blob/1.21.1/fabric-command-api-v2/src/main/java/net/fabricmc/fabric/api/command/v2/CommandRegistrationCallback.java)

```java
package app.qwertz.commands;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

import java.util.ArrayList;
import java.util.List;

public class CommandRegistrationEvent {
    private static final List<CommandRegistrationCallback> callbacks = new ArrayList<>();

    public static void register(CommandRegistrationCallback callback) {
        callbacks.add(callback);
    }

    public static void invoke(CommandDispatcher<ServerCommandSource> dispatcher, CommandManager.RegistrationEnvironment environment) {
        for (CommandRegistrationCallback callback : callbacks) {
            callback.register(dispatcher, environment);
        }
    }
}```
and the interface:
```java
package app.qwertz.commands;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;

public interface CommandRegistrationCallback {
    void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandManager.RegistrationEnvironment environment);
}```
rugged widget
tardy ginkgo
rugged widget
#

You might be fine building it yourself and shading the command module, alternatively copy-paste the relevant impl code

tardy ginkgo
#

on the fork

rugged widget
#

It looks like the command-api code hasn't been touched at all, yeah, maybe the 1.21.1 version just works?

tardy ginkgo
rugged widget
#

The mixins are kinda integral to making the module work

tardy ginkgo
#

@rugged widget do you think the new official loader API jar will release soon?

rugged widget
#

I'd do the following:

  1. Clone https://github.com/FabricMC/fabric/pull/4033
  2. Build and publish locally to your machine via ./gradlew :fabric-command-api-v2:publishToMavenLocal (and maybe its dependencies as well, like lifecycles and base, not sure if Gradle does that automatically)
  3. Add mavenLocal() to the top of your own project's repositories block
  4. Depend on the specific modules only, not the whole FAPI package
tardy ginkgo
rugged widget
rugged widget
rugged widget
#

You need to add each module manually, directly depending on a specific module doesn't pull in its dependencies unfortunately

#

From what I can see, you need fabric-api-base, fabric-lifecycle-events-v1 and fabric-command-api-v2

tardy ginkgo
#

alright

#

thanks for pointing that out

#

@rugged widget one last thing, after i published to mavenlocal and added it to my repo block, how do i access the fabric api in the code?

rugged widget
#

Ah, now that you mention it, I think fabricApi.module doesn't work for local FAPI builds

tardy ginkgo
rugged widget
#

You need ```gradle
modImplementation("net.fabricmc.fabric-api:fabric-api-base:0.4.42+50d9c33b19")
modImplementation("net.fabricmc.fabric-api:fabric-lifecycle-events-v1:2.3.12+50d9c33b19")
modImplementation("net.fabricmc.fabric-api:fabric-command-api-v2:2.2.28+6ced4dd919")

#

Might need to replace the hashes, not sure if they're machine-dependent They're not

tardy ginkgo
#

it worked 😄

#

thank you so much @rugged widget !

rugged widget
#

Nice :D

tardy ginkgo
rugged widget
#

Oh no 😅

tardy ginkgo
#

i need to clean it up somehow

rugged widget
#

The important thing is it works, can always replace it with the official API when the port releases (hopefully next week)

tardy ginkgo
#

probably not

rugged widget
#

You can bundle the modules for now (via include)

tardy ginkgo
#

ohh ok

#

it is not possible to have multiple mixin files, right? it will always load the one with the mod id name

#

@rugged widget

rugged widget
#

It will load the one referenced in your fabric.mod.json (doesn't necessarily have to start with your mod-id)

tardy ginkgo
#

oh i see

#

its an array

#

means i can specify more than 1?

rugged widget
#

I'm actually not sure if you can have multiple ones per FMJ, I suppose yes (since the mixins field is an array

tardy ginkgo
#

i will try

rugged widget
#

Why though?

tardy ginkgo
#

one mixin for my project

rugged widget
#

Ah, since you have them in different packages, makes sense

tardy ginkgo
#

mixin*

rugged widget
tardy ginkgo
#

oh wow, it worked

#

i removed all commands from vanilla

tardy ginkgo
rugged widget
#

Ah, a fellow German, I see ;)

tardy ginkgo
#

yup

rugged widget
#

Sollte selber auch langsam ins Bett, gute Nacht! ^^

tardy ginkgo
#

gute nacht