Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 6c270e4

Browse files
committed
feat(undefined-command): add BaseUndefinedCommand#addIncorrectMessage method
1 parent aab4a17 commit 6c270e4

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

api/src/main/java/com/undefined/api/nms/ServerPlayer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.google.gson.JsonObject
44
import com.google.gson.JsonParser
55
import com.undefined.api.nms.interfaces.NMSLivingEntity
66
import com.undefined.api.nms.interfaces.NMSPlayer
7+
import org.bukkit.Bukkit
78
import java.io.InputStreamReader
89
import java.net.URI
910
import kotlin.math.floor
@@ -35,7 +36,7 @@ fun NMSPlayer.getSkinTexture(name: String): Array<String> {
3536
}
3637
}
3738
} catch (e: Exception) {
38-
println("Failed to get texture: ${e.message}")
39+
Bukkit.getLogger().severe("Failed to get texture: ${e.message}")
3940
}
4041

4142
return arrayOf(texture, signature)

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ allprojects {
7878
implementation("org.jetbrains.kotlin:kotlin-stdlib")
7979
implementation("net.kyori:adventure-platform-bukkit:4.3.3")
8080
implementation("net.kyori:adventure-text-minimessage:4.17.0")
81+
implementation("net.kyori:adventure-text-serializer-plain:4.17.0")
8182
implementation("org.json:json:20231013")
8283
implementation("com.googlecode.json-simple:json-simple:1.1.1")
8384
}

common/src/main/java/com/undefined/api/command/BaseUndefinedCommand.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
package com.undefined.api.command
22

3-
import com.undefined.api.command.info.AllCommand
3+
import com.undefined.api.command.info.FullCommand
44
import com.undefined.api.command.info.ListSubCommandInfo
55
import com.undefined.api.command.sub.*
6+
import net.kyori.adventure.text.Component
67
import org.bukkit.command.CommandSender
78
import org.bukkit.command.ConsoleCommandSender
89
import org.bukkit.entity.Player
910

1011
abstract class BaseUndefinedCommand {
1112

12-
internal val mainExecute: MutableList<AllCommand.() -> Boolean> = mutableListOf()
13-
internal val genExecute: MutableList<CommandSender.() -> Boolean> = mutableListOf()
13+
internal val incorrectMessages: MutableList<FullCommand.() -> Component> = mutableListOf()
14+
15+
internal val fullExecute: MutableList<FullCommand.() -> Boolean> = mutableListOf()
16+
internal val generalExecute: MutableList<CommandSender.() -> Boolean> = mutableListOf()
1417
internal val playerExecute: MutableList<Player.() -> Boolean> = mutableListOf()
1518
internal val consoleExecute: MutableList<ConsoleCommandSender.() -> Boolean> = mutableListOf()
1619

1720
val subCommandList: MutableList<UndefinedSubCommand> = mutableListOf()
1821

19-
fun addFullExecute(c: AllCommand.() -> Boolean): BaseUndefinedCommand {
20-
mainExecute.add(c)
22+
fun addIncorrectMessage(incorrectMessage: FullCommand.() -> Component): BaseUndefinedCommand {
23+
incorrectMessages.add(incorrectMessage)
24+
return this
25+
}
26+
27+
fun addIncorrectMessage(incorrectMessage: String): BaseUndefinedCommand {
28+
incorrectMessages.add { Component.text(incorrectMessage) }
29+
return this
30+
}
31+
32+
fun addIncorrectMessage(incorrectMessage: Component): BaseUndefinedCommand {
33+
incorrectMessages.add { incorrectMessage }
34+
return this
35+
}
36+
37+
fun addFullExecute(c: FullCommand.() -> Boolean): BaseUndefinedCommand {
38+
fullExecute.add(c)
2139
return this
2240
}
2341

2442
fun addExecute(c: CommandSender.() -> Boolean): BaseUndefinedCommand {
25-
genExecute.add(c)
43+
generalExecute.add(c)
2644
return this
2745
}
2846

@@ -112,7 +130,7 @@ abstract class BaseUndefinedCommand {
112130
return subCommand
113131
}
114132

115-
fun clearExecute() = genExecute.clear()
133+
fun clearExecute() = generalExecute.clear()
116134
fun clearPlayerExecute() = playerExecute.clear()
117135
fun clearConsoleExecute() = consoleExecute.clear()
118136

common/src/main/java/com/undefined/api/command/UndefinedCommand.kt

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.undefined.api.command
22

3-
import com.undefined.api.command.info.AllCommand
3+
import com.undefined.api.command.info.FullCommand
44
import com.undefined.api.command.sub.UndefinedSubCommand
5+
import com.undefined.api.extension.sendMessage
56
import com.undefined.api.nms.extensions.getPrivateField
7+
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer
68
import org.bukkit.Bukkit
79
import org.bukkit.command.CommandMap
810
import org.bukkit.command.CommandSender
@@ -12,7 +14,7 @@ import org.bukkit.entity.Player
1214
import org.bukkit.util.StringUtil
1315

1416

15-
class UndefinedCommand(name: String, permission: String? = null, description: String = "", aliases: List<String> = emptyList()): BaseUndefinedCommand() {
17+
class UndefinedCommand(name: String, permission: String? = null, description: String = "", aliases: List<String> = emptyList()) : BaseUndefinedCommand() {
1618

1719
init {
1820
UndefinedCoreCommand(name, permission, description, aliases, {
@@ -29,19 +31,18 @@ class UndefinedCommand(name: String, permission: String? = null, description: St
2931
}
3032
}
3133

32-
genExecute.forEach { execution -> if (!execution.invoke(sender)) return@UndefinedCoreCommand false }
33-
mainExecute.forEach { execution -> if (!execution.invoke(AllCommand(sender, arg))) return@UndefinedCoreCommand false}
34+
generalExecute.forEach { execution -> if (!execution.invoke(sender)) return@UndefinedCoreCommand false }
35+
fullExecute.forEach { execution -> if (!execution.invoke(FullCommand(sender, args))) return@UndefinedCoreCommand false}
3436

35-
if (arg.isNullOrEmpty()) return@UndefinedCoreCommand false
37+
if (args.isNullOrEmpty()) return@UndefinedCoreCommand false
3638

37-
var lastSub: BaseUndefinedCommand = this@UndefinedCommand
38-
arg.forEach { arg ->
39-
val sub = getAndRun(lastSub, arg, this.arg, sender is Player, sender, this.arg.indexOf(arg)) ?: return@UndefinedCoreCommand false
40-
lastSub = sub
39+
var lastSubCommand: BaseUndefinedCommand = this@UndefinedCommand
40+
args.forEach { arg ->
41+
val currentSubCommand = getAndRun(lastSubCommand, arg, this.args, sender is Player, sender, this.args.indexOf(arg)) ?: return@UndefinedCoreCommand false
42+
lastSubCommand = currentSubCommand
4143
}
4244

4345
return@UndefinedCoreCommand true
44-
4546
}, {
4647
if (this.second.isNullOrEmpty()) return@UndefinedCoreCommand mutableListOf()
4748
val index = this.second!!.size - 1
@@ -69,31 +70,40 @@ class UndefinedCommand(name: String, permission: String? = null, description: St
6970
private fun getAndRun(
7071
sub: BaseUndefinedCommand,
7172
name: String,
72-
arg: Array<out String>?,
73+
args: Array<out String>?,
7374
isPlayer: Boolean,
74-
commandSender: CommandSender,
75+
sender: CommandSender,
7576
indexOf: Int
7677
) : UndefinedSubCommand? {
7778
val command = sub.getSubCommand(name)
7879
command?.let {
7980
if (isPlayer) {
8081
it.playerExecute.forEach { func ->
81-
(commandSender as? Player)?.let { player ->
82+
(sender as? Player)?.let { player ->
8283
if (!func.invoke(player)) return null
8384
}
8485
}
8586
} else {
8687
it.consoleExecute.forEach { func ->
87-
(commandSender as? ConsoleCommandSender)?.let { console ->
88+
(sender as? ConsoleCommandSender)?.let { console ->
8889
if (!func.invoke(console)) return null
8990
}
9091
}
9192
}
92-
it.genExecute.forEach { if (!it.invoke(commandSender)) return null }
93-
it.mainExecute.forEach { if (!it.invoke(AllCommand(commandSender, arg))) return null}
94-
95-
if (!it.runSpecialExecute(arg!!, commandSender, indexOf)) return null
93+
it.generalExecute.forEach { if (!it.invoke(sender)) return null }
94+
it.fullExecute.forEach { if (!it.invoke(FullCommand(sender, args))) return null }
9695

96+
if (!it.runSpecialExecute(args!!, sender, indexOf)) return null
97+
} ?: run {
98+
if (isPlayer) {
99+
(sender as? Player)?.let { player ->
100+
incorrectMessages.forEach { player.sendMessage(it.invoke(FullCommand(sender, args))) }
101+
}
102+
} else {
103+
(sender as? ConsoleCommandSender)?.let { console ->
104+
incorrectMessages.forEach { console.sendMessage(PlainTextComponentSerializer.plainText().serialize(it.invoke(FullCommand(sender, args)))) }
105+
}
106+
}
97107
}
98108
return command
99109
}
@@ -103,9 +113,9 @@ class UndefinedCommand(name: String, permission: String? = null, description: St
103113

104114
}
105115

106-
class UndefinedCoreCommand(name: String, permission: String? = null, description: String = "", aliases: List<String> = emptyList(), private val execution: AllCommand.() -> Boolean, private val tabCompletion: Pair<CommandSender, Array<out String>?>.() -> MutableList<String>) : BukkitCommand(name) {
116+
class UndefinedCoreCommand(name: String, permission: String? = null, description: String = "", aliases: List<String> = emptyList(), private val execution: FullCommand.() -> Boolean, private val tabCompletion: Pair<CommandSender, Array<out String>?>.() -> MutableList<String>) : BukkitCommand(name) {
107117

108-
override fun execute(sender: CommandSender, alias: String, args: Array<out String>?): Boolean = execution.invoke(AllCommand(sender, args))
118+
override fun execute(sender: CommandSender, alias: String, args: Array<out String>?): Boolean = execution.invoke(FullCommand(sender, args))
109119
override fun tabComplete(sender: CommandSender, alias: String, args: Array<out String>?): MutableList<String> = tabCompletion.invoke(Pair(sender, args))
110120

111121
init {

common/src/main/java/com/undefined/api/command/info/CommandInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.bukkit.entity.Player
66

77
open class TargetCommand(open val sender: CommandSender, open val target: Player)
88

9-
data class AllCommand(val sender: CommandSender, val arg: Array<out String>?)
9+
data class FullCommand(val sender: CommandSender, val args: Array<out String>?)
1010

1111
data class StringSubCommandInfo(val sender: CommandSender, val string: String)
1212

server/src/main/java/com/undefined/api/Main.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.undefined.api
22

33
import com.undefined.api.command.UndefinedCommand
44
import com.undefined.api.extension.sendActionBar
5-
import com.undefined.api.nms.triggerTotem
65
import com.undefined.api.scheduler.TimeUnit
76
import net.kyori.adventure.text.Component
87
import org.bukkit.plugin.java.JavaPlugin
@@ -15,9 +14,14 @@ class Main : JavaPlugin() {
1514
api = UndefinedAPI(this)
1615

1716
UndefinedCommand("test")
17+
.addIncorrectMessage("test")
18+
.addSubCommand("hi")
19+
.addIncorrectMessage("hi")
20+
.addSubCommand("hi")
21+
.addIncorrectMessage("hi")
1822
.addExecutePlayer {
1923
sendActionBar(Component.text("hi!"), 1, TimeUnit.SECONDS)
20-
return@addExecutePlayer false
24+
return@addExecutePlayer true
2125
}
2226
}
2327
}

v1_20_6/src/main/java/com/undefined/api/nms/v1_20_6/extensions/DisplayExtensions.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ fun Display.getTranslation(): Vector3f = entityData.get(getTranslationAccessor()
3737

3838
fun Display.setScaleX(scale: Float): SynchedEntityData {
3939
val v = getScale()
40-
println(v.x)
4140
val v2 = v.set(scale, v.y, v.z)
42-
println(v2.x)
4341
entityData.set(getScaleAccessor(), v2)
4442
return entityData
4543
}

v1_21/src/main/java/com/undefined/api/nms/v1_21/extensions/DisplayExtensions.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ fun Display.getTranslation(): Vector3f = entityData.get(getTranslationAccessor()
3737

3838
fun Display.setScaleX(scale: Float): SynchedEntityData {
3939
val v = getScale()
40-
println(v.x)
4140
val v2 = v.set(scale, v.y, v.z)
42-
println(v2.x)
4341
entityData.set(getScaleAccessor(), v2)
4442
return entityData
4543
}

0 commit comments

Comments
 (0)