diff --git a/src/main/java/net/axay/kspigot/chat/KColors.java b/src/main/java/net/axay/kspigot/chat/KColors.java index 5fdbd35e..a43a1029 100644 --- a/src/main/java/net/axay/kspigot/chat/KColors.java +++ b/src/main/java/net/axay/kspigot/chat/KColors.java @@ -877,5 +877,5 @@ public class KColors { *


*/ public static final ChatColor YELLOWGREEN = ChatColor.of(new Color(0.6039216f, 0.8039216f, 0.19607843f)); - + } diff --git a/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt b/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt index 2dcb3e1a..8be2dbc5 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt @@ -37,7 +37,11 @@ class KSpigotComponentBuilder { this += SelectorComponent(selector).apply(builder) } - inline fun translatable(translatable: String, with: Array, builder: TranslatableComponent.() -> Unit = { }) { + inline fun translatable( + translatable: String, + with: Array, + 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) { components += baseComponents } + operator fun plusAssign(baseComponent: BaseComponent) { + components += baseComponent + } + + operator fun plusAssign(baseComponents: Array) { + components += baseComponents + } fun create() = components.toTypedArray() diff --git a/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt index 78a65ce0..d87bd4f5 100644 --- a/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt +++ b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt @@ -28,24 +28,26 @@ import kotlin.reflect.KProperty * exist and no default config is specified. */ inline fun kSpigotJsonConfig( - file: File, - noinline default: (() -> T)? = null, + file: File, + noinline default: (() -> T)? = null, ) = ConfigDelegate(T::class, file, default) /** * @see kSpigotJsonConfig */ -class ConfigDelegate ( - private val configClass: KClass, - private val file: File, - private val defaultCallback: (() -> T)? +class ConfigDelegate( + private val configClass: KClass, + 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 ( /** * 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 loadConfig(file: File, configClass: KClass): T - = FileReader(file).use { reader -> return getGson(false).fromJson(reader, configClass.java) } + fun loadConfig(file: File, configClass: KClass): T = + FileReader(file).use { reader -> return getGson(false).fromJson(reader, configClass.java) } fun saveConfig(file: File, config: T, pretty: Boolean = true) { file.createIfNotExists() diff --git a/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt b/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt index f3d18f8c..07d43651 100644 --- a/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt +++ b/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt @@ -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 - } - ) \ No newline at end of file +class PluginFile(path: String, child: String? = null) : File( + "${KSpigotMainInstance.dataFolder}", + run { + if (child == null) path else File(path, child).path + } +) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt b/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt index 3920546d..4ece6851 100644 --- a/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt +++ b/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt @@ -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() - } -} \ No newline at end of file +val ItemStack.nbtData: NBTData + get() { + CraftItemStack.asNMSCopy(this).let { + return if (it.hasTag()) NBTData(it.tag) else NBTData() + } + } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt b/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt index 7b2d7f0c..76a56fde 100644 --- a/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt +++ b/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt @@ -11,17 +11,27 @@ interface NBTDataType { companion object { - val COMPOUND = nbtDataType({ NBTData(it) }, { key, data, compound -> compound.set(key, data.nbtTagCompound) }) - val BYTE = nbtDataType({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) }) - val BYTE_ARRAY = nbtDataType({ it.bytes }, { key, data, compound -> compound.setByteArray(key, data) }) - val DOUBLE = nbtDataType({ it.asDouble() }, { key, data, compound -> compound.setDouble(key, data) }) - val FLOAT = nbtDataType({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) }) + val COMPOUND = nbtDataType({ NBTData(it) }, + { key, data, compound -> compound.set(key, data.nbtTagCompound) }) + val BYTE = + nbtDataType({ it.asByte() }, { key, data, compound -> compound.setByte(key, data) }) + val BYTE_ARRAY = nbtDataType({ it.bytes }, + { key, data, compound -> compound.setByteArray(key, data) }) + val DOUBLE = nbtDataType({ it.asDouble() }, + { key, data, compound -> compound.setDouble(key, data) }) + val FLOAT = + nbtDataType({ it.asFloat() }, { key, data, compound -> compound.setFloat(key, data) }) val INT = nbtDataType({ it.asInt() }, { key, data, compound -> compound.setInt(key, data) }) - val INT_ARRAY = nbtDataType({ it.ints }, { key, data, compound -> compound.setIntArray(key, data) }) - val LONG = nbtDataType({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) }) - val LONG_ARRAY = nbtDataType({ it.longs }, { key, data, compound -> compound.set(key, NBTTagLongArray(data)) }) - val SHORT = nbtDataType({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) }) - val STRING = nbtDataType({ it.asString() }, { key, data, compound -> compound.setString(key, data) }) + val INT_ARRAY = nbtDataType({ it.ints }, + { key, data, compound -> compound.setIntArray(key, data) }) + val LONG = + nbtDataType({ it.asLong() }, { key, data, compound -> compound.setLong(key, data) }) + val LONG_ARRAY = nbtDataType({ it.longs }, + { key, data, compound -> compound.set(key, NBTTagLongArray(data)) }) + val SHORT = + nbtDataType({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) }) + val STRING = nbtDataType({ it.asString() }, + { key, data, compound -> compound.setString(key, data) }) } @@ -32,15 +42,16 @@ interface NBTDataType { * @property E the NBT data type */ private inline fun 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 { return object : NBTDataType { 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) } diff --git a/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt b/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt index 0c704404..f6fe5560 100644 --- a/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt +++ b/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt @@ -26,9 +26,9 @@ fun Listener.unregister() = HandlerList.unregisterAll(this) * @param executor handles incoming events */ inline fun 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 : Listener { * @param ignoreCancelled if manual cancellation should be ignored */ inline fun SingleListener.register( - priority: EventPriority = EventPriority.NORMAL, - ignoreCancelled: Boolean = false + priority: EventPriority = EventPriority.NORMAL, + ignoreCancelled: Boolean = false ) { register(priority, ignoreCancelled) { _, event -> (event as? T)?.let { this.onEvent(it) } @@ -64,9 +64,9 @@ inline fun SingleListener.register( * @param onEvent the event callback */ inline fun listen( - priority: EventPriority = EventPriority.NORMAL, - ignoreCancelled: Boolean = false, - crossinline onEvent: (T) -> Unit + priority: EventPriority = EventPriority.NORMAL, + ignoreCancelled: Boolean = false, + crossinline onEvent: (T) -> Unit ): SingleListener { val listener = object : SingleListener { override fun onEvent(event: T) = onEvent.invoke(event) diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt index 43959024..e767dc27 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt @@ -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") \ No newline at end of file +fun CommandSender.printColoredPrefix(text: String, textColor: ChatColor, prefix: String, prefixColor: ChatColor) = + sendMessage("${prefixColor}[${prefix}]${textColor} $text") \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt index 92c80784..30c1da3f 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt @@ -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) } diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/LocationExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/LocationExtensions.kt index 5f514cbd..fe1ee5e7 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/LocationExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/LocationExtensions.kt @@ -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!") \ No newline at end of file +val Location.worldOrException: World + get() = world + ?: throw NullPointerException("The world of the location is null!") \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt index 006a6ef5..bbf5c0d6 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt @@ -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) } \ No newline at end of file +fun PrepareItemCraftEvent.cancel() { + this.inventory.result = ItemStack(Material.AIR) +} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt b/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt index 9c2b2d9a..da69ed8b 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt @@ -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() (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 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) } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt index 57e76074..a7542bf1 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt @@ -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) diff --git a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt index 09926620..566e49a5 100644 --- a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt +++ b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt @@ -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) { 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 -} \ No newline at end of file +private val Long.isCounterValue: Boolean + get() = when (this) { + 1L, 2L, 3L, 4L, 5L, 10L, 15L, 20L, 30L -> true + 0L -> false + else -> this % 60 == 0L + } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt index baa75262..72a16650 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt @@ -64,8 +64,8 @@ object InventoryGUIHolder : AutoCloseable { // EVENT class InventoryGUIClickEvent( - val bukkitEvent: InventoryClickEvent, - val gui: InventoryGUI + val bukkitEvent: InventoryClickEvent, + val gui: InventoryGUI ) /* @@ -84,16 +84,22 @@ class InventoryGUIData( ) abstract class InventoryGUI( - val data: InventoryGUIData + val data: InventoryGUIData ) { 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( // Inventory GUI implementations class InventoryGUIShared( - inventoryGUIData: InventoryGUIData + inventoryGUIData: InventoryGUIData ) : InventoryGUI(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 diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt index 088eb8ee..13a37055 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt @@ -6,12 +6,12 @@ import net.axay.kspigot.inventory.elements.* import org.bukkit.inventory.ItemStack fun inventoryGUI( - type: InventoryType, - builder: InventoryGUIBuilder.() -> Unit, + type: InventoryType, + builder: InventoryGUIBuilder.() -> Unit, ) = InventoryGUIBuilder(type).apply(builder).build() class InventoryGUIBuilder( - val type: InventoryType + val type: InventoryType ) { var title: String = "" @@ -37,14 +37,14 @@ class InventoryGUIBuilder( } internal fun build() = InventoryGUIShared( - InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement) + InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement) ).apply { register() } } class InventoryGUIPageBuilder( - val type: InventoryType, - val page: Int + val type: InventoryType, + val page: Int ) { private val guiSlots = HashMap>() @@ -59,74 +59,96 @@ class InventoryGUIPageBuilder( * actions. If clicked, the specified [onClick] * function is invoked. */ - fun button(slots: InventorySlotCompound, itemStack: ItemStack, onClick: (InventoryGUIClickEvent) -> Unit) - = defineSlots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick)) + fun button(slots: InventorySlotCompound, itemStack: ItemStack, onClick: (InventoryGUIClickEvent) -> Unit) = + defineSlots(slots, InventoryGUIButton(InventoryGUIElementData(itemStack), onClick)) /** * An item protected from any player actions. * This is not a button. */ - fun placeholder(slots: InventorySlotCompound, itemStack: ItemStack) - = defineSlots(slots, InventoryGUIPlaceholder(InventoryGUIElementData(itemStack))) + fun placeholder(slots: InventorySlotCompound, 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) - = defineSlots(slots, InventoryGUIFreeSlot()) + fun freeSlot(slots: InventorySlotCompound) = defineSlots(slots, InventoryGUIFreeSlot()) /** * This is a button which loads the specified * [toPage] if clicked. */ - fun pageChanger(slots: InventorySlotCompound, itemStack: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) - = defineSlots(slots, InventoryGUIButtonPageChange( + fun pageChanger( + slots: InventorySlotCompound, + itemStack: ItemStack, + toPage: Int, + onChange: ((InventoryGUIClickEvent) -> 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, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) - = defineSlots(slots, InventoryGUIButtonPageChange( + fun previousPage( + slots: InventorySlotCompound, + itemStack: ItemStack, + onChange: ((InventoryGUIClickEvent) -> 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, itemStack: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) - = defineSlots(slots, InventoryGUIButtonPageChange( + fun nextPage( + slots: InventorySlotCompound, + itemStack: ItemStack, + onChange: ((InventoryGUIClickEvent) -> 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, itemStack: ItemStack, newGUI: () -> InventoryGUI<*>, newPage: Int? = null, onChange: ((InventoryGUIClickEvent) -> Unit)? = null) - = defineSlots(slots, InventoryGUIButtonInventoryChange( + fun changeGUI( + slots: InventorySlotCompound, + itemStack: ItemStack, + newGUI: () -> InventoryGUI<*>, + newPage: Int? = null, + onChange: ((InventoryGUIClickEvent) -> Unit)? = null + ) = defineSlots( + slots, InventoryGUIButtonInventoryChange( InventoryGUIElementData(itemStack), newGUI, newPage, onChange - )) + ) + ) - private fun defineSlots(slots: InventorySlotCompound, element: InventoryGUISlot) - = slots.withInvType(type).forEach { curSlot -> - curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } + private fun defineSlots(slots: InventorySlotCompound, element: InventoryGUISlot) = + slots.withInvType(type).forEach { curSlot -> + curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } } } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt index 7efa60c9..119372fa 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt @@ -9,11 +9,11 @@ abstract class InventoryGUISlot { // ELEMENT class InventoryGUIElementData( - val itemStack: ItemStack + val itemStack: ItemStack ) abstract class InventoryGUIElement( - val inventoryGUIElementData: InventoryGUIElementData + val inventoryGUIElementData: InventoryGUIElementData ) : InventoryGUISlot() { final override fun onClick(clickEvent: InventoryGUIClickEvent) { diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt index 87c4d364..b777da7b 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt @@ -7,13 +7,13 @@ abstract class InventoryGUIPageChangeCalculator { abstract fun calculateNewPage(currentPage: Int, pages: Collection): Int? object InventoryGUIPreviousPageCalculator : InventoryGUIPageChangeCalculator() { - override fun calculateNewPage(currentPage: Int, pages: Collection) - = pages.sortedDescending().find { it < currentPage } + override fun calculateNewPage(currentPage: Int, pages: Collection) = + pages.sortedDescending().find { it < currentPage } } object InventoryGUINextPageCalculator : InventoryGUIPageChangeCalculator() { - override fun calculateNewPage(currentPage: Int, pages: Collection) - = pages.sorted().find { it > currentPage } + override fun calculateNewPage(currentPage: Int, pages: Collection) = + 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) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt index 1df1cc65..52bf65bd 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt @@ -10,10 +10,11 @@ data class InventoryDimensions(val width: Int, val heigth: Int) { val invSlots by lazy { ArrayList().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 { 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 { fun withInvType(invType: InventoryType): Collection - fun realSlotsWithInvType(invType: InventoryType) - = withInvType(invType).mapNotNull { it.realSlotIn(invType.dimensions) } + fun realSlotsWithInvType(invType: InventoryType) = + withInvType(invType).mapNotNull { it.realSlotIn(invType.dimensions) } } open class SingleInventorySlot internal constructor( - val inventorySlot: InventorySlot + val inventorySlot: InventorySlot ) : InventorySlotCompound { constructor(row: Int, slotInRow: Int) : this(InventorySlot(row, slotInRow)) @@ -99,10 +100,10 @@ internal enum class InventorySlotRangeType { class InventorySlotRange internal constructor( - startSlot: SingleInventorySlot, - endSlot: SingleInventorySlot, + startSlot: SingleInventorySlot, + endSlot: SingleInventorySlot, - private val type: InventorySlotRangeType + private val type: InventorySlotRangeType ) : InventorySlotCompound, ClosedRange { @@ -115,40 +116,39 @@ class InventorySlotRange internal constructor( endInclusive = minMaxPair.max } - override fun withInvType(invType: InventoryType) - = LinkedHashSet().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) = LinkedHashSet().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 internal constructor( * This range contains all slots having an index between * the indeces of the two given slots. */ -infix fun SingleInventorySlot.linTo(slot: SingleInventorySlot) - = InventorySlotRange(this, slot, InventorySlotRangeType.LINEAR) +infix fun SingleInventorySlot.linTo(slot: SingleInventorySlot) = + 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 SingleInventorySlot.rectTo(slot: SingleInventorySlot) - = InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE) +infix fun SingleInventorySlot.rectTo(slot: SingleInventorySlot) = + InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE) class InventoryRowSlots internal constructor( - val row: Int + val row: Int ) : InventorySlotCompound { override fun withInvType(invType: InventoryType) = HashSet().apply { - for (slotInRow in 1 .. invType.dimensions.width) + for (slotInRow in 1..invType.dimensions.width) this += InventorySlot(row, slotInRow) } } class InventoryColumnSlots internal constructor( - val column: Int + val column: Int ) : InventorySlotCompound { override fun withInvType(invType: InventoryType) = HashSet().apply { - for (row in 1 .. invType.dimensions.heigth) + for (row in 1..invType.dimensions.heigth) this += InventorySlot(row, column) } } class InventoryBorderSlots internal constructor( - val padding: Int + val padding: Int ) : InventorySlotCompound { override fun withInvType(invType: InventoryType) = HashSet().apply { @@ -197,7 +197,7 @@ class InventoryBorderSlots 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 internal constructor( } class InventoryCornerSlots internal constructor( - val ifBottomLeft: Boolean = false, - val ifBottomRight: Boolean = false, - val ifTopLeft: Boolean = false, - val ifTopRight: Boolean = false -) : InventorySlotCompound { + val ifBottomLeft: Boolean = false, + val ifBottomRight: Boolean = false, + val ifTopLeft: Boolean = false, + val ifTopRight: Boolean = false +) : InventorySlotCompound { override fun withInvType(invType: InventoryType) = HashSet().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(1, 7) val RowOneSlotEight = SingleInventorySlot(1, 8) val RowOneSlotNine = SingleInventorySlot(1, 9) + // ROW TWO val RowTwoSlotOne = SingleInventorySlot(2, 1) val RowTwoSlotTwo = SingleInventorySlot(2, 2) @@ -288,6 +297,7 @@ object Slots { val RowTwoSlotSeven = SingleInventorySlot(2, 7) val RowTwoSlotEight = SingleInventorySlot(2, 8) val RowTwoSlotNine = SingleInventorySlot(2, 9) + // ROW THREE val RowThreeSlotOne = SingleInventorySlot(3, 1) val RowThreeSlotTwo = SingleInventorySlot(3, 2) @@ -298,6 +308,7 @@ object Slots { val RowThreeSlotSeven = SingleInventorySlot(3, 7) val RowThreeSlotEight = SingleInventorySlot(3, 8) val RowThreeSlotNine = SingleInventorySlot(3, 9) + // ROW FOUR val RowFourSlotOne = SingleInventorySlot(4, 1) val RowFourSlotTwo = SingleInventorySlot(4, 2) @@ -308,6 +319,7 @@ object Slots { val RowFourSlotSeven = SingleInventorySlot(4, 7) val RowFourSlotEight = SingleInventorySlot(4, 8) val RowFourSlotNine = SingleInventorySlot(4, 9) + // ROW FIVE val RowFiveSlotOne = SingleInventorySlot(5, 1) val RowFiveSlotTwo = SingleInventorySlot(5, 2) @@ -318,6 +330,7 @@ object Slots { val RowFiveSlotSeven = SingleInventorySlot(5, 7) val RowFiveSlotEight = SingleInventorySlot(5, 8) val RowFiveSlotNine = SingleInventorySlot(5, 9) + // ROW SIX val RowSixSlotOne = SingleInventorySlot(6, 1) val RowSixSlotTwo = SingleInventorySlot(6, 2) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt index 210a25fe..8510a25d 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt @@ -8,8 +8,8 @@ import org.bukkit.inventory.Inventory import org.bukkit.inventory.InventoryHolder class InventoryType( - 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( val FOUR_BY_NINE = InventoryType(InventoryDimensions(9, 4)) val FIVE_BY_NINE = InventoryType(InventoryDimensions(9, 5)) val SIX_BY_NINE = InventoryType(InventoryDimensions(9, 6)) - val ONE_BY_FIVE = InventoryType(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER) - val THREE_BY_THREE = InventoryType(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER) + val ONE_BY_FIVE = + InventoryType(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER) + val THREE_BY_THREE = + InventoryType(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 \ No newline at end of file +interface ForInventoryWidthNine : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, + ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButton.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButton.kt index 83383f89..903c14f2 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButton.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButton.kt @@ -6,8 +6,8 @@ import net.axay.kspigot.inventory.InventoryGUIElement import net.axay.kspigot.inventory.InventoryGUIElementData open class InventoryGUIButton( - inventoryGUIElementData: InventoryGUIElementData, - val action: (InventoryGUIClickEvent) -> Unit, + inventoryGUIElementData: InventoryGUIElementData, + val action: (InventoryGUIClickEvent) -> Unit, ) : InventoryGUIElement(inventoryGUIElementData) { override fun onClickElement(clickEvent: InventoryGUIClickEvent) { diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonInventoryChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonInventoryChange.kt index 6c00bc92..5180ceb8 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonInventoryChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonInventoryChange.kt @@ -3,17 +3,16 @@ package net.axay.kspigot.inventory.elements import net.axay.kspigot.inventory.* class InventoryGUIButtonInventoryChange( - inventoryGUIElementData: InventoryGUIElementData, - changeToGUICallback: () -> InventoryGUI<*>, - changeToPageInt: Int?, - onChange: ((InventoryGUIClickEvent) -> Unit)? -) - : InventoryGUIButton(inventoryGUIElementData, { + inventoryGUIElementData: InventoryGUIElementData, + changeToGUICallback: () -> InventoryGUI<*>, + changeToPageInt: Int?, + onChange: ((InventoryGUIClickEvent) -> Unit)? +) : InventoryGUIButton(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 diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonPageChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonPageChange.kt index 0d5f3648..19fad581 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIButtonPageChange.kt @@ -3,18 +3,17 @@ package net.axay.kspigot.inventory.elements import net.axay.kspigot.inventory.* class InventoryGUIButtonPageChange( - inventoryGUIElementData: InventoryGUIElementData, - calculator: InventoryGUIPageChangeCalculator, - onChange: ((InventoryGUIClickEvent) -> Unit)? -) - : InventoryGUIButton(inventoryGUIElementData, { + inventoryGUIElementData: InventoryGUIElementData, + calculator: InventoryGUIPageChangeCalculator, + onChange: ((InventoryGUIClickEvent) -> Unit)? +) : InventoryGUIButton(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) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIFreeSlot.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIFreeSlot.kt index 7c385e1a..cc2bc3eb 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIFreeSlot.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIFreeSlot.kt @@ -5,5 +5,7 @@ import net.axay.kspigot.inventory.InventoryGUIClickEvent import net.axay.kspigot.inventory.InventoryGUISlot class InventoryGUIFreeSlot : InventoryGUISlot() { - override fun onClick(clickEvent: InventoryGUIClickEvent) { /* do nothing */ } + override fun onClick(clickEvent: InventoryGUIClickEvent) { + /* do nothing */ + } } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIPlaceholder.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIPlaceholder.kt index 3543f5a6..004c6133 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIPlaceholder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUIPlaceholder.kt @@ -6,7 +6,7 @@ import net.axay.kspigot.inventory.InventoryGUIElement import net.axay.kspigot.inventory.InventoryGUIElementData class InventoryGUIPlaceholder( - inventoryGUIElementData: InventoryGUIElementData + inventoryGUIElementData: InventoryGUIElementData ) : InventoryGUIElement(inventoryGUIElementData) { override fun onClickElement(clickEvent: InventoryGUIClickEvent) { diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt index b25f6cbc..daac87dc 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt @@ -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 diff --git a/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt b/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt index 167054a4..1ea7e75a 100644 --- a/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt +++ b/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt @@ -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() { diff --git a/src/main/kotlin/net/axay/kspigot/kotlinextensions/GeneralExtensions.kt b/src/main/kotlin/net/axay/kspigot/kotlinextensions/GeneralExtensions.kt index 7f3505f7..9d0ea7ae 100644 --- a/src/main/kotlin/net/axay/kspigot/kotlinextensions/GeneralExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/kotlinextensions/GeneralExtensions.kt @@ -9,10 +9,15 @@ internal val Lazy.valueIfInitialized get() = ifInitialized { value } internal fun Lazy.closeIfInitialized() = ifInitialized { value.close() } internal class MinMaxPair>(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 + } } } diff --git a/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt b/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt index 3003aff9..71428ca9 100644 --- a/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt +++ b/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt @@ -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 diff --git a/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt b/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt index 521be771..467214da 100644 --- a/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt +++ b/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt @@ -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) \ No newline at end of file +fun Player.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null) = + KSpigotParticle(particle).applyIfNotNull(builder).spawnFor(this) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt index 3f3f9c4f..28d7c163 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt @@ -82,8 +82,7 @@ class ChainedRunnablePartFirst( sync: Boolean ) : ChainedRunnablePart(sync) { - override fun execute() - = start(Unit) + override fun execute() = start(Unit) override fun executeCatchingImpl( exceptionClass: KClass, @@ -101,8 +100,7 @@ class ChainedRunnablePartThen( val previous: ChainedRunnablePart<*, T> ) : ChainedRunnablePart(sync) { - override fun execute() - = previous.execute() + override fun execute() = previous.execute() override fun executeCatchingImpl( exceptionClass: KClass, @@ -115,13 +113,13 @@ class ChainedRunnablePartThen( } // FIRST -fun firstDo(sync: Boolean, runnable: () -> R) - = ChainedRunnablePartFirst(runnable, sync) +fun firstDo(sync: Boolean, runnable: () -> R) = ChainedRunnablePartFirst(runnable, sync) fun firstSync(runnable: () -> R) = firstDo(true, runnable) fun firstAsync(runnable: () -> R) = firstDo(false, runnable) // THEN -fun ChainedRunnablePart.thenDo(sync: Boolean, runnable: (R) -> U) - = ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this } +fun ChainedRunnablePart.thenDo(sync: Boolean, runnable: (R) -> U) = + ChainedRunnablePartThen(runnable, sync, this).apply { previous.next = this } + fun ChainedRunnablePart.thenSync(runnable: (R) -> U) = thenDo(true, runnable) fun ChainedRunnablePart.thenAsync(runnable: (R) -> U) = thenDo(false, runnable) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt b/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt index b9f51f1e..f59086ec 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt @@ -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) \ No newline at end of file +fun async(runnable: () -> Unit) = Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/serialization/SpigotSerializable.kt b/src/main/kotlin/net/axay/kspigot/serialization/SpigotSerializable.kt index 07a58498..781688b4 100644 --- a/src/main/kotlin/net/axay/kspigot/serialization/SpigotSerializable.kt +++ b/src/main/kotlin/net/axay/kspigot/serialization/SpigotSerializable.kt @@ -15,13 +15,12 @@ interface SpigotSerialzableCompanion /** * @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 SpigotSerialzableCompanion.deserialize(json: String): T - = ValueHolder.getGson(false).fromJson(json, T::class.java) \ No newline at end of file +inline fun SpigotSerialzableCompanion.deserialize(json: String): T = + ValueHolder.getGson(false).fromJson(json, T::class.java) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableLocation.kt b/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableLocation.kt index ffcb19df..828e65f7 100644 --- a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableLocation.kt +++ b/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableLocation.kt @@ -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() } } \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/sound/KSpigotSound.kt b/src/main/kotlin/net/axay/kspigot/sound/KSpigotSound.kt index bcd761aa..4fce09d6 100644 --- a/src/main/kotlin/net/axay/kspigot/sound/KSpigotSound.kt +++ b/src/main/kotlin/net/axay/kspigot/sound/KSpigotSound.kt @@ -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) diff --git a/src/main/kotlin/net/axay/kspigot/structures/Circle.kt b/src/main/kotlin/net/axay/kspigot/structures/Circle.kt index dd8ab61b..e821dce8 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/Circle.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/Circle.kt @@ -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-- diff --git a/src/main/kotlin/net/axay/kspigot/structures/Structure.kt b/src/main/kotlin/net/axay/kspigot/structures/Structure.kt index 5b625705..437dadc7 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/Structure.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/Structure.kt @@ -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 + val structureData: Set ) { constructor(vararg structureDataSets: Set) : 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) { diff --git a/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt b/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt index 3024faee..c1e929dc 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt @@ -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().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().apply { + structureData.forEach { + this += SingleStructureData( + vectorRotation.invoke(it.location.toVector(), Math.toRadians(angle.toDouble())).toSimpleLoc(), + it.structureData + ) } - ) \ No newline at end of file + } +) \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt b/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt index 9efa9e41..0d10efb8 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt @@ -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 get() +val LocationArea.fillBlocks: Set + get() = LinkedHashSet().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 get() +val LocationArea.entities: Set + get() = HashSet().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 + } + } } \ No newline at end of file