From cf48510756bb7bc13d47fb8bb6b1d382109ef549 Mon Sep 17 00:00:00 2001
From: Jakob K <52456572+bluefireoly@users.noreply.github.com>
Date: Wed, 12 May 2021 14:17:44 +0200
Subject: [PATCH] Improve code style
---
.../java/net/axay/kspigot/chat/KColors.java | 2 -
.../net/axay/kspigot/chat/KSpigotChat.kt | 12 +---
.../axay/kspigot/chat/input/PlayerInput.kt | 15 ++---
.../implementations/PlayerInputAnvilInv.kt | 5 +-
.../input/implementations/PlayerInputBook.kt | 12 +---
.../input/implementations/PlayerInputChat.kt | 3 +-
.../net/axay/kspigot/config/ConfigManager.kt | 12 +---
.../kotlin/net/axay/kspigot/data/NBTData.kt | 4 --
.../net/axay/kspigot/data/NBTDataType.kt | 11 +---
.../axay/kspigot/event/KSpigotListeners.kt | 6 +-
.../kspigot/extensions/GeneralExtensions.kt | 1 -
.../extensions/bukkit/ColorExtensions.kt | 8 ---
.../extensions/bukkit/EntityExtensions.kt | 2 +-
.../extensions/bukkit/InventoryExtensions.kt | 2 +-
.../extensions/bukkit/MetaExtensions.kt | 18 +++---
.../events/InteractEventExtensions.kt | 4 --
.../extensions/geometry/KSpigotLocations.kt | 13 +----
.../extensions/geometry/LocationArea.kt | 18 +-----
.../geometry/ModificationExtensions.kt | 7 ---
.../net/axay/kspigot/game/GamePhases.kt | 11 +---
.../axay/kspigot/game/GamePhasesBuilder.kt | 8 +--
src/main/kotlin/net/axay/kspigot/gui/GUI.kt | 50 ++--------------
.../kotlin/net/axay/kspigot/gui/GUIBuilder.kt | 42 +++++---------
.../net/axay/kspigot/gui/GUIClickEvent.kt | 2 +-
.../kotlin/net/axay/kspigot/gui/GUICreator.kt | 2 +-
.../net/axay/kspigot/gui/GUIElements.kt | 6 --
.../net/axay/kspigot/gui/GUIExtensions.kt | 2 -
.../kotlin/net/axay/kspigot/gui/GUIHolder.kt | 17 ------
.../kotlin/net/axay/kspigot/gui/GUIPage.kt | 2 +-
.../net/axay/kspigot/gui/GUIPageChange.kt | 28 +--------
.../kotlin/net/axay/kspigot/gui/GUISlots.kt | 58 +++----------------
.../kotlin/net/axay/kspigot/gui/GUIType.kt | 10 +---
.../axay/kspigot/gui/elements/GUIButton.kt | 3 -
.../gui/elements/GUIButtonInventoryChange.kt | 6 +-
.../gui/elements/GUIButtonPageChange.kt | 5 +-
.../kspigot/gui/elements/GUIPlaceholder.kt | 5 +-
.../kspigot/gui/elements/GUISpaceCompound.kt | 34 ++---------
.../elements/GUISpaceCompoundScrollButton.kt | 11 +---
.../axay/kspigot/ipaddress/BadIPDetection.kt | 24 ++------
.../axay/kspigot/ipaddress/IPAddressData.kt | 7 ---
.../badipdetectionservices/GetIPIntel.kt | 7 +--
.../ipaddress/badipdetectionservices/IPHub.kt | 5 +-
.../badipdetectionservices/IPInfo.kt | 5 +-
.../badipdetectionservices/VPNBlocker.kt | 3 -
.../kspigot/items/CustomItemIdentifier.kt | 2 -
.../net/axay/kspigot/items/ItemStackUtils.kt | 51 +++++++---------
.../net/axay/kspigot/items/KSpigotItems.kt | 9 ---
.../kotlinextensions/LazyExtensions.kt | 2 -
.../kotlinextensions/MinMaxPair.kt | 2 -
.../kotlinextensions/StringBuilderUtils.kt | 5 --
.../localization/KSpigotLocalization.kt | 2 -
.../kotlin/net/axay/kspigot/main/KSpigot.kt | 7 ---
.../net/axay/kspigot/main/ValueHolder.kt | 4 --
.../kspigot/particles/KSpigotParticles.kt | 4 +-
.../BungeePluginMessageReceiver.kt | 13 +----
.../pluginmessages/PluginMessagePresets.kt | 12 ++--
.../kspigot/pluginmessages/PluginMessages.kt | 8 +--
.../kspigot/runnables/ChainableRunnables.kt | 21 ++-----
.../kspigot/runnables/KSpigotRunnables.kt | 17 +-----
.../ConfigurationSerializableAdapter.kt | 6 +-
.../serializables/SerializableLocation.kt | 10 ++--
.../serializables/SerializableVector.kt | 4 +-
.../serializables/SerializableWorld.kt | 4 +-
.../net/axay/kspigot/sound/KSpigotSound.kt | 4 +-
.../net/axay/kspigot/structures/Circle.kt | 14 -----
.../net/axay/kspigot/structures/Structure.kt | 21 ++-----
.../net/axay/kspigot/utils/DirectionUtils.kt | 8 ---
.../net/axay/kspigot/utils/KSpigotFirework.kt | 13 -----
.../kspigot/utils/MinecraftObjectMarker.kt | 2 -
69 files changed, 144 insertions(+), 609 deletions(-)
diff --git a/src/main/java/net/axay/kspigot/chat/KColors.java b/src/main/java/net/axay/kspigot/chat/KColors.java
index 7c528247..a81899a4 100644
--- a/src/main/java/net/axay/kspigot/chat/KColors.java
+++ b/src/main/java/net/axay/kspigot/chat/KColors.java
@@ -6,7 +6,6 @@ import java.awt.*;
@SuppressWarnings("unused")
public class KColors {
-
// DEFAULT CODES
public static final ChatColor MAGIC = ChatColor.MAGIC;
@@ -877,5 +876,4 @@ 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 4123964e..84922db8 100644
--- a/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt
+++ b/src/main/kotlin/net/axay/kspigot/chat/KSpigotChat.kt
@@ -13,11 +13,8 @@ inline fun chatComponent(builder: KSpigotComponentBuilder.() -> Unit): Array()
-
// COMPONENTS
-
inline fun text(text: String, builder: TextComponent.() -> Unit = { }) {
this += TextComponent(text).apply(builder)
}
@@ -40,13 +37,11 @@ class KSpigotComponentBuilder {
inline fun translatable(
translatable: String,
with: Array,
- builder: TranslatableComponent.() -> Unit = { }
+ builder: TranslatableComponent.() -> Unit = { },
) {
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) }
}
@@ -60,15 +55,11 @@ 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()))
}
@@ -84,7 +75,6 @@ fun BaseComponent.hoverEventEntity(type: String, id: String, baseComponent: Base
fun BaseComponent.clickEvent(action: ClickEvent.Action, value: String) {
clickEvent = ClickEvent(action, value)
}
-
/*
* GLOBAL SHORTCUTS
*/
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 d07a640d..d725c5b5 100644
--- a/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt
+++ b/src/main/kotlin/net/axay/kspigot/chat/input/PlayerInput.kt
@@ -19,7 +19,7 @@ import org.bukkit.event.Listener
fun Player.awaitChatInput(
question: String = "Type your input in the chat!",
timeoutSeconds: Int = 1 * 60,
- callback: (PlayerInputResult) -> Unit
+ callback: (PlayerInputResult) -> Unit,
) {
PlayerInputChat(this, callback, timeoutSeconds, question)
}
@@ -36,7 +36,7 @@ fun Player.awaitAnvilInput(
"submit your input!"
),
timeoutSeconds: Int = 1 * 60,
- callback: (PlayerInputResult) -> Unit
+ callback: (PlayerInputResult) -> Unit,
) {
PlayerInputAnvilInv(this, callback, timeoutSeconds, invTitle, startText, renameItemDescription)
}
@@ -48,7 +48,7 @@ fun Player.awaitAnvilInput(
*/
fun Player.awaitBookInputAsString(
timeoutSeconds: Int = 1 * 60,
- callback: (PlayerInputResult) -> Unit
+ callback: (PlayerInputResult) -> Unit,
) {
PlayerInputBookComprehensive(this, callback, timeoutSeconds)
}
@@ -61,7 +61,7 @@ fun Player.awaitBookInputAsString(
*/
fun Player.awaitBookInputAsList(
timeoutSeconds: Int = 1 * 60,
- callback: (PlayerInputResult>) -> Unit
+ callback: (PlayerInputResult>) -> Unit,
) {
PlayerInputBookPaged(this, callback, timeoutSeconds)
}
@@ -70,17 +70,13 @@ fun Player.awaitBookInputAsList(
* @param input The input the player gave. Null on timeout or invalid input.
*/
class PlayerInputResult internal constructor(val input: T?)
-
internal abstract class PlayerInput(
protected val player: Player,
private val callback: (PlayerInputResult) -> Unit,
- timeoutSeconds: Int
+ timeoutSeconds: Int,
) {
-
private var received = false
-
protected abstract val inputListeners: List
-
protected fun onReceive(input: T?) {
if (!received) {
inputListeners.forEach { it.unregister() }
@@ -99,5 +95,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 091f5f07..5b5e06ac 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
@@ -19,9 +19,8 @@ internal class PlayerInputAnvilInv(
timeoutSeconds: Int,
invTitle: String,
startText: String,
- renameItemDescription: List
+ renameItemDescription: List,
) : PlayerInput(player, callback, timeoutSeconds) {
-
private val anvilInv =
AnvilGUI.Builder().plugin(KSpigotMainInstance)
.onClose { onReceive(null) }
@@ -42,7 +41,6 @@ internal class PlayerInputAnvilInv(
)
.text("${KColors.ORANGERED}$startText")
.open(player)
-
override val inputListeners = listOf(
listen {
if (it.clickedInventory == anvilInv.inventory)
@@ -53,5 +51,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 c5d4f37b..5b016a0a 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
@@ -17,7 +17,7 @@ import org.bukkit.persistence.PersistentDataType
internal class PlayerInputBookComprehensive(
player: Player,
callback: (PlayerInputResult) -> Unit,
- timeoutSeconds: Int
+ timeoutSeconds: Int,
) : PlayerInputBook(player, callback, timeoutSeconds) {
override fun loadBookContent(bookMeta: BookMeta) = bookMeta.content
}
@@ -25,7 +25,7 @@ internal class PlayerInputBookComprehensive(
internal class PlayerInputBookPaged(
player: Player,
callback: (PlayerInputResult>) -> Unit,
- timeoutSeconds: Int
+ timeoutSeconds: Int,
) : PlayerInputBook>(player, callback, timeoutSeconds) {
override fun loadBookContent(bookMeta: BookMeta): List = bookMeta.pages
}
@@ -33,9 +33,8 @@ internal class PlayerInputBookPaged(
internal abstract class PlayerInputBook(
player: Player,
callback: (PlayerInputResult) -> Unit,
- timeoutSeconds: Int
+ timeoutSeconds: Int,
) : PlayerInput(player, callback, timeoutSeconds) {
-
private val id = getID()
init {
@@ -47,7 +46,6 @@ internal abstract class PlayerInputBook(
}
abstract fun loadBookContent(bookMeta: BookMeta): T
-
override val inputListeners = listOf(
listen {
val meta = it.newBookMeta
@@ -64,16 +62,12 @@ 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 3f6a52b7..85e035b8 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
@@ -12,7 +12,7 @@ internal class PlayerInputChat(
player: Player,
callback: (PlayerInputResult) -> Unit,
timeoutSeconds: Int,
- question: String
+ question: String,
) : PlayerInput(player, callback, timeoutSeconds) {
init {
@@ -27,5 +27,4 @@ internal class PlayerInputChat(
}
}
)
-
}
\ No newline at end of file
diff --git a/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt
index 860884dd..4d7ab8a7 100644
--- a/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt
+++ b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt
@@ -43,11 +43,9 @@ class ConfigDelegate(
private val configClass: KClass,
private val file: File,
private val saveAfterLoad: Boolean,
- private val defaultCallback: (() -> T)?
+ private val defaultCallback: (() -> T)?,
) {
-
private var internalConfig: T = loadIt()
-
var data: T
get() = internalConfig
set(value) {
@@ -55,7 +53,6 @@ class ConfigDelegate(
}
operator fun getValue(thisRef: Any?, property: KProperty<*>) = internalConfig
-
operator fun setValue(thisRef: Any?, property: KProperty<*>, config: T): Boolean {
internalConfig = config
return true
@@ -79,7 +76,6 @@ class ConfigDelegate(
}
private fun loadIt(): T {
-
val loaded = if (defaultCallback == null)
GsonConfigManager.loadConfig(file, configClass)
else
@@ -90,13 +86,10 @@ class ConfigDelegate(
saveIt(loaded)
return loaded
-
}
-
}
internal object GsonConfigManager {
-
fun loadConfig(file: File, configClass: KClass): T =
FileReader(file).use { reader -> return getGson().fromJson(reader, configClass.java) }
@@ -111,7 +104,7 @@ internal object GsonConfigManager {
file: File,
configClass: KClass,
pretty: Boolean = true,
- default: () -> T
+ default: () -> T,
): T {
try {
return loadConfig(file, configClass)
@@ -122,5 +115,4 @@ internal object GsonConfigManager {
}
}
}
-
}
\ 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 73102eae..6894eea2 100644
--- a/src/main/kotlin/net/axay/kspigot/data/NBTData.kt
+++ b/src/main/kotlin/net/axay/kspigot/data/NBTData.kt
@@ -8,7 +8,6 @@ import net.minecraft.server.v1_16_R3.NBTTagCompound
@NMS_General
class NBTData {
-
val nbtTagCompound: NBTTagCompound
constructor(nbtTagCompound: NBTTagCompound?) {
@@ -60,9 +59,6 @@ class NBTData {
operator fun minusAssign(key: String) = remove(key)
companion object {
-
fun deserialize(nbtString: String) = NBTData(nbtString)
-
}
-
}
\ 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 d905132d..29d3c614 100644
--- a/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt
+++ b/src/main/kotlin/net/axay/kspigot/data/NBTDataType.kt
@@ -5,12 +5,10 @@ import net.minecraft.server.v1_16_R3.*
@NMS_General
interface NBTDataType {
-
fun decodeNMS(nbtBase: NBTBase): T?
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 =
@@ -32,9 +30,7 @@ interface NBTDataType {
nbtDataType({ it.asShort() }, { key, data, compound -> compound.setShort(key, data) })
val STRING = nbtDataType({ it.asString() },
{ key, data, compound -> compound.setString(key, data) })
-
}
-
}
/**
@@ -43,16 +39,11 @@ interface NBTDataType {
*/
private inline fun nbtDataType(
crossinline decodeNMS: (E) -> T,
- crossinline writeToCompound: (key: String, data: T, compound: NBTTagCompound) -> Unit
+ 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)
-
}
-
}
\ 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 c16ce7f4..b9a3663d 100644
--- a/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt
+++ b/src/main/kotlin/net/axay/kspigot/event/KSpigotListeners.kt
@@ -28,7 +28,7 @@ fun Listener.unregister() = HandlerList.unregisterAll(this)
inline fun Listener.register(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
- noinline executor: (Listener, Event) -> Unit
+ noinline executor: (Listener, Event) -> Unit,
) {
pluginManager.registerEvent(T::class.java, this, priority, executor, KSpigotMainInstance, ignoreCancelled)
}
@@ -50,7 +50,7 @@ interface SingleListener : Listener {
*/
inline fun SingleListener.register(
priority: EventPriority = EventPriority.NORMAL,
- ignoreCancelled: Boolean = false
+ ignoreCancelled: Boolean = false,
) {
register(priority, ignoreCancelled) { _, event ->
(event as? T)?.let { this.onEvent(it) }
@@ -68,7 +68,7 @@ inline fun listen(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
register: Boolean = true,
- crossinline onEvent: (event: T) -> Unit
+ crossinline onEvent: (event: 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/GeneralExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt
index b56b4a5c..b9cb27c5 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/GeneralExtensions.kt
@@ -4,7 +4,6 @@ import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
import org.bukkit.command.CommandSender
-import org.bukkit.entity.Entity
import org.bukkit.entity.Player
/**
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 6c885393..46c3e6f3 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/ColorExtensions.kt
@@ -1,7 +1,5 @@
package net.axay.kspigot.extensions.bukkit
-
// FROM BUNGEE COLOR
-
/**
* Returns the corresponding Bukkit Color object.
*/
@@ -14,9 +12,7 @@ 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.
*/
@@ -28,9 +24,7 @@ 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.
*/
@@ -42,9 +36,7 @@ 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.
*/
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 af3efa3b..96cabdf9 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/EntityExtensions.kt
@@ -133,7 +133,7 @@ fun Player.title(
subText: String? = null,
fadeIn: Int = 10,
stay: Int = 70,
- fadeOut: Int = 20
+ fadeOut: Int = 20,
) {
sendTitle(mainText, subText, fadeIn, stay, fadeOut)
}
diff --git a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/InventoryExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/InventoryExtensions.kt
index 198db94a..abe04b6b 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/InventoryExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/InventoryExtensions.kt
@@ -15,7 +15,7 @@ fun Inventory.closeForViewers() = HashSet(viewers).forEach { it.closeInventory()
val InventoryAction.isSimple
get() = when (this) {
InventoryAction.PLACE_ALL, InventoryAction.PLACE_ONE,
- InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_HALF, InventoryAction.PICKUP_ONE
+ InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_HALF, InventoryAction.PICKUP_ONE,
-> true
else -> false
}
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 7ad711b1..2a479c47 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/bukkit/MetaExtensions.kt
@@ -1,13 +1,13 @@
package net.axay.kspigot.extensions.bukkit
import org.bukkit.inventory.meta.BookMeta
-import java.lang.StringBuilder
-val BookMeta.content get() =
- StringBuilder().apply {
- for (it in pages) {
- if (isNotEmpty())
- append('\n')
- append(it)
- }
- }.toString()
\ No newline at end of file
+val BookMeta.content
+ get() =
+ StringBuilder().apply {
+ for (it in pages) {
+ if (isNotEmpty())
+ append('\n')
+ append(it)
+ }
+ }.toString()
\ No newline at end of file
diff --git a/src/main/kotlin/net/axay/kspigot/extensions/events/InteractEventExtensions.kt b/src/main/kotlin/net/axay/kspigot/extensions/events/InteractEventExtensions.kt
index b25e29c6..9d12c541 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/events/InteractEventExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/events/InteractEventExtensions.kt
@@ -40,16 +40,12 @@ val PlayerInteractEvent.clickedBlockExceptAir: Block?
get() {
return clickedBlock ?: kotlin.run {
return@run if (this.action == Action.RIGHT_CLICK_AIR) {
-
val p: Player = this.player
-
// check for sight blocking entities
for (nearbyEntity: Entity in p.getNearbyEntities(5.0, 5.0, 5.0))
if (p.hasLineOfSight(nearbyEntity)) return@run null
-
// get first block in line of sight which is not air
p.getLineOfSight(null, 5).find { block -> !block.type.isAir }
-
} else null
}
}
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 bbce6135..3bfa168e 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/KSpigotLocations.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/KSpigotLocations.kt
@@ -9,7 +9,7 @@ import org.bukkit.util.Vector
data class SimpleLocation2D(
val x: Double,
- val y: Double
+ val y: Double,
) {
constructor(x: Number, y: Number)
: this(x.toDouble(), y.toDouble())
@@ -19,30 +19,23 @@ data class SimpleLocation3D(
val x: Double,
val y: Double,
val z: Double,
- val direction: Vector = vec(0, 0, 0)
+ val direction: Vector = vec(0, 0, 0),
) {
-
constructor(x: Number, y: Number, z: Number)
: this(x.toDouble(), y.toDouble(), z.toDouble())
val chunk: SimpleChunkLocation
get() = SimpleChunkLocation(x.toInt() shr 4, z.toInt() shr 4)
-
}
data class SimpleChunkLocation(
val x: Int,
- val z: 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
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 f06ef177..1ecc8907 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/LocationArea.kt
@@ -10,21 +10,17 @@ import kotlin.math.max
import kotlin.math.min
class SimpleLocationPair(loc1: Location, loc2: Location) {
-
val world = loc1.worldOrException.let {
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,
- tolerance: Int = 0
+ tolerance: Int = 0,
): Boolean {
-
// checking world
if (loc.world != world) return false
@@ -37,11 +33,9 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
// checking y
if (check3d) loc.y >= minSimpleLoc.y - tolerance && loc.y <= maxSimpleLoc.y + tolerance else true
} else false
-
}
val touchedSimpleChunks: Set by lazy {
-
val foundChunks = HashSet()
(minSimpleLoc.chunk.x until maxSimpleLoc.chunk.x + 1).forEach { curX ->
@@ -51,13 +45,10 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
}
return@lazy foundChunks
-
}
-
}
class LocationArea(loc1: Location, loc2: Location) {
-
var loc1: Location = loc1
set(value) {
field = value
@@ -68,19 +59,14 @@ 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
+ 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 a6b5ee5c..a800eb9e 100644
--- a/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/ModificationExtensions.kt
@@ -36,15 +36,10 @@ 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())
-
val Location.blockLoc: Location get() = Location(world, blockX.toDouble(), blockY.toDouble(), blockZ.toDouble())
-
infix fun Location.relationTo(loc: Location) = this.subtract(loc).toSimple()
// operator functions
@@ -88,11 +83,9 @@ 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
diff --git a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt
index 566e49a5..d88b0e00 100644
--- a/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt
+++ b/src/main/kotlin/net/axay/kspigot/game/GamePhases.kt
@@ -18,13 +18,11 @@ fun buildCounterMessageCallback(
secondPlural: String = "s",
hourSingular: String = hourPlural,
minuteSingular: String = minutePlural,
- secondSingular: String = secondPlural
+ secondSingular: String = secondPlural,
): (Long) -> String = { curSeconds ->
-
StringBuilder().apply {
if (beforeTime != null)
append(beforeTime)
-
val hourTime = (curSeconds / 3600)
val minuteTime = ((curSeconds % 3600) / 60)
val secondsTime = (curSeconds % 60)
@@ -46,14 +44,13 @@ fun buildCounterMessageCallback(
if (afterTime != null)
append(afterTime)
}.toString()
-
}
class GamePhase(
val length: Long,
val start: (() -> Unit)?,
val end: (() -> Unit)?,
- val counterMessage: ((secondsLeft: Long) -> String)?
+ val counterMessage: ((secondsLeft: Long) -> String)?,
) {
fun startIt(phaseQueue: MutableList) {
start?.invoke()
@@ -61,21 +58,17 @@ class GamePhase(
period = 20,
howOften = (length / 20) + 1,
endCallback = {
-
end?.invoke()
if (phaseQueue.isNotEmpty())
phaseQueue.removeAt(0).startIt(phaseQueue)
-
}
) {
-
if (counterMessage != null) {
val currentCounter = it.counterDownToZero
if (currentCounter?.isCounterValue == true)
broadcast(counterMessage.invoke(currentCounter))
}
-
}
}
}
diff --git a/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt b/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt
index 10d8b0fb..8408837a 100644
--- a/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt
+++ b/src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt
@@ -3,22 +3,17 @@
package net.axay.kspigot.game
class GamePhaseSystemBuilder {
-
private val gamePhases = mutableListOf()
fun build() = GamePhaseSystem(*gamePhases.toTypedArray())
-
fun phase(length: Long, builder: GamePhaseBuilder.() -> Unit) {
gamePhases += GamePhaseBuilder(length).apply(builder).build()
}
-
}
class GamePhaseBuilder(val length: Long) {
-
private var start: (() -> Unit)? = null
private var end: (() -> Unit)? = null
private var counterMessage: ((secondsLeft: Long) -> String)? = null
-
fun start(callback: () -> Unit) {
start = callback
}
@@ -36,13 +31,12 @@ class GamePhaseBuilder(val length: Long) {
afterTime: String? = null,
hours: String = "h",
minutes: String = "m",
- seconds: String = "s"
+ seconds: String = "s",
) {
counterMessage = buildCounterMessageCallback(beforeTime, afterTime, hours, minutes, seconds)
}
fun build() = GamePhase(length, start, end, counterMessage)
-
}
fun buildGame(builder: GamePhaseSystemBuilder.() -> Unit) = GamePhaseSystemBuilder().apply(builder).build()
\ No newline at end of file
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUI.kt b/src/main/kotlin/net/axay/kspigot/gui/GUI.kt
index f46bccd5..cb4c87e8 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUI.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUI.kt
@@ -9,8 +9,6 @@ import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
-import java.util.*
-import kotlin.collections.HashSet
class GUIData(
val guiType: GUIType,
@@ -19,13 +17,12 @@ class GUIData(
val defaultPage: Int,
val transitionTo: InventoryChangeEffect?,
val transitionFrom: InventoryChangeEffect?,
- internal val generalOnClick: ((GUIClickEvent) -> Unit)?
+ internal val generalOnClick: ((GUIClickEvent) -> Unit)?,
)
abstract class GUI(
- val data: GUIData
+ val data: GUIData,
) {
-
/**
* Returns the instance beloning to the given player.
* If not existing, a new instance will be created.
@@ -42,20 +39,17 @@ abstract class GUI(
* all instances.
*/
abstract fun closeGUI()
-
protected fun unregisterAndClose() {
getAllInstances().forEach {
it.bukkitInventory.closeForViewers()
it.unregister()
}
}
-
}
class GUIShared(
- guiData: GUIData
+ guiData: GUIData,
) : GUI(guiData) {
-
private var _singleInstance: GUIInstance? = null
val singleInstance
get() = _singleInstance ?: GUIInstance(this, null).apply {
@@ -64,29 +58,23 @@ class GUIShared(
}
override fun getInstance(player: Player) = singleInstance
-
override fun getAllInstances() = _singleInstance?.let { listOf(it) } ?: emptyList()
-
override fun closeGUI() {
unregisterAndClose()
_singleInstance = null
}
-
}
class GUIIndividual(
guiData: GUIData,
resetOnClose: Boolean,
- resetOnQuit: Boolean
+ 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
@@ -94,14 +82,12 @@ class GUIIndividual(
}
fun deleteInstance(player: Player) = playerInstances.remove(player)?.unregister()
-
override fun closeGUI() {
unregisterAndClose()
playerInstances.clear()
}
init {
-
if (resetOnClose) {
listen {
deleteInstance(it.player as? Player ?: return@listen)
@@ -113,22 +99,16 @@ class GUIIndividual(
deleteInstance(it.player)
}
}
-
}
-
}
class GUIInstance(
val gui: GUI,
- holder: Player?
+ 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)
@@ -143,15 +123,12 @@ class GUIInstance(
}
internal fun loadPageUnsafe(page: GUIPage<*>, offsetHorizontally: Int = 0, offsetVertically: Int = 0) {
-
val ifOffset = offsetHorizontally != 0 || offsetVertically != 0
if (!ifOffset) {
-
// unregister this inv from all elements on the previous page
currentElements.forEach { it.stopUsing(this) }
currentElements.clear()
-
// register this inv for all new elements
HashSet(page.slots.values).forEach {
if (it is GUIElement) {
@@ -161,23 +138,18 @@ class GUIInstance(
}
currentPageInt = page.number
-
}
loadContent(page.slots, offsetHorizontally, offsetVertically)
-
}
internal fun loadContent(
content: Map>,
offsetHorizontally: Int = 0,
- offsetVertically: Int = 0
+ offsetVertically: Int = 0,
) {
-
val ifOffset = offsetHorizontally != 0 || offsetVertically != 0
-
val dimensions = gui.data.guiType.dimensions
-
// clear the space which will be redefined
if (ifOffset) {
dimensions.invSlots.forEach {
@@ -185,13 +157,10 @@ class GUIInstance(
if (slotToClear != null) bukkitInventory.clear(slotToClear)
}
} else bukkitInventory.clear()
-
// render the given content
content.forEach {
-
val slot = it.value
if (slot is GUIElement) {
-
if (ifOffset) {
val invSlot = InventorySlot.fromRealSlot(it.key, dimensions)
if (invSlot != null) {
@@ -199,11 +168,8 @@ class GUIInstance(
if (offsetSlot != null) bukkitInventory.setItem(offsetSlot, slot.getItemStack(offsetSlot))
}
} else bukkitInventory.setItem(it.key, slot.getItemStack(it.key))
-
}
-
}
-
}
/**
@@ -218,14 +184,11 @@ class GUIInstance(
* GUI anymore.
*/
fun unregister() {
-
@Suppress("UNCHECKED_CAST")
GUIHolder.unregister(this as GUIInstance)
-
// unregister this inv from all elements
currentElements.forEach { it.stopUsing(this) }
currentElements.clear()
-
}
/**
@@ -259,5 +222,4 @@ class GUIInstance(
if (!isInMove)
loadPage(currentPage)
}
-
}
\ No newline at end of file
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt
index 44527871..e0a99586 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIBuilder.kt
@@ -14,9 +14,8 @@ fun kSpigotGUI(
class GUIBuilder(
val type: GUIType,
- private val guiCreator: GUICreator
+ private val guiCreator: GUICreator,
) {
-
/**
* The title of this GUI.
* This title will be visible for every page of
@@ -42,9 +41,7 @@ class GUIBuilder(
* GUI instance.
*/
var defaultPage = 1
-
private val guiSlots = HashMap>()
-
private var onClickElement: ((GUIClickEvent) -> Unit)? = null
/**
@@ -67,21 +64,16 @@ class GUIBuilder(
internal fun build() = guiCreator.createInstance(
GUIData(type, title, guiSlots, defaultPage, transitionTo, transitionFrom, onClickElement)
)
-
}
class GUIPageBuilder(
private val type: GUIType,
- val page: Int
+ val page: Int,
) {
-
private val guiSlots = HashMap>()
-
var transitionTo: PageChangeEffect? = null
var transitionFrom: PageChangeEffect? = null
-
internal fun build() = GUIPage(page, guiSlots, transitionTo, transitionFrom)
-
private fun defineSlots(slots: InventorySlotCompound, element: GUISlot) =
slots.withInvType(type).forEach { curSlot ->
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
@@ -118,7 +110,7 @@ class GUIPageBuilder(
slots: InventorySlotCompound,
icon: ItemStack,
toPage: Int,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = pageChanger(slots, icon, toPage, null, onChange)
/**
@@ -130,7 +122,7 @@ class GUIPageBuilder(
icon: ItemStack,
toPage: Int,
shouldChange: ((GUIClickEvent) -> Boolean)? = null,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = defineSlots(
slots, GUIButtonPageChange(
icon,
@@ -149,7 +141,7 @@ class GUIPageBuilder(
fun previousPage(
slots: InventorySlotCompound,
icon: ItemStack,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = previousPage(slots, icon, null, onChange)
/**
@@ -161,7 +153,7 @@ class GUIPageBuilder(
slots: InventorySlotCompound,
icon: ItemStack,
shouldChange: ((GUIClickEvent) -> Boolean)? = null,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = defineSlots(
slots, GUIButtonPageChange(
icon,
@@ -180,7 +172,7 @@ class GUIPageBuilder(
fun nextPage(
slots: InventorySlotCompound,
icon: ItemStack,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = nextPage(slots, icon, null, onChange)
/**
@@ -192,7 +184,7 @@ class GUIPageBuilder(
slots: InventorySlotCompound,
icon: ItemStack,
shouldChange: ((GUIClickEvent) -> Boolean)? = null,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = defineSlots(
slots, GUIButtonPageChange(
icon,
@@ -211,7 +203,7 @@ class GUIPageBuilder(
icon: ItemStack,
newGUI: () -> GUI<*>,
newPage: Int? = null,
- onChange: ((GUIClickEvent) -> Unit)? = null
+ onChange: ((GUIClickEvent) -> Unit)? = null,
) = defineSlots(
slots, GUIButtonInventoryChange(
icon,
@@ -235,7 +227,7 @@ class GUIPageBuilder(
*/
fun createCompound(
iconGenerator: (E) -> ItemStack,
- onClick: ((clickEvent: GUIClickEvent, element: E) -> Unit)? = null
+ onClick: ((clickEvent: GUIClickEvent, element: E) -> Unit)? = null,
) = GUISpaceCompound(type, iconGenerator, onClick)
/**
@@ -244,7 +236,7 @@ class GUIPageBuilder(
*/
fun compoundSpace(
slots: InventorySlotCompound,
- compound: GUISpaceCompound
+ compound: GUISpaceCompound,
) {
compound.addSlots(slots)
defineSlots(
@@ -264,14 +256,11 @@ class GUIPageBuilder(
*/
fun createSimpleRectCompound(
fromSlot: SingleInventorySlot,
- toSlot: SingleInventorySlot
+ toSlot: SingleInventorySlot,
) = createRectCompound>(
-
fromSlot, toSlot,
-
iconGenerator = { it.icon },
onClick = { clickEvent, element -> element.onClick?.invoke(clickEvent) }
-
)
/**
@@ -283,7 +272,7 @@ class GUIPageBuilder(
fromSlot: SingleInventorySlot,
toSlot: SingleInventorySlot,
iconGenerator: (E) -> ItemStack,
- onClick: ((clickEvent: GUIClickEvent, element: E) -> Unit)? = null
+ onClick: ((clickEvent: GUIClickEvent, element: E) -> Unit)? = null,
): GUIRectSpaceCompound {
val rectSlotCompound = fromSlot rectTo toSlot
return GUIRectSpaceCompound(
@@ -310,7 +299,7 @@ class GUIPageBuilder(
compound: GUISpaceCompound,
scrollDistance: Int = 1,
scrollTimes: Int = 1,
- reverse: Boolean = false
+ reverse: Boolean = false,
) = defineSlots(
slots,
GUISpaceCompoundScrollButton(icon, compound, scrollDistance.absoluteValue, scrollTimes, reverse)
@@ -325,10 +314,9 @@ class GUIPageBuilder(
icon: ItemStack,
compound: GUIRectSpaceCompound,
scrollTimes: Int = 1,
- reverse: Boolean = false
+ reverse: Boolean = false,
) = defineSlots(
slots,
GUISpaceCompoundScrollButton(icon, compound, scrollTimes, reverse)
)
-
}
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt
index 19d983f6..c045591e 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIClickEvent.kt
@@ -6,5 +6,5 @@ import org.bukkit.event.inventory.InventoryClickEvent
class GUIClickEvent(
val bukkitEvent: InventoryClickEvent,
val guiInstance: GUIInstance,
- val player: Player
+ 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 97b4cd72..b7c0d93e 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUICreator.kt
@@ -10,7 +10,7 @@ class SharedGUICreator : GUICreator() {
class IndividualGUICreator(
private val resetOnClose: Boolean = true,
- private val resetOnQuit: Boolean = true
+ 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 6ff303ea..8c1d85c8 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIElements.kt
@@ -5,21 +5,15 @@ 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 d53f2640..7670e43e 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIExtensions.kt
@@ -9,10 +9,8 @@ fun Player.openGUI(gui: GUI<*>, page: Int? = null): InventoryView? {
}
internal fun Player.openGUIInstance(guiInstance: GUIInstance<*>, page: Int? = null): InventoryView? {
-
if (page != null)
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 85ed4d80..ecf4c85a 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIHolder.kt
@@ -7,9 +7,7 @@ import org.bukkit.event.inventory.*
import org.bukkit.inventory.Inventory
object GUIHolder : AutoCloseable {
-
private val registered = HashMap>()
-
fun register(guiInstance: GUIInstance) {
registered[guiInstance.bukkitInventory] = guiInstance
}
@@ -19,13 +17,9 @@ object GUIHolder : AutoCloseable {
}
init {
-
listen {
-
val clickedInv = it.clickedInventory ?: return@listen
-
val gui = registered[clickedInv] ?: return@listen
-
val player = it.playerOrCancel ?: return@listen
if (gui.isInMove) {
@@ -39,26 +33,20 @@ object GUIHolder : AutoCloseable {
}
else
it.isCancelled = true
-
}
listen {
-
val inv = it.inventory
val gui = registered[inv] ?: return@listen
-
val player = it.playerOrCancel ?: return@listen
-
var ifCancel = false
for (slotIndex in it.inventorySlots) {
-
val slot = gui.currentPage.slots[slotIndex]
if (slot == null) {
ifCancel = true
break
}
-
val clickEvent = InventoryClickEvent(
it.view,
it.view.getSlotType(slotIndex),
@@ -73,26 +61,21 @@ object GUIHolder : AutoCloseable {
ifCancel = true
break
}
-
}
if (ifCancel)
it.isCancelled = true
-
}
-
}
override fun close() {
registered.keys.forEach { it.closeForViewers() }
registered.clear()
}
-
}
private val InventoryAction.isGUIClick
get() = this == InventoryAction.PICKUP_ALL || this == InventoryAction.PICKUP_HALF
-
private val InventoryInteractEvent.playerOrCancel: Player?
get() = (whoClicked as? Player) ?: kotlin.run {
isCancelled = true
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt
index 3a67449f..41310bb3 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIPage.kt
@@ -4,5 +4,5 @@ class GUIPage(
val number: Int,
internal val slots: Map>,
val transitionTo: PageChangeEffect?,
- val transitionFrom: PageChangeEffect?
+ val transitionFrom: PageChangeEffect?,
)
\ No newline at end of file
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUIPageChange.kt b/src/main/kotlin/net/axay/kspigot/gui/GUIPageChange.kt
index 8ce071fc..b34ab03e 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIPageChange.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIPageChange.kt
@@ -3,7 +3,6 @@ package net.axay.kspigot.gui
import net.axay.kspigot.runnables.task
abstract class GUIPageChangeCalculator {
-
abstract fun calculateNewPage(currentPage: Int, pages: Collection): Int?
object GUIPreviousPageCalculator : GUIPageChangeCalculator() {
@@ -19,7 +18,6 @@ abstract class GUIPageChangeCalculator {
class GUIConsistentPageCalculator(private val toPage: Int) : GUIPageChangeCalculator() {
override fun calculateNewPage(currentPage: Int, pages: Collection) = toPage
}
-
}
enum class PageChangeEffect {
@@ -31,7 +29,7 @@ enum class PageChangeEffect {
}
enum class InventoryChangeEffect(
- val effect: PageChangeEffect
+ val effect: PageChangeEffect,
) {
INSTANT(PageChangeEffect.INSTANT)
}
@@ -46,18 +44,14 @@ fun GUIInstance<*>.gotoPage(page: Int, overrideEffect: PageChangeEffect? = null)
internal fun GUIInstance<*>.changePage(
effect: PageChangeEffect,
fromPage: GUIPage<*>,
- toPage: GUIPage<*>
+ toPage: GUIPage<*>,
) {
-
val fromPageInt = fromPage.number
val toPageInt = toPage.number
when (effect) {
-
PageChangeEffect.INSTANT -> loadPageUnsafe(toPage)
-
PageChangeEffect.SLIDE_HORIZONTALLY -> {
-
val width = gui.data.guiType.dimensions.width
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
@@ -69,11 +63,8 @@ internal fun GUIInstance<*>.changePage(
loadPageUnsafe(toPage, offsetHorizontally = width - currentOffset)
}
}
-
}
-
PageChangeEffect.SLIDE_VERTICALLY -> {
-
val height = gui.data.guiType.dimensions.height
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
@@ -85,11 +76,8 @@ internal fun GUIInstance<*>.changePage(
loadPageUnsafe(toPage, offsetVertically = height - currentOffset)
}
}
-
}
-
PageChangeEffect.SWIPE_HORIZONTALLY -> {
-
val width = gui.data.guiType.dimensions.width
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
@@ -99,11 +87,8 @@ internal fun GUIInstance<*>.changePage(
loadPageUnsafe(toPage, offsetHorizontally = width - currentOffset)
}
}
-
}
-
PageChangeEffect.SWIPE_VERTICALLY -> {
-
val height = gui.data.guiType.dimensions.height
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
@@ -113,16 +98,14 @@ internal fun GUIInstance<*>.changePage(
loadPageUnsafe(toPage, offsetVertically = height - currentOffset)
}
}
-
}
-
}
}
internal fun GUIInstance<*>.changeGUI(
effect: InventoryChangeEffect,
fromPage: GUIPage<*>,
- toPage: GUIPage<*>
+ toPage: GUIPage<*>,
) = changePage(effect.effect, fromPage, toPage)
private inline fun changePageEffect(
@@ -131,20 +114,15 @@ private inline fun changePageEffect(
doFor: Int,
crossinline effect: (currentOffset: Int, ifInverted: Boolean) -> Unit,
) {
-
val ifInverted = fromPage >= toPage
-
var currentOffset = 1
task(
sync = true,
period = 1,
howOften = doFor.toLong()
) {
-
effect.invoke(currentOffset, ifInverted)
currentOffset++
-
}
-
}
diff --git a/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt b/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt
index 4b63afa3..5e1b5976 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUISlots.kt
@@ -5,11 +5,8 @@ package net.axay.kspigot.gui
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 ->
@@ -19,7 +16,6 @@ data class InventoryDimensions(val width: Int, val height: Int) {
}
}
}
-
val invSlotsWithRealSlots by lazy {
HashMap().apply {
invSlots.forEach { curSlot ->
@@ -27,15 +23,10 @@ data class InventoryDimensions(val width: Int, val height: Int) {
}
}
}
-
val realSlots by lazy { invSlotsWithRealSlots.values }
-
}
-
// SLOTS
-
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
@@ -65,28 +56,21 @@ 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) }
-
}
open class SingleInventorySlot internal constructor(
- val inventorySlot: InventorySlot
+ val inventorySlot: InventorySlot,
) : InventorySlotCompound {
-
constructor(row: Int, slotInRow: Int) : this(InventorySlot(row, slotInRow))
private val slotAsList = listOf(inventorySlot)
-
override fun withInvType(invType: GUIType) = slotAsList
-
}
internal enum class InventorySlotRangeType {
@@ -95,14 +79,10 @@ internal enum class InventorySlotRangeType {
}
class InventorySlotRange internal constructor(
-
startSlot: SingleInventorySlot,
endSlot: SingleInventorySlot,
-
- private val type: InventorySlotRangeType
-
+ private val type: InventorySlotRangeType,
) : InventorySlotCompound, ClosedRange {
-
override val start: InventorySlot
override val endInclusive: InventorySlot
@@ -114,7 +94,6 @@ class InventorySlotRange internal constructor(
override fun withInvType(invType: GUIType) = LinkedHashSet().apply {
when (type) {
-
InventorySlotRangeType.RECTANGLE -> {
// all possible combinations between the two slots
// -> form a rectangle
@@ -122,7 +101,6 @@ class InventorySlotRange internal constructor(
for (slotInRow in start.slotInRow..endInclusive.slotInRow)
this += InventorySlot(row, slotInRow)
}
-
InventorySlotRangeType.LINEAR -> {
if (endInclusive.row > start.row) {
// from start --->| to end of row
@@ -142,10 +120,8 @@ class InventorySlotRange internal constructor(
this += InventorySlot(start.row, slotInRow)
}
}
-
}
}
-
}
/**
@@ -163,33 +139,27 @@ infix fun SingleInventorySlot.rectTo(slot: SingleInven
InventorySlotRange(this, slot, InventorySlotRangeType.RECTANGLE)
class InventoryRowSlots internal constructor(
- val row: Int
+ val row: Int,
) : InventorySlotCompound {
-
override fun withInvType(invType: GUIType) = HashSet().apply {
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: GUIType) = HashSet().apply {
for (row in 1..invType.dimensions.height)
this += InventorySlot(row, column)
}
-
}
class InventoryBorderSlots internal constructor(
- val padding: Int
+ val padding: Int,
) : InventorySlotCompound {
-
override fun withInvType(invType: GUIType) = HashSet().apply {
-
val dimensions = invType.dimensions
for (currentPadding in 0 until padding) {
@@ -202,39 +172,30 @@ class InventoryBorderSlots internal constructor(
this += InventorySlot(row, dimensions.width)
}
}
-
}
-
}
class InventoryCornerSlots internal constructor(
val ifBottomLeft: Boolean = false,
val ifBottomRight: Boolean = false,
val ifTopLeft: Boolean = false,
- val ifTopRight: Boolean = false
+ val ifTopRight: Boolean = false,
) : InventorySlotCompound {
-
override fun withInvType(invType: GUIType) = HashSet().apply {
-
val dimensions = invType.dimensions
if (ifBottomLeft) this += InventorySlot(1, 1)
if (ifBottomRight) this += InventorySlot(1, dimensions.width)
if (ifTopLeft) this += InventorySlot(dimensions.height, 1)
if (ifTopRight) this += InventorySlot(dimensions.height, dimensions.width)
-
}
-
}
-class InventoryAllSlots : InventorySlotCompound {
+class InventoryAllSlots : InventorySlotCompound {
override fun withInvType(invType: GUIType) = invType.dimensions.invSlots
}
-
// SLOT TYPE SAFETY
-
// COLUMNS
-
interface ForColumnOne : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine
interface ForColumnTwo : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine
interface ForColumnThree : ForInventoryWidthThree, ForInventoryWidthFive, ForInventoryWidthNine
@@ -244,9 +205,7 @@ interface ForColumnSix : ForInventoryWidthNine
interface ForColumnSeven : ForInventoryWidthNine
interface ForColumnEight : ForInventoryWidthNine
interface ForColumnNine : ForInventoryWidthNine
-
// ROWS
-
interface ForRowOne : ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine, ForInventoryFourByNine,
ForInventoryFiveByNine, ForInventorySixByNine
@@ -273,9 +232,7 @@ interface ForRowThreeSlotOneToThree : ForRowThree, ForInventoryThreeByThree
interface ForCompleteRowOne : ForRowOne, ForRowOneSlotOneToThree, ForRowOneSlotFourToFive
interface ForCompleteRowTwo : ForRowTwo, ForRowTwoSlotOneToThree
interface ForCompleteRowThree : ForRowThree, ForRowThreeSlotOneToThree
-
object Slots {
-
// ROW ONE
val RowOneSlotOne = SingleInventorySlot(1, 1)
val RowOneSlotTwo = SingleInventorySlot(1, 2)
@@ -380,5 +337,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 9a28e561..eaf5f337 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/GUIType.kt
@@ -9,11 +9,9 @@ import org.bukkit.inventory.InventoryHolder
class GUIType(
val dimensions: InventoryDimensions,
- val bukkitType: InventoryType? = null
+ val bukkitType: InventoryType? = null,
) {
-
companion object {
-
val ONE_BY_NINE = GUIType(InventoryDimensions(9, 1))
val TWO_BY_NINE = GUIType(InventoryDimensions(9, 2))
val THREE_BY_NINE = GUIType(InventoryDimensions(9, 3))
@@ -24,7 +22,6 @@ class GUIType(
GUIType(InventoryDimensions(5, 1), bukkitType = InventoryType.HOPPER)
val THREE_BY_THREE =
GUIType(InventoryDimensions(3, 3), bukkitType = InventoryType.DROPPER)
-
}
fun createBukkitInv(holder: InventoryHolder? = null, title: String? = null): Inventory {
@@ -34,13 +31,9 @@ class GUIType(
else -> Bukkit.createInventory(holder, dimensions.slotAmount, realTitle)
}
}
-
}
-
// INVENTORY TYPE SAFETY
-
interface ForInventory
-
interface ForInventoryThreeByThree : ForInventoryThreeByNine
interface ForInventoryOneByFive : ForInventoryOneByNine
interface ForInventoryOneByNine : ForInventoryTwoByNine
@@ -49,7 +42,6 @@ interface ForInventoryThreeByNine : ForInventoryFourByNine
interface ForInventoryFourByNine : ForInventoryFiveByNine
interface ForInventoryFiveByNine : ForInventorySixByNine
interface ForInventorySixByNine : ForInventory
-
interface ForEveryInventory
: ForInventoryOneByNine, ForInventoryTwoByNine, ForInventoryThreeByNine,
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 076be5f0..f9e32e32 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButton.kt
@@ -9,12 +9,9 @@ open class GUIButton(
private val icon: ItemStack,
private val action: (GUIClickEvent) -> Unit,
) : GUIElement() {
-
final override fun getItemStack(slot: Int) = icon
-
override fun onClickElement(clickEvent: GUIClickEvent) {
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 26b7fa00..a764ac1f 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonInventoryChange.kt
@@ -7,14 +7,11 @@ class GUIButtonInventoryChange(
icon: ItemStack,
changeToGUICallback: () -> GUI<*>,
changeToPageInt: Int?,
- onChange: ((GUIClickEvent) -> Unit)?
+ onChange: ((GUIClickEvent) -> Unit)?,
) : GUIButton(icon, {
-
val changeToGUI = changeToGUICallback.invoke().getInstance(it.player)
-
val effect = (changeToGUI.gui.data.transitionTo ?: it.guiInstance.gui.data.transitionFrom)
?: InventoryChangeEffect.INSTANT
-
val changeToPage = changeToGUI.getPage(changeToPageInt) ?: changeToGUI.currentPage
changeToGUI.changeGUI(effect, it.guiInstance.currentPage, changeToPage)
@@ -22,5 +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/GUIButtonPageChange.kt b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt
index 2e7d57eb..b72b297e 100644
--- a/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt
+++ b/src/main/kotlin/net/axay/kspigot/gui/elements/GUIButtonPageChange.kt
@@ -7,9 +7,8 @@ class GUIButtonPageChange(
icon: ItemStack,
calculator: GUIPageChangeCalculator,
shouldChange: ((GUIClickEvent) -> Boolean)?,
- onChange: ((GUIClickEvent) -> Unit)?
+ onChange: ((GUIClickEvent) -> Unit)?,
) : GUIButton(icon, {
-
val currentPage = it.guiInstance.currentPage
val newPage = it.guiInstance.getPage(
calculator.calculateNewPage(
@@ -18,7 +17,6 @@ class GUIButtonPageChange