Skip to content

Commit 186ddaf

Browse files
committed
Convert some commands to PlayerOptionalCommand
1 parent cd27c4d commit 186ddaf

File tree

6 files changed

+44
-89
lines changed

6 files changed

+44
-89
lines changed

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/ClientLocale.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
package io.github.rothes.esu.bukkit.module.essentialcommands
22

3-
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.user
4-
import io.github.rothes.esu.core.command.annotation.ShortPerm
3+
import io.github.rothes.esu.bukkit.user
4+
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
55
import io.github.rothes.esu.core.configuration.data.MessageData
66
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
77
import io.github.rothes.esu.core.module.configuration.FeatureToggle
88
import io.github.rothes.esu.core.user.User
99
import io.github.rothes.esu.core.util.ComponentUtils.unparsed
10-
import org.incendo.cloud.annotations.Command
10+
import org.bukkit.entity.Player
1111

12-
object ClientLocale : BaseCommand<FeatureToggle.DefaultTrue, ClientLocale.Lang>() {
12+
object ClientLocale : PlayerOptionalCommand<FeatureToggle.DefaultTrue, ClientLocale.Lang>() {
1313

14-
override fun onEnable() {
15-
registerCommands(object {
16-
@Command("clientLocale [player]")
17-
@ShortPerm
18-
fun clientLocale(sender: User, player: User = sender) {
19-
sender.message(lang, { message }, user(player), unparsed("locale", player.clientLocale))
20-
}
21-
})
14+
override val receivesSilentFlag: Boolean
15+
get() = false
16+
17+
override fun onPerform(sender: User, player: Player, silent: Boolean) {
18+
val playerUser = player.user
19+
sender.message(lang, { message }, player(player), unparsed("locale", playerUser.clientLocale))
2220
}
2321

2422
data class Lang(

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Feed.kt

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
11
package io.github.rothes.esu.bukkit.module.essentialcommands
22

33
import io.github.rothes.esu.bukkit.user
4-
import io.github.rothes.esu.bukkit.user.PlayerUser
54
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
65
import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.syncTick
7-
import io.github.rothes.esu.core.command.annotation.ShortPerm
86
import io.github.rothes.esu.core.configuration.data.MessageData
97
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
108
import io.github.rothes.esu.core.module.configuration.FeatureToggle
119
import io.github.rothes.esu.core.user.User
1210
import org.bukkit.entity.Player
13-
import org.incendo.cloud.annotations.Command
14-
import org.incendo.cloud.annotations.Flag
1511

16-
object Feed : BaseCommand<FeatureToggle.DefaultTrue, Feed.Lang>() {
12+
object Feed : PlayerOptionalCommand<FeatureToggle.DefaultTrue, Feed.Lang>() {
1713

18-
override fun onEnable() {
19-
registerCommands(object {
20-
@Command("feed")
21-
@ShortPerm
22-
fun feed(sender: User) {
23-
val user = sender as PlayerUser
24-
feed(sender, user.player, true)
14+
override fun onPerform(sender: User, player: Player, silent: Boolean) {
15+
player.syncTick {
16+
player.foodLevel = 20
17+
player.saturation = 20.0f // 5.0f on respawn
18+
player.exhaustion = 0.0f
19+
sender.message(lang, { fedPlayer }, player(player))
20+
if (!silent) {
21+
player.user.message(lang, { fed })
2522
}
26-
27-
@Command("feed <player>")
28-
@ShortPerm("others")
29-
fun feed(sender: User, player: Player, @Flag("silent") silent: Boolean = sender.uuid != player.uniqueId) {
30-
player.syncTick {
31-
player.foodLevel = 20
32-
player.saturation = 20.0f // 5.0f on respawn
33-
player.exhaustion = 0.0f
34-
sender.message(lang, { fedPlayer }, player(player))
35-
if (!silent) {
36-
player.user.message(lang, { fed })
37-
}
38-
}
39-
}
40-
})
23+
}
4124
}
4225

4326
data class Lang(

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Heal.kt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,25 @@
11
package io.github.rothes.esu.bukkit.module.essentialcommands
22

33
import io.github.rothes.esu.bukkit.user
4-
import io.github.rothes.esu.bukkit.user.PlayerUser
54
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
65
import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.syncTick
76
import io.github.rothes.esu.bukkit.util.version.adapter.AttributeAdapter
8-
import io.github.rothes.esu.core.command.annotation.ShortPerm
97
import io.github.rothes.esu.core.configuration.data.MessageData
108
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
119
import io.github.rothes.esu.core.module.configuration.FeatureToggle
1210
import io.github.rothes.esu.core.user.User
1311
import org.bukkit.entity.Player
14-
import org.incendo.cloud.annotations.Command
15-
import org.incendo.cloud.annotations.Flag
1612

17-
object Heal : BaseCommand<FeatureToggle.DefaultTrue, Heal.Lang>() {
13+
object Heal : PlayerOptionalCommand<FeatureToggle.DefaultTrue, Heal.Lang>() {
1814

19-
override fun onEnable() {
20-
registerCommands(object {
21-
@Command("heal")
22-
@ShortPerm
23-
fun heal(sender: User) {
24-
val user = sender as PlayerUser
25-
heal(sender, user.player, true)
15+
override fun onPerform(sender: User, player: Player, silent: Boolean) {
16+
player.syncTick {
17+
player.heal(player.getAttribute(AttributeAdapter.MAX_HEALTH)!!.value)
18+
sender.message(lang, { healedPlayer }, player(player))
19+
if (!silent) {
20+
player.user.message(lang, { healed })
2621
}
27-
28-
@Command("heal <player>")
29-
@ShortPerm("others")
30-
fun heal(sender: User, player: Player, @Flag("silent") silent: Boolean = sender.uuid != player.uniqueId) {
31-
player.syncTick {
32-
player.heal(player.getAttribute(AttributeAdapter.MAX_HEALTH)!!.value)
33-
sender.message(lang, { healedPlayer }, player(player))
34-
if (!silent) {
35-
player.user.message(lang, { healed })
36-
}
37-
}
38-
}
39-
})
22+
}
4023
}
4124

4225
data class Lang(

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Ip.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
package io.github.rothes.esu.bukkit.module.essentialcommands
22

3-
import io.github.rothes.esu.bukkit.user.PlayerUser
43
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
5-
import io.github.rothes.esu.core.command.annotation.ShortPerm
64
import io.github.rothes.esu.core.configuration.data.MessageData
75
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
86
import io.github.rothes.esu.core.module.configuration.FeatureToggle
97
import io.github.rothes.esu.core.user.User
108
import io.github.rothes.esu.core.util.ComponentUtils.unparsed
119
import org.bukkit.entity.Player
12-
import org.incendo.cloud.annotations.Command
1310

14-
object Ip : BaseCommand<FeatureToggle.DefaultTrue, Ip.Lang>() {
11+
object Ip : PlayerOptionalCommand<FeatureToggle.DefaultTrue, Ip.Lang>() {
1512

16-
override fun onEnable() {
17-
registerCommands(object {
18-
@Command("ip [player]")
19-
@ShortPerm
20-
fun ip(sender: User, player: Player = (sender as PlayerUser).player) {
21-
sender.message(lang, { message }, player(player), unparsed("address", player.address!!.hostString))
22-
}
23-
})
13+
override val receivesSilentFlag: Boolean
14+
get() = false
15+
16+
override fun onPerform(sender: User, player: Player, silent: Boolean) {
17+
sender.message(lang, { message }, player(player), unparsed("address", player.address!!.hostString))
2418
}
2519

2620
data class Lang(

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Ping.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
package io.github.rothes.esu.bukkit.module.essentialcommands
22

3-
import io.github.rothes.esu.bukkit.user.PlayerUser
43
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
5-
import io.github.rothes.esu.core.command.annotation.ShortPerm
64
import io.github.rothes.esu.core.configuration.data.MessageData
75
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
86
import io.github.rothes.esu.core.module.configuration.FeatureToggle
97
import io.github.rothes.esu.core.user.User
108
import io.github.rothes.esu.core.util.ComponentUtils.unparsed
119
import org.bukkit.entity.Player
12-
import org.incendo.cloud.annotations.Command
1310

14-
object Ping : BaseCommand<FeatureToggle.DefaultTrue, Ping.Lang>() {
11+
object Ping : PlayerOptionalCommand<FeatureToggle.DefaultTrue, Ping.Lang>() {
1512

16-
override fun onEnable() {
17-
registerCommands(object {
18-
@Command("ping [player]")
19-
@ShortPerm
20-
fun ping(sender: User, player: Player = (sender as PlayerUser).player) {
21-
sender.message(lang, { message }, player(player), unparsed("ping", player.ping))
22-
}
23-
})
13+
override val receivesSilentFlag: Boolean
14+
get() = false
15+
16+
override fun onPerform(sender: User, player: Player, silent: Boolean) {
17+
sender.message(lang, { message }, player(player), unparsed("ping", player.ping))
2418
}
2519

2620
data class Lang(

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/PlayerOptionalCommand.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import org.incendo.cloud.kotlin.extension.commandBuilder
88

99
abstract class PlayerOptionalCommand<C, L>: BaseCommand<C, L>() {
1010

11+
protected open val receivesSilentFlag: Boolean
12+
get() = true
13+
1114
override fun onEnable() {
1215
withCommandManager {
1316
commandBuilder(name) {
@@ -22,7 +25,7 @@ abstract class PlayerOptionalCommand<C, L>: BaseCommand<C, L>() {
2225
}
2326
permission(cmdShortPerm("other"))
2427
required("player", PlayerParser.playerParser())
25-
flag("silent")
28+
if (receivesSilentFlag) flag("silent")
2629
handler { ctx ->
2730
val sender = ctx.sender()
2831
val player = ctx.get<Player>("player")

0 commit comments

Comments
 (0)