Skip to content

Commit dd34091

Browse files
committed
HubCommandBack works
1 parent 28f1f4e commit dd34091

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ org.gradle.parallel=true
99
loader_version=0.15.3
1010

1111
# Mod Properties
12-
mod_version = 1.6.0
12+
mod_version = 1.6.1
1313
maven_group = com.udu3324
1414
archives_base_name = poinpow
1515

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.udu3324.poinpow.mixin;
2+
3+
import com.udu3324.poinpow.utils.HubCommandBack;
4+
import net.minecraft.client.gui.screen.ChatScreen;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
9+
10+
@Mixin(ChatScreen.class)
11+
public class ClientChatMixin {
12+
@Inject(method = "sendMessage", at = @At("HEAD"), cancellable = true)
13+
14+
private void onSendMessage(String message, boolean addToHistory, CallbackInfoReturnable<Boolean> cir) {
15+
HubCommandBack.scan(message, cir);
16+
}
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.udu3324.poinpow.utils;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.tree.CommandNode;
5+
import com.udu3324.poinpow.Poinpow;
6+
import net.minecraft.client.MinecraftClient;
7+
import net.minecraft.client.network.ClientPlayNetworkHandler;
8+
import net.minecraft.client.network.ClientPlayerEntity;
9+
import net.minecraft.command.CommandSource;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
11+
12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
14+
public class HubCommandBack {
15+
public static final String name = "hubCommandBack";
16+
public static final String description = "Adds back the /hub command in sub-servers that don't have a hub.";
17+
18+
public static AtomicBoolean toggled = new AtomicBoolean(true);
19+
20+
public static void scan(String msg, CallbackInfoReturnable<Boolean> cir) {
21+
// return if toggled off (no need for bool)
22+
if (!toggled.get()) return;
23+
24+
// return if not on mh
25+
if (!Poinpow.onMinehut) return;
26+
27+
// return if the message isn't the command
28+
if (!msg.contains("/hub")) return;
29+
30+
// return if the sub-server already has /hub registered as a command
31+
ClientPlayNetworkHandler clientPlayNetworkHandler = MinecraftClient.getInstance().getNetworkHandler();
32+
if (clientPlayNetworkHandler == null) return;
33+
34+
CommandDispatcher<CommandSource> dispatcher = clientPlayNetworkHandler.getCommandDispatcher();
35+
if (isCommandRegistered(dispatcher, "hub")) return;
36+
37+
// Prevent original message from sending
38+
cir.setReturnValue(true);
39+
40+
ClientPlayerEntity player = MinecraftClient.getInstance().player;
41+
if (player == null) return;
42+
43+
player.networkHandler.sendChatCommand("mh");
44+
}
45+
46+
public static boolean isCommandRegistered(CommandDispatcher<CommandSource> dispatcher, String commandName) {
47+
CommandNode<CommandSource> rootCommand = dispatcher.getRoot();
48+
49+
// Check if the command is present in the command tree
50+
return rootCommand.getChild(commandName) != null;
51+
}
52+
}

src/main/resources/poinpow.mixins.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"MapartMixin"
99
],
1010
"client": [
11-
"ChatMixin"
11+
"ChatMixin",
12+
"ClientChatMixin"
1213
],
1314
"injectors": {
1415
"defaultRequire": 1

0 commit comments

Comments
 (0)