Skip to content

Commit 2d01a5e

Browse files
Merge pull request #20 from SLNE-Development/feat/add-ping-command
Feat/add ping command
2 parents ab9589e + c0fbc58 commit 2d01a5e

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

src/main/kotlin/dev/slne/surf/essentials/PaperCommandManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ object PaperCommandManager {
7575
timeCommand()
7676
skinChangeCommand()
7777
worldCommand()
78+
pingCommand()
7879
}
7980
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dev.slne.surf.essentials.command
2+
3+
import dev.jorel.commandapi.kotlindsl.*
4+
import dev.slne.surf.essentials.util.permission.EssentialsPermissionRegistry
5+
import dev.slne.surf.essentials.util.util.coloredPing
6+
import dev.slne.surf.surfapi.core.api.messages.adventure.sendText
7+
import org.bukkit.entity.Player
8+
9+
fun pingCommand() = commandTree("ping") {
10+
withPermission(EssentialsPermissionRegistry.PING_COMMAND)
11+
playerExecutor { player, _ ->
12+
val ping = player.ping.toLong()
13+
14+
player.sendText {
15+
appendPrefix()
16+
info("Du hast einen Ping von ")
17+
coloredPing(ping)
18+
info(".")
19+
}
20+
}
21+
22+
entitySelectorArgumentOnePlayer("target") {
23+
withPermission(EssentialsPermissionRegistry.PING_COMMAND_OTHER)
24+
anyExecutor { executor, args ->
25+
val target: Player by args
26+
27+
val ping = target.ping.toLong()
28+
29+
executor.sendText {
30+
appendPrefix()
31+
variableValue(target.name)
32+
info(" hat einen Ping von ")
33+
coloredPing(ping)
34+
info(".")
35+
}
36+
}
37+
}
38+
}

src/main/kotlin/dev/slne/surf/essentials/util/permission/EssentialsPermissionRegistry.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ object EssentialsPermissionRegistry : PermissionRegistry() {
108108
val WORLD_COMMAND_LIST = create("$PREFIX.world.command.list")
109109
val WORLD_COMMAND_JOIN = create("$PREFIX.world.command.join")
110110
val WORLD_BYPASS = create("$PREFIX.world.bypass")
111+
val PING_COMMAND = create("$PREFIX.ping.command")
112+
val PING_COMMAND_OTHER = create("$PREFIX.ping.command.other")
111113
}

src/main/kotlin/dev/slne/surf/essentials/util/util/surf-util.kt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,50 @@ fun Duration.userContent(): String {
8989
if (parts.isEmpty() && millis > 0) parts.add("$millis Millisekunden")
9090

9191
return parts.joinToString(", ")
92-
}
92+
}
93+
94+
fun SurfComponentBuilder.coloredDuration(
95+
duration: Duration,
96+
good: Duration = Duration.ofMillis(200),
97+
okay: Duration = Duration.ofMillis(1000)
98+
) {
99+
val millis = duration.toMillis()
100+
when {
101+
duration < good -> text(
102+
millis.toString() + "ms",
103+
Colors.GREEN
104+
)
105+
106+
duration < okay -> text(
107+
millis.toString() + "ms",
108+
Colors.YELLOW
109+
)
110+
111+
else -> text(millis.toString() + "ms", Colors.RED)
112+
}
113+
}
114+
115+
fun SurfComponentBuilder.coloredPing(
116+
ping: Long
117+
) = coloredDuration(Duration.ofMillis(ping), Duration.ofMillis(100), Duration.ofMillis(300))
118+
119+
fun Long.coloredComponent(good: Long = 200L, okay: Long = 1000L) =
120+
buildText {
121+
when {
122+
this@coloredComponent < good -> append(
123+
Component.text(
124+
this@coloredComponent.toString() + "ms",
125+
Colors.GREEN
126+
)
127+
)
128+
129+
this@coloredComponent < okay -> append(
130+
Component.text(
131+
this@coloredComponent.toString() + "ms",
132+
Colors.YELLOW
133+
)
134+
)
135+
136+
else -> append(Component.text(this@coloredComponent.toString() + "ms", Colors.RED))
137+
}
138+
}

0 commit comments

Comments
 (0)