Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Forked from Ensamisten/PingCommand1.java
Last active February 3, 2024 04:22
Show Gist options
  • Select an option

  • Save Densamisten/29922908ebc1d58c285e82e70511ab8d to your computer and use it in GitHub Desktop.

Select an option

Save Densamisten/29922908ebc1d58c285e82e70511ab8d to your computer and use it in GitHub Desktop.
PingCommand. Usage: /ping <ip>
@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);
}
}
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);
}
}
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);
long startTime = System.nanoTime();
if (host.isReachable(5000)) {
long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;
// Format the elapsed time to have two digits for milliseconds
String formattedTime = String.format("%02d", elapsedTime);
// Calculate the approximate number of bytes sent during the ping (32 bytes per packet)
double bytesSent = (elapsedTime / 8.0) * (32.0 / 1000000.0); // Convert nanoseconds to megabytes
source.sendSuccess(() -> Component.literal("Pinging " + ipAddress + " [" + host.getHostAddress() +
"] with " + String.format("%.2f", bytesSent) + " bytes of data:"), false);
source.sendSuccess(() -> Component.literal("Reply from " + host.getHostAddress() + ":" + " time=" + formattedTime), false);
} else {
source.sendSuccess(() -> Component.literal("Sorry, we can't reach this host"), false);
}
} catch (UnknownHostException e) {
source.sendSuccess(() -> Component.literal("Ping request could not find host: " + ipAddress + " Please check the name and try again."), 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