[cleanup] Utils lambda inlining

This commit is contained in:
Xiaro 2021-03-06 18:27:38 -05:00
parent 3d9445b4a7
commit 9c6bcd3ddb
No known key found for this signature in database
GPG Key ID: 996D265D6E155377
5 changed files with 28 additions and 26 deletions

View File

@ -76,7 +76,9 @@ object CommandManager : AbstractCommandManager<ClientExecuteEvent>(), AsyncLoade
?: throw SubCommandNotFoundException(event.args, command)
onMainThread {
finalArg.invoke(event)
runBlocking {
finalArg.invoke(event)
}
}
}

View File

@ -18,8 +18,8 @@ import org.kamiblue.client.util.threads.onMainThreadSafe
* or slot 0 if none
*/
inline fun <reified I : Block> SafeClientEvent.swapToBlockOrMove(
crossinline predicateItem: (ItemStack) -> Boolean = { true },
noinline predicateSlot: (ItemStack) -> Boolean = { true }
predicateItem: (ItemStack) -> Boolean = { true },
predicateSlot: (ItemStack) -> Boolean = { true }
): Boolean {
return if (swapToBlock<I>(predicateItem)) {
true
@ -59,8 +59,8 @@ fun SafeClientEvent.swapToBlockOrMove(
* or slot 0 if none
*/
inline fun <reified I : Item> SafeClientEvent.swapToItemOrMove(
crossinline predicateItem: (ItemStack) -> Boolean = { true },
noinline predicateSlot: (ItemStack) -> Boolean = { true }
predicateItem: (ItemStack) -> Boolean = { true },
predicateSlot: (ItemStack) -> Boolean = { true }
): Boolean {
return if (swapToItem<I>(predicateItem)) {
true
@ -117,7 +117,7 @@ fun SafeClientEvent.swapToItemOrMove(
/**
* Try to swap selected hotbar slot to [I] that matches with [predicate]
*/
inline fun <reified I : Block> SafeClientEvent.swapToBlock(crossinline predicate: (ItemStack) -> Boolean = { true }): Boolean {
inline fun <reified I : Block> SafeClientEvent.swapToBlock(predicate: (ItemStack) -> Boolean = { true }): Boolean {
return player.hotbarSlots.firstBlock<I, HotbarSlot>(predicate)?.let {
swapToSlot(it)
true
@ -137,7 +137,7 @@ fun SafeClientEvent.swapToBlock(block: Block, predicate: (ItemStack) -> Boolean
/**
* Try to swap selected hotbar slot to [I] that matches with [predicate]
*/
inline fun <reified I : Item> SafeClientEvent.swapToItem(crossinline predicate: (ItemStack) -> Boolean = { true }): Boolean {
inline fun <reified I : Item> SafeClientEvent.swapToItem(predicate: (ItemStack) -> Boolean = { true }): Boolean {
return player.hotbarSlots.firstItem<I, HotbarSlot>(predicate)?.let {
swapToSlot(it)
true
@ -184,7 +184,7 @@ fun SafeClientEvent.swapToSlot(slot: Int) {
* Swaps the item in [slotFrom] with the first empty hotbar slot
* or matches with [predicate] or slot 0 if none of those found
*/
fun SafeClientEvent.moveToHotbar(slotFrom: Slot, predicate: (ItemStack) -> Boolean): Short {
inline fun SafeClientEvent.moveToHotbar(slotFrom: Slot, predicate: (ItemStack) -> Boolean): Short {
return moveToHotbar(slotFrom.slotNumber, predicate)
}
@ -192,7 +192,7 @@ fun SafeClientEvent.moveToHotbar(slotFrom: Slot, predicate: (ItemStack) -> Boole
* Swaps the item in [slotFrom] with the first empty hotbar slot
* or matches with [predicate] or slot 0 if none of those found
*/
fun SafeClientEvent.moveToHotbar(slotFrom: Int, predicate: (ItemStack) -> Boolean): Short {
inline fun SafeClientEvent.moveToHotbar(slotFrom: Int, predicate: (ItemStack) -> Boolean): Short {
val hotbarSlots = player.hotbarSlots
val slotTo = hotbarSlots.firstItem(Items.AIR)?.hotbarSlot
?: hotbarSlots.firstByStack(predicate)?.hotbarSlot ?: 0

View File

@ -40,7 +40,7 @@ fun Container.getSlots(range: IntRange): List<Slot> =
fun Iterable<Slot>.countEmpty() =
count { it.stack.isEmpty }
inline fun <reified B : Block> Iterable<Slot>.countBlock(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified B : Block> Iterable<Slot>.countBlock(predicate: (ItemStack) -> Boolean = { true }) =
countByStack { itemStack ->
itemStack.item.let { it is ItemBlock && it.block is B } && predicate(itemStack)
}
@ -50,7 +50,7 @@ fun Iterable<Slot>.countBlock(block: Block, predicate: (ItemStack) -> Boolean =
itemStack.item.let { it is ItemBlock && it.block == block } && predicate(itemStack)
}
inline fun <reified I : Item> Iterable<Slot>.countItem(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified I : Item> Iterable<Slot>.countItem(predicate: (ItemStack) -> Boolean = { true }) =
countByStack { it.item is I && predicate(it) }
fun Iterable<Slot>.countItem(item: Item, predicate: (ItemStack) -> Boolean = { true }) =
@ -59,7 +59,7 @@ fun Iterable<Slot>.countItem(item: Item, predicate: (ItemStack) -> Boolean = { t
fun Iterable<Slot>.countID(itemID: Int, predicate: (ItemStack) -> Boolean = { true }) =
countByStack { it.item.id == itemID && predicate(it) }
fun Iterable<Slot>.countByStack(predicate: (ItemStack) -> Boolean = { true }) =
inline fun Iterable<Slot>.countByStack(predicate: (ItemStack) -> Boolean = { true }) =
sumBy { slot ->
slot.stack.let { if (predicate(it)) it.count else 0 }
}
@ -68,7 +68,7 @@ fun Iterable<Slot>.countByStack(predicate: (ItemStack) -> Boolean = { true }) =
fun <T : Slot> Iterable<T>.firstEmpty() =
firstByStack { it.isEmpty }
inline fun <reified B : Block, T : Slot> Iterable<T>.firstBlock(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified B : Block, T : Slot> Iterable<T>.firstBlock(predicate: (ItemStack) -> Boolean = { true }) =
firstByStack { itemStack ->
itemStack.item.let { it is ItemBlock && it.block is B } && predicate(itemStack)
}
@ -78,7 +78,7 @@ fun <T : Slot> Iterable<T>.firstBlock(block: Block, predicate: (ItemStack) -> Bo
itemStack.item.let { it is ItemBlock && it.block == block } && predicate(itemStack)
}
inline fun <reified I : Item, T : Slot> Iterable<T>.firstItem(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified I : Item, T : Slot> Iterable<T>.firstItem(predicate: (ItemStack) -> Boolean = { true }) =
firstByStack {
it.item is I && predicate(it)
}
@ -93,14 +93,14 @@ fun <T : Slot> Iterable<T>.firstID(itemID: Int, predicate: (ItemStack) -> Boolea
it.item.id == itemID && predicate(it)
}
fun <T : Slot> Iterable<T>.firstByStack(predicate: (ItemStack) -> Boolean): T? =
inline fun <T : Slot> Iterable<T>.firstByStack(predicate: (ItemStack) -> Boolean): T? =
firstOrNull { predicate(it.stack) }
inline fun <reified B : Block, T : Slot> Iterable<T>.forEmpty() =
filterByStack { it.isEmpty }
inline fun <reified B : Block, T : Slot> Iterable<T>.filterByBlock(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified B : Block, T : Slot> Iterable<T>.filterByBlock(predicate: (ItemStack) -> Boolean = { true }) =
filterByStack { itemStack ->
itemStack.item.let { it is ItemBlock && it.block is B } && predicate(itemStack)
}
@ -110,7 +110,7 @@ fun <T : Slot> Iterable<T>.filterByBlock(block: Block, predicate: (ItemStack) ->
itemStack.item.let { it is ItemBlock && it.block == block } && predicate(itemStack)
}
inline fun <reified I : Item, T : Slot> Iterable<T>.filterByItem(crossinline predicate: (ItemStack) -> Boolean = { true }) =
inline fun <reified I : Item, T : Slot> Iterable<T>.filterByItem(predicate: (ItemStack) -> Boolean = { true }) =
filterByStack {
it.item is I && predicate(it)
}
@ -123,7 +123,7 @@ fun <T : Slot> Iterable<T>.filterByItem(item: Item, predicate: (ItemStack) -> Bo
fun <T : Slot> Iterable<T>.filterByID(itemID: Int, predicate: (ItemStack) -> Boolean = { true }) =
filterByStack { it.item.id == itemID && predicate(it) }
fun <T : Slot> Iterable<T>.filterByStack(predicate: (ItemStack) -> Boolean = { true }) =
inline fun <T : Slot> Iterable<T>.filterByStack(predicate: (ItemStack) -> Boolean = { true }) =
filter { predicate(it.stack) }

View File

@ -29,14 +29,14 @@ object MainThreadExecutor {
runBlocking {
mutex.withLock {
jobs.forEach {
launch { it.run() }
it.run()
}
jobs.clear()
}
}
}
suspend fun <T> add(block: suspend () -> T) =
suspend fun <T> add(block: () -> T) =
MainThreadJob(block).apply {
if (Wrapper.minecraft.isCallingFromMinecraftThread) {
run()
@ -47,10 +47,10 @@ object MainThreadExecutor {
}
}.deferred
private class MainThreadJob<T>(private val block: suspend () -> T) {
private class MainThreadJob<T>(private val block: () -> T) {
val deferred = CompletableDeferred<T>()
suspend fun run() {
fun run() {
deferred.completeWith(
runCatching { block.invoke() }
)

View File

@ -34,11 +34,11 @@ fun ClientExecuteEvent.toSafe() =
if (world != null && player != null && playerController != null && connection != null) SafeExecuteEvent(world, player, playerController, connection, this)
else null
fun runSafe(block: SafeClientEvent.() -> Unit) {
inline fun runSafe(block: SafeClientEvent.() -> Unit) {
ClientEvent().toSafe()?.let { block(it) }
}
fun <R> runSafeR(block: SafeClientEvent.() -> R): R? {
inline fun <R> runSafeR(block: SafeClientEvent.() -> R): R? {
return ClientEvent().toSafe()?.let { block(it) }
}
@ -54,7 +54,7 @@ suspend fun <R> runSafeSuspend(block: suspend SafeClientEvent.() -> R): R? {
*
* @see [onMainThread]
*/
suspend fun <T> onMainThreadSafe(block: suspend SafeClientEvent.() -> T) =
suspend fun <T> onMainThreadSafe(block: SafeClientEvent.() -> T) =
onMainThread { ClientEvent().toSafe()?.block() }
/**
@ -64,5 +64,5 @@ suspend fun <T> onMainThreadSafe(block: suspend SafeClientEvent.() -> T) =
*
* @see [onMainThreadSafe]
*/
suspend fun <T> onMainThread(block: suspend () -> T) =
suspend fun <T> onMainThread(block: () -> T) =
MainThreadExecutor.add(block)