Created
October 28, 2023 14:08
-
-
Save Ensamisten/4efa09039a6f768b31d24038e407181f to your computer and use it in GitHub Desktop.
PingCommand. Usage: /ping <ip>
This file contains hidden or 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
| @Override | |
| public void onInitializeClient() { | |
| FabrientRegistries.registerModStuffs(); | |
| DebugArmorSetup.registerFabrientItems(); | |
| CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { | |
| dispatcher.register( | |
| CommandManager.literal("ping") | |
| .then(CommandManager.argument("ip", StringArgumentType.word()) | |
| .executes(context -> { | |
| String ipAddress = StringArgumentType.getString(context, "ip"); | |
| executePing(context.getSource(), ipAddress); | |
| return 1; | |
| }) | |
| ) | |
| ); | |
| }); | |
| } | |
| private static void executePing(ServerCommandSource source, String ipAddress) { | |
| try { | |
| InetAddress host = InetAddress.getByName(ipAddress); | |
| source.sendFeedback(() -> Text.of("Pinging host: " + ipAddress), false); | |
| if (host.isReachable(5000)) { | |
| source.sendFeedback(() -> Text.of("Host is reachable"), false); | |
| } else { | |
| source.sendFeedback(() -> Text.of("Sorry, we can't reach this host"), false); | |
| } | |
| } catch (UnknownHostException e) { | |
| source.sendFeedback(() -> Text.of("Unknown host: " + ipAddress), false); | |
| } catch (IOException e) { | |
| source.sendFeedback(() -> Text.literal("Error while pinging the host: " + ipAddress), false); | |
| } | |
| } |
This file contains hidden or 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
| package net.kaupenjoe.mccourse.command; | |
| import com.mojang.brigadier.CommandDispatcher; | |
| import com.mojang.brigadier.arguments.StringArgumentType; | |
| import net.minecraft.commands.CommandSourceStack; | |
| import net.minecraft.commands.Commands; | |
| import net.minecraft.network.chat.Component; | |
| import java.io.IOException; | |
| import java.net.HttpURLConnection; | |
| import java.net.InetSocketAddress; | |
| import java.net.Proxy; | |
| import java.net.URI; | |
| import java.net.URISyntaxException; | |
| public class PingCommand { | |
| public PingCommand(CommandDispatcher<CommandSourceStack> dispatcher) { | |
| dispatcher.register(Commands.literal("ping") | |
| .then(Commands.argument("proxy", StringArgumentType.word()) | |
| .then(Commands.argument("ip", StringArgumentType.word()) | |
| .executes(context -> execute(context.getSource(), | |
| StringArgumentType.getString(context, "proxy"), | |
| StringArgumentType.getString(context, "ip")) | |
| ) | |
| ) | |
| ) | |
| ); | |
| } | |
| private static int execute(CommandSourceStack source, String proxyAddress, String urlString) { | |
| try { | |
| Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyAddress, 9050)); | |
| URI uri = new URI(urlString); | |
| source.sendSuccess(() -> Component.literal(createText("Started socks request! URL: " + urlString)), true); | |
| long startTime = System.currentTimeMillis(); | |
| HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(proxy); | |
| connection.setConnectTimeout(10000); // 10 seconds connection timeout | |
| connection.setReadTimeout(10000); // 10 seconds read timeout | |
| int responseCode = connection.getResponseCode(); | |
| long responseTime = System.currentTimeMillis() - startTime; | |
| boolean status = (responseCode == HttpURLConnection.HTTP_OK); | |
| source.sendSuccess(() -> Component.literal(createText("Response Time: " + responseTime + " ms")), false); | |
| source.sendSuccess(() -> Component.literal(createText("Load Time: " + responseTime + " ms")), false); | |
| source.sendSuccess(() -> Component.literal(createText("Status: " + status)), true); | |
| source.sendSuccess(() -> Component.literal(createText("Loaded: " + status)), true); | |
| connection.disconnect(); | |
| } catch (IOException | URISyntaxException e) { | |
| source.sendFailure(Component.literal(createText("Host unreachable error"))); | |
| e.printStackTrace(); | |
| } | |
| return 1; | |
| } | |
| private static String createText(String message) { | |
| return (message); | |
| } | |
| } |
This file contains hidden or 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
| package net.kaupenjoe.mccourse.command; | |
| import com.mojang.brigadier.CommandDispatcher; | |
| import com.mojang.brigadier.arguments.StringArgumentType; | |
| import com.mojang.brigadier.context.CommandContext; | |
| import net.minecraft.commands.CommandSourceStack; | |
| import net.minecraft.commands.Commands; | |
| import net.minecraft.network.chat.Component; | |
| import java.io.IOException; | |
| import java.net.InetAddress; | |
| import java.net.UnknownHostException; | |
| public class PingCommand { | |
| public PingCommand(CommandDispatcher<CommandSourceStack> dispatcher) { | |
| dispatcher.register( | |
| Commands.literal("ping") | |
| .then(Commands.argument("ip", StringArgumentType.word()) | |
| .executes(context -> { | |
| String ipAddress = StringArgumentType.getString(context, "ip"); | |
| executePing(context.getSource(), ipAddress); | |
| return 1; | |
| }) | |
| ) | |
| ); | |
| } | |
| private static void executePing(CommandSourceStack source, String ipAddress) { | |
| try { | |
| InetAddress host = InetAddress.getByName(ipAddress); | |
| source.sendSuccess(() -> Component.literal("Pinging host: " + ipAddress), false); | |
| if (host.isReachable(5000)) { | |
| source.sendSuccess(() -> Component.literal("Host is reachable"), false); | |
| } else { | |
| source.sendSuccess(() -> Component.literal("Sorry, we can't reach this host"), false); | |
| } | |
| } catch (UnknownHostException e) { | |
| source.sendSuccess(() -> Component.literal("Unknown host: " + ipAddress), false); | |
| } catch (IOException e) { | |
| source.sendSuccess(() -> Component.literal("Error while pinging the host: " + ipAddress), false); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment