diff --git a/src/main/kotlin/net/axay/kspigot/annotations/KSpigotAnnotations.kt b/src/main/kotlin/net/axay/kspigot/annotations/KSpigotAnnotations.kt index c71a5ea2..b2107d7f 100644 --- a/src/main/kotlin/net/axay/kspigot/annotations/KSpigotAnnotations.kt +++ b/src/main/kotlin/net/axay/kspigot/annotations/KSpigotAnnotations.kt @@ -28,4 +28,4 @@ annotation class NMS_1_16_1 * which is more likely to stay the same * over a long period of time. */ -annotation class NMS_General \ No newline at end of file +annotation class NMS_General diff --git a/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt b/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt index 84922db8..1906dedd 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt @@ -7,6 +7,7 @@ import net.md_5.bungee.api.chat.* import net.md_5.bungee.api.chat.hover.content.Entity import net.md_5.bungee.api.chat.hover.content.Item import net.md_5.bungee.api.chat.hover.content.Text +import java.awt.Color inline fun chatComponent(builder: KSpigotComponentBuilder.() -> Unit): Array { return KSpigotComponentBuilder().apply(builder).create() @@ -14,7 +15,7 @@ inline fun chatComponent(builder: KSpigotComponentBuilder.() -> Unit): Array() - // COMPONENTS + inline fun text(text: String, builder: TextComponent.() -> Unit = { }) { this += TextComponent(text).apply(builder) } @@ -41,7 +42,7 @@ class KSpigotComponentBuilder { ) { this += TranslatableComponent(translatable, with).apply(builder) } - // SPECIAL + fun legacyText(text: String, color: ChatColor = ChatColor.WHITE, builder: BaseComponent.() -> Unit = { }) { this += TextComponent.fromLegacyText(text, color).onEach { it.apply(builder) } } @@ -56,10 +57,7 @@ class KSpigotComponentBuilder { fun create() = components.toTypedArray() } -/* - * BASE COMPONENT - */ -// extensions + inline fun BaseComponent.hoverEventText(builder: KSpigotComponentBuilder.() -> Unit) { hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text(KSpigotComponentBuilder().apply(builder).create())) } @@ -75,8 +73,7 @@ fun BaseComponent.hoverEventEntity(type: String, id: String, baseComponent: Base fun BaseComponent.clickEvent(action: ClickEvent.Action, value: String) { clickEvent = ClickEvent(action, value) } -/* - * GLOBAL SHORTCUTS - */ -fun col(hex: String): ChatColor = ChatColor.of(hex) \ No newline at end of file +fun col(hex: String): ChatColor = ChatColor.of(hex) + +fun col(rgb: Int): ChatColor = ChatColor.of(Color(rgb)) diff --git a/src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt b/src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt index 9abf83ec..2edb082c 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/MessageExtensions.kt @@ -11,4 +11,4 @@ fun CommandSender.sendMessage(vararg components: BaseComponent) { fun CommandSender.sendMessage(builder: KSpigotComponentBuilder.() -> Unit) { this.spigot().sendMessage(*KSpigotComponentBuilder().apply(builder).create()) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt b/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt index d725c5b5..b2dcaa5d 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt @@ -76,7 +76,9 @@ internal abstract class PlayerInput( timeoutSeconds: Int, ) { private var received = false + protected abstract val inputListeners: List + protected fun onReceive(input: T?) { if (!received) { inputListeners.forEach { it.unregister() } @@ -95,4 +97,4 @@ internal abstract class PlayerInput( onReceive(null) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt index 5b5e06ac..c68179ca 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputAnvilInv.kt @@ -41,6 +41,7 @@ internal class PlayerInputAnvilInv( ) .text("${KColors.ORANGERED}$startText") .open(player) + override val inputListeners = listOf( listen { if (it.clickedInventory == anvilInv.inventory) @@ -51,4 +52,4 @@ internal class PlayerInputAnvilInv( override fun onTimeout() { anvilInv.inventory.closeForViewers() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt index 5b016a0a..32740df2 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputBook.kt @@ -46,6 +46,7 @@ internal abstract class PlayerInputBook( } abstract fun loadBookContent(bookMeta: BookMeta): T + override val inputListeners = listOf( listen { val meta = it.newBookMeta @@ -63,11 +64,13 @@ internal abstract class PlayerInputBook( companion object { val idKey = NamespacedKey(KSpigotMainInstance, "kspigot_bookinput_id") + internal val usedIDs = ArrayList() + fun getID(): Int { var returnID = (0..Int.MAX_VALUE).random() while (usedIDs.contains(returnID)) returnID = (0..Int.MAX_VALUE).random() return returnID } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt index 85e035b8..1584ef1f 100644 --- a/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt +++ b/src/main/kotlin/net/axay/kspigot/chat/input/implementations/PlayerInputChat.kt @@ -14,7 +14,6 @@ internal class PlayerInputChat( timeoutSeconds: Int, question: String, ) : PlayerInput(player, callback, timeoutSeconds) { - init { player.sendMessage("${KColors.ORANGERED}$question") } @@ -27,4 +26,4 @@ internal class PlayerInputChat( } } ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt b/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt index 07d43651..1a7537d8 100644 --- a/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt +++ b/src/main/kotlin/net/axay/kspigot/config/PluginFile.kt @@ -8,4 +8,4 @@ class PluginFile(path: String, child: String? = null) : File( 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/NBTData.kt b/src/main/kotlin/net/axay/kspigot/data/NBTData.kt index 6894eea2..a395fa2f 100644 --- a/src/main/kotlin/net/axay/kspigot/data/NBTData.kt +++ b/src/main/kotlin/net/axay/kspigot/data/NBTData.kt @@ -61,4 +61,4 @@ class NBTData { companion object { fun deserialize(nbtString: String) = NBTData(nbtString) } -} \ 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 eb4bb71d..61c58bdb 100644 --- a/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt +++ b/src/main/kotlin/net/axay/kspigot/data/NBTDataLoader.kt @@ -24,4 +24,4 @@ val ItemStack.nbtData: NBTData 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 29d3c614..007daa16 100644 --- a/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt +++ b/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt @@ -9,27 +9,50 @@ interface NBTDataType { fun writeToCompound(key: String, data: T, compound: NBTTagCompound) 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 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 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) } + ) } } @@ -46,4 +69,4 @@ private inline fun nbtDataType( override fun writeToCompound(key: String, data: T, compound: NBTTagCompound) = writeToCompound.invoke(key, data, compound) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt b/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt index b9a3663d..73fcb97e 100644 --- a/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt +++ b/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt @@ -75,4 +75,4 @@ inline fun listen( } if (register) listener.register(priority, ignoreCancelled) return listener -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt index b9cb27c5..5d89993b 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt @@ -46,4 +46,4 @@ val console get() = Bukkit.getConsoleSender() /** * Shortcut for creating a new [NamespacedKey] */ -fun pluginKey(key: String) = NamespacedKey(KSpigotMainInstance, key) \ No newline at end of file +fun pluginKey(key: String) = NamespacedKey(KSpigotMainInstance, key) diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt index 46c3e6f3..7871ae34 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt @@ -1,5 +1,7 @@ package net.axay.kspigot.extensions.bukkit + // FROM BUNGEE COLOR + /** * Returns the corresponding Bukkit Color object. */ @@ -12,7 +14,9 @@ val net.md_5.bungee.api.ChatColor.bukkitColor */ val net.md_5.bungee.api.ChatColor.javaAwtColor: java.awt.Color get() = color + // FROM BUKKIT COLOR + /** * Returns the corresponding Bungee Color object. */ @@ -24,7 +28,9 @@ val org.bukkit.Color.bungeeColor: net.md_5.bungee.api.ChatColor */ val org.bukkit.Color.javaAwtColor: java.awt.Color get() = java.awt.Color(asRGB()) + // FROM JAVA AWT COLOR + /** * Returns the corresponding Bukkit Color object. */ @@ -36,7 +42,9 @@ val java.awt.Color.bukkitColor */ val java.awt.Color.bungeeColor: net.md_5.bungee.api.ChatColor get() = net.md_5.bungee.api.ChatColor.of(this) + // FROM BUKKIT CHAT COLOR + /** * Returns the corresponding Bukkit Color object. */ @@ -53,4 +61,4 @@ val org.bukkit.ChatColor.bungeeColor: net.md_5.bungee.api.ChatColor * Returns the corresponding Java Color object. */ val org.bukkit.ChatColor.javaAwtColor: java.awt.Color - get() = bungeeColor.javaAwtColor \ No newline at end of file + get() = bungeeColor.javaAwtColor diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/CommandExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/CommandExtensions.kt index 80ef4986..7ad1b931 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/CommandExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/CommandExtensions.kt @@ -17,4 +17,4 @@ fun CommandExecutor.register(commandName: String): Boolean { return true } return false -} \ No newline at end of file +} 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 e767dc27..798fd81e 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ConsoleSenderExtensions.kt @@ -29,4 +29,4 @@ fun CommandSender.error(text: String, plugin: Plugin? = KSpigotMainInstance) = * 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 + sendMessage("${prefixColor}[${prefix}]${textColor} $text") 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 96cabdf9..0ba9ee06 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt @@ -14,11 +14,6 @@ import org.bukkit.entity.* import org.bukkit.inventory.EquipmentSlot import org.bukkit.inventory.ItemStack -/** - * Checks if the entity is completely in water. - */ -val LivingEntity.isInWater: Boolean get() = isFeetInWater && isHeadInWater - /** * Checks if the entities' head is in water. */ @@ -167,4 +162,4 @@ fun Player.sendToServer(servername: String) { * Adds the given ItemStacks to the player's inventory. * @return The items that did not fit into the player's inventory. */ -fun Player.give(vararg itemStacks: ItemStack) = inventory.addItem(*itemStacks) \ No newline at end of file +fun Player.give(vararg itemStacks: ItemStack) = inventory.addItem(*itemStacks) diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GameModeExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GameModeExtensions.kt index 30490bea..05a35d0e 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GameModeExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GameModeExtensions.kt @@ -9,4 +9,4 @@ val GameMode.isDamageable: Boolean get() = when (this) { GameMode.SURVIVAL, GameMode.ADVENTURE -> true GameMode.SPECTATOR, GameMode.CREATIVE -> false - } \ No newline at end of file + } diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GeoExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GeoExtensions.kt index 9bba4418..139e67fb 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GeoExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/GeoExtensions.kt @@ -23,4 +23,4 @@ val Chunk.allBlocks for (z in 0 until 16) add(getBlock(x, y, z)) } - } \ No newline at end of file + } diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt index 2a479c47..958d4162 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt @@ -10,4 +10,4 @@ val BookMeta.content append('\n') append(it) } - }.toString() \ No newline at end of file + }.toString() 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 bbf5c0d6..848a3ff6 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/events/PrepareItemCraftEventExtensions.kt @@ -18,4 +18,4 @@ val PrepareItemCraftEvent.isCancelled: Boolean */ 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/KSpigotLocations.kt b/src/main/kotlin/net/axay/kspigot/extensions/geometry/KSpigotLocations.kt index 3bfa168e..6c0d362e 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/KSpigotLocations.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/KSpigotLocations.kt @@ -32,10 +32,11 @@ data class SimpleChunkLocation( val x: Int, val z: Int, ) + // CONVERTER fun Location.toSimple() = SimpleLocation3D(x, y, z) fun Chunk.toSimple() = SimpleChunkLocation(x, z) fun SimpleLocation3D.withWorld(world: World) = Location(world, x, y, z).apply { direction = this@withWorld.direction } fun SimpleChunkLocation.withWorld(world: World) = world.getChunkAt(x, z) fun Vector.toSimpleLoc() = SimpleLocation3D(x, y, z) -fun SimpleLocation3D.toVector() = Vector(x, y, z) \ No newline at end of file +fun SimpleLocation3D.toVector() = Vector(x, y, z) 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 1ecc8907..12072831 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt @@ -14,8 +14,10 @@ class SimpleLocationPair(loc1: Location, loc2: Location) { if (it == loc2.worldOrException) it else throw IllegalArgumentException("The given locations worlds are not the same!") } + val minSimpleLoc = SimpleLocation3D(min(loc1.x, loc2.x), min(loc1.y, loc2.y), min(loc1.z, loc2.z)) 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, @@ -59,14 +61,19 @@ class LocationArea(loc1: Location, loc2: Location) { field = value simpleLocationPair = SimpleLocationPair(loc1, value) } + var simpleLocationPair = SimpleLocationPair(loc1, loc2); private set + val world: World get() = simpleLocationPair.world + val minLoc: Location get() = simpleLocationPair.minSimpleLoc.withWorld(simpleLocationPair.world) val maxLoc: Location get() = simpleLocationPair.maxSimpleLoc.withWorld(simpleLocationPair.world) + val touchedChunks: Set get() = simpleLocationPair.touchedSimpleChunks.mapTo(HashSet()) { it.withWorld(world) } + fun isInArea( 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 a800eb9e..ae1d7f09 100644 --- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt @@ -36,6 +36,7 @@ infix fun Location.reduceZ(distance: Number) = subtract(0.0, 0.0, distance) infix fun Location.reduceXY(distance: Number) = subtract(distance, distance, 0.0) infix fun Location.reduceYZ(distance: Number) = subtract(0.0, distance, distance) infix fun Location.reduceXZ(distance: Number) = subtract(distance, 0.0, distance) + // extensions fun Location.add(x: Number, y: Number, z: Number) = add(x.toDouble(), y.toDouble(), z.toDouble()) fun Location.subtract(x: Number, y: Number, z: Number) = subtract(x.toDouble(), y.toDouble(), z.toDouble()) @@ -83,9 +84,11 @@ infix fun Location.increase(loc: Location) = add(loc) infix fun Location.reduce(loc: Location) = subtract(loc) infix fun Location.increase(loc: SimpleLocation3D) = add(loc.x, loc.y, loc.z) infix fun Location.reduce(loc: SimpleLocation3D) = subtract(loc.x, loc.y, loc.z) + /* * VECTOR */ + val Vector.isFinite: Boolean get() = x.isFinite() && y.isFinite() && z.isFinite() // fast construct @@ -125,4 +128,4 @@ operator fun Vector.timesAssign(num: Number) { infix fun Vector.increase(vec: Vector) = add(vec) infix fun Vector.reduce(vec: Vector) = subtract(vec) infix fun Vector.multiply(vec: Vector) = multiply(vec) -infix fun Vector.multiply(num: Number) = multiply(num.toDouble()) \ No newline at end of file +infix fun Vector.multiply(num: Number) = multiply(num.toDouble()) diff --git a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt index d88b0e00..6a23dd97 100644 --- a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt +++ b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt @@ -23,6 +23,7 @@ fun buildCounterMessageCallback( StringBuilder().apply { if (beforeTime != null) append(beforeTime) + val hourTime = (curSeconds / 3600) val minuteTime = ((curSeconds % 3600) / 60) val secondsTime = (curSeconds % 60) @@ -78,4 +79,4 @@ private val Long.isCounterValue: Boolean 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/game/GamePhasesBuilder.kt b/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt index 8408837a..4300859b 100644 --- a/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt @@ -39,4 +39,4 @@ class GamePhaseBuilder(val length: Long) { fun build() = GamePhase(length, start, end, counterMessage) } -fun buildGame(builder: GamePhaseSystemBuilder.() -> Unit) = GamePhaseSystemBuilder().apply(builder).build() \ No newline at end of file +fun buildGame(builder: GamePhaseSystemBuilder.() -> Unit) = GamePhaseSystemBuilder().apply(builder).build() diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUI.kt b/src/main/kotlin/net/axay/kspigot/gui/GUI.kt index cb4c87e8..c8c70afe 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUI.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUI.kt @@ -39,6 +39,7 @@ abstract class GUI( * all instances. */ abstract fun closeGUI() + protected fun unregisterAndClose() { getAllInstances().forEach { it.bukkitInventory.closeForViewers() @@ -58,7 +59,9 @@ class GUIShared( } override fun getInstance(player: Player) = singleInstance + override fun getAllInstances() = _singleInstance?.let { listOf(it) } ?: emptyList() + override fun closeGUI() { unregisterAndClose() _singleInstance = null @@ -71,10 +74,12 @@ class GUIIndividual( resetOnQuit: Boolean, ) : GUI(guiData) { private val playerInstances = HashMap>() + override fun getInstance(player: Player) = playerInstances[player] ?: createInstance(player) override fun getAllInstances() = playerInstances.values + private fun createInstance(player: Player) = GUIInstance(this, player).apply { playerInstances[player] = this @@ -82,6 +87,7 @@ class GUIIndividual( } fun deleteInstance(player: Player) = playerInstances.remove(player)?.unregister() + override fun closeGUI() { unregisterAndClose() playerInstances.clear() @@ -107,8 +113,11 @@ class GUIInstance( holder: Player?, ) { internal val bukkitInventory = gui.data.guiType.createBukkitInv(holder, gui.data.title) + private val currentElements = HashSet>() + internal var isInMove: Boolean = false + var currentPageInt: Int = gui.data.defaultPage; private set val currentPage get() = getPage(currentPageInt) @@ -222,4 +231,4 @@ class GUIInstance( if (!isInMove) loadPage(currentPage) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt index c045591e..ac4603a2 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt @@ -7,4 +7,4 @@ class GUIClickEvent( val bukkitEvent: InventoryClickEvent, val guiInstance: GUIInstance, val player: Player, -) \ No newline at end of file +) diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt b/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt index b7c0d93e..774f9771 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt @@ -13,4 +13,4 @@ class IndividualGUICreator( private val resetOnQuit: Boolean = true, ) : GUICreator() { override fun createInstance(guiData: GUIData) = GUIIndividual(guiData, resetOnClose, resetOnQuit) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt index 8c1d85c8..1fd89035 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt @@ -5,15 +5,17 @@ import org.bukkit.inventory.ItemStack abstract class GUISlot { abstract fun onClick(clickEvent: GUIClickEvent) } -// ELEMENT + abstract class GUIElement : GUISlot() { abstract fun getItemStack(slot: Int): ItemStack + final override fun onClick(clickEvent: GUIClickEvent) { clickEvent.guiInstance.gui.data.generalOnClick?.invoke(clickEvent) onClickElement(clickEvent) } protected abstract fun onClickElement(clickEvent: GUIClickEvent) + internal open fun startUsing(gui: GUIInstance<*>) {} internal open fun stopUsing(gui: GUIInstance<*>) {} -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt index 7670e43e..5373f3cc 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt @@ -13,4 +13,4 @@ internal fun Player.openGUIInstance(guiInstance: GUIInstance<*>, page: Int? = nu guiInstance.loadPageUnsafe(page) return openInventory(guiInstance.bukkitInventory) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt index ecf4c85a..b8a7d281 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt @@ -8,6 +8,7 @@ import org.bukkit.inventory.Inventory object GUIHolder : AutoCloseable { private val registered = HashMap>() + fun register(guiInstance: GUIInstance) { registered[guiInstance.bukkitInventory] = guiInstance } @@ -39,6 +40,7 @@ object GUIHolder : AutoCloseable { val inv = it.inventory val gui = registered[inv] ?: return@listen val player = it.playerOrCancel ?: return@listen + var ifCancel = false for (slotIndex in it.inventorySlots) { @@ -80,4 +82,4 @@ private val InventoryInteractEvent.playerOrCancel: Player? get() = (whoClicked as? Player) ?: kotlin.run { isCancelled = true return null - } \ No newline at end of file + } diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt index 41310bb3..44263e65 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt @@ -5,4 +5,4 @@ class GUIPage( internal val slots: Map>, val transitionTo: PageChangeEffect?, val transitionFrom: PageChangeEffect?, -) \ No newline at end of file +) diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt b/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt index 5e1b5976..4855f479 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt @@ -7,6 +7,7 @@ import net.axay.kspigot.languageextensions.kotlinextensions.MinMaxPair // INVENTORY data class InventoryDimensions(val width: Int, val height: Int) { val slotAmount = width * height + val invSlots by lazy { ArrayList().apply { (1..height).forEach { row -> @@ -60,6 +61,7 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable { fun withInvType(invType: GUIType): Collection + fun realSlotsWithInvType(invType: GUIType) = withInvType(invType).mapNotNull { it.realSlotIn(invType.dimensions) } } @@ -70,6 +72,7 @@ open class SingleInventorySlot internal constructor( constructor(row: Int, slotInRow: Int) : this(InventorySlot(row, slotInRow)) private val slotAsList = listOf(inventorySlot) + override fun withInvType(invType: GUIType) = slotAsList } @@ -194,8 +197,10 @@ class InventoryCornerSlots internal constructor( class InventoryAllSlots : InventorySlotCompound { override fun withInvType(invType: GUIType) = invType.dimensions.invSlots } + // SLOT TYPE SAFETY -// COLUMNS + +// columns interface ForColumnOne : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine interface ForColumnTwo : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine interface ForColumnThree : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine @@ -205,7 +210,7 @@ interface ForColumnSix : ForInventoryWidthNine interface ForColumnSeven : ForInventoryWidthNine interface ForColumnEight : ForInventoryWidthNine interface ForColumnNine : ForInventoryWidthNine -// ROWS +// rows interface ForRowOne : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine @@ -216,22 +221,19 @@ interface ForRowThree : ForInventoryThreeByNine, ForInventoryFourByNine, ForInve interface ForRowFour : ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine interface ForRowFive : ForInventoryFiveByNine, ForInventorySixByNine interface ForRowSix : ForInventorySixByNine - -// EDGE CASES: -// ROW ONE +// edge cases: +// row one interface ForRowOneSlotOneToThree : ForRowOne, ForInventoryOneByFive, ForInventoryThreeByThree interface ForRowOneSlotFourToFive : ForRowOne, ForInventoryOneByFive - -// ROW TWO +// row two interface ForRowTwoSlotOneToThree : ForRowTwo, ForInventoryThreeByThree - -// ROW THREE +// row three interface ForRowThreeSlotOneToThree : ForRowThree, ForInventoryThreeByThree - -// COMPLETE ROWS (including the edge cases) +// complete rows (including the edge cases) interface ForCompleteRowOne : ForRowOne, ForRowOneSlotOneToThree, ForRowOneSlotFourToFive interface ForCompleteRowTwo : ForRowTwo, ForRowTwoSlotOneToThree interface ForCompleteRowThree : ForRowThree, ForRowThreeSlotOneToThree + object Slots { // ROW ONE val RowOneSlotOne = SingleInventorySlot(1, 1) @@ -337,4 +339,4 @@ object Slots { // ALL val All = InventoryAllSlots() -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt index eaf5f337..f13c9aee 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt @@ -32,6 +32,7 @@ class GUIType( } } } + // INVENTORY TYPE SAFETY interface ForInventory interface ForInventoryThreeByThree : ForInventoryThreeByNine @@ -42,12 +43,11 @@ interface ForInventoryThreeByNine : ForInventoryFourByNine interface ForInventoryFourByNine : ForInventoryFiveByNine interface ForInventoryFiveByNine : ForInventorySixByNine interface ForInventorySixByNine : ForInventory -interface ForEveryInventory - : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, +interface ForEveryInventory : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, 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 + ForInventoryFourByNine, ForInventoryFiveByNine, ForInventorySixByNine diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt index f9e32e32..ef48979a 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt @@ -14,4 +14,4 @@ open class GUIButton( clickEvent.bukkitEvent.isCancelled = true action(clickEvent) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt index a764ac1f..310e5546 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt @@ -19,4 +19,4 @@ class GUIButtonInventoryChange( it.player.openGUIInstance(changeToGUI) onChange?.invoke(it) -}) \ No newline at end of file +}) diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIFreeSlot.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIFreeSlot.kt index b5dc1339..c37afdbf 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIFreeSlot.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIFreeSlot.kt @@ -8,4 +8,4 @@ class GUIFreeSlot : GUISlot() { override fun onClick(clickEvent: GUIClickEvent) { /* do nothing */ } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIPlaceholder.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIPlaceholder.kt index cbd3a4bd..bdce3b7b 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIPlaceholder.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIPlaceholder.kt @@ -12,4 +12,4 @@ class GUIPlaceholder( override fun onClickElement(clickEvent: GUIClickEvent) { clickEvent.bukkitEvent.isCancelled = true } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/gui/elements/GUISpaceCompound.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUISpaceCompound.kt index a23a6436..b3fdddc7 100644 --- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUISpaceCompound.kt +++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUISpaceCompound.kt @@ -10,9 +10,11 @@ class GUISpaceCompoundElement internal constructor( private val compound: AbstractGUISpaceCompound, ) : GUIElement() { override fun getItemStack(slot: Int) = compound.getItemStack(slot) + override fun onClickElement(clickEvent: GUIClickEvent) { compound.onClickElement(clickEvent) } + // the following two methods register and unregister the instance // for each compound element, but that is ok because it gets // added/removed to/from a HashSet @@ -44,12 +46,19 @@ abstract class AbstractGUISpaceCompound internal constructo private val onClick: ((GUIClickEvent, E) -> Unit)?, ) { private val content = ArrayList() + private var currentContent: List = emptyList() + private val internalSlots: MutableList = ArrayList() + private var scrollProgress: Int = 0 + private var contentSort: () -> Unit = { } + private val registeredGUIs = HashSet>() + private fun contentAtSlot(slot: Int) = currentContent.getOrNull(internalSlots.indexOf(slot)) + private fun recalculateCurrentContent() { if (scrollProgress > content.size) throw IllegalStateException("The scrollProgress is greater than the content size.") @@ -83,6 +92,7 @@ abstract class AbstractGUISpaceCompound internal constructo } internal abstract fun handleScrollEndReached(newProgress: Int, internalSlotsSize: Int, contentSize: Int): Boolean + internal fun getItemStack(slot: Int): ItemStack { return contentAtSlot(slot)?.let { return@let iconGenerator.invoke(it) } ?: ItemStack(Material.AIR) @@ -180,4 +190,4 @@ abstract class AbstractGUISpaceCompound internal constructo open class GUICompoundElement( internal val icon: ItemStack, internal val onClick: ((GUIClickEvent) -> Unit)? = null, -) \ No newline at end of file +) diff --git a/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt b/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt index 530193b5..c07e2f6e 100644 --- a/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt +++ b/src/main/kotlin/net/axay/kspigot/items/CustomItemIdentifier.kt @@ -37,4 +37,4 @@ data class CustomItemIdentifier(val customModelData: Int, val placeHolderMateria itemStack } else null } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/items/ItemStackUtils.kt b/src/main/kotlin/net/axay/kspigot/items/ItemStackUtils.kt index 61cdec60..c8996c85 100644 --- a/src/main/kotlin/net/axay/kspigot/items/ItemStackUtils.kt +++ b/src/main/kotlin/net/axay/kspigot/items/ItemStackUtils.kt @@ -61,4 +61,4 @@ val CharSequence.lengthWithoutMinecraftColour: Int } return count - } \ No newline at end of file + } diff --git a/src/main/kotlin/net/axay/kspigot/items/KSpigotItems.kt b/src/main/kotlin/net/axay/kspigot/items/KSpigotItems.kt index 892fa786..6ea83f95 100644 --- a/src/main/kotlin/net/axay/kspigot/items/KSpigotItems.kt +++ b/src/main/kotlin/net/axay/kspigot/items/KSpigotItems.kt @@ -6,15 +6,11 @@ import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.ItemMeta -/* - ITEM STACK - */ -// creation /** * Creates a new [ItemStack] and opens a builder for it. */ inline fun itemStack(material: Material, builder: ItemStack.() -> Unit) = ItemStack(material).apply(builder) -// extensions + /** * Opens a builder with the current meta. * @param T the specific type of the meta @@ -44,10 +40,7 @@ inline fun ItemStack.setMeta(builder: T.() -> Unit) { /** @see setMeta */ @JvmName("simpleSetMeta") inline fun ItemStack.setMeta(builder: ItemMeta.() -> Unit) = setMeta(builder) -/* - ITEM META - */ -// creation + /** * Creates new a [ItemMeta] instance of the given material and opens a builder for it. * @param T the specific type of the meta @@ -60,7 +53,7 @@ inline fun itemMeta(material: Material, builder: T.() -> /** @see itemMeta */ @JvmName("simpleItemMeta") inline fun itemMeta(material: Material, builder: ItemMeta.() -> Unit) = itemMeta(material, builder) -// extensions + /** * Sets the lore (description) of the item. */ @@ -127,4 +120,4 @@ var ItemMeta.customModel: Int? */ var ItemMeta.localName: String get() = localizedName - set(value) = setLocalizedName(value) \ No newline at end of file + set(value) = setLocalizedName(value) diff --git a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/FileExtensions.kt b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/FileExtensions.kt index e61859fc..5b576991 100644 --- a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/FileExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/FileExtensions.kt @@ -2,10 +2,12 @@ package net.axay.kspigot.languageextensions.kotlinextensions import java.io.File -internal fun File.createIfNotExists(): Boolean { +fun File.createIfNotExists(): Boolean { return if (!exists()) { if (!parentFile.exists()) parentFile.mkdirs() - createNewFile() + if (isDirectory) + mkdir() + else createNewFile() } else true } diff --git a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/GeneralExtensions.kt b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/GeneralExtensions.kt index ad44dd30..8e5f0be8 100644 --- a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/GeneralExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/GeneralExtensions.kt @@ -4,4 +4,4 @@ internal fun T.applyIfNotNull(block: (T.() -> Unit)?): T { if (block != null) apply(block) return this -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/LazyExtensions.kt b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/LazyExtensions.kt index 000e526a..accc209a 100644 --- a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/LazyExtensions.kt +++ b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/LazyExtensions.kt @@ -1,5 +1,7 @@ package net.axay.kspigot.languageextensions.kotlinextensions internal inline fun Lazy.ifInitialized(block: (T) -> R) = if (isInitialized()) block(value) else null + internal val Lazy.valueIfInitialized get() = ifInitialized { value } -internal fun Lazy.closeIfInitialized() = ifInitialized { value.close() } \ No newline at end of file + +internal fun Lazy.closeIfInitialized() = ifInitialized { value.close() } diff --git a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/MinMaxPair.kt b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/MinMaxPair.kt index 1da45e22..41d21fee 100644 --- a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/MinMaxPair.kt +++ b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/MinMaxPair.kt @@ -13,4 +13,4 @@ internal class MinMaxPair>(a: T, b: T) { min = a; max = b } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/StringBuilderUtils.kt b/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/StringBuilderUtils.kt deleted file mode 100644 index 8561f7d6..00000000 --- a/src/main/kotlin/net/axay/kspigot/languageextensions/kotlinextensions/StringBuilderUtils.kt +++ /dev/null @@ -1,12 +0,0 @@ -package net.axay.kspigot.languageextensions.kotlinextensions - -internal fun stringBuilder(builder: StringBuilder.() -> Unit) = StringBuilder().apply(builder).toString() -inline fun multiLine(builder: MultiLineBuilder.() -> Unit) = MultiLineBuilder().apply(builder).build() -class MultiLineBuilder { - private val stringBuilder = StringBuilder() - operator fun String.unaryPlus() { - stringBuilder.appendLine(this) - } - - fun build() = stringBuilder.toString() -} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/localization/KSpigotLocalization.kt b/src/main/kotlin/net/axay/kspigot/localization/KSpigotLocalization.kt index a845740a..b9b69744 100644 --- a/src/main/kotlin/net/axay/kspigot/localization/KSpigotLocalization.kt +++ b/src/main/kotlin/net/axay/kspigot/localization/KSpigotLocalization.kt @@ -73,4 +73,4 @@ fun Player.localize(key: String) = * @see Localization.get */ fun Player.localize(key: String, vararg args: Pair) = - Localization.get(Localization.localeProvider(this), key, *args) \ No newline at end of file + Localization.get(Localization.localeProvider(this), key, *args) diff --git a/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt b/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt index 028df845..efea238a 100644 --- a/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt +++ b/src/main/kotlin/net/axay/kspigot/main/KSpigot.kt @@ -56,4 +56,4 @@ abstract class KSpigot : JavaPlugin() { } } -lateinit var KSpigotMainInstance: KSpigot private set \ No newline at end of file +lateinit var KSpigotMainInstance: KSpigot private set diff --git a/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt b/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt deleted file mode 100644 index cb6fcfb9..00000000 --- a/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt +++ /dev/null @@ -1,13 +0,0 @@ -package net.axay.kspigot.main - -import com.google.gson.Gson -import com.google.gson.GsonBuilder - -object ValueHolder { - private val gsonBuilder by lazy { - GsonBuilder() - } - private val gson: Gson by lazy { gsonBuilder.create() } - private val gsonPretty: Gson by lazy { gsonBuilder.setPrettyPrinting().create() } - fun getGson(pretty: Boolean = false) = if (pretty) gsonPretty else gson -} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt b/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt index eae6f985..8bc18084 100644 --- a/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt +++ b/src/main/kotlin/net/axay/kspigot/particles/KSpigotParticles.kt @@ -79,4 +79,4 @@ fun Location.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? * @see KSpigotParticle */ fun Player.particle(particle: Particle, builder: (KSpigotParticle.() -> Unit)? = null) = - KSpigotParticle(particle).applyIfNotNull(builder).spawnFor(this) \ No newline at end of file + KSpigotParticle(particle).applyIfNotNull(builder).spawnFor(this) diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessage.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessage.kt index 5ac261b8..0ebd299d 100644 --- a/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessage.kt +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessage.kt @@ -11,4 +11,4 @@ interface BungeePluginMessageRandomPlayer { interface BungeePluginMessagePlayerSpecific { fun sendWithPlayer(player: Player) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt index c98799e7..5c37e440 100644 --- a/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/BungeePluginMessageReceiver.kt @@ -34,4 +34,4 @@ private object BungeePluginMessageReceiver : PluginMessageListener { callback.onResponse.invoke(msgin) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessagePresets.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessagePresets.kt index 41ab212f..6774130e 100644 --- a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessagePresets.kt +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessagePresets.kt @@ -86,4 +86,4 @@ class PluginMessageGetServers( ) { response.invoke(it.readUTF().split(", ")) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt index 623638d9..e25e0c09 100644 --- a/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt +++ b/src/main/kotlin/net/axay/kspigot/pluginmessages/PluginMessages.kt @@ -62,4 +62,4 @@ fun sendPluginMessageToBungeeCord( BungeePluginMessageResponseCallback(subChannel, responseTimeout, onResponse) player.sendPluginMessage(KSpigotMainInstance, "BungeeCord", msgbytes.toByteArray()) -} \ 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 e70ab898..f23ca097 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/ChainableRunnables.kt @@ -8,6 +8,7 @@ abstract class ChainedRunnablePart( val sync: Boolean, ) { var next: ChainedRunnablePart? = null + protected abstract fun invoke(data: T): R /** @@ -79,6 +80,7 @@ class ChainedRunnablePartFirst( sync: Boolean, ) : ChainedRunnablePart(sync) { override fun execute() = start(Unit) + override fun executeCatchingImpl( exceptionClass: KClass, exceptionSync: Boolean, @@ -94,6 +96,7 @@ class ChainedRunnablePartThen( val previous: ChainedRunnablePart<*, T>, ) : ChainedRunnablePart(sync) { override fun execute() = previous.execute() + override fun executeCatchingImpl( exceptionClass: KClass, exceptionSync: Boolean, @@ -113,4 +116,4 @@ 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 +fun ChainedRunnablePart.thenAsync(runnable: (R) -> U) = thenDo(false, runnable) diff --git a/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt b/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt index 8bb494cd..be170886 100644 --- a/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt +++ b/src/main/kotlin/net/axay/kspigot/runnables/KSpigotRunnables.kt @@ -127,4 +127,4 @@ fun sync(runnable: () -> Unit) = Bukkit.getScheduler().runTask(KSpigotMainInstan /** * 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) diff --git a/src/main/kotlin/net/axay/kspigot/serialization/ConfigurationSerializableAdapter.kt b/src/main/kotlin/net/axay/kspigot/serialization/ConfigurationSerializableAdapter.kt index dcb68f18..6db0d1bb 100644 --- a/src/main/kotlin/net/axay/kspigot/serialization/ConfigurationSerializableAdapter.kt +++ b/src/main/kotlin/net/axay/kspigot/serialization/ConfigurationSerializableAdapter.kt @@ -24,10 +24,12 @@ object ItemMetaSerializer : KSerializerForBukkit(ItemMeta::class) object ItemStackSerializer : KSerializerForBukkit(ItemStack::class) object LocationSerializer : KSerializerForBukkit(Location::class) object VectorSerializer : KSerializerForBukkit(Vector::class) + open class KSerializerForBukkit( private val kClass: KClass, ) : KSerializer { override val descriptor = ByteArraySerializer().descriptor + override fun serialize(encoder: Encoder, value: T) { val bytes = ByteArrayOutputStream() BukkitObjectOutputStream(bytes).use { @@ -45,4 +47,4 @@ open class KSerializerForBukkit( ?: throw IllegalStateException("The object can not be deserialized to an object of the type ${kClass.simpleName}") } } -} \ 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 deleted file mode 100644 index a09ad45d..00000000 --- a/src/main/kotlin/net/axay/kspigot/serialization/SpigotSerializable.kt +++ /dev/null @@ -1,26 +0,0 @@ -package net.axay.kspigot.serialization - -import net.axay.kspigot.main.ValueHolder - -interface SpigotSerializable { - /** - * Converts this serializable object - * into the corresponding spigot object. - */ - fun toSpigot(): T -} - -interface SpigotSerializableCompanion - -/** - * @return A json string. - */ -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 SpigotSerializableCompanion.deserialize(json: String): T = - ValueHolder.getGson().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 deleted file mode 100644 index 46c01c8a..00000000 --- a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableLocation.kt +++ /dev/null @@ -1,26 +0,0 @@ -@file:Suppress("MemberVisibilityCanBePrivate") - -package net.axay.kspigot.serialization.serializables - -import net.axay.kspigot.serialization.SpigotSerializable -import net.axay.kspigot.serialization.SpigotSerializableCompanion -import org.bukkit.Location - -data class SerializableLocation( - val world: SerializableWorld?, - val x: Double, - val y: Double, - val z: Double, - val direction: SerializableVector, -) : SpigotSerializable { - companion object : SpigotSerializableCompanion - - constructor(loc: Location) : this(loc.world?.let { SerializableWorld(it) }, - loc.x, - loc.y, - loc.z, - SerializableVector(loc.direction)) - - override fun toSpigot() = Location(world?.toSpigot(), x, y, z) - .apply { direction = this@SerializableLocation.direction.toSpigot() } -} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableVector.kt b/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableVector.kt deleted file mode 100644 index 73ba79a2..00000000 --- a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableVector.kt +++ /dev/null @@ -1,19 +0,0 @@ -@file:Suppress("MemberVisibilityCanBePrivate") - -package net.axay.kspigot.serialization.serializables - -import net.axay.kspigot.serialization.SpigotSerializable -import net.axay.kspigot.serialization.SpigotSerializableCompanion -import org.bukkit.util.Vector - -data class SerializableVector( - val x: Double, - val y: Double, - val z: Double, -) : SpigotSerializable { - companion object : SpigotSerializableCompanion - - constructor(vec: Vector) : this(vec.x, vec.y, vec.z) - - override fun toSpigot() = Vector(x, y, z) -} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableWorld.kt b/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableWorld.kt deleted file mode 100644 index c01ed9a6..00000000 --- a/src/main/kotlin/net/axay/kspigot/serialization/serializables/SerializableWorld.kt +++ /dev/null @@ -1,19 +0,0 @@ -@file:Suppress("MemberVisibilityCanBePrivate") - -package net.axay.kspigot.serialization.serializables - -import net.axay.kspigot.serialization.SpigotSerializable -import net.axay.kspigot.serialization.SpigotSerializableCompanion -import org.bukkit.Bukkit -import org.bukkit.World - -class SerializableWorld( - val name: String, -) : SpigotSerializable { - companion object : SpigotSerializableCompanion - - constructor(world: World) : this(world.name) - - override fun toSpigot() = Bukkit.getWorld(name) - ?: throw NullPointerException("The world \"$name\" does not exist") -} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/structures/Circle.kt b/src/main/kotlin/net/axay/kspigot/structures/Circle.kt index ff1b2ac0..4ec2cbf8 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/Circle.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/Circle.kt @@ -10,6 +10,7 @@ import org.bukkit.entity.EntityType private fun circleEdgeLocations(radius: Number) = HashSet().apply { val currentRadius = radius.toDouble() + var d = -currentRadius var x = currentRadius var y = 0 @@ -40,6 +41,7 @@ private fun MutableSet.addSimpleLoc2D(first: Number, second: N abstract class Circle(val radius: Number) { protected abstract val data: StructureData + val fillLocations by lazy { var currentRadius = radius.toDouble() @@ -66,8 +68,10 @@ abstract class Circle(val radius: Number) { val edgeLocations by lazy { circleEdgeLocations(radius) } + val filledStructure by lazy { structure(true) } val edgeStructure by lazy { structure(false) } + fun structure(filled: Boolean) = Structure( HashSet().apply { val locations = if (filled) fillLocations else edgeLocations @@ -87,4 +91,4 @@ class ParticleCircle(radius: Number, particle: KSpigotParticle) : Circle(radius) class EntityCircle(radius: Number, entityType: EntityType) : Circle(radius) { override val data = StructureDataEntity(entityType) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/structures/Structure.kt b/src/main/kotlin/net/axay/kspigot/structures/Structure.kt index 4b828f78..ecc434c6 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/Structure.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/Structure.kt @@ -28,9 +28,6 @@ data class Structure( constructor(vararg structureDataSets: Set) : this(structureDataSets.flatMapTo(HashSet()) { it }) } -/* - * Structure data implementations. - */ data class StructureDataMaterial( val material: Material, @@ -73,4 +70,4 @@ data class StructureDataParticle( override fun createAt(loc: Location) { particle.spawnAt(loc) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt b/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt index c1e929dc..5d6c9167 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/StructureBuilder.kt @@ -33,4 +33,4 @@ inline fun Structure.rotate(angle: Number, vectorRotation: (Vector, Double) -> V ) } } -) \ 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 0d10efb8..01a25731 100644 --- a/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt +++ b/src/main/kotlin/net/axay/kspigot/structures/StructureLoader.kt @@ -30,8 +30,7 @@ fun LocationArea.loadStructure(includeBlocks: Boolean = true, includeEntities: B * Sorted by their coordinates. */ val LocationArea.fillBlocks: Set - get() - = LinkedHashSet().apply { + 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 -> @@ -45,12 +44,11 @@ val LocationArea.fillBlocks: Set * @return All entities in the given [LocationArea]. */ val LocationArea.entities: Set - get() - = HashSet().apply { + get() = HashSet().apply { touchedChunks.forEach { it.entities.forEach { en -> if (simpleLocationPair.isInArea(en.location)) this += en } } - } \ No newline at end of file + } diff --git a/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt b/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt index 8f10323e..5fe87128 100644 --- a/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt +++ b/src/main/kotlin/net/axay/kspigot/utils/DirectionUtils.kt @@ -49,4 +49,4 @@ enum class CardinalDirection { } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/utils/FireworkBuilder.kt b/src/main/kotlin/net/axay/kspigot/utils/FireworkBuilder.kt index 1b7cce6c..5411adb5 100644 --- a/src/main/kotlin/net/axay/kspigot/utils/FireworkBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/utils/FireworkBuilder.kt @@ -31,4 +31,4 @@ fun FireworkMeta.addEffect(builder: FireworkEffect.Builder.() -> Unit) = */ fun Firework.editMeta(builder: FireworkMeta.() -> Unit) { fireworkMeta = fireworkMeta.apply(builder) -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/utils/KSpigotFirework.kt b/src/main/kotlin/net/axay/kspigot/utils/KSpigotFirework.kt index 51edb10c..76e0dac3 100644 --- a/src/main/kotlin/net/axay/kspigot/utils/KSpigotFirework.kt +++ b/src/main/kotlin/net/axay/kspigot/utils/KSpigotFirework.kt @@ -17,6 +17,7 @@ object KSpigotFirework { class KSpigotFireworkBuilder { val effects = ArrayList() var power: Int? = null + inline fun effect(builder: FireworkEffectBuilder.() -> Unit) { effects += FireworkEffectBuilder().apply(builder).fireworkEffect } @@ -32,9 +33,11 @@ class KSpigotFireworkBuilder { class FireworkEffectBuilder { private val fireworkBuilder = FireworkEffect.builder() + var type: FireworkEffect.Type? = null var trail: Boolean? = null var flicker: Boolean? = null + fun fade(vararg colors: Color) { fireworkBuilder.withFade(*colors) } @@ -51,4 +54,4 @@ class FireworkEffectBuilder { return fireworkBuilder.build() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/utils/MinecraftObjectMarker.kt b/src/main/kotlin/net/axay/kspigot/utils/MinecraftObjectMarker.kt index 8b61894c..87a346e5 100644 --- a/src/main/kotlin/net/axay/kspigot/utils/MinecraftObjectMarker.kt +++ b/src/main/kotlin/net/axay/kspigot/utils/MinecraftObjectMarker.kt @@ -33,7 +33,7 @@ fun PersistentDataHolder.unmark(key: String) { * this objects' markings. */ fun PersistentDataHolder.hasMark(key: String) = persistentDataContainer.has(markerKey(key), PersistentDataType.BYTE) -// quick access for ItemStacks + /** @see PersistentDataHolder.mark */ fun ItemStack.mark(key: String) = meta { mark(key) } @@ -45,4 +45,4 @@ fun ItemStack.hasMark(key: String): Boolean { var result: Boolean = false meta { result = hasMark(key) } return result -} \ No newline at end of file +}