-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/udu3324/poinpow/mixin/ClientChatMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
src/main/java/com/udu3324/poinpow/utils/HubCommandBack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters