Skip to content

Instantly share code, notes, and snippets.

@DevSrSouza
Created December 27, 2019 22:46
Show Gist options
  • Select an option

  • Save DevSrSouza/12bd98fb49995e4db1481f01702d4e61 to your computer and use it in GitHub Desktop.

Select an option

Save DevSrSouza/12bd98fb49995e4db1481f01702d4e61 to your computer and use it in GitHub Desktop.
Argument DSL from old KotlinBukkitAPI
fun <T : CommandSender> Executor<T>.argumentExecutorBuilder(
posIndex: Int = 1,
label: String
) = Executor(
sender,
[email protected] + " " + label,
runCatching { args.sliceArray(posIndex..args.size) }.getOrDefault((emptyArray())),
command
)
inline fun <T : CommandSender> Executor<T>.argumentPlayer(
notOnline: BaseComponent = PLAYER_NOT_ONLINE,
argMissing: BaseComponent = PLAYER_MISSING_PARAMETER,
index: Int = 0,
block: Executor<T>.(Player) -> Unit
) {
val player = player(index, argMissing, notOnline)
argumentExecutorBuilder(index + 1, player.name).block(player)
}
inline fun <T : CommandSender> Executor<T>.argumentOfflinePlayer(
argMissing: BaseComponent = PLAYER_MISSING_PARAMETER,
index: Int = 0,
block: Executor<T>.(OfflinePlayer) -> Unit
) {
val player = offlinePlayer(index, argMissing)
argumentExecutorBuilder(index + 1, player.name).block(player)
}
inline fun <T : CommandSender> Executor<T>.argumentMaterial(
argMissing: BaseComponent = MATERIAL_MISSING_PARAMETER,
notFound: BaseComponent = MATERIAL_NOT_FOUND,
index: Int = 0,
block: Executor<T>.(Material) -> Unit
) {
val material = material(index, argMissing, notFound)
argumentExecutorBuilder(index + 1, material.name).block(material)
}
inline fun <T : CommandSender> Executor<T>.argumentMaterialData(
argMissing: BaseComponent = MATERIAL_MISSING_PARAMETER,
notFound: BaseComponent = MATERIAL_NOT_FOUND,
dataFormat: BaseComponent = DATA_FORMAT,
index: Int = 0,
block: Executor<T>.(MaterialData) -> Unit
) {
val material = materialData(index, argMissing, notFound, dataFormat)
argumentExecutorBuilder(index + 1, "${material.itemType.name}:${material.data}").block(material)
}
inline fun <T : CommandSender> Executor<T>.argumentGameMode(
argMissing: BaseComponent = GAMEMODE_MISSING_PARAMETER,
notFound: BaseComponent = GAMEMODE_NOT_FOUND,
index: Int = 0,
block: Executor<T>.(GameMode) -> Unit
) {
val gameMode = gameMode(index, argMissing, notFound)
argumentExecutorBuilder(index + 1, gameMode.name).block(gameMode)
}
inline fun <T : CommandSender> Executor<T>.booleanArgument(
argMissing: BaseComponent = MISSING_BOOLEAN_PARAMETER,
booleanFormat: BaseComponent = BOOLEAN_FORMAT,
trueCases: Array<String> = TRUE_CASES,
falseCases: Array<String> = FALSE_CASES,
index: Int = 0,
block: Executor<T>.(Boolean) -> Unit
) {
val boolean = boolean(index, argMissing, booleanFormat, trueCases, falseCases)
argumentExecutorBuilder(index + 1, "$boolean").block(boolean)
}
inline fun <T : CommandSender> Executor<T>.argumentInt(
argMissing: BaseComponent = MISSING_NUMBER_PARAMETER,
numberFormat: BaseComponent = NUMBER_FORMAT,
index: Int = 0,
block: Executor<T>.(Int) -> Unit
) {
val int = int(index, argMissing, numberFormat)
argumentExecutorBuilder(index + 1, "$int").block(int)
}
inline fun <T : CommandSender> Executor<T>.argumentDouble(
argMissing: BaseComponent = MISSING_NUMBER_PARAMETER,
numberFormat: BaseComponent = NUMBER_FORMAT,
index: Int = 0,
block: Executor<T>.(Double) -> Unit
) {
val double = double(index, argMissing, numberFormat)
argumentExecutorBuilder(index + 1, "$double").block(double)
}
inline fun <T : CommandSender> Executor<T>.argumentWorld(
argMissing: BaseComponent = MISSING_WORLD_ARGUMENT,
notFound: BaseComponent = WORLD_NOT_FOUND,
index: Int = 0,
block: Executor<T>.(World) -> Unit
) {
val world = world(index, argMissing, notFound)
argumentExecutorBuilder(
index + 1,
world.name
).block(world)
}
inline fun <T : CommandSender> Executor<T>.argumentCoordinate(
world: World,
argMissing: BaseComponent = MISSING_COORDINATE_ARGUMENT,
numberFormat: BaseComponent = COORDINATE_NUMBER_FORMAT,
startIndex: Int = 0,
block: Executor<T>.(Location) -> Unit
) {
val location = coordinate(startIndex,
startIndex + 1,
startIndex + 2,
world, argMissing, numberFormat)
argumentExecutorBuilder(
startIndex + 3,
"${location.x} ${location.y} ${location.z}"
).block(location)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment