Skip to content

Instantly share code, notes, and snippets.

View DevSrSouza's full-sized avatar

Gabriel Souza DevSrSouza

View GitHub Profile
@DevSrSouza
DevSrSouza / EnchantmentCustom.kt
Last active March 19, 2019 18:25
Simple enchantment API using KotlinBukkitAPI / API simples de encantamento usando KotlinBukkitAPI
import br.com.devsrsouza.kotlinbukkitapi.attributestorage.setStorageData
import br.com.devsrsouza.kotlinbukkitapi.dsl.item.meta
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import java.util.*
class EnchantmentCustom(
val uuid: UUID,
val name: String,
val maxLevel: Int
@DevSrSouza
DevSrSouza / BinaryTree.kt
Last active April 6, 2019 17:40
Arvore Binaria em Kotlin
class Root<E>(var value: E, var root: Root<E>?, var left: Root<E>? = null, var right: Root<E>? = null)
enum class Ordem { V, R, L }
interface IBinaryTree<E> {
val size: Int
fun insert(element: E)
@DevSrSouza
DevSrSouza / BlockNames.kt
Last active November 22, 2020 21:50
Nome das entidades, itens, blocos e encatamentos do minecraft em Português para BukkitAPI (1.8.8)
/**
* @author DevSrSouza
* github: https://github.com/DevSrSouza
* twitter: https://twitter.com/DevSrSouza
*/
val BLOCK_NAMES_PT_BR = mapOf(
MaterialData(Material.ACACIA_FENCE, 0) to "Cerca de Acácia",
MaterialData(Material.ACACIA_FENCE_GATE, 0) to "Portão de Acácia",
MaterialData(Material.ACTIVATOR_RAIL, 0) to "Trilho Ativador",
@DevSrSouza
DevSrSouza / Item.kt
Created December 22, 2019 19:38
Exposed delegate for Bukkit ItemStack
import br.com.devsrsouza.kotlinbukkitapi.server.extensions.itemFromByteArray
import br.com.devsrsouza.kotlinbukkitapi.server.extensions.toByteArray
import org.bukkit.inventory.ItemStack
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.sql.Column
import java.sql.Blob
import javax.sql.rowset.serial.SerialBlob
import kotlin.reflect.KProperty
fun Entity<*>.itemStack(column: Column<Blob>) = ItemStackExposedDelegate(column)
@DevSrSouza
DevSrSouza / ArgumentDSL.kt
Created December 27, 2019 22:46
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
)
@DevSrSouza
DevSrSouza / TakeMaxMilliseconds.kt
Created January 11, 2020 13:57
Take max milliseconds time to do a work and wait to continue doing.
package br.com.devsrsouza.kotlinbukkitapi.controllers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext
import kotlin.system.measureTimeMillis
fun main() {
@DevSrSouza
DevSrSouza / GoldsMenu.kt
Last active January 23, 2020 12:01
KotlinBukkitAPI Menu Pagination sample
private val items = Array<ItemStack>(64) {
item(Material.GOLD_INGOT, it).displayName("$it")
}.toMutableList()
private var menuCache: MenuDSL? = null
val goldsMenu: MenuDSL
get() {
if(menuCache == null)
menuCache = generateMenUConfiguration()
}
@DevSrSouza
DevSrSouza / ServiceExtensions.kt
Last active October 25, 2023 18:14
Connecting to a service using Kotlin Coroutines
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
suspend inline fun <reified S : Service, B : IBinder> Context.connectService(
@DevSrSouza
DevSrSouza / RouletteWheel.kt
Last active April 19, 2020 01:30
Fitness proportionate selection in Kotlin (RouletteWheel)
inline fun <T> List<T>.selectRouletteWheelWithMinimum(
min: Double = 100.0,
sum: (T) -> Double
): T? {
if(isEmpty()) return null
val total = reversed().sumByDouble(sum)
val random = (Math.random() * if(total > min) total else min)
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
typealias InflateBindingCallback<BINDING> = (LayoutInflater, ViewGroup, viewType: Int) -> BINDING
typealias GetViewFromBindingCallback<BINDING> = (BINDING) -> View