Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ping display #3044

Open
wants to merge 5 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class OutsideSbFeature(private val displayName: String) {
CUSTOM_TEXT_BOX("Custom Text Box"),
REAL_TIME("Real Time"),
TPS_DISPLAY("TPS Display"),
PING_DISPLAY("Ping Display"),
MARKED_PLAYERS("Marked Players"),
FARMING_WEIGHT("Farming Weight"),
NEXT_JACOB_CONTEST("Next Jacobs's Contest"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ public class GUIConfig {
@ConfigLink(owner = GUIConfig.class, field = "tpsDisplay")
public Position tpsDisplayPosition = new Position(10, 10, false, true);

@Expose
@ConfigOption(name = "Ping Display", desc = "Show your current ping, like in Skytils.")
Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
@ConfigEditorBoolean
@FeatureToggle
public boolean pingDisplay = false;

@Expose
@ConfigLink(owner = GUIConfig.class, field = "pingDisplay")
public Position pingDisplayPosition = new Position(10, 22, false, true);

@Expose
@ConfigOption(name = "Config Button", desc = "Add a button to the pause menu to configure SkyHanni.")
@ConfigEditorBoolean
Expand Down
138 changes: 138 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/features/misc/PingDisplay.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.commands.CommandCategory
import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent
import at.hannibal2.skyhanni.config.enums.OutsideSbFeature
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.events.minecraft.ClientDisconnectEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.renderString
import net.minecraft.client.Minecraft
import net.minecraft.network.play.client.C16PacketClientStatus
import net.minecraft.network.play.server.S01PacketJoinGame
import net.minecraft.network.play.server.S37PacketStatistics
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.math.abs
import kotlin.math.round

@SkyHanniModule
object PingDisplay {

private val config get() = SkyHanniMod.feature.gui

private val mc: Minecraft = Minecraft.getMinecraft()
Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
private var lastPingAt: Long = -1L
private var invokedCommand = false
private var autoPingerEnabled = false
private var display: String? = null

var latestPing: Double = 0.0


Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
fun sendPing(command: Boolean) {
if (lastPingAt > 0) {
if (invokedCommand) {
ChatUtils.chat("§cAlready pinging!")
return
}
}
mc.thePlayer.sendQueue.networkManager.sendPacket(
C16PacketClientStatus(C16PacketClientStatus.EnumState.REQUEST_STATS)
)
lastPingAt = System.nanoTime()
invokedCommand = command
}

@HandleEvent
fun onProfileJoin(event: ProfileJoinEvent) {
if (autoPingerEnabled) return
lastPingAt = -1L
invokedCommand = false
startPingUpdater()
}


Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
@HandleEvent
fun onPacketReceived(event: PacketReceivedEvent) {
val packet = event.packet
if (lastPingAt > 0) {
Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
when (packet) {
is S01PacketJoinGame -> {
lastPingAt = -1L
invokedCommand = false
startPingUpdater()
}

is S37PacketStatistics -> {
val diff = abs(System.nanoTime() - lastPingAt) / 1_000_000.0
lastPingAt = -1L
latestPing = diff
updateDisplay()
if (invokedCommand) {
invokedCommand = false
ChatUtils.chat(formatPingMessage(latestPing))
}
}
}
}
}


Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
@HandleEvent
fun onCommandRegister(event: CommandRegistrationEvent) {
event.register("shping") {
description = "Check your ping"
category = CommandCategory.USERS_ACTIVE
callback { sendPing(true) }
}
}

private fun formatPingMessage(ping: Double): String {
val color = when {
ping < 50 -> "2"
ping < 100 -> "a"
ping < 149 -> "6"
ping < 249 -> "c"
else -> "4"
}
return "§$color${round(ping * 100) / 100} §7ms"
}


Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
@HandleEvent
fun onDisconnect(event: ClientDisconnectEvent) {
autoPingerEnabled = false
}

fun startPingUpdater() {
if (autoPingerEnabled) return
autoPingerEnabled = true
val scheduler = Executors.newScheduledThreadPool(1)
ChatUtils.debug("Starting Ping Updater")
scheduler.scheduleAtFixedRate({
sendPing(false)
}, 0, 5, TimeUnit.SECONDS)
}
Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved

private fun updateDisplay() {
display = "§ePing: ${formatPingMessage(latestPing)}"
}

@SubscribeEvent
fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) {
if (!isEnabled()) return

config.pingDisplayPosition.renderString(display, posLabel = "Ping Display")
}


Not-a-cowfr marked this conversation as resolved.
Show resolved Hide resolved
private fun isEnabled() = config.pingDisplay && (LorenzUtils.inSkyBlock || OutsideSbFeature.PING_DISPLAY.isSelected())
}
Loading