From 251814d0d8d576ef31f0f41e12206d15c7a5ec58 Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Tue, 11 Jun 2024 10:00:01 +0200 Subject: [PATCH] chore: Fixed several code deps --- .../energeticstorage/DependencyLoader.java | 1 - .../customblockdata/BlockDataListener.kt | 4 +- .../customblockdata/CustomBlockData.kt | 20 ++++----- .../extensions/ItemExtensions.kt | 3 +- .../energeticstorage/items/ItemBuilder.kt | 45 ++++--------------- .../listeners/BlockPlaceListener.kt | 1 - .../listeners/PlayerInteractListener.kt | 3 +- .../managers/RegisterManager.kt | 3 +- .../serialization/ItemStackConverter.kt | 15 ++++--- 9 files changed, 31 insertions(+), 64 deletions(-) diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/DependencyLoader.java b/src/main/kotlin/com/liamxsage/energeticstorage/DependencyLoader.java index d762f12..85d5687 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/DependencyLoader.java +++ b/src/main/kotlin/com/liamxsage/energeticstorage/DependencyLoader.java @@ -25,7 +25,6 @@ public void classloader(PluginClasspathBuilder classpathBuilder) { try (BufferedReader reader = new BufferedReader(new InputStreamReader( Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(".dependencies"))))) { reader.lines().forEach(dependency -> { - LOGGER.log(Level.INFO, "Adding dependency: " + dependency); maven.addDependency(new Dependency(new DefaultArtifact(dependency), null)); }); diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt index b18a19f..ccffaa2 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt @@ -154,8 +154,8 @@ internal class BlockDataListener : Listener { fun reverse(map: Map): Map { val reversed = LinkedHashMap() val keys = ArrayList(map.keys) - Collections.reverse(keys) - keys.forEach { key -> reversed[key] = map[key]!! } + keys.reverse() + keys.forEach { key -> reversed[key] = map[key] ?: error("Key not found") } return reversed } } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/CustomBlockData.kt b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/CustomBlockData.kt index d0905d2..13d1e27 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/CustomBlockData.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/CustomBlockData.kt @@ -88,7 +88,7 @@ class CustomBlockData( return block.chunk.persistentDataContainer.has(getKey(plugin, block), PersistentDataType.TAG_CONTAINER) } - fun getBlocksWithCustomData(plugin: Plugin, chunk: Chunk): Collection? { + fun getBlocksWithCustomData(plugin: Plugin, chunk: Chunk): Collection { val dummy = NamespacedKey(plugin, "dummy") return getBlocksWithCustomData(chunk, dummy) } @@ -102,7 +102,7 @@ class CustomBlockData( }.filterNotNull().toList() } - fun getBlocksWithCustomData(namespace: String, chunk: Chunk): List? { + fun getBlocksWithCustomData(namespace: String, chunk: Chunk): List { val dummy = NamespacedKey(namespace, "dummy") return getBlocksWithCustomData(chunk, dummy) } @@ -133,7 +133,7 @@ class CustomBlockData( private fun getPersistentDataContainer(): PersistentDataContainer { val chunkPDC: PersistentDataContainer = chunk.persistentDataContainer - val blockPDC: PersistentDataContainer? = chunkPDC.get(getKey(plugin, block), PersistentDataType.TAG_CONTAINER) + val blockPDC: PersistentDataContainer? = chunkPDC[getKey(plugin, block), PersistentDataType.TAG_CONTAINER] return blockPDC ?: chunkPDC.adapterContext.newPersistentDataContainer() } @@ -144,10 +144,10 @@ class CustomBlockData( private fun save() { setDirty(plugin, blockEntry) - if (pdc.isEmpty()) { + if (pdc.isEmpty) { chunk.persistentDataContainer.remove(getKey(plugin, block)) } else { - chunk.persistentDataContainer.set(getKey(plugin, block), PersistentDataType.TAG_CONTAINER, pdc) + chunk.persistentDataContainer[getKey(plugin, block), PersistentDataType.TAG_CONTAINER] = pdc } } @@ -158,17 +158,13 @@ class CustomBlockData( if (key == null) return@forEach val dataType: PersistentDataType<*, *>? = getDataType(this, key) if (dataType != null) { - newCbd.set( - key, - dataType as PersistentDataType, - this[key, dataType] as Any - ) + newCbd[key, dataType as PersistentDataType] = this[key, dataType] as Any } } } override fun set(key: NamespacedKey, type: PersistentDataType, value: Z & Any) { - pdc.set(key, type, value) + pdc[key, type] = value save() } @@ -205,7 +201,7 @@ class CustomBlockData( } override fun isEmpty(): Boolean { - return pdc.isEmpty() + return pdc.isEmpty } override fun copyTo(pdc: PersistentDataContainer, replace: Boolean) { diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/ItemExtensions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/ItemExtensions.kt index 2a3d0e8..c3a1173 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/ItemExtensions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/ItemExtensions.kt @@ -3,9 +3,8 @@ package com.liamxsage.energeticstorage.extensions import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.items.ItemBuilder import org.bukkit.Material -import org.bukkit.inventory.ItemStack import org.bukkit.NamespacedKey -import org.bukkit.block.Block +import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType /** diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt index 3dbc5c6..1351e1a 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt @@ -7,6 +7,7 @@ import com.liamxsage.energeticstorage.items.mineskin.SkinTexture import com.liamxsage.energeticstorage.listeners.ItemClickListener import com.mojang.authlib.GameProfile import com.mojang.authlib.properties.Property +import dev.fruxz.ascend.extension.forceCastOrNull import dev.fruxz.ascend.extension.logging.getItsLogger import dev.fruxz.stacked.text import net.kyori.adventure.text.Component @@ -68,7 +69,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni * @return The updated ItemBuilder instance. */ fun meta(dsl: T.() -> Unit): ItemBuilder { - val meta = itemStack.itemMeta as T // TODO <- forceCastOrNull() please! :) + val meta = itemStack.itemMeta.forceCastOrNull() ?: return this dsl.invoke(meta) itemStack.itemMeta = meta return this @@ -83,7 +84,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni */ fun addPersistentData(key: NamespacedKey, value: String): ItemBuilder { val meta = itemStack.itemMeta - meta.persistentDataContainer.set(key, PersistentDataType.STRING, value) + meta.persistentDataContainer[key, PersistentDataType.STRING] = value itemStack.itemMeta = meta return this } @@ -118,7 +119,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni fun addPersistentDataIf(key: NamespacedKey, value: String, condition: Boolean = false): ItemBuilder { if (condition) { val meta = itemStack.itemMeta - meta.persistentDataContainer.set(key, PersistentDataType.STRING, value) + meta.persistentDataContainer[key, PersistentDataType.STRING] = value itemStack.itemMeta = meta return this } @@ -160,7 +161,6 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni * * @param T the type of the item to perform the operation on */ - @FunctionalInterface fun interface Performer { fun perform(itemBuilder: T): T } @@ -201,11 +201,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni * @return The updated ItemBuilder object. */ fun setOwner(uuid: UUID): ItemBuilder { - if (itemStack.type != Material.PLAYER_HEAD) return this - val skullMeta = itemStack.itemMeta as SkullMeta - skullMeta.owningPlayer = Bukkit.getOfflinePlayer(uuid) - itemStack.itemMeta = skullMeta - return this + return owner(uuid) } /** @@ -271,7 +267,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni val skullMeta = itemStack.itemMeta as SkullMeta val profileField: Field = skullMeta.javaClass.getDeclaredField("profile") profileField.isAccessible = true - profileField.set(skullMeta, profile) + profileField[skullMeta] = profile itemStack.setItemMeta(skullMeta) } catch (e1: NoSuchFieldException) { e1.printStackTrace() @@ -291,7 +287,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni */ fun textureFromMineSkin(mineSkinUUID: String): ItemBuilder { if (textureCache.containsKey(mineSkinUUID)) { - return textureFromSkinTexture(textureCache[mineSkinUUID]!!) + return textureFromSkinTexture(textureCache[mineSkinUUID] ?: return this) } val target = URL("https://api.mineskin.org/get/uuid/$mineSkinUUID") @@ -541,32 +537,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni } return this } - - - /** - * Sets the material of the item stack. - * - * @param material the material to set - * @return the ItemBuilder instance - */ - fun type(material: Material): ItemBuilder { - itemStack.type = material - return this - } - - /** - * Sets the type of the material in the ItemBuilder, if the given condition is true. - * - * @param material The material to set. - * @param condition The condition that determines whether the type should be set. - * @return The ItemBuilder instance. - */ - fun typeIf(material: Material, condition: Boolean = false): ItemBuilder { - if (condition) { - itemStack.type = material - } - return this - } + /** * Creates a deep copy of the current ItemBuilder object. diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt index ddaa5db..f116125 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt @@ -10,7 +10,6 @@ import com.liamxsage.energeticstorage.network.getNetworkInterfaceType import org.bukkit.Material import org.bukkit.block.Block import org.bukkit.block.data.type.ChiseledBookshelf -import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.BlockPlaceEvent diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt index e14ab37..c513acf 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt @@ -1,7 +1,7 @@ package com.liamxsage.energeticstorage.listeners -import com.liamxsage.energeticstorage.DISK_ID_NAMESPACE import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE +import com.liamxsage.energeticstorage.DISK_ID_NAMESPACE import com.liamxsage.energeticstorage.cache.DiskCache import com.liamxsage.energeticstorage.cache.DiskDriveCache import com.liamxsage.energeticstorage.database.saveToDB @@ -13,7 +13,6 @@ import com.liamxsage.energeticstorage.network.getConnectedNetworkInterfaces import com.liamxsage.energeticstorage.network.getNetworkInterfaceType import org.bukkit.Material import org.bukkit.block.Block -import org.bukkit.block.data.type.ChiseledBookshelf import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt b/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt index f669e62..ad4623b 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt @@ -12,6 +12,7 @@ import com.liamxsage.energeticstorage.listeners.BlockBreakListener import com.liamxsage.energeticstorage.listeners.BlockPlaceListener import com.liamxsage.energeticstorage.listeners.ItemClickListener import com.liamxsage.energeticstorage.listeners.PlayerInteractListener +import dev.fruxz.ascend.extension.forceCastOrNull import org.bukkit.Bukkit import org.bukkit.command.CommandExecutor import org.bukkit.command.PluginCommand @@ -35,7 +36,7 @@ object RegisterManager { try { val loadedClass = classInfo.load().kotlin if (clazzType.isInstance(loadedClass.javaObjectType.getDeclaredConstructor().newInstance())) { - classes.add(loadedClass as KClass) + classes.add(loadedClass.forceCastOrNull>() ?: continue) } } catch (_: Exception) { // Ignore diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt index 1eb20ec..99909c3 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt @@ -12,6 +12,9 @@ import java.io.ByteArrayOutputStream import java.io.IOException +private const val UNABLE_TO_SAVE_ITEM_STACKS = "Unable to save item stacks." +private const val UNABLE_TO_DECODE_CLASS_TYPE = "Unable to decode class type." + object ItemStackConverter { @@ -61,7 +64,7 @@ object ItemStackConverter { dataOutput.close() Base64Coder.encodeLines(outputStream.toByteArray()) } catch (e: Exception) { - throw IllegalStateException("Unable to save item stacks.", e) + throw IllegalStateException(UNABLE_TO_SAVE_ITEM_STACKS, e) } } @@ -92,7 +95,7 @@ object ItemStackConverter { dataOutput.close() Base64Coder.encodeLines(outputStream.toByteArray()) } catch (e: Exception) { - throw IllegalStateException("Unable to save item stacks.", e) + throw IllegalStateException(UNABLE_TO_SAVE_ITEM_STACKS, e) } } @@ -129,7 +132,7 @@ object ItemStackConverter { dataOutput.close() Base64Coder.encodeLines(outputStream.toByteArray()) } catch (e: Exception) { - throw IllegalStateException("Unable to save item stacks.", e) + throw IllegalStateException(UNABLE_TO_SAVE_ITEM_STACKS, e) } } @@ -168,7 +171,7 @@ object ItemStackConverter { dataInput.close() inventory } catch (e: ClassNotFoundException) { - throw IOException("Unable to decode class type.", e) + throw IOException(UNABLE_TO_DECODE_CLASS_TYPE, e) } } @@ -198,7 +201,7 @@ object ItemStackConverter { dataInput.close() items } catch (e: ClassNotFoundException) { - throw IOException("Unable to decode class type.", e) + throw IOException(UNABLE_TO_DECODE_CLASS_TYPE, e) } } @@ -227,7 +230,7 @@ object ItemStackConverter { dataInput.close() item } catch (e: ClassNotFoundException) { - throw IOException("Unable to decode class type.", e) + throw IOException(UNABLE_TO_DECODE_CLASS_TYPE, e) } }