Skip to content
This repository was archived by the owner on Feb 28, 2021. It is now read-only.

Commit 9dbba9d

Browse files
committed
Initial update to 1.14, rework API
1 parent a383568 commit 9dbba9d

File tree

9 files changed

+80
-45
lines changed

9 files changed

+80
-45
lines changed

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<img src="icon.png" align="right" width="180px"/>
22

3-
# Client Commands
3+
# Cotton Client Commands
4+
5+
[![Maven metadata URL](https://img.shields.io/maven-metadata/v/http/server.bbkr.space:8081/artifactory/libs-snapshot/io/github/cottonmc/client-commands/maven-metadata.xml.svg)](http://server.bbkr.space:8081/artifactory/libs-snapshot/io/github/cottonmc/client-commands)
46

57
[>> Downloads <<](https://github.com/CottonMC/ClientCommands/releases)
68

@@ -21,27 +23,39 @@ repositories {
2123
}
2224
2325
dependencies {
24-
modCompile "io.github.cottonmc:client-commands:0.2.1+1.14-pre5-SNAPSHOT"
26+
modCompile "io.github.cottonmc:client-commands:0.3.0+1.14-SNAPSHOT"
2527
}
2628
```
2729

28-
Register the commands in a `ClientModInitializer`:
30+
Register the commands in a `CommandProvider`:
2931

3032
```java
31-
public class ExampleMod implements ClientModInitializer {
32-
@Override
33-
public void onInitializeClient() {
34-
ClientCommands.registerCommand(dispatcher -> {
35-
dispatcher.register(ArgumentBuilders.literal("client-commands").executes(
36-
source -> {
37-
Feedback.sendFeedback(new StringTextComponent("Hello, world!"));
38-
return 1;
39-
}
40-
));
41-
});
33+
import com.mojang.brigadier.CommandDispatcher;
34+
import io.github.cottonmc.clientcommands.CommandProvider;
35+
import net.minecraft.server.command.CommandSource;
36+
37+
public class MyCommands implements CommandProvider {
38+
@Override
39+
public void registerCommands(CommandDispatcher<CommandSource> dispatcher) {
40+
dispatcher.register(ArgumentBuilders.literal("client-commands").executes(
41+
source -> {
42+
Feedback.sendFeedback(new StringTextComponent("Hello, world!"));
43+
return 1;
44+
}
45+
));
4246
}
4347
}
4448
```
4549

50+
And register the `CommandProvider` in your `fabric.mod.json`:
51+
52+
```json
53+
{
54+
"entrypoints": {
55+
"cotton-client-commands": ["path.to.MyCommands"]
56+
}
57+
}
58+
```
59+
4660
The classes `ArgumentBuilders` and `Feedback` are provided as
4761
alternatives to `CommandManager` and the feedback methods in `ServerCommandSource`.

project.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
ext {
2-
projectName = "client-commands"
2+
projectName = "cotton-client-commands"
33
group = "io.github.cottonmc"
4-
version = "0.2.1+1.14-pre5"
4+
version = "0.3.0+1.14"
55
snapshot = true
66

7-
minecraft = "1.14 Pre-Release 5"
8-
mappings = "1.14 Pre-Release 5+build.4"
9-
loader = "0.4.2+build.130"
7+
minecraft = "1.14"
8+
mappings = "1.14+build.3"
9+
loader = "0.4.4+build.139"
1010
fabricMod = null
1111
silkMod = null
1212
jankson = null
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
package io.github.cottonmc.clientcommands;
22

3-
import com.mojang.brigadier.CommandDispatcher;
4-
import net.minecraft.server.command.CommandSource;
3+
import net.fabricmc.loader.api.FabricLoader;
54

6-
import java.util.Collection;
7-
import java.util.Collections;
8-
import java.util.HashSet;
9-
import java.util.Set;
10-
import java.util.function.Consumer;
5+
import java.util.List;
116

127
public final class ClientCommands {
13-
private static final Set<Consumer<CommandDispatcher<CommandSource>>> commands = new HashSet<>();
8+
public static final String ENTRYPOINT_TYPE = "cotton-client-commands";
149

15-
public static Collection<Consumer<CommandDispatcher<CommandSource>>> getCommands() {
16-
return Collections.unmodifiableSet(commands);
17-
}
18-
19-
/**
20-
* Registers a function that adds client-side commands.
21-
* The provided {@link CommandDispatcher} will exist only on the client.
22-
*
23-
* @param command the function
24-
*/
25-
public static void registerCommand(Consumer<CommandDispatcher<CommandSource>> command) {
26-
// TODO: (Maybe) error if a common/dedicated command with the same base is already registered
27-
commands.add(command);
10+
public static List<CommandProvider> getCommandProviders() {
11+
return FabricLoader.getInstance().getEntrypoints(ENTRYPOINT_TYPE, CommandProvider.class);
2812
}
2913
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.cottonmc.clientcommands;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import net.minecraft.server.command.CommandSource;
5+
6+
public interface CommandProvider {
7+
void registerCommands(CommandDispatcher<CommandSource> dispatcher);
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.cottonmc.clientcommands.impl;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import io.github.cottonmc.clientcommands.ClientCommands;
5+
import net.fabricmc.api.EnvType;
6+
import net.fabricmc.api.Environment;
7+
import net.minecraft.server.command.CommandSource;
8+
9+
import java.util.Collections;
10+
11+
@Environment(EnvType.CLIENT)
12+
public final class CommandCache {
13+
private CommandCache() {}
14+
15+
private static final CommandDispatcher<CommandSource> DISPATCHER = new CommandDispatcher<>();
16+
17+
public static void build() {
18+
ClientCommands.getCommandProviders().forEach(provider -> provider.registerCommands(DISPATCHER));
19+
}
20+
21+
public static boolean hasCommand(String name) {
22+
return DISPATCHER.findNode(Collections.singleton(name)) != null;
23+
}
24+
}

src/main/java/io/github/cottonmc/clientcommands/mixin/NetworkHandlerMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.authlib.GameProfile;
44
import com.mojang.brigadier.CommandDispatcher;
55
import io.github.cottonmc.clientcommands.ClientCommands;
6+
import io.github.cottonmc.clientcommands.impl.CommandCache;
67
import net.minecraft.client.MinecraftClient;
78
import net.minecraft.client.gui.Screen;
89
import net.minecraft.client.network.ClientPlayNetworkHandler;
@@ -28,10 +29,11 @@ private void onCommandTree(CommandTreeS2CPacket packet, CallbackInfo info) {
2829
@Inject(method = "<init>", at = @At("RETURN"))
2930
private void onConstruct(MinecraftClient mc, Screen screen, ClientConnection cc, GameProfile gp, CallbackInfo info) {
3031
addCommands();
32+
CommandCache.build();
3133
}
3234

3335
@Unique
3436
private void addCommands() {
35-
ClientCommands.getCommands().forEach(c -> c.accept(commandDispatcher));
37+
ClientCommands.getCommandProviders().forEach(c -> c.registerCommands(commandDispatcher));
3638
}
3739
}

src/main/java/io/github/cottonmc/clientcommands/mixin/PlayerMixin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.cottonmc.clientcommands.mixin;
22

33
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4+
import io.github.cottonmc.clientcommands.impl.CommandCache;
45
import net.minecraft.client.MinecraftClient;
56
import net.minecraft.client.network.ClientCommandSource;
67
import net.minecraft.client.network.ClientPlayerEntity;
@@ -25,6 +26,8 @@ public abstract class PlayerMixin {
2526
@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
2627
private void onChatMessage(String msg, CallbackInfo info) {
2728
if (msg.length() < 2 || !msg.startsWith("/")) return;
29+
if (!CommandCache.hasCommand(msg.substring(1).split(" ")[0])) return;
30+
// TODO: Test
2831
boolean cancel = false;
2932
try {
3033
// The game freezes when using heavy commands. Run your heavy code somewhere else pls

src/main/resources/fabric.mod.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"schemaVersion": 1,
3-
"id": "clientcommands",
4-
"name": "Client Commands",
5-
"description": "Allows purely client-side commands.",
3+
"id": "cotton-client-commands",
4+
"name": "Cotton Client Commands",
5+
"description": "An API for purely client-side commands.",
66
"version": "$version",
77
"environment": "client",
88
"license": "MIT",
@@ -15,7 +15,7 @@
1515
"fabricloader": ">=0.4.0"
1616
},
1717
"mixins": [
18-
"mixins.client-commands.json"
18+
"mixins.cotton-client-commands.json"
1919
],
2020
"icon": "icon.png",
2121
"custom": {
File renamed without changes.

0 commit comments

Comments
 (0)