Skip to content

Commit

Permalink
HubCommandBack works
Browse files Browse the repository at this point in the history
  • Loading branch information
udu3324 committed Dec 29, 2023
1 parent 28f1f4e commit dd34091
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.parallel=true
loader_version=0.15.3

# Mod Properties
mod_version = 1.6.0
mod_version = 1.6.1
maven_group = com.udu3324
archives_base_name = poinpow

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/udu3324/poinpow/mixin/ClientChatMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.udu3324.poinpow.mixin;

import com.udu3324.poinpow.utils.HubCommandBack;
import net.minecraft.client.gui.screen.ChatScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ChatScreen.class)
public class ClientChatMixin {
@Inject(method = "sendMessage", at = @At("HEAD"), cancellable = true)

private void onSendMessage(String message, boolean addToHistory, CallbackInfoReturnable<Boolean> cir) {
HubCommandBack.scan(message, cir);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/udu3324/poinpow/utils/HubCommandBack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.udu3324.poinpow.utils;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode;
import com.udu3324.poinpow.Poinpow;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.concurrent.atomic.AtomicBoolean;

public class HubCommandBack {
public static final String name = "hubCommandBack";
public static final String description = "Adds back the /hub command in sub-servers that don't have a hub.";

public static AtomicBoolean toggled = new AtomicBoolean(true);

public static void scan(String msg, CallbackInfoReturnable<Boolean> cir) {
// return if toggled off (no need for bool)
if (!toggled.get()) return;

// return if not on mh
if (!Poinpow.onMinehut) return;

// return if the message isn't the command
if (!msg.contains("/hub")) return;

// return if the sub-server already has /hub registered as a command
ClientPlayNetworkHandler clientPlayNetworkHandler = MinecraftClient.getInstance().getNetworkHandler();
if (clientPlayNetworkHandler == null) return;

CommandDispatcher<CommandSource> dispatcher = clientPlayNetworkHandler.getCommandDispatcher();
if (isCommandRegistered(dispatcher, "hub")) return;

// Prevent original message from sending
cir.setReturnValue(true);

ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player == null) return;

player.networkHandler.sendChatCommand("mh");
}

public static boolean isCommandRegistered(CommandDispatcher<CommandSource> dispatcher, String commandName) {
CommandNode<CommandSource> rootCommand = dispatcher.getRoot();

// Check if the command is present in the command tree
return rootCommand.getChild(commandName) != null;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/poinpow.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"MapartMixin"
],
"client": [
"ChatMixin"
"ChatMixin",
"ClientChatMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit dd34091

Please sign in to comment.