CottonMC / ClientCommands

A Minecraft mod that adds support for client-side commands.
MIT License
21 stars 7 forks source link

Change Prefix #14

Closed lasnikr closed 4 years ago

lasnikr commented 4 years ago

Is there any way of making my own prefix for this libary? I want to have a custom client and no "/" but for example a "#"

Juuxel commented 4 years ago

There's no built-in way as CCC is meant to only work with the / prefix.

lasnikr commented 4 years ago

Could I make mixin anywhere to get it working with other?

Leo40Git commented 4 years ago

Theoretically, you could mixin here: https://github.com/CottonMC/ClientCommands/blob/master/src/main/java/io/github/cottonmc/clientcommands/mixin/PlayerMixin.java#L35 The problem is that this will affect all mods that use Client Commands.

lasnikr commented 4 years ago

But it is not working for me

Juuxel commented 4 years ago

You can't mixin to mixins. As for changing the prefix, I don't think this functionality belongs to CCC itself since client commands are meant to look the same as server ones.

kotx commented 4 years ago

You can't mixin to mixins. As for changing the prefix, I don't think this functionality belongs to CCC itself since client commands are meant to look the same as server ones.

It would be nice to have this functionality though. Is this a limitation of Brigadier or Minecraft itself? I don't see why we should disallow this because of an assumed use case. Also, missing client commands would result in the command being sent to the server. This isn't desirable some of the time and would require changing a command prefix to fix.

Juuxel commented 4 years ago

It's an intentional limitation of CCC as there's no clean way to register multiple prefixes.

kotx commented 4 years ago

Well that sucks. I guess I'll have to make my own implementation 😭

lasnikr commented 4 years ago

Could I somehow support you with this or could you send me an update on this when you finished it?

kotx commented 4 years ago

Could I somehow support you with this or could you send me an update on this when you finished it?

There's a chance I won't make it but I'll update you, sure. Not very experienced with Fabric or even Java for that matter.

lasnikr commented 4 years ago

You could add me on dc: Lasniker#0294

kotx commented 4 years ago

It's an intentional limitation of CCC as there's no clean way to register multiple prefixes.

Out of curiosity could you elaborate on what you meant by "no clean way"? What would be required that would "dirty" the codebase?

Juuxel commented 4 years ago

The Brigadier command dispatcher has no concept of a prefix, so there would have to be a Map<Character, CommandDispatcher> to hold all the different command dispatchers for the different prefixes. This would basically require rewriting ClientCommandPlugin as well.

Frontrider commented 4 years ago

Personally, I do not see any true benefit to doing this. The only thing it avoids is duplicating commands, in which case both of you do the same thing and one of them is redundant, or one of you should rename the command for clarity.

And then it also has a side effect where the user has to remember 20 different prefixes, as it makes that look like a good practice.

kotx commented 4 years ago

Personally, I do not see any true benefit to doing this. The only thing it avoids is duplicating commands, in which case both of you do the same thing and one of them is redundant, or one of you should rename the command for clarity.

And then it also has a side effect where the user has to remember 20 different prefixes, as it makes that look like a good practice.

Client-only mods would benefit from this: This would allow mods to separate vanilla commands from mod commands (e.g. baritone commands use # prefix). Commands with the default / prefix would also get sent to the server if missing, which isn't desirable for some client-only mods. Different prefixes also reduces the chance of command collisions- you said that one mod should rename the command, but communication between mod developers isn't always that smooth. At the very least it reduces the likelihood of a collision if two mod developers are uncooperative.

Those are just my thoughts on the matter, whether or not CCC implements this is different.

kotx commented 4 years ago

@lasnikprogram I've come up with a very rudimentary solution 😛

If it's stupid and it works, it ain't stupid

I got custom prefixes by copying the Client Commands files to my src/main/java directory So: src/main/java/io/github/cottonmc/clientcommands/* and adding the mixins in assets/clientcommands.mixins.json:

{
  "required": true,
  "package": "io.github.cottonmc.clientcommands.mixin",
  "compatibilityLevel": "JAVA_8",
  "client": [
    "ClientCommandSourceMixin",
    "PlayerMixin"
  ],
  "injectors": {
    "defaultRequire": 1
  }
}

I also had to add clientcommands.mixins.json file into fabric.mod.json:

"mixins": [
    "clientcommands.mixins.json"
  ],

I deleted NetworkHandlerMixin and changed 3 lines in PlayerMixin to support the custom prefix:

if (!msg.startsWith(MyMod.getInstance().getSettings().getPrefix())) return; // <-- this line
if (!CommandCache.hasCommand(msg.substring(MyMod.getInstance().getSettings().getPrefix().length()).split(" ")[0])) return; // <-- this line
boolean cancel = false;
try {
    // The game freezes when using heavy commands. Run your heavy code somewhere else pls
    int result = CommandCache.execute(
        msg.substring(MyMod.getInstance().getSettings().getPrefix().length()), (CottonClientCommandSource) new ClientCommandSource(networkHandler, client) // <-- this line
    );
...

Obviously change it to however you obtain a custom prefix, so don't copy MyMod.getInstance().getSettings().getPrefix(). Once again this is a very rudimentary solution so don't count on it.

kotx commented 4 years ago

I'm actually not sure if this will conflict with other mods using CCC as its still under the same package name, but I don't expect much compatibility for my mod anyways. Hopefully it doesn't conflict since they're different JARs...