|
| 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 | +} |
0 commit comments