1+ package io.github.rothes.esu.bukkit.module.essentialcommands
2+
3+ import io.github.rothes.esu.bukkit.user.PlayerUser
4+ import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
5+ import io.github.rothes.esu.bukkit.util.ServerCompatibility.tp
6+ import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.nextTick
7+ import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.nextTickDeferred
8+ import io.github.rothes.esu.bukkit.util.version.adapter.TickThreadAdapter.Companion.checkTickThread
9+ import io.github.rothes.esu.core.command.annotation.ShortPerm
10+ import io.github.rothes.esu.core.configuration.data.MessageData
11+ import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
12+ import io.github.rothes.esu.core.module.configuration.FeatureToggle
13+ import org.bukkit.GameMode
14+ import org.bukkit.entity.Player
15+ import org.incendo.cloud.annotations.Argument
16+ import org.incendo.cloud.annotations.Command
17+
18+ object Spectate: BaseCommand<FeatureToggle.DefaultTrue, Spectate.Lang>() {
19+
20+ override fun onEnable () {
21+ registerCommands(object {
22+ @Command(" spectate <target>" )
23+ @ShortPerm
24+ suspend fun spectate (sender : PlayerUser , @Argument(" target" ) target : Player ) {
25+ val caller = sender.player
26+ if (caller == target) {
27+ return sender.message(lang, { cannotSpectateSelf })
28+ }
29+ if (caller.gameMode != GameMode .SPECTATOR ) {
30+ caller.nextTickDeferred {
31+ caller.gameMode = GameMode .SPECTATOR
32+ }.join()
33+ }
34+ target.nextTick {
35+ if (caller.checkTickThread())
36+ caller.spectatorTarget = target
37+ else {
38+ caller.tp(target.location) {
39+ caller.spectatorTarget = target
40+ }
41+ }
42+ sender.message(lang, { spectatingTarget }, player(target, " target" ))
43+ }
44+ }
45+ }) { parser ->
46+ parser.registerBuilderDecorator {
47+ it.senderType(PlayerUser ::class .java)
48+ }
49+ }
50+ }
51+
52+ data class Lang (
53+ val cannotSpectateSelf : MessageData = " <ec>You cannot spectate yourself!" .message,
54+ val spectatingTarget : MessageData = " <pc>Spectating <pdc><target></pdc> now." .message,
55+ )
56+
57+ }
0 commit comments