Merge pull request #6 from bluefireoly/codestyle

Applied kotlin style conventions
This commit is contained in:
Jakob K
2020-10-18 18:38:53 +02:00
committed by GitHub
39 changed files with 509 additions and 385 deletions

View File

@@ -877,5 +877,5 @@ public class KColors {
* <div style="border:1px solid black;width:120px;height:120px;background-color:#9ACD32;float:right;margin: 0 10px 0 0"></div><br/><br/>
*/
public static final ChatColor YELLOWGREEN = ChatColor.of(new Color(0.6039216f, 0.8039216f, 0.19607843f));
}

View File

@@ -37,7 +37,11 @@ class KSpigotComponentBuilder {
this += SelectorComponent(selector).apply(builder)
}
inline fun translatable(translatable: String, with: Array<BaseComponent>, builder: TranslatableComponent.() -> Unit = { }) {
inline fun translatable(
translatable: String,
with: Array<BaseComponent>,
builder: TranslatableComponent.() -> Unit = { }
) {
this += TranslatableComponent(translatable, with).apply(builder)
}
@@ -47,8 +51,13 @@ class KSpigotComponentBuilder {
this += TextComponent.fromLegacyText(text, color)
}
operator fun plusAssign(baseComponent: BaseComponent) { components += baseComponent }
operator fun plusAssign(baseComponents: Array<out BaseComponent>) { components += baseComponents }
operator fun plusAssign(baseComponent: BaseComponent) {
components += baseComponent
}
operator fun plusAssign(baseComponents: Array<out BaseComponent>) {
components += baseComponents
}
fun create() = components.toTypedArray()

View File

@@ -28,24 +28,26 @@ import kotlin.reflect.KProperty
* exist and no default config is specified.
*/
inline fun <reified T : Any> kSpigotJsonConfig(
file: File,
noinline default: (() -> T)? = null,
file: File,
noinline default: (() -> T)? = null,
) = ConfigDelegate(T::class, file, default)
/**
* @see kSpigotJsonConfig
*/
class ConfigDelegate<T : Any> (
private val configClass: KClass<T>,
private val file: File,
private val defaultCallback: (() -> T)?
class ConfigDelegate<T : Any>(
private val configClass: KClass<T>,
private val file: File,
private val defaultCallback: (() -> T)?
) {
private var internalConfig: T = loadIt()
var data: T
get() = internalConfig
set(value) { internalConfig = value }
set(value) {
internalConfig = value
}
operator fun getValue(thisRef: Any?, property: KProperty<*>) = internalConfig
@@ -62,25 +64,26 @@ class ConfigDelegate<T : Any> (
/**
* Loads the current state of the config on disk to the config object.
*/
fun reload() { loadIt() }
fun reload() {
loadIt()
}
private fun saveIt(toSave: T) {
GsonConfigManager.saveConfig(file, toSave, true)
internalConfig = toSave
}
private fun loadIt()
= if (defaultCallback == null)
GsonConfigManager.loadConfig(file, configClass)
else
GsonConfigManager.loadOrCreateDefault(file, configClass, true, defaultCallback)
private fun loadIt() = if (defaultCallback == null)
GsonConfigManager.loadConfig(file, configClass)
else
GsonConfigManager.loadOrCreateDefault(file, configClass, true, defaultCallback)
}
internal object GsonConfigManager {
fun <T : Any> loadConfig(file: File, configClass: KClass<T>): T
= FileReader(file).use { reader -> return getGson(false).fromJson(reader, configClass.java) }
fun <T : Any> loadConfig(file: File, configClass: KClass<T>): T =
FileReader(file).use { reader -> return getGson(false).fromJson(reader, configClass.java) }
fun <T : Any> saveConfig(file: File, config: T, pretty: Boolean = true) {
file.createIfNotExists()

View File

@@ -3,10 +3,9 @@ package net.axay.kspigot.config
import net.axay.kspigot.main.KSpigotMainInstance
import java.io.File
class PluginFile(path: String, child: String? = null)
: File(
"${KSpigotMainInstance.dataFolder}",
run {
if (child == null) path else File(path, child).path
}
)
class PluginFile(path: String, child: String? = null) : File(
"${KSpigotMainInstance.dataFolder}",
run {
if (child == null) path else File(path, child).path
}
)

View File

@@ -19,8 +19,9 @@ var Entity.nbtData: NBTData
}
@NMS_General
val ItemStack.nbtData: NBTData get() {
CraftItemStack.asNMSCopy(this).let {
return if (it.hasTag()) NBTData(it.tag) else NBTData()
}
}
val ItemStack.nbtData: NBTData
get() {
CraftItemStack.asNMSCopy(this).let {
return if (it.hasTag()) NBTData(it.tag) else NBTData()
}
}

View File

@@ -11,17 +11,27 @@ interface NBTDataType<T> {
companion object {
val COMPOUND = nbtDataType<NBTData, NBTTagCompound>({ NBTData(it) }, { key, data, compound -> compound.set(key, data.nbtTagCompound) })
val BYTE = nbtDataType<Byte, NBTTagByte>({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) })
val BYTE_ARRAY = nbtDataType<ByteArray, NBTTagByteArray>({ it.bytes }, { key, data, compound -> compound.setByteArray(key, data) })
val DOUBLE = nbtDataType<Double, NBTTagDouble>({ it.asDouble() }, { key, data, compound -> compound.setDouble(key, data) })
val FLOAT = nbtDataType<Float, NBTTagFloat>({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) })
val COMPOUND = nbtDataType<NBTData, NBTTagCompound>({ NBTData(it) },
{ key, data, compound -> compound.set(key, data.nbtTagCompound) })
val BYTE =
nbtDataType<Byte, NBTTagByte>({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) })
val BYTE_ARRAY = nbtDataType<ByteArray, NBTTagByteArray>({ it.bytes },
{ key, data, compound -> compound.setByteArray(key, data) })
val DOUBLE = nbtDataType<Double, NBTTagDouble>({ it.asDouble() },
{ key, data, compound -> compound.setDouble(key, data) })
val FLOAT =
nbtDataType<Float, NBTTagFloat>({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) })
val INT = nbtDataType<Int, NBTTagInt>({ it.asInt() }, { key, data, compound -> compound.setInt(key, data) })
val INT_ARRAY = nbtDataType<IntArray, NBTTagIntArray>({ it.ints }, { key, data, compound -> compound.setIntArray(key, data) })
val LONG = nbtDataType<Long, NBTTagLong>({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) })
val LONG_ARRAY = nbtDataType<LongArray, NBTTagLongArray>({ it.longs }, { key, data, compound -> compound.set(key, NBTTagLongArray(data)) })
val SHORT = nbtDataType<Short, NBTTagShort>({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) })
val STRING = nbtDataType<String, NBTTagString>({ it.asString() }, { key, data, compound -> compound.setString(key, data) })
val INT_ARRAY = nbtDataType<IntArray, NBTTagIntArray>({ it.ints },
{ key, data, compound -> compound.setIntArray(key, data) })
val LONG =
nbtDataType<Long, NBTTagLong>({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) })
val LONG_ARRAY = nbtDataType<LongArray, NBTTagLongArray>({ it.longs },
{ key, data, compound -> compound.set(key, NBTTagLongArray(data)) })
val SHORT =
nbtDataType<Short, NBTTagShort>({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) })
val STRING = nbtDataType<String, NBTTagString>({ it.asString() },
{ key, data, compound -> compound.setString(key, data) })
}
@@ -32,15 +42,16 @@ interface NBTDataType<T> {
* @property E the NBT data type
*/
private inline fun <T, reified E> nbtDataType(
crossinline decodeNMS: (E) -> T,
crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit
crossinline decodeNMS: (E) -> T,
crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit
): NBTDataType<T> {
return object : NBTDataType<T> {
override fun decodeNMS(nbtBase: NBTBase) = if (nbtBase is E) decodeNMS.invoke(nbtBase) else null
override fun writeToCompound(key: String, data: T, compound: NBTTagCompound) = writeToCompound.invoke(key, data, compound)
override fun writeToCompound(key: String, data: T, compound: NBTTagCompound) =
writeToCompound.invoke(key, data, compound)
}

View File

@@ -26,9 +26,9 @@ fun Listener.unregister() = HandlerList.unregisterAll(this)
* @param executor handles incoming events
*/
inline fun <reified T : Event> Listener.register(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
noinline executor: (Listener, Event) -> Unit
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
noinline executor: (Listener, Event) -> Unit
) {
pluginManager.registerEvent(T::class.java, this, priority, executor, KSpigotMainInstance, ignoreCancelled)
}
@@ -49,8 +49,8 @@ interface SingleListener<T : Event> : Listener {
* @param ignoreCancelled if manual cancellation should be ignored
*/
inline fun <reified T : Event> SingleListener<T>.register(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false
) {
register<T>(priority, ignoreCancelled) { _, event ->
(event as? T)?.let { this.onEvent(it) }
@@ -64,9 +64,9 @@ inline fun <reified T : Event> SingleListener<T>.register(
* @param onEvent the event callback
*/
inline fun <reified T : Event> listen(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
crossinline onEvent: (T) -> Unit
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
crossinline onEvent: (T) -> Unit
): SingleListener<T> {
val listener = object : SingleListener<T> {
override fun onEvent(event: T) = onEvent.invoke(event)

View File

@@ -6,27 +6,27 @@ import org.bukkit.command.CommandSender
import org.bukkit.plugin.Plugin
/** @see printColoredPrefix */
fun CommandSender.print(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.RESET, plugin?.name ?: "INFO", ChatColor.GRAY)
fun CommandSender.print(text: String, plugin: Plugin? = KSpigotMainInstance) =
printColoredPrefix(text, ChatColor.RESET, plugin?.name ?: "INFO", ChatColor.GRAY)
/** @see printColoredPrefix */
fun CommandSender.info(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.WHITE, plugin?.name ?: "INFO", ChatColor.DARK_AQUA)
fun CommandSender.info(text: String, plugin: Plugin? = KSpigotMainInstance) =
printColoredPrefix(text, ChatColor.WHITE, plugin?.name ?: "INFO", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.success(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.GREEN, plugin?.name ?: "SUCCESS", ChatColor.DARK_AQUA)
fun CommandSender.success(text: String, plugin: Plugin? = KSpigotMainInstance) =
printColoredPrefix(text, ChatColor.GREEN, plugin?.name ?: "SUCCESS", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.warn(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.WHITE, plugin?.name?.plus(" - WARN") ?: "WARN", ChatColor.YELLOW)
fun CommandSender.warn(text: String, plugin: Plugin? = KSpigotMainInstance) =
printColoredPrefix(text, ChatColor.WHITE, plugin?.name?.plus(" - WARN") ?: "WARN", ChatColor.YELLOW)
/** @see printColoredPrefix */
fun CommandSender.error(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.RED, plugin?.name?.plus(" - ERROR") ?: "ERROR", ChatColor.DARK_RED)
fun CommandSender.error(text: String, plugin: Plugin? = KSpigotMainInstance) =
printColoredPrefix(text, ChatColor.RED, plugin?.name?.plus(" - ERROR") ?: "ERROR", ChatColor.DARK_RED)
/**
* Sends the given message and adds the given prefix with the given color to it.
*/
fun CommandSender.printColoredPrefix(text: String, textColor: ChatColor, prefix: String, prefixColor: ChatColor)
= sendMessage("${prefixColor}[${prefix}]${textColor} $text")
fun CommandSender.printColoredPrefix(text: String, textColor: ChatColor, prefix: String, prefixColor: ChatColor) =
sendMessage("${prefixColor}[${prefix}]${textColor} $text")

View File

@@ -43,7 +43,7 @@ fun Damageable.kill() {
*/
fun LivingEntity.heal() {
health = getAttribute(Attribute.GENERIC_MAX_HEALTH)?.value
?: throw NullPointerException("The entity does not have a max health value!")
?: throw NullPointerException("The entity does not have a max health value!")
}
/**
@@ -110,7 +110,13 @@ fun Location.spawnCleanEntity(entityType: EntityType): Entity? {
* @param stay time in ticks for titles to stay
* @param fadeOut time in ticks for titles to fade out
*/
fun Player.title(mainText: String? = null, subText: String? = null, fadeIn: Int = 10, stay: Int = 70, fadeOut: Int = 20) {
fun Player.title(
mainText: String? = null,
subText: String? = null,
fadeIn: Int = 10,
stay: Int = 70,
fadeOut: Int = 20
) {
sendTitle(mainText, subText, fadeIn, stay, fadeOut)
}

View File

@@ -7,5 +7,6 @@ import org.bukkit.World
* Assumes that this Location has world data.
* If not, an exception will be thrown.
*/
val Location.worldOrException: World get() = world
?: throw NullPointerException("The world of the location is null!")
val Location.worldOrException: World
get() = world
?: throw NullPointerException("The world of the location is null!")

View File

@@ -10,10 +10,12 @@ import org.bukkit.inventory.ItemStack
* the result is equal to [Material.AIR].
*/
val PrepareItemCraftEvent.isCancelled: Boolean
get() = this.inventory.result?.type == Material.AIR
get() = this.inventory.result?.type == Material.AIR
/**
* "Cancels" this event by
* setting the result to [Material.AIR].
*/
fun PrepareItemCraftEvent.cancel() { this.inventory.result = ItemStack(Material.AIR) }
fun PrepareItemCraftEvent.cancel() {
this.inventory.result = ItemStack(Material.AIR)
}

View File

@@ -20,16 +20,16 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
val maxSimpleLoc = SimpleLocation3D(max(loc1.x, loc2.x), max(loc1.y, loc2.y), max(loc1.z, loc2.z))
fun isInArea(
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
): Boolean {
// checking world
if (loc.world != world) return false
return if (
// checking x
// checking x
loc.x >= minSimpleLoc.x - tolerance && loc.x <= maxSimpleLoc.x + tolerance &&
// checking z
loc.z >= minSimpleLoc.z - tolerance && loc.z <= maxSimpleLoc.z + tolerance
@@ -45,9 +45,10 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
val foundChunks = HashSet<SimpleChunkLocation>()
(minSimpleLoc.chunk.x until maxSimpleLoc.chunk.x + 1).forEach { curX ->
(minSimpleLoc.chunk.z until maxSimpleLoc.chunk.z + 1).forEach { curZ ->
foundChunks += SimpleChunkLocation(curX, curZ)
} }
(minSimpleLoc.chunk.z until maxSimpleLoc.chunk.z + 1).forEach { curZ ->
foundChunks += SimpleChunkLocation(curX, curZ)
}
}
return@lazy foundChunks
@@ -77,9 +78,9 @@ class LocationArea(loc1: Location, loc2: Location) {
val touchedChunks: Set<Chunk> get() = simpleLocationPair.touchedSimpleChunks.mapTo(HashSet()) { it.withWorld(world) }
fun isInArea(
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
) = simpleLocationPair.isInArea(loc, check3d, tolerance)
}

View File

@@ -12,10 +12,12 @@ import org.bukkit.util.Vector
// INCREASE
// all
infix fun Location.increase(distance: Number) = add(distance, distance, distance)
// single
infix fun Location.increaseX(distance: Number) = add(distance, 0.0, 0.0)
infix fun Location.increaseY(distance: Number) = add(0.0, distance, 0.0)
infix fun Location.increaseZ(distance: Number) = add(0.0, 0.0, distance)
// pair
infix fun Location.increaseXY(distance: Number) = add(distance, distance, 0.0)
infix fun Location.increaseYZ(distance: Number) = add(0.0, distance, distance)
@@ -24,10 +26,12 @@ infix fun Location.increaseXZ(distance: Number) = add(distance, 0.0, distance)
// REDUCE
// all
infix fun Location.reduce(distance: Number) = substract(distance, distance, distance)
// single
infix fun Location.reduceX(distance: Number) = substract(distance, 0.0, 0.0)
infix fun Location.reduceY(distance: Number) = substract(0.0, distance, 0.0)
infix fun Location.reduceZ(distance: Number) = substract(0.0, 0.0, distance)
// pair
infix fun Location.reduceXY(distance: Number) = substract(distance, distance, 0.0)
infix fun Location.reduceYZ(distance: Number) = substract(0.0, distance, distance)
@@ -50,13 +54,32 @@ operator fun Location.plus(loc: Location) = clone().add(loc)
operator fun Location.minus(loc: Location) = clone().subtract(loc)
operator fun Location.plus(loc: SimpleLocation3D) = clone().add(loc.x, loc.y, loc.z)
operator fun Location.minus(loc: SimpleLocation3D) = clone().subtract(loc.x, loc.y, loc.z)
// mutable
operator fun Location.plusAssign(vec: Vector) { add(vec) }
operator fun Location.minusAssign(vec: Vector) { subtract(vec) }
operator fun Location.plusAssign(loc: Location) { add(loc) }
operator fun Location.minusAssign(loc: Location) { subtract(loc) }
operator fun Location.plusAssign(loc: SimpleLocation3D) { add(loc.x, loc.y, loc.z) }
operator fun Location.minusAssign(loc: SimpleLocation3D) { subtract(loc.x, loc.y, loc.z) }
operator fun Location.plusAssign(vec: Vector) {
add(vec)
}
operator fun Location.minusAssign(vec: Vector) {
subtract(vec)
}
operator fun Location.plusAssign(loc: Location) {
add(loc)
}
operator fun Location.minusAssign(loc: Location) {
subtract(loc)
}
operator fun Location.plusAssign(loc: SimpleLocation3D) {
add(loc.x, loc.y, loc.z)
}
operator fun Location.minusAssign(loc: SimpleLocation3D) {
subtract(loc.x, loc.y, loc.z)
}
// mutable with return
infix fun Location.increase(vec: Vector) = add(vec)
infix fun Location.reduce(vec: Vector) = subtract(vec)
@@ -86,11 +109,24 @@ operator fun Vector.plus(vec: Vector) = clone().add(vec)
operator fun Vector.minus(vec: Vector) = clone().subtract(vec)
operator fun Vector.times(vec: Vector) = clone().multiply(vec)
operator fun Vector.times(num: Number) = clone().multiply(num.toDouble())
// mutable
operator fun Vector.plusAssign(vec: Vector) { add(vec) }
operator fun Vector.minusAssign(vec: Vector) { subtract(vec) }
operator fun Vector.timesAssign(vec: Vector) { multiply(vec) }
operator fun Vector.timesAssign(num: Number) { multiply(num.toDouble()) }
operator fun Vector.plusAssign(vec: Vector) {
add(vec)
}
operator fun Vector.minusAssign(vec: Vector) {
subtract(vec)
}
operator fun Vector.timesAssign(vec: Vector) {
multiply(vec)
}
operator fun Vector.timesAssign(num: Number) {
multiply(num.toDouble())
}
// mutable with return
infix fun Vector.increase(vec: Vector) = add(vec)
infix fun Vector.reduce(vec: Vector) = subtract(vec)

View File

@@ -11,14 +11,14 @@ class GamePhaseSystem(vararg gamePhases: GamePhase) {
}
fun buildCounterMessageCallback(
beforeTime: String? = null,
afterTime: String? = null,
hourPlural: String = "h",
minutePlural: String = "m",
secondPlural: String = "s",
hourSingular: String = hourPlural,
minuteSingular: String = minutePlural,
secondSingular: String = secondPlural
beforeTime: String? = null,
afterTime: String? = null,
hourPlural: String = "h",
minutePlural: String = "m",
secondPlural: String = "s",
hourSingular: String = hourPlural,
minuteSingular: String = minutePlural,
secondSingular: String = secondPlural
): (Long) -> String = { curSeconds ->
StringBuilder().apply {
@@ -50,24 +50,24 @@ fun buildCounterMessageCallback(
}
class GamePhase(
val length: Long,
val start: (() -> Unit)?,
val end: (() -> Unit)?,
val counterMessage: ((secondsLeft: Long) -> String)?
val length: Long,
val start: (() -> Unit)?,
val end: (() -> Unit)?,
val counterMessage: ((secondsLeft: Long) -> String)?
) {
fun startIt(phaseQueue: MutableList<GamePhase>) {
start?.invoke()
task(
period = 20,
howOften = (length / 20) + 1,
endCallback = {
period = 20,
howOften = (length / 20) + 1,
endCallback = {
end?.invoke()
end?.invoke()
if (phaseQueue.isNotEmpty())
phaseQueue.removeAt(0).startIt(phaseQueue)
if (phaseQueue.isNotEmpty())
phaseQueue.removeAt(0).startIt(phaseQueue)
}
}
) {
if (counterMessage != null) {
@@ -80,8 +80,9 @@ class GamePhase(
}
}
private val Long.isCounterValue: Boolean get() = when (this) {
1L, 2L, 3L, 4L, 5L, 10L, 15L, 20L, 30L -> true
0L -> false
else -> this % 60 == 0L
}
private val Long.isCounterValue: Boolean
get() = when (this) {
1L, 2L, 3L, 4L, 5L, 10L, 15L, 20L, 30L -> true
0L -> false
else -> this % 60 == 0L
}

View File

@@ -64,8 +64,8 @@ object InventoryGUIHolder : AutoCloseable {
// EVENT
class InventoryGUIClickEvent<T : ForInventory>(
val bukkitEvent: InventoryClickEvent,
val gui: InventoryGUI<T>
val bukkitEvent: InventoryClickEvent,
val gui: InventoryGUI<T>
)
/*
@@ -84,16 +84,22 @@ class InventoryGUIData<T : ForInventory>(
)
abstract class InventoryGUI<T : ForInventory>(
val data: InventoryGUIData<T>
val data: InventoryGUIData<T>
) {
var currentPageInt: Int = DEFAULT_PAGE; protected set
val currentPage get() = getPage(currentPageInt)
val currentPage
get() = getPage(currentPageInt)
?: throw IllegalStateException("The currentPageInt has no associated page!")
internal abstract val bukkitInventory: Inventory
internal abstract fun loadPageUnsafe(page: InventoryGUIPage<*>, offsetHorizontally: Int = 0, offsetVertically: Int = 0)
internal abstract fun loadPageUnsafe(
page: InventoryGUIPage<*>,
offsetHorizontally: Int = 0,
offsetVertically: Int = 0
)
internal abstract fun loadPageUnsafe(page: Int, offsetHorizontally: Int = 0, offsetVertically: Int = 0)
/**
@@ -135,12 +141,14 @@ abstract class InventoryGUI<T : ForInventory>(
// Inventory GUI implementations
class InventoryGUIShared<T : ForInventory>(
inventoryGUIData: InventoryGUIData<T>
inventoryGUIData: InventoryGUIData<T>
) : InventoryGUI<T>(inventoryGUIData) {
override val bukkitInventory = data.inventoryType.createBukkitInv(null, data.title)
init { loadPageUnsafe(DEFAULT_PAGE) }
init {
loadPageUnsafe(DEFAULT_PAGE)
}
override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory

View File

@@ -6,12 +6,12 @@ import net.axay.kspigot.inventory.elements.*
import org.bukkit.inventory.ItemStack
fun <T : ForInventory> inventoryGUI(
type: InventoryType<T>,
builder: InventoryGUIBuilder<T>.() -> Unit,
type: InventoryType<T>,
builder: InventoryGUIBuilder<T>.() -> Unit,
) = InventoryGUIBuilder(type).apply(builder).build()
class InventoryGUIBuilder<T : ForInventory>(
val type: InventoryType<T>
val type: InventoryType<T>
) {
var title: String = ""
@@ -37,14 +37,14 @@ class InventoryGUIBuilder<T : ForInventory>(
}
internal fun build() = InventoryGUIShared(
InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
).apply { register() }
}
class InventoryGUIPageBuilder<T : ForInventory>(
val type: InventoryType<T>,
val page: Int
val type: InventoryType<T>,
val page: Int
) {
private val guiSlots = HashMap<Int, InventoryGUISlot<T>>()
@@ -59,74 +59,96 @@ class InventoryGUIPageBuilder<T : ForInventory>(
* actions. If clicked, the specified [onClick]
* function is invoked.
*/
fun button(slots: InventorySlotCompound<T>, itemStack: ItemStack, onClick: (InventoryGUIClickEvent<T>) -> Unit)
= defineSlots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick))
fun button(slots: InventorySlotCompound<T>, itemStack: ItemStack, onClick: (InventoryGUIClickEvent<T>) -> Unit) =
defineSlots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick))
/**
* An item protected from any player actions.
* This is not a button.
*/
fun placeholder(slots: InventorySlotCompound<T>, itemStack: ItemStack)
= defineSlots(slots, InventoryGUIPlaceholder(InventoryGUIElementData(itemStack)))
fun placeholder(slots: InventorySlotCompound<T>, itemStack: ItemStack) =
defineSlots(slots, InventoryGUIPlaceholder(InventoryGUIElementData(itemStack)))
/**
* A free slot does not block any player actions.
* The player can put items in this slot or take
* items out of it.
*/
fun freeSlot(slots: InventorySlotCompound<T>)
= defineSlots(slots, InventoryGUIFreeSlot())
fun freeSlot(slots: InventorySlotCompound<T>) = defineSlots(slots, InventoryGUIFreeSlot())
/**
* This is a button which loads the specified
* [toPage] if clicked.
*/
fun pageChanger(slots: InventorySlotCompound<T>, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= defineSlots(slots, InventoryGUIButtonPageChange(
fun pageChanger(
slots: InventorySlotCompound<T>,
itemStack: ItemStack,
toPage: Int,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUIConsistentPageCalculator(toPage),
onChange
))
)
)
/**
* This button always tries to find the previous
* page if clicked, and if a previous page
* exists it is loaded.
*/
fun previousPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= defineSlots(slots, InventoryGUIButtonPageChange(
fun previousPage(
slots: InventorySlotCompound<T>,
itemStack: ItemStack,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUIPreviousPageCalculator,
onChange
))
)
)
/**
* This button always tries to find the next
* page if clicked, and if a next page
* exists it is loaded.
*/
fun nextPage(slots: InventorySlotCompound<T>, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= defineSlots(slots, InventoryGUIButtonPageChange(
fun nextPage(
slots: InventorySlotCompound<T>,
itemStack: ItemStack,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, InventoryGUIButtonPageChange(
InventoryGUIElementData(itemStack),
InventoryGUIPageChangeCalculator.InventoryGUINextPageCalculator,
onChange
))
)
)
/**
* By pressing this button, the player switches to another
* InventoryGUI. The transition effect is applied.
*/
fun changeGUI(slots: InventorySlotCompound<T>, itemStack: ItemStack, newGUI: () -> InventoryGUI<*>, newPage: Int? = null, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= defineSlots(slots, InventoryGUIButtonInventoryChange(
fun changeGUI(
slots: InventorySlotCompound<T>,
itemStack: ItemStack,
newGUI: () -> InventoryGUI<*>,
newPage: Int? = null,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null
) = defineSlots(
slots, InventoryGUIButtonInventoryChange(
InventoryGUIElementData(itemStack),
newGUI,
newPage,
onChange
))
)
)
private fun defineSlots(slots: InventorySlotCompound<T>, element: InventoryGUISlot<T>)
= slots.withInvType(type).forEach { curSlot ->
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
private fun defineSlots(slots: InventorySlotCompound<T>, element: InventoryGUISlot<T>) =
slots.withInvType(type).forEach { curSlot ->
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
}
}

View File

@@ -9,11 +9,11 @@ abstract class InventoryGUISlot<T : ForInventory> {
// ELEMENT
class InventoryGUIElementData(
val itemStack: ItemStack
val itemStack: ItemStack
)
abstract class InventoryGUIElement<T : ForInventory>(
val inventoryGUIElementData: InventoryGUIElementData
val inventoryGUIElementData: InventoryGUIElementData
) : InventoryGUISlot<T>() {
final override fun onClick(clickEvent: InventoryGUIClickEvent<T>) {

View File

@@ -7,13 +7,13 @@ abstract class InventoryGUIPageChangeCalculator {
abstract fun calculateNewPage(currentPage: Int, pages: Collection<Int>): Int?
object InventoryGUIPreviousPageCalculator : InventoryGUIPageChangeCalculator() {
override fun calculateNewPage(currentPage: Int, pages: Collection<Int>)
= pages.sortedDescending().find { it < currentPage }
override fun calculateNewPage(currentPage: Int, pages: Collection<Int>) =
pages.sortedDescending().find { it < currentPage }
}
object InventoryGUINextPageCalculator : InventoryGUIPageChangeCalculator() {
override fun calculateNewPage(currentPage: Int, pages: Collection<Int>)
= pages.sorted().find { it > currentPage }
override fun calculateNewPage(currentPage: Int, pages: Collection<Int>) =
pages.sorted().find { it > currentPage }
}
class InventoryGUIConsistentPageCalculator(private val toPage: Int) : InventoryGUIPageChangeCalculator() {
@@ -119,19 +119,19 @@ internal fun InventoryGUI<*>.changeGUI(
) = changePage(effect.effect, fromPage, toPage)
private inline fun changePageEffect(
fromPage: Int,
toPage: Int,
doFor: Int,
crossinline effect: (currentOffset: Int, ifInverted: Boolean) -> Unit,
fromPage: Int,
toPage: Int,
doFor: Int,
crossinline effect: (currentOffset: Int, ifInverted: Boolean) -> Unit,
) {
val ifInverted = fromPage >= toPage
var currentOffset = 1
task(
sync = true,
period = 1,
howOften = doFor.toLong()
sync = true,
period = 1,
howOften = doFor.toLong()
) {
effect.invoke(currentOffset, ifInverted)

View File

@@ -10,10 +10,11 @@ data class InventoryDimensions(val width: Int, val heigth: Int) {
val invSlots by lazy {
ArrayList<InventorySlot>().apply {
(1 .. heigth).forEach { row ->
(1 .. width).forEach { slotInRow ->
(1..heigth).forEach { row ->
(1..width).forEach { slotInRow ->
this += InventorySlot(row, slotInRow)
} }
}
}
}
}
@@ -40,8 +41,8 @@ data class InventoryDimensions(val width: Int, val heigth: Int) {
data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable<InventorySlot> {
companion object {
fun fromRealSlot(realSlot: Int, dimensions: InventoryDimensions)
= dimensions.invSlotsWithRealSlots.toList().find { it.second == realSlot }?.first
fun fromRealSlot(realSlot: Int, dimensions: InventoryDimensions) =
dimensions.invSlotsWithRealSlots.toList().find { it.second == realSlot }?.first
}
override fun compareTo(other: InventorySlot) = when {
@@ -61,12 +62,12 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable<Inventor
return ((rowsUnder * inventoryDimensions.width) + slotInRow) - 1
}
fun isInDimension(inventoryDimensions: InventoryDimensions)
= (1 .. inventoryDimensions.width).contains(slotInRow) && (1 .. inventoryDimensions.heigth).contains(row)
fun isInDimension(inventoryDimensions: InventoryDimensions) =
(1..inventoryDimensions.width).contains(slotInRow) && (1..inventoryDimensions.heigth).contains(row)
fun add(offsetHorizontally: Int, offsetVertically: Int) = InventorySlot(
row + offsetVertically,
slotInRow + offsetHorizontally
row + offsetVertically,
slotInRow + offsetHorizontally
)
}
@@ -75,13 +76,13 @@ interface InventorySlotCompound<out T : ForInventory> {
fun withInvType(invType: InventoryType<T>): Collection<InventorySlot>
fun realSlotsWithInvType(invType: InventoryType<T>)
= withInvType(invType).mapNotNull { it.realSlotIn(invType.dimensions) }
fun realSlotsWithInvType(invType: InventoryType<T>) =
withInvType(invType).mapNotNull { it.realSlotIn(invType.dimensions) }
}
open class SingleInventorySlot<T : ForInventory> internal constructor(
val inventorySlot: InventorySlot
val inventorySlot: InventorySlot
) : InventorySlotCompound<T> {
constructor(row: Int, slotInRow: Int) : this(InventorySlot(row, slotInRow))
@@ -99,10 +100,10 @@ internal enum class InventorySlotRangeType {
class InventorySlotRange<out T : ForInventory> internal constructor(
startSlot: SingleInventorySlot<out T>,
endSlot: SingleInventorySlot<out T>,
startSlot: SingleInventorySlot<out T>,
endSlot: SingleInventorySlot<out T>,
private val type: InventorySlotRangeType
private val type: InventorySlotRangeType
) : InventorySlotCompound<T>, ClosedRange<InventorySlot> {
@@ -115,40 +116,39 @@ class InventorySlotRange<out T : ForInventory> internal constructor(
endInclusive = minMaxPair.max
}
override fun withInvType(invType: InventoryType<T>)
= LinkedHashSet<InventorySlot>().apply {
when (type) {
InventorySlotRangeType.RECTANGLE -> {
// all possible combinations between the two slots
// -> form a rectangle
for (row in start.row .. endInclusive.row)
for (slotInRow in start.slotInRow .. endInclusive.slotInRow)
this += InventorySlot(row, slotInRow)
}
InventorySlotRangeType.LINEAR -> {
if (endInclusive.row > start.row) {
// from start --->| to end of row
for (slotInRow in start.slotInRow .. invType.dimensions.width)
this += InventorySlot(start.row, slotInRow)
// all rows in between
if (endInclusive.row > start.row + 1)
for (row in start.row + 1 until endInclusive.row)
for (slotInRow in 1 .. invType.dimensions.width)
this += InventorySlot(row, slotInRow)
// from start of row |----> to endInclusive
for (slotInRow in 1 .. endInclusive.slotInRow)
this += InventorySlot(endInclusive.row, slotInRow)
} else if (endInclusive.row == start.row) {
// from start ---> to endInclusive in the same row
for (slotInRow in start.slotInRow .. endInclusive.slotInRow)
this += InventorySlot(start.row, slotInRow)
}
}
override fun withInvType(invType: InventoryType<T>) = LinkedHashSet<InventorySlot>().apply {
when (type) {
InventorySlotRangeType.RECTANGLE -> {
// all possible combinations between the two slots
// -> form a rectangle
for (row in start.row..endInclusive.row)
for (slotInRow in start.slotInRow..endInclusive.slotInRow)
this += InventorySlot(row, slotInRow)
}
InventorySlotRangeType.LINEAR -> {
if (endInclusive.row > start.row) {
// from start --->| to end of row
for (slotInRow in start.slotInRow..invType.dimensions.width)
this += InventorySlot(start.row, slotInRow)
// all rows in between
if (endInclusive.row > start.row + 1)
for (row in start.row + 1 until endInclusive.row)
for (slotInRow in 1..invType.dimensions.width)
this += InventorySlot(row, slotInRow)
// from start of row |----> to endInclusive
for (slotInRow in 1..endInclusive.slotInRow)
this += InventorySlot(endInclusive.row, slotInRow)
} else if (endInclusive.row == start.row) {
// from start ---> to endInclusive in the same row
for (slotInRow in start.slotInRow..endInclusive.slotInRow)
this += InventorySlot(start.row, slotInRow)
}
}
}
}
}
@@ -156,40 +156,40 @@ class InventorySlotRange<out T : ForInventory> internal constructor(
* This range contains all slots having an index between
* the indeces of the two given slots.
*/
infix fun <T : ForInventory> SingleInventorySlot<out T>.linTo(slot: SingleInventorySlot<out T>)
= InventorySlotRange(this, slot, InventorySlotRangeType.LINEAR)
infix fun <T : ForInventory> SingleInventorySlot<out T>.linTo(slot: SingleInventorySlot<out T>) =
InventorySlotRange(this, slot, InventorySlotRangeType.LINEAR)
/**
* This range contains all slots inside of a thought rectangle
* with the two given slots as two opposite corners of the rectangle.
*/
infix fun <T : ForInventory> SingleInventorySlot<out T>.rectTo(slot: SingleInventorySlot<out T>)
= InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE)
infix fun <T : ForInventory> SingleInventorySlot<out T>.rectTo(slot: SingleInventorySlot<out T>) =
InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE)
class InventoryRowSlots<T : ForInventory> internal constructor(
val row: Int
val row: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
for (slotInRow in 1 .. invType.dimensions.width)
for (slotInRow in 1..invType.dimensions.width)
this += InventorySlot(row, slotInRow)
}
}
class InventoryColumnSlots<T : ForInventory> internal constructor(
val column: Int
val column: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
for (row in 1 .. invType.dimensions.heigth)
for (row in 1..invType.dimensions.heigth)
this += InventorySlot(row, column)
}
}
class InventoryBorderSlots<T : ForInventory> internal constructor(
val padding: Int
val padding: Int
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
@@ -197,7 +197,7 @@ class InventoryBorderSlots<T : ForInventory> internal constructor(
val dimensions = invType.dimensions
for (currentPadding in 0 until padding) {
for (slotInRow in 1 + currentPadding .. dimensions.width - currentPadding) {
for (slotInRow in 1 + currentPadding..dimensions.width - currentPadding) {
this += InventorySlot(1, slotInRow)
this += InventorySlot(dimensions.heigth, slotInRow)
}
@@ -212,11 +212,11 @@ class InventoryBorderSlots<T : ForInventory> internal constructor(
}
class InventoryCornerSlots<T : ForInventory> internal constructor(
val ifBottomLeft: Boolean = false,
val ifBottomRight: Boolean = false,
val ifTopLeft: Boolean = false,
val ifTopRight: Boolean = false
) : InventorySlotCompound<T> {
val ifBottomLeft: Boolean = false,
val ifBottomRight: Boolean = false,
val ifTopLeft: Boolean = false,
val ifTopRight: Boolean = false
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
@@ -247,20 +247,28 @@ interface ForColumnNine : ForInventoryWidthNine
// ROWS
interface ForRowOne : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowTwo : ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowOne : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine,
ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowTwo : ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine,
ForInventorySixByNine
interface ForRowThree : ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowFour : ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowFive : ForInventoryFiveByNine, ForInventorySixByNine
interface ForRowSix : ForInventorySixByNine
// EDGE CASES:
// ROW ONE
interface ForRowOneSlotOneToThree : ForRowOne, ForInventoryOneByFive, ForInventoryThreeByThree
interface ForRowOneSlotFourToFive : ForRowOne, ForInventoryOneByFive
// ROW TWO
interface ForRowTwoSlotOneToThree : ForRowTwo, ForInventoryThreeByThree
// ROW THREE
interface ForRowThreeSlotOneToThree : ForRowThree, ForInventoryThreeByThree
// COMPLETE ROWS (including the edge cases)
interface ForCompleteRowOne : ForRowOne, ForRowOneSlotOneToThree, ForRowOneSlotFourToFive
interface ForCompleteRowTwo : ForRowTwo, ForRowTwoSlotOneToThree
@@ -278,6 +286,7 @@ object Slots {
val RowOneSlotSeven = SingleInventorySlot<ForRowOne>(1, 7)
val RowOneSlotEight = SingleInventorySlot<ForRowOne>(1, 8)
val RowOneSlotNine = SingleInventorySlot<ForRowOne>(1, 9)
// ROW TWO
val RowTwoSlotOne = SingleInventorySlot<ForRowTwoSlotOneToThree>(2, 1)
val RowTwoSlotTwo = SingleInventorySlot<ForRowTwoSlotOneToThree>(2, 2)
@@ -288,6 +297,7 @@ object Slots {
val RowTwoSlotSeven = SingleInventorySlot<ForRowTwo>(2, 7)
val RowTwoSlotEight = SingleInventorySlot<ForRowTwo>(2, 8)
val RowTwoSlotNine = SingleInventorySlot<ForRowTwo>(2, 9)
// ROW THREE
val RowThreeSlotOne = SingleInventorySlot<ForRowThreeSlotOneToThree>(3, 1)
val RowThreeSlotTwo = SingleInventorySlot<ForRowThreeSlotOneToThree>(3, 2)
@@ -298,6 +308,7 @@ object Slots {
val RowThreeSlotSeven = SingleInventorySlot<ForRowThree>(3, 7)
val RowThreeSlotEight = SingleInventorySlot<ForRowThree>(3, 8)
val RowThreeSlotNine = SingleInventorySlot<ForRowThree>(3, 9)
// ROW FOUR
val RowFourSlotOne = SingleInventorySlot<ForRowFour>(4, 1)
val RowFourSlotTwo = SingleInventorySlot<ForRowFour>(4, 2)
@@ -308,6 +319,7 @@ object Slots {
val RowFourSlotSeven = SingleInventorySlot<ForRowFour>(4, 7)
val RowFourSlotEight = SingleInventorySlot<ForRowFour>(4, 8)
val RowFourSlotNine = SingleInventorySlot<ForRowFour>(4, 9)
// ROW FIVE
val RowFiveSlotOne = SingleInventorySlot<ForRowFive>(5, 1)
val RowFiveSlotTwo = SingleInventorySlot<ForRowFive>(5, 2)
@@ -318,6 +330,7 @@ object Slots {
val RowFiveSlotSeven = SingleInventorySlot<ForRowFive>(5, 7)
val RowFiveSlotEight = SingleInventorySlot<ForRowFive>(5, 8)
val RowFiveSlotNine = SingleInventorySlot<ForRowFive>(5, 9)
// ROW SIX
val RowSixSlotOne = SingleInventorySlot<ForRowSix>(6, 1)
val RowSixSlotTwo = SingleInventorySlot<ForRowSix>(6, 2)

View File

@@ -8,8 +8,8 @@ import org.bukkit.inventory.Inventory
import org.bukkit.inventory.InventoryHolder
class InventoryType<in T : ForInventory>(
val dimensions: InventoryDimensions,
val bukkitType: InventoryType? = null
val dimensions: InventoryDimensions,
val bukkitType: InventoryType? = null
) {
private val size = dimensions.width * dimensions.heigth
@@ -22,8 +22,10 @@ class InventoryType<in T : ForInventory>(
val FOUR_BY_NINE = InventoryType<ForInventoryFourByNine>(InventoryDimensions(9, 4))
val FIVE_BY_NINE = InventoryType<ForInventoryFiveByNine>(InventoryDimensions(9, 5))
val SIX_BY_NINE = InventoryType<ForInventorySixByNine>(InventoryDimensions(9, 6))
val ONE_BY_FIVE = InventoryType<ForInventoryOneByFive>(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER)
val THREE_BY_THREE = InventoryType<ForInventoryThreeByThree>(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER)
val ONE_BY_FIVE =
InventoryType<ForInventoryOneByFive>(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER)
val THREE_BY_THREE =
InventoryType<ForInventoryThreeByThree>(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER)
}
@@ -52,9 +54,10 @@ interface ForInventorySixByNine : ForInventory
interface ForEveryInventory
: ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine,
ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine,
ForInventoryThreeByThree, ForInventoryOneByFive
ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine,
ForInventoryThreeByThree, ForInventoryOneByFive
interface ForInventoryWidthThree : ForInventoryThreeByThree
interface ForInventoryWidthFive : ForInventoryOneByFive
interface ForInventoryWidthNine : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine
interface ForInventoryWidthNine : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine,
ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine

View File

@@ -6,8 +6,8 @@ import net.axay.kspigot.inventory.InventoryGUIElement
import net.axay.kspigot.inventory.InventoryGUIElementData
open class InventoryGUIButton<T : ForInventory>(
inventoryGUIElementData: InventoryGUIElementData,
val action: (InventoryGUIClickEvent<T>) -> Unit,
inventoryGUIElementData: InventoryGUIElementData,
val action: (InventoryGUIClickEvent<T>) -> Unit,
) : InventoryGUIElement<T>(inventoryGUIElementData) {
override fun onClickElement(clickEvent: InventoryGUIClickEvent<T>) {

View File

@@ -3,17 +3,16 @@ package net.axay.kspigot.inventory.elements
import net.axay.kspigot.inventory.*
class InventoryGUIButtonInventoryChange<T : ForInventory>(
inventoryGUIElementData: InventoryGUIElementData,
changeToGUICallback: () -> InventoryGUI<*>,
changeToPageInt: Int?,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
)
: InventoryGUIButton<T>(inventoryGUIElementData, {
inventoryGUIElementData: InventoryGUIElementData,
changeToGUICallback: () -> InventoryGUI<*>,
changeToPageInt: Int?,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
) : InventoryGUIButton<T>(inventoryGUIElementData, {
val changeToGUI = changeToGUICallback.invoke()
val effect = (changeToGUI.data.transitionTo ?: it.gui.data.transitionFrom)
?: InventoryChangeEffect.INSTANT
?: InventoryChangeEffect.INSTANT
val changeToPage = changeToGUI.getPage(changeToPageInt) ?: changeToGUI.currentPage

View File

@@ -3,18 +3,17 @@ package net.axay.kspigot.inventory.elements
import net.axay.kspigot.inventory.*
class InventoryGUIButtonPageChange<T : ForInventory>(
inventoryGUIElementData: InventoryGUIElementData,
calculator: InventoryGUIPageChangeCalculator,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
)
: InventoryGUIButton<T>(inventoryGUIElementData, {
inventoryGUIElementData: InventoryGUIElementData,
calculator: InventoryGUIPageChangeCalculator,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
) : InventoryGUIButton<T>(inventoryGUIElementData, {
val currentPage = it.gui.currentPage
val newPage = it.gui.getPage(calculator.calculateNewPage(it.gui.currentPageInt, it.gui.data.pages.keys))
if (newPage != null) {
val effect = (newPage.transitionTo ?: currentPage.transitionFrom)
?: PageChangeEffect.INSTANT
?: PageChangeEffect.INSTANT
it.gui.changePage(effect, currentPage, newPage)

View File

@@ -5,5 +5,7 @@ import net.axay.kspigot.inventory.InventoryGUIClickEvent
import net.axay.kspigot.inventory.InventoryGUISlot
class InventoryGUIFreeSlot<T : ForInventory> : InventoryGUISlot<T>() {
override fun onClick(clickEvent: InventoryGUIClickEvent<T>) { /* do nothing */ }
override fun onClick(clickEvent: InventoryGUIClickEvent<T>) {
/* do nothing */
}
}

View File

@@ -6,7 +6,7 @@ import net.axay.kspigot.inventory.InventoryGUIElement
import net.axay.kspigot.inventory.InventoryGUIElementData
class InventoryGUIPlaceholder<T : ForInventory>(
inventoryGUIElementData: InventoryGUIElementData
inventoryGUIElementData: InventoryGUIElementData
) : InventoryGUIElement<T>(inventoryGUIElementData) {
override fun onClickElement(clickEvent: InventoryGUIClickEvent<T>) {

View File

@@ -8,7 +8,8 @@ import org.bukkit.entity.Player
import java.net.URL
private const val IP_API = "http://ip-api.com/json/"
private const val IP_API_FIELDS = "status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,query"
private const val IP_API_FIELDS =
"status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,query"
/**
* @see ipAddressData
@@ -27,8 +28,8 @@ fun Player.ipAddressData(language: IPAddressDataLanguage = IPAddressDataLanguage
val hostString = address?.hostString ?: return null
val jsonObject = ValueHolder.getGson(false).fromJson(
URL("$IP_API${hostString}?fields=${IP_API_FIELDS}?lang=${language.code}").readText(),
JsonObject::class.java
URL("$IP_API${hostString}?fields=${IP_API_FIELDS}?lang=${language.code}").readText(),
JsonObject::class.java
) ?: return null
if (jsonObject["status"].toString() == "fail") return null

View File

@@ -17,16 +17,16 @@ import org.bukkit.inventory.meta.ItemMeta
data class CustomItemIdentifier(val customModelData: Int, val placeHolderMaterial: Material) {
constructor(itemStack: ItemStack) :
this(
kotlin.run {
val itemMeta = itemStack.itemMeta
if (itemMeta != null && itemMeta.hasCustomModelData()) {
return@run itemMeta.customModelData
}
return@run 0
},
itemStack.type
)
this(
kotlin.run {
val itemMeta = itemStack.itemMeta
if (itemMeta != null && itemMeta.hasCustomModelData()) {
return@run itemMeta.customModelData
}
return@run 0
},
itemStack.type
)
val itemStack: ItemStack?
get() {

View File

@@ -9,10 +9,15 @@ internal val <T> Lazy<T>.valueIfInitialized get() = ifInitialized { value }
internal fun Lazy<AutoCloseable>.closeIfInitialized() = ifInitialized { value.close() }
internal class MinMaxPair<T : Comparable<T>>(a: T, b: T) {
val min: T; val max: T
val min: T;
val max: T
init {
if (a >= b) { min = b; max = a }
else { min = a; max = b }
if (a >= b) {
min = b; max = a
} else {
min = a; max = b
}
}
}

View File

@@ -30,17 +30,17 @@ abstract class KSpigot : JavaPlugin() {
/**
* Called when the plugin was loaded
*/
open fun load() { }
open fun load() {}
/**
* Called when the plugin was enabled
*/
open fun startup() { }
open fun startup() {}
/**
* Called when the plugin gets disabled
*/
open fun shutdown() { }
open fun shutdown() {}
final override fun onLoad() {
KSpigotMainInstance = this

View File

@@ -16,12 +16,12 @@ import org.bukkit.util.Vector
* @param force Determines whether the client should be encouraged to display the particles.
*/
data class KSpigotParticle(
val particle: Particle,
var amount: Int = 1,
var offset: Vector? = null,
var extra: Number = 1.0,
var data: Any? = null,
var force: Boolean = false
val particle: Particle,
var amount: Int = 1,
var offset: Vector? = null,
var extra: Number = 1.0,
var data: Any? = null,
var force: Boolean = false
) {
/**
@@ -30,15 +30,15 @@ data class KSpigotParticle(
*/
fun spawnAt(loc: Location) {
loc.worldOrException.spawnParticle(
particle,
loc,
amount,
offset?.x ?: 0.0,
offset?.y ?: 0.0,
offset?.z ?: 0.0,
extra.toDouble(),
data,
force
particle,
loc,
amount,
offset?.x ?: 0.0,
offset?.y ?: 0.0,
offset?.z ?: 0.0,
extra.toDouble(),
data,
force
)
}
@@ -48,14 +48,14 @@ data class KSpigotParticle(
*/
fun spawnFor(player: Player) {
player.spawnParticle(
particle,
player.location,
amount,
offset?.x ?: 0.0,
offset?.y ?: 0.0,
offset?.z ?: 0.0,
extra.toDouble(),
data
particle,
player.location,
amount,
offset?.x ?: 0.0,
offset?.y ?: 0.0,
offset?.z ?: 0.0,
extra.toDouble(),
data
)
}
@@ -65,21 +65,20 @@ data class KSpigotParticle(
* Accesses the particle builder.
* @see KSpigotParticle
*/
fun particle(particle: Particle, builder: KSpigotParticle.() -> Unit)
= KSpigotParticle(particle).apply(builder)
fun particle(particle: Particle, builder: KSpigotParticle.() -> Unit) = KSpigotParticle(particle).apply(builder)
/**
* Accesses the particle builder and then immediately
* spawns the particle at the given location.
* @see KSpigotParticle
*/
fun Location.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null)
= KSpigotParticle(particle).applyIfNotNull(builder).spawnAt(this)
fun Location.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null) =
KSpigotParticle(particle).applyIfNotNull(builder).spawnAt(this)
/**
* Accesses the particle builder and then immediately
* spawns the particle for the player.
* @see KSpigotParticle
*/
fun Player.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null)
= KSpigotParticle(particle).applyIfNotNull(builder).spawnFor(this)
fun Player.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null) =
KSpigotParticle(particle).applyIfNotNull(builder).spawnFor(this)

View File

@@ -82,8 +82,7 @@ class ChainedRunnablePartFirst<R>(
sync: Boolean
) : ChainedRunnablePart<Unit, R>(sync) {
override fun execute()
= start(Unit)
override fun execute() = start(Unit)
override fun <E : Exception> executeCatchingImpl(
exceptionClass: KClass<E>,
@@ -101,8 +100,7 @@ class ChainedRunnablePartThen<T, R>(
val previous: ChainedRunnablePart<*, T>
) : ChainedRunnablePart<T, R>(sync) {
override fun execute()
= previous.execute()
override fun execute() = previous.execute()
override fun <E : Exception> executeCatchingImpl(
exceptionClass: KClass<E>,
@@ -115,13 +113,13 @@ class ChainedRunnablePartThen<T, R>(
}
// FIRST
fun <R> firstDo(sync: Boolean, runnable: () -> R)
= ChainedRunnablePartFirst(runnable, sync)
fun <R> firstDo(sync: Boolean, runnable: () -> R) = ChainedRunnablePartFirst(runnable, sync)
fun <R> firstSync(runnable: () -> R) = firstDo(true, runnable)
fun <R> firstAsync(runnable: () -> R) = firstDo(false, runnable)
// THEN
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R) -> U)
= ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this }
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R) -> U) =
ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this }
fun <T, R, U> ChainedRunnablePart<T, R>.thenSync(runnable: (R) -> U) = thenDo(true, runnable)
fun <T, R, U> ChainedRunnablePart<T, R>.thenAsync(runnable: (R) -> U) = thenDo(false, runnable)

View File

@@ -21,19 +21,18 @@ internal object KRunnableHolder : AutoCloseable {
runnableEndCallbacks.clear()
}
fun add(runnable: BukkitRunnable, callback: () -> Unit, safe: Boolean)
= runnableEndCallbacks.put(runnable, Pair(callback, safe))
fun remove(runnable: BukkitRunnable)
= runnableEndCallbacks.remove(runnable)
fun activate(runnable: BukkitRunnable)
= runnableEndCallbacks.remove(runnable)?.first?.invoke()
fun add(runnable: BukkitRunnable, callback: () -> Unit, safe: Boolean) =
runnableEndCallbacks.put(runnable, Pair(callback, safe))
fun remove(runnable: BukkitRunnable) = runnableEndCallbacks.remove(runnable)
fun activate(runnable: BukkitRunnable) = runnableEndCallbacks.remove(runnable)?.first?.invoke()
}
abstract class KSpigotRunnable(
var counterUp: Long? = null,
var counterDownToOne: Long? = null,
var counterDownToZero: Long? = null
var counterUp: Long? = null,
var counterDownToOne: Long? = null,
var counterDownToZero: Long? = null
) : BukkitRunnable()
/**
@@ -49,13 +48,13 @@ abstract class KSpigotRunnable(
* @param runnable the runnable which should be executed each repetition
*/
fun task(
sync: Boolean = true,
delay: Long = 0,
period: Long? = null,
howOften: Long? = null,
safe: Boolean = false,
endCallback: (() -> Unit)? = null,
runnable: ((KSpigotRunnable) -> Unit)? = null
sync: Boolean = true,
delay: Long = 0,
period: Long? = null,
howOften: Long? = null,
safe: Boolean = false,
endCallback: (() -> Unit)? = null,
runnable: ((KSpigotRunnable) -> Unit)? = null
) {
if (howOften != null && howOften == 0L) return
@@ -119,11 +118,9 @@ fun bukkitRun(sync: Boolean, runnable: () -> Unit) {
/**
* Starts a synchronous task.
*/
fun sync(runnable: () -> Unit)
= Bukkit.getScheduler().runTask(KSpigotMainInstance, runnable)
fun sync(runnable: () -> Unit) = Bukkit.getScheduler().runTask(KSpigotMainInstance, runnable)
/**
* Starts an asynchronous task.
*/
fun async(runnable: () -> Unit)
= Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable)
fun async(runnable: () -> Unit) = Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable)

View File

@@ -15,13 +15,12 @@ interface SpigotSerialzableCompanion<T>
/**
* @return A json string.
*/
fun SpigotSerializable<*>.serialize(pretty: Boolean = true): String
= ValueHolder.getGson(pretty).toJson(this)
fun SpigotSerializable<*>.serialize(pretty: Boolean = true): String = ValueHolder.getGson(pretty).toJson(this)
/**
* Deserializes the given json string and
* returns the deserialized object.
*/
@Suppress("unused")
inline fun <reified T> SpigotSerialzableCompanion<T>.deserialize(json: String): T
= ValueHolder.getGson(false).fromJson(json, T::class.java)
inline fun <reified T> SpigotSerialzableCompanion<T>.deserialize(json: String): T =
ValueHolder.getGson(false).fromJson(json, T::class.java)

View File

@@ -19,8 +19,7 @@ class SerializableLocation(
constructor(loc: Location) : this(loc.world?.name, loc.x, loc.y, loc.z, SerializableVector(loc.direction))
override fun toSpigot()
= Location(world?.let { Bukkit.getWorld(world) }, x, y, z)
.apply { direction = this@SerializableLocation.direction.toSpigot() }
override fun toSpigot() = Location(world?.let { Bukkit.getWorld(world) }, x, y, z)
.apply { direction = this@SerializableLocation.direction.toSpigot() }
}

View File

@@ -44,21 +44,20 @@ data class KSpigotSound(
* Accesses the sound builder.
* @see KSpigotSound
*/
fun sound(sound: Sound, builder: KSpigotSound.() -> Unit)
= KSpigotSound(sound).apply(builder)
fun sound(sound: Sound, builder: KSpigotSound.() -> Unit) = KSpigotSound(sound).apply(builder)
/**
* Accesses the sound builder and then immediately
* plays the sound at the given location.
* @see KSpigotSound
*/
fun Location.sound(sound: Sound, builder: (KSpigotSound.() -> Unit)? = null)
= KSpigotSound(sound).applyIfNotNull(builder).playAt(this)
fun Location.sound(sound: Sound, builder: (KSpigotSound.() -> Unit)? = null) =
KSpigotSound(sound).applyIfNotNull(builder).playAt(this)
/**
* Accesses the sound builder and then immediately
* plays the sound for the player.
* @see KSpigotSound
*/
fun Player.sound(sound: Sound, builder: (KSpigotSound.() -> Unit)? = null)
= KSpigotSound(sound).applyIfNotNull(builder).playFor(this)
fun Player.sound(sound: Sound, builder: (KSpigotSound.() -> Unit)? = null) =
KSpigotSound(sound).applyIfNotNull(builder).playFor(this)

View File

@@ -55,8 +55,16 @@ abstract class Circle(val radius: Number) {
while (currentRadius >= 0) {
this += circleEdgeLocations(currentRadius).mapTo(HashSet()) {
mutableSetOf(it).apply {
this += SimpleLocation2D(it.x + when { it.x >= 1 -> -1; it.x <= -1 -> 1; else -> 0 }, it.y)
this += SimpleLocation2D(it.x, it.y + when { it.y >= 1 -> -1; it.y <= -1 -> 1; else -> 0 })
this += SimpleLocation2D(
it.x + when {
it.x >= 1 -> -1; it.x <= -1 -> 1; else -> 0
}, it.y
)
this += SimpleLocation2D(
it.x, it.y + when {
it.y >= 1 -> -1; it.y <= -1 -> 1; else -> 0
}
)
}
}.flatten()
currentRadius--

View File

@@ -18,12 +18,12 @@ interface StructureData {
}
class SingleStructureData(
val location: SimpleLocation3D,
val structureData: StructureData
val location: SimpleLocation3D,
val structureData: StructureData
)
data class Structure(
val structureData: Set<SingleStructureData>
val structureData: Set<SingleStructureData>
) {
constructor(vararg structureDataSets: Set<SingleStructureData>)
: this(structureDataSets.flatMapTo(HashSet()) { it })
@@ -34,7 +34,7 @@ data class Structure(
*/
data class StructureDataMaterial(
val material: Material
val material: Material
) : StructureData {
override fun createAt(loc: Location) {
@@ -44,8 +44,8 @@ data class StructureDataMaterial(
}
data class StructureDataBlock(
val material: Material,
val blockData: BlockData
val material: Material,
val blockData: BlockData
) : StructureData {
constructor(block: Block) : this(block.type, block.blockData)
@@ -61,8 +61,8 @@ data class StructureDataBlock(
@NMS_General
data class StructureDataEntity(
val entityType: EntityType,
val nbtData: NBTData
val entityType: EntityType,
val nbtData: NBTData
) : StructureData {
constructor(entity: Entity) : this(entity.type, entity.nbtData)
@@ -75,7 +75,7 @@ data class StructureDataEntity(
}
data class StructureDataParticle(
val particle: KSpigotParticle
val particle: KSpigotParticle
) : StructureData {
override fun createAt(loc: Location) {

View File

@@ -15,26 +15,22 @@ fun Structure.buildAt(loc: Location) {
}
/** @see Structure.rotate */
fun Structure.rotateAroundX(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
fun Structure.rotateAroundX(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
/** @see Structure.rotate */
fun Structure.rotateAroundY(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundY(rad) }
fun Structure.rotateAroundY(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundY(rad) }
/** @see Structure.rotate */
fun Structure.rotateAroundZ(angle: Number)
= rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
fun Structure.rotateAroundZ(angle: Number) = rotate(angle) { it, rad -> it.rotateAroundZ(rad) }
/** @param angle The angle of rotation in degrees.*/
inline fun Structure.rotate(angle: Number, vectorRotation: (Vector, Double) -> Vector)
= Structure(
HashSet<SingleStructureData>().apply {
structureData.forEach {
this += SingleStructureData(
vectorRotation.invoke(it.location.toVector(), Math.toRadians(angle.toDouble())).toSimpleLoc(),
it.structureData
)
}
inline fun Structure.rotate(angle: Number, vectorRotation: (Vector, Double) -> Vector) = Structure(
HashSet<SingleStructureData>().apply {
structureData.forEach {
this += SingleStructureData(
vectorRotation.invoke(it.location.toVector(), Math.toRadians(angle.toDouble())).toSimpleLoc(),
it.structureData
)
}
)
}
)

View File

@@ -12,38 +12,45 @@ import org.bukkit.entity.Entity
/**
* @return A [Structure] containing all data of the given [LocationArea].
*/
fun LocationArea.loadStructure(includeBlocks: Boolean = true, includeEntities: Boolean = false)
= Structure(
if (includeBlocks)
fillBlocks.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataBlock(it)) }
else emptySet(),
if (includeEntities)
entities.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataEntity(it)) }
else emptySet()
)
fun LocationArea.loadStructure(includeBlocks: Boolean = true, includeEntities: Boolean = false) = Structure(
if (includeBlocks)
fillBlocks.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataBlock(it))
}
else emptySet(),
if (includeEntities)
entities.mapTo(HashSet()) {
SingleStructureData(it.location relationTo minLoc.blockLoc, StructureDataEntity(it))
}
else emptySet()
)
/**
* @return All blocks in the given [LocationArea].
* Sorted by their coordinates.
*/
val LocationArea.fillBlocks: Set<Block> get()
val LocationArea.fillBlocks: Set<Block>
get()
= LinkedHashSet<Block>().apply {
(minLoc.blockX until maxLoc.blockX + 1).forEach { x ->
(minLoc.blockY until maxLoc.blockY + 1).forEach { y ->
(minLoc.blockZ until maxLoc.blockZ + 1).forEach { z ->
this += Location(world, x.toDouble(), y.toDouble(), z.toDouble()).block
} } }
(minLoc.blockY until maxLoc.blockY + 1).forEach { y ->
(minLoc.blockZ until maxLoc.blockZ + 1).forEach { z ->
this += Location(world, x.toDouble(), y.toDouble(), z.toDouble()).block
}
}
}
}
/**
* @return All entities in the given [LocationArea].
*/
val LocationArea.entities: Set<Entity> get()
val LocationArea.entities: Set<Entity>
get()
= HashSet<Entity>().apply {
touchedChunks.forEach { it.entities.forEach { en ->
if (simpleLocationPair.isInArea(en.location))
this += en
} }
touchedChunks.forEach {
it.entities.forEach { en ->
if (simpleLocationPair.isInArea(en.location))
this += en
}
}
}