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

Commit b102507

Browse files
authored
Merge pull request #200 from DockyardMC/pika/some-random-fixes
Some random fixes
2 parents cfdf713 + fc6e2d9 commit b102507

File tree

82 files changed

+306
-318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+306
-318
lines changed

src/main/kotlin/io/github/dockyardmc/advancement/Advancement.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Advancement(
5959
private val eventPool = EventPool()
6060

6161
init {
62-
if (icon.material == Items.AIR) throw IllegalArgumentException("advancement icon can't be air")
62+
require(icon.material != Items.AIR) { "advancement icon can't be air" }
6363

6464
parent?.innerChildren?.let {
6565
synchronized(it) {

src/main/kotlin/io/github/dockyardmc/advancement/AdvancementBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class AdvancementBuilder(val id: String) {
106106
}
107107
}
108108

109-
fun advancement(id: String, builder: AdvancementBuilder.() -> Unit): Advancement {
109+
inline fun advancement(id: String, builder: AdvancementBuilder.() -> Unit): Advancement {
110110
val adv = AdvancementBuilder(id)
111111
builder.invoke(adv)
112112
return adv.build()

src/main/kotlin/io/github/dockyardmc/apis/Hologram.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HologramBuilder {
2727
}
2828
}
2929

30-
fun hologram(location: Location, unit: HologramBuilder.() -> Unit): Hologram {
30+
inline fun hologram(location: Location, unit: HologramBuilder.() -> Unit): Hologram {
3131
val builder = HologramBuilder()
3232
unit.invoke(builder)
3333
return location.world.spawnEntity(Hologram(location, builder)) as Hologram

src/main/kotlin/io/github/dockyardmc/apis/bounds/Bound.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ class Bound(
9292
}
9393

9494
fun resize(newFirstLocation: Location, newSecondLocation: Location) {
95-
if (newFirstLocation.world != newSecondLocation.world) throw IllegalArgumentException("The two locations cannot be in different worlds (${firstLocation.world.name} - ${secondLocation.world.name})")
95+
require(newFirstLocation.world == newSecondLocation.world) { "The two locations cannot be in different worlds (${firstLocation.world.name} - ${secondLocation.world.name})" }
96+
9697
this.firstLocation = getBoundPositionRelative(newFirstLocation, newSecondLocation)
9798
this.secondLocation = getBoundPositionRelative(newSecondLocation, newFirstLocation)
9899

99100
getEntities().filterIsInstance<Player>().forEach { player ->
100-
if(members.contains(player)) return@forEach
101+
if (members.contains(player)) return@forEach
101102
val event = PlayerEnterBoundEvent(player, this)
102103
Events.dispatch(event)
103104

src/main/kotlin/io/github/dockyardmc/apis/sidebar/Sidebar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Sidebar(initialTitle: String, initialLines: Map<Int, SidebarLine>) : Viewa
142142
}
143143
}
144144

145-
fun sidebar(unit: Sidebar.Builder.() -> Unit): Sidebar {
145+
inline fun sidebar(unit: Sidebar.Builder.() -> Unit): Sidebar {
146146
val builder = Sidebar.Builder()
147147
unit.invoke(builder)
148148
return builder.build()

src/main/kotlin/io/github/dockyardmc/commands/Command.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.github.dockyardmc.scroll.LegacyTextColor
55
import io.github.dockyardmc.utils.Console
66

77
@Suppress("UNCHECKED_CAST")
8-
class Command: Cloneable {
8+
class Command : Cloneable {
99
lateinit var internalExecutorDoNotUse: (CommandExecutor) -> Unit
1010
var arguments: MutableMap<String, CommandArgumentData> = mutableMapOf()
1111
var permission: String = ""
@@ -33,11 +33,12 @@ class Command: Cloneable {
3333

3434

3535
inline fun <reified T> getArgument(argumentName: String): T {
36-
if(T::class.java.isEnum && T::class != LegacyTextColor::class) throw IllegalStateException("Supplied generic is of type enum, please use getEnumArgument method instead.")
37-
if(arguments[argumentName] == null) throw IllegalStateException("Argument with name $argumentName does not exist")
38-
if(arguments[argumentName]!!.returnedValue == null) throw IllegalStateException("Argument value of $argumentName is null. Use getOrNull to get nullable value")
36+
require(!T::class.java.isEnum || T::class == LegacyTextColor::class) { "Supplied generic is of type enum, please use getEnumArgument method instead." }
3937

40-
return arguments[argumentName]!!.returnedValue as T
38+
return requireNotNull(arguments[argumentName]) { "Argument with name $argumentName does not exist" }
39+
.let { argument ->
40+
requireNotNull(argument.returnedValue) { "Argument value of $argumentName is null. Use getOrNull to get nullable value" } as T
41+
}
4142
}
4243

4344
inline fun <reified T : Enum<T>> getEnumArgument(argumentName: String): T {
@@ -46,30 +47,31 @@ class Command: Cloneable {
4647
}
4748

4849
inline fun <reified T : Enum<T>> getEnumArgumentOrNull(argumentName: String): T? {
49-
if(arguments[argumentName] == null) return null
50+
if (arguments[argumentName] == null) return null
5051
val value = getArgumentOrNull<String>(argumentName) ?: return null
5152
return T::class.java.enumConstants.firstOrNull { it.name == value.uppercase() } ?: throw Exception("Enum ${T::class.simpleName} does not contain \"${value.uppercase()}\"")
5253
}
5354

5455
inline fun <reified T> getArgumentOrNull(argumentName: String): T? {
55-
if(T::class.java.isEnum && T::class != LegacyTextColor::class) throw IllegalStateException("Supplied generic is of type enum, please use getEnumArgumentOrNull method instead.")
56-
if(arguments[argumentName] == null) return null
57-
if(arguments[argumentName]!!.returnedValue == null) return null
58-
return arguments[argumentName]!!.returnedValue as T
56+
require(!T::class.java.isEnum || T::class == LegacyTextColor::class) { "Supplied generic is of type enum, please use getEnumArgumentOrNull method instead." }
57+
58+
return arguments[argumentName]?.returnedValue as T?
5959
}
6060

6161
fun addArgument(name: String, argument: CommandArgument, suggestions: ((Player) -> Collection<String>)? = null) {
62-
if(subcommands.isNotEmpty()) throw IllegalStateException("Command cannot have both arguments and subcommands!")
62+
check(subcommands.isEmpty()) { "Command cannot have both arguments and subcommands!" }
63+
6364
val data = CommandArgumentData(argument, false, expectedReturnValueType = argument.expectedType, suggestions = suggestions)
6465
arguments[name] = data
6566
val before = arguments.values.indexOf(data) - 1
66-
if(before <= 0 ) return
67-
if(arguments.values.toList()[before].optional) throw IllegalStateException("Cannot put argument after optional argument!")
67+
if (before <= 0) return
6868

69+
check(!arguments.values.toList()[before].optional) { "Cannot put argument after optional argument!" }
6970
}
7071

7172
fun addOptionalArgument(name: String, argument: CommandArgument, suggestions: ((Player) -> Collection<String>)? = null) {
72-
if(subcommands.isNotEmpty()) throw IllegalStateException("Command cannot have both arguments and subcommands at the same time!")
73+
check(subcommands.isEmpty()) { "Command cannot have both arguments and subcommands at the same time!" }
74+
7375
arguments[name] = CommandArgumentData(argument, true, expectedReturnValueType = argument.expectedType, suggestions = suggestions)
7476
}
7577

@@ -81,7 +83,8 @@ class Command: Cloneable {
8183
fun build(): Command = this
8284

8385
fun addSubcommand(name: String, builder: Command.() -> Unit) {
84-
if(arguments.isNotEmpty()) throw IllegalStateException("Command cannot have both arguments and subcommands at the same time!")
86+
check(arguments.isEmpty()) { "Command cannot have both arguments and subcommands at the same time!" }
87+
8588
val sanitizedName = name.lowercase().removePrefix("/")
8689
val subcommand = Command()
8790
builder.invoke(subcommand)
@@ -110,14 +113,14 @@ data class CommandExecutor(
110113
) {
111114

112115
fun getPlayerOrThrow(): Player {
113-
if(player == null) throw CommandException("Command was not executed by player")
116+
if (player == null) throw CommandException("Command was not executed by player")
114117
return player
115118
}
116119

117120
fun sendMessage(message: String, isSystem: Boolean = false) {
118-
if(this.isPlayer) this.player!!.sendMessage(message, isSystem) else this.console.sendMessage(message)
121+
if (this.isPlayer) this.player!!.sendMessage(message, isSystem) else this.console.sendMessage(message)
119122
}
120123

121124
fun hasPermission(permission: String): Boolean =
122-
if(this.isPlayer) this.player!!.hasPermission(permission) else true
125+
if (this.isPlayer) this.player!!.hasPermission(permission) else true
123126
}

src/main/kotlin/io/github/dockyardmc/commands/Commands.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ object Commands {
88
var warnAboutCaseSensitivity: Boolean = true
99
var warnWithClosestMatchToInvalidCommand: Boolean = false
1010

11-
fun add(name: String, builder: Command.() -> Unit): Command {
11+
inline fun add(name: String, builder: Command.() -> Unit): Command {
1212
val command = Command()
13+
builder.invoke(command)
14+
return add(name, command)
15+
}
16+
17+
fun add(name: String, command: Command): Command {
1318
val sanitizedName = name.lowercase().removePrefix("/")
1419
command.name = sanitizedName
15-
builder.invoke(command)
1620

1721
commands[sanitizedName] = command
1822
command.aliases.forEach {

src/main/kotlin/io/github/dockyardmc/config/Config.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Config {
4141
this.maxPlayers = maxPlayers
4242
}
4343

44-
fun withImplementations(implementationConfigBuilder: ImplementationConfig.() -> Unit) {
44+
inline fun withImplementations(implementationConfigBuilder: ImplementationConfig.() -> Unit) {
4545
implementationConfigBuilder.invoke(implementationConfig)
4646
}
4747

src/main/kotlin/io/github/dockyardmc/data/CRC32CHasher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ object CRC32CHasher {
126126
return EMPTY_MAP
127127
}
128128

129-
fun of(unit: HashStruct.Builder.() -> Unit): HashStruct {
129+
inline fun of(unit: HashStruct.Builder.() -> Unit): HashStruct {
130130
val builder = HashStruct.Builder()
131131
unit.invoke(builder)
132132
return HashStruct(builder.fields)

src/main/kotlin/io/github/dockyardmc/data/DataComponentPatch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class DataComponentPatch(internal val components: Int2ObjectMap<DataComponent?>,
211211
entry.value!!.write(buffer)
212212
} else {
213213
// Need to length prefix it, so write to another buffer first then copy.
214-
val componentData = Buffer.makeArray { b -> entry.value!!.write(b) }
214+
val componentData = byteBufBytes { b -> entry.value!!.write(b) }
215215
buffer.writeByteArray(componentData)
216216
}
217217
}

0 commit comments

Comments
 (0)