From 2ef1cc94c4f27d472f018b7f052098e6094e3eee Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Tue, 11 Jun 2024 14:28:58 +0200 Subject: [PATCH] feat: Network Restructure --- .github/FUNDING.yml | 2 +- .github/ISSUE_TEMPLATE/bug_report.md | 8 +- CONTRIBUTING.md | 27 ++++-- README.md | 50 ++++++----- gradle.properties | 8 -- settings.gradle.kts | 1 - .../liamxsage/energeticstorage/Variables.kt | 2 +- .../energeticstorage/cache/DiskCache.kt | 2 +- .../energeticstorage/cache/DiskDriveCache.kt | 38 --------- .../cache/NetworkInterfaceCache.kt | 39 +++++++++ .../commands/ESDebugCommand.kt | 7 +- .../commands/ESGiveCommand.kt | 28 +++++-- .../customblockdata/BlockDataListener.kt | 1 - .../energeticstorage/database/DiskTable.kt | 17 ++-- .../extensions/JDKExtensions.kt | 2 +- .../extensions/PlayerExtensions.kt | 3 +- .../energeticstorage/gui/DiskDriveGui.kt | 6 +- .../energeticstorage/gui/GuiFunctions.kt | 6 +- .../energeticstorage/items/ItemBuilder.kt | 2 +- .../listeners/BlockBreakListener.kt | 23 ++--- .../listeners/BlockPlaceListener.kt | 23 +++-- .../listeners/ItemClickListener.kt | 2 +- .../listeners/PlayerInteractListener.kt | 33 ++++---- .../managers/RegisterManager.kt | 4 +- .../liamxsage/energeticstorage/model/Cable.kt | 16 +++- .../liamxsage/energeticstorage/model/Core.kt | 19 ++++- .../liamxsage/energeticstorage/model/Disk.kt | 2 +- .../energeticstorage/model/DiskDrive.kt | 20 +++-- .../energeticstorage/model/Terminal.kt | 18 +++- .../network/NetworkFunctions.kt | 83 ++++++++++++------- .../network/NetworkInterface.kt | 10 +++ .../serialization/ItemStackConverter.kt | 2 +- 32 files changed, 325 insertions(+), 179 deletions(-) delete mode 100644 src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskDriveCache.kt create mode 100644 src/main/kotlin/com/liamxsage/energeticstorage/cache/NetworkInterfaceCache.kt create mode 100644 src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkInterface.kt diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 099f4e5..755276b 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,3 +1,3 @@ # These are supported funding model platforms -github: [coasterfreakde] \ No newline at end of file +github: [ coasterfreakde ] \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 241fa9d..3582c2b 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -12,6 +12,7 @@ A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behavior: + 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' @@ -24,9 +25,10 @@ A clear and concise description of what you expected to happen. If applicable, add screenshots to help explain your problem. **Desktop (please complete the following information):** - - OS: [e.g. Win11] - - IDE [e.g. intellij, vsc] - - Gradle Verson [e.g. 1.3.3] + +- OS: [e.g. Win11] +- IDE [e.g. intellij, vsc] +- Gradle Verson [e.g. 1.3.3] **Additional context** Add any other context about the problem here. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1522230..e1da6d4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,5 @@ # Contributing to DevSky Boilerplates + We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's: - Reporting a bug @@ -8,10 +9,13 @@ We love your input! We want to make contributing to this project as easy and tra - Becoming a maintainer ## We Develop with Github + We use github to host code, to track issues and feature requests, as well as accept pull requests. ## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests -Pull requests are the best way to propose changes to the codebase (we use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests: + +Pull requests are the best way to propose changes to the codebase (we +use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests: 1. Fork the repo and create your branch from `master`. 2. If you've added code that should be tested, add tests. @@ -21,20 +25,28 @@ Pull requests are the best way to propose changes to the codebase (we use [Githu 6. Issue that pull request! ## Any contributions you make will be under the MIT Software License -In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern. + +In short, when you submit code changes, your submissions are understood to be under the +same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the +maintainers if that's a concern. ## Report bugs using Github's [issues](https://github.com/briandk/transcriptase-atom/issues) + We use GitHub issues to track public bugs. Report a bug by [opening a new issue](); it's that easy! ## Write bug reports with detail, background, and sample code -[This is an example](http://stackoverflow.com/q/12488905/180626) of a bug report I wrote, and I think it's not a bad model. Here's [another example from Craig Hockenberry](http://www.openradar.me/11905408), an app developer whom I greatly respect. + +[This is an example](http://stackoverflow.com/q/12488905/180626) of a bug report I wrote, and I think it's not a bad +model. Here's [another example from Craig Hockenberry](http://www.openradar.me/11905408), an app developer whom I +greatly respect. **Great Bug Reports** tend to have: - A quick summary and/or background - Steps to reproduce - - Be specific! - - Give sample code if you can. [My stackoverflow question](http://stackoverflow.com/q/12488905/180626) includes sample code that *anyone* with a base R setup can run to reproduce what I was seeing + - Be specific! + - Give sample code if you can. [My stackoverflow question](http://stackoverflow.com/q/12488905/180626) includes + sample code that *anyone* with a base R setup can run to reproduce what I was seeing - What you expected would happen - What actually happens - Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) @@ -46,7 +58,10 @@ People *love* thorough bug reports. I'm not even kidding. * Stay close to the languages code style ## License + By contributing, you agree that your contributions will be licensed under its MIT License. ## References -This document was adapted from the open-source contribution guidelines for [Facebook's Draft](https://github.com/facebook/draft-js/blob/main/CONTRIBUTING.md) + +This document was adapted from the open-source contribution guidelines +for [Facebook's Draft](https://github.com/facebook/draft-js/blob/main/CONTRIBUTING.md) diff --git a/README.md b/README.md index 51d8d1e..6e992e9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ -

Energetic Storage Reborn

+

Energetic Storage Reborn

Energetic Storage Reborn is a plugin that is heavily inspired by the forge mods named [Applied Energistics 2](https://www.curseforge.com/minecraft/mc-mods/applied-energistics-2) @@ -13,7 +13,8 @@ and [Refined Storage](https://www.curseforge.com/minecraft/mc-mods/refined-stora If you don't know what Applied Energistics 2 is, it's a mod that allows the player to store large numbers of items, sometimes hundreds of thousands, in just a few blocks. -Energetic Storage Reborn is a complete recode for Minecraft 1.20.6 of the original [Energetic Storage](https://github.com/SeanOMik/EnergeticStorage). +Energetic Storage Reborn is a complete recode for Minecraft 1.20.6 of the +original [Energetic Storage](https://github.com/SeanOMik/EnergeticStorage). It is significantly more modern, designed exclusively for PaperMC, Java 21, Kotlin, and Minecraft 1.20.6+, utilizing the latest features the game has to offer. @@ -26,29 +27,31 @@ If you run into any issues, --- ## Features: + * [ ] Networking - * [x] Cables (Data Transmissions) - * [ ] Network Cores - * [x] DiskDrives - * [x] Disks - * [ ] Terminals - * [ ] Terminal Sorting - * [ ] Importer - * [ ] Hopper Input - * [ ] Exporter - * [x] Configurable Network Length + * [x] Cables (Data Transmissions) + * [ ] Network Cores + * [x] DiskDrives + * [x] Disks + * [ ] Terminals + * [ ] Terminal Sorting + * [ ] Importer + * [ ] Hopper Input + * [ ] Exporter + * [x] Configurable Network Length * [ ] Player/Region Whitelist - * [ ] Lands Integration + * [ ] Lands Integration * [x] Support for 1.20.6 * [x] MySQL, MariaDB, SQLite, PostgreSQL, and H2 support - - ## Items And Blocks: + * 1k, 4k, 16k, and 64k, drives - * The drives are needed to store items. 1k can store 1024 items, 4k can store 4096 and so on (1024 * how many thousand). - * All drives have a type limit set at 64, 128, 256 or 512. This means that only this amount of different item types can be added to a single drive. - * Can be changed in config. + * The drives are needed to store items. 1k can store 1024 items, 4k can store 4096 and so on (1024 * how many + thousand). + * All drives have a type limit set at 64, 128, 256 or 512. This means that only this amount of different item types + can be added to a single drive. + * Can be changed in config. * ES System * The ES System is what keeps the drives inside it and is used to access the items stored on the drives. * You can trust players into the system; they will have just as much access as you do. @@ -59,20 +62,26 @@ If you run into any issues, * Only trusted players and the owner of the system can destroy it. ## Permissions + * `energeticstorage.*`: Gives all Energetic Storage permissions. Default: `op` * `energeticstorage.reload`: Gives permission to reload the Energetic Storage configuration files. Default: `op` * `energeticstorage.esgive`: Gives permission to give an Energetic Storage item to themselves. Default: `op` * `energeticstorage.esgive.others`: Gives permission to give an Energetic Storage item to others. Default: `op` -* `energeticstorage.system.open.untrusted`: Gives permission to open an ES System that the player is not trusted on. Default: `op` +* `energeticstorage.system.open.untrusted`: Gives permission to open an ES System that the player is not trusted on. + Default: `op` * `energeticstorage.system.create`: Gives permission to create an ES System. Default: `Everyone` -* `energeticstorage.system.destroy.untrusted`: Gives permission to destroy a system the player isn't trusted on. Default: `op` +* `energeticstorage.system.destroy.untrusted`: Gives permission to destroy a system the player isn't trusted on. + Default: `op` ## Commands + * /esreload * /esgive [1k, 4k, 16k, 64k, system] (player) ## Config: + Config.yml: + ```yaml drives: # Sets the drives max type limit. @@ -108,6 +117,7 @@ storage: username: "root" password: "password" ``` + ---

diff --git a/gradle.properties b/gradle.properties index 59df127..54b7f14 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,24 +1,16 @@ kotlin.code.style=official - minecraftVersion=1.20.6 - kotlinxSerializationJsonVersion=1.7.0 - exposedVersion=0.51.1 hikariCPVersion=5.1.0 - mysqlVersion=8.4.0 postgresqlVersion=42.7.3 mariaDBVersion=3.4.0 sqliteVersion=3.46.0.0 h2Version=2.2.224 - fruxzAscendVersion=2024.2.1 fruxzStackedVersion=2024.1.1 - kotlinxCoroutinesCoreVersion=1.9.0-RC kotlinxCollectionsImmutableVersion=0.3.7 - gsonVersion=2.11.0 - slf4jVersion=2.0.13 \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index f3ce854..da6d890 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,2 @@ - rootProject.name = "EnergeticStorage-Reborn" diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/Variables.kt b/src/main/kotlin/com/liamxsage/energeticstorage/Variables.kt index cac5135..2c2284b 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/Variables.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/Variables.kt @@ -12,8 +12,8 @@ const val TEXT_GRADIENT_DEFAULT = "" const val PACKAGE_NAME = "com.liamxsage.energeticstorage" val DISK_ID_NAMESPACE = NamespacedKey(EnergeticStorage.instance, "disk_id") -val DISK_DRIVE_ID_NAMESPACE = NamespacedKey(EnergeticStorage.instance, "disk_drive_id") val NETWORK_INTERFACE_NAMESPACE = NamespacedKey(EnergeticStorage.instance, "network_interface") +val NETWORK_INTERFACE_ID_NAMESPACE = NamespacedKey(EnergeticStorage.instance, "network_interface_id") val PLAYER_DEBUG_MODE_NAMESPACE = NamespacedKey(EnergeticStorage.instance, "debug_mode") diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskCache.kt b/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskCache.kt index edfd32a..2df8670 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskCache.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskCache.kt @@ -1,7 +1,7 @@ package com.liamxsage.energeticstorage.cache import com.liamxsage.energeticstorage.model.Disk -import java.util.UUID +import java.util.* import java.util.concurrent.locks.ReadWriteLock import java.util.concurrent.locks.ReentrantReadWriteLock diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskDriveCache.kt b/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskDriveCache.kt deleted file mode 100644 index 443b455..0000000 --- a/src/main/kotlin/com/liamxsage/energeticstorage/cache/DiskDriveCache.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.liamxsage.energeticstorage.cache - -import com.liamxsage.energeticstorage.model.DiskDrive -import java.util.UUID -import java.util.concurrent.locks.ReadWriteLock -import java.util.concurrent.locks.ReentrantReadWriteLock - -object DiskDriveCache { - - private val cacheLock: ReadWriteLock = ReentrantReadWriteLock() - private var cache = mapOf() - - fun getDiskDriveByUUID(diskDriveUUID: UUID): DiskDrive? { - cacheLock.readLock().lock() - val system = cache[diskDriveUUID] - cacheLock.readLock().unlock() - return system - } - - fun addDiskDrive(diskDrive: DiskDrive) { - cacheLock.writeLock().lock() - cache = cache + (diskDrive.uuid to diskDrive) - cacheLock.writeLock().unlock() - } - - fun removeDiskDrive(diskDriveUUID: UUID) { - cacheLock.writeLock().lock() - cache = cache - diskDriveUUID - cacheLock.writeLock().unlock() - } - - fun getDiskDrives(): List { - cacheLock.readLock().lock() - val diskDrives = cache.values.toList() - cacheLock.readLock().unlock() - return diskDrives - } -} \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/cache/NetworkInterfaceCache.kt b/src/main/kotlin/com/liamxsage/energeticstorage/cache/NetworkInterfaceCache.kt new file mode 100644 index 0000000..0e20723 --- /dev/null +++ b/src/main/kotlin/com/liamxsage/energeticstorage/cache/NetworkInterfaceCache.kt @@ -0,0 +1,39 @@ +package com.liamxsage.energeticstorage.cache + +import com.liamxsage.energeticstorage.network.NetworkInterface +import java.util.* +import java.util.concurrent.locks.ReadWriteLock +import java.util.concurrent.locks.ReentrantReadWriteLock + +object NetworkInterfaceCache { + + private val cacheLock: ReadWriteLock = ReentrantReadWriteLock() + private var cache = mapOf() + + fun getNetworkInterfaceByUUID(interfaceUUID: UUID): NetworkInterface? { + cacheLock.readLock().lock() + val networkInterface = cache[interfaceUUID] + cacheLock.readLock().unlock() + return networkInterface + } + + fun addNetworkInterface(networkInterface: NetworkInterface) { + cacheLock.writeLock().lock() + cache = cache + (networkInterface.uuid to networkInterface) + cacheLock.writeLock().unlock() + } + + fun removeNetworkInterfaceByUUID(interfaceUUID: UUID) { + cacheLock.writeLock().lock() + cache = cache - interfaceUUID + cacheLock.writeLock().unlock() + } + + fun getCachedNetworkInterfaces(): List { + cacheLock.readLock().lock() + val networkInterfaces = cache.values.toList() + cacheLock.readLock().unlock() + return networkInterfaces + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESDebugCommand.kt b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESDebugCommand.kt index 495ebe5..22ab777 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESDebugCommand.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESDebugCommand.kt @@ -32,7 +32,12 @@ class ESDebugCommand : CommandExecutor, TabExecutor { return true } - override fun onTabComplete(sender: CommandSender, command: Command, label: String, args: Array): List { + override fun onTabComplete( + sender: CommandSender, + command: Command, + label: String, + args: Array + ): List { return when (args.size) { 1 -> listOf("on", "off") else -> emptyList() diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt index 0eff484..c327322 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt @@ -40,17 +40,22 @@ class ESGiveCommand : CommandExecutor, TabExecutor { "system" -> { DiskDrive().createDiskDriveItem() } + "core" -> { Core().createCoreItem() } + "cable" -> { Cable().createCableItem() } + "terminal" -> { Terminal().createTerminalItem() } + else -> { - val diskSize = DiskSize.entries.find { it.diskName.lowercase(Locale.getDefault()) == item.replace("_", " ") } + val diskSize = + DiskSize.entries.find { it.diskName.lowercase(Locale.getDefault()) == item.replace("_", " ") } if (diskSize == null) { sender.sendMessagePrefixed("Invalid drive size") return true @@ -67,15 +72,28 @@ class ESGiveCommand : CommandExecutor, TabExecutor { return true } - override fun onTabComplete(sender: CommandSender, command: Command, label: String, args: Array): List { + override fun onTabComplete( + sender: CommandSender, + command: Command, + label: String, + args: Array + ): List { return when (args.size) { 1 -> { - listOf("system", "core", "cable", "terminal", *DiskSize.entries.map { it.diskName.replace(" ", "_") }.toTypedArray()) - .filter { it.startsWith(args[0], ignoreCase = true)} + listOf( + "system", + "core", + "cable", + "terminal", + *DiskSize.entries.map { it.diskName.replace(" ", "_") }.toTypedArray() + ) + .filter { it.startsWith(args[0], ignoreCase = true) } } + 2 -> { - Bukkit.getOnlinePlayers().map { it.name }.filter { it.startsWith(args[1], ignoreCase = true)} + Bukkit.getOnlinePlayers().map { it.name }.filter { it.startsWith(args[1], ignoreCase = true) } } + else -> { emptyList() } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt index ccffaa2..017e8cf 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/customblockdata/BlockDataListener.kt @@ -15,7 +15,6 @@ import org.bukkit.event.block.* import org.bukkit.event.entity.EntityChangeBlockEvent import org.bukkit.event.entity.EntityExplodeEvent import org.bukkit.event.world.StructureGrowEvent -import java.util.* import java.util.function.Predicate internal class BlockDataListener : Listener { diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt b/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt index 73cbb93..c75f493 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/database/DiskTable.kt @@ -1,10 +1,10 @@ package com.liamxsage.energeticstorage.database import com.liamxsage.energeticstorage.EnergeticStorage -import com.liamxsage.energeticstorage.model.DiskSize import com.liamxsage.energeticstorage.model.Disk -import com.liamxsage.energeticstorage.model.ESItem import com.liamxsage.energeticstorage.model.DiskDrive +import com.liamxsage.energeticstorage.model.DiskSize +import com.liamxsage.energeticstorage.model.ESItem import dev.fruxz.ascend.tool.time.calendar.Calendar import org.jetbrains.exposed.dao.id.UUIDTable import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq @@ -33,7 +33,8 @@ fun Disk.saveToDB() = transaction { it[DiskTable.id] = this@saveToDB.uuid it[DiskTable.diskDrive] = this@saveToDB.diskDriveUUID it[DiskTable.size] = this@saveToDB.size - it[DiskTable.items] = ExposedBlob(EnergeticStorage.instance.gson.toJson(this@saveToDB.items.toTypedArray()).toByteArray()) + it[DiskTable.items] = + ExposedBlob(EnergeticStorage.instance.gson.toJson(this@saveToDB.items.toTypedArray()).toByteArray()) it[DiskTable.updatedAt] = Calendar.now().javaInstant } } @@ -49,7 +50,10 @@ fun loadFromDB(driveUUID: UUID): Disk = transaction { val systemUUID = result[DiskTable.diskDrive] val size = result[DiskTable.size] - val items = EnergeticStorage.instance.gson.fromJson(result[DiskTable.items].bytes.decodeToString(), Array::class.java).toMutableList() + val items = EnergeticStorage.instance.gson.fromJson( + result[DiskTable.items].bytes.decodeToString(), + Array::class.java + ).toMutableList() return@transaction Disk(driveUUID, size, items, systemUUID) } @@ -63,7 +67,10 @@ fun DiskDrive.loadDisks() = transaction { val drives = DiskTable.selectAll().where(DiskTable.diskDrive eq this@loadDisks.uuid).map { row -> val driveUUID = row[DiskTable.id].value val size = row[DiskTable.size] - val items = EnergeticStorage.instance.gson.fromJson(row[DiskTable.items].bytes.decodeToString(), Array::class.java).toMutableList() + val items = EnergeticStorage.instance.gson.fromJson( + row[DiskTable.items].bytes.decodeToString(), + Array::class.java + ).toMutableList() Disk(driveUUID, size, items, this@loadDisks.uuid) } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/JDKExtensions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/JDKExtensions.kt index 3d82419..70bd63c 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/JDKExtensions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/JDKExtensions.kt @@ -1,7 +1,7 @@ package com.liamxsage.energeticstorage.extensions -import org.slf4j.LoggerFactory import dev.fruxz.ascend.tool.time.calendar.Calendar +import org.slf4j.LoggerFactory import java.time.Instant import java.time.ZoneId import java.time.ZonedDateTime diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/PlayerExtensions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/PlayerExtensions.kt index 8515565..a7e36f9 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/extensions/PlayerExtensions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/extensions/PlayerExtensions.kt @@ -4,7 +4,8 @@ import com.liamxsage.energeticstorage.BLOCK_PREFIX import com.liamxsage.energeticstorage.PLAYER_DEBUG_MODE_NAMESPACE import com.liamxsage.energeticstorage.PREFIX import dev.fruxz.stacked.text -import org.bukkit.* +import org.bukkit.Bukkit +import org.bukkit.Sound import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.persistence.PersistentDataType diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/gui/DiskDriveGui.kt b/src/main/kotlin/com/liamxsage/energeticstorage/gui/DiskDriveGui.kt index f91d6d6..5bcd21d 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/gui/DiskDriveGui.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/gui/DiskDriveGui.kt @@ -94,14 +94,16 @@ class DiskDriveGui : InventoryHolder, Listener { val thisInventory = player.openInventory.topInventory val playerInventory = player.inventory - when(clickType) { - in setOf(ClickType.SWAP, ClickType.SWAP_RIGHT_CLICK) -> return@with + when (clickType) { + in setOf(ClickType.SWAP, ClickType.SWAP_RIGHT_CLICK) -> return@with in setOf(ClickType.INVENTORY_CLICK, ClickType.SHIFT_IN, ClickType.INTO_HALF, ClickType.INTO) -> { handleInsertDisk(diskDrive, player, thisInventory, playerInventory, block) } + in setOf(ClickType.OUT, ClickType.OUT_HALF, ClickType.SHIFT_OUT) -> { handleRemoveDisk(player, thisInventory, playerInventory, diskDrive, block) } + else -> return@with } } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/gui/GuiFunctions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/gui/GuiFunctions.kt index fc629cf..8962da9 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/gui/GuiFunctions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/gui/GuiFunctions.kt @@ -30,7 +30,11 @@ fun InventoryHolder.findClickType(event: InventoryClickEvent, title: Component): * @param isShiftClick indicates if the click is a shift-click * @return the determined [ClickType] for the click */ -private fun determineClickTypeForNonHolderClick(viewTitle: Component, title: Component, isShiftClick: Boolean): ClickType { +private fun determineClickTypeForNonHolderClick( + viewTitle: Component, + title: Component, + isShiftClick: Boolean +): ClickType { return if (viewTitle == title) { if (isShiftClick) ClickType.SHIFT_IN else ClickType.INVENTORY_CLICK } else { diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt index b390645..b3d5489 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/items/ItemBuilder.kt @@ -97,7 +97,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni * @param value The value of the persistent data. * @return The updated ItemBuilder instance. */ - fun addPersistentData( + fun addPersistentData( key: NamespacedKey, persistentDataType: PersistentDataType, value: I diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt index e3fde12..e614b0c 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt @@ -1,18 +1,19 @@ package com.liamxsage.energeticstorage.listeners -import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE -import com.liamxsage.energeticstorage.cache.DiskDriveCache +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache import com.liamxsage.energeticstorage.database.saveToDB import com.liamxsage.energeticstorage.extensions.isNetworkInterface import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.sendMessagePrefixed import com.liamxsage.energeticstorage.extensions.sendSuccessSound +import com.liamxsage.energeticstorage.model.Cable import com.liamxsage.energeticstorage.model.Core import com.liamxsage.energeticstorage.model.DiskDrive -import com.liamxsage.energeticstorage.model.Cable import com.liamxsage.energeticstorage.model.Terminal import com.liamxsage.energeticstorage.network.NetworkInterfaceType import com.liamxsage.energeticstorage.network.getNetworkInterface +import com.liamxsage.energeticstorage.network.getNetworkInterfaceFromBlock import com.liamxsage.energeticstorage.network.getNetworkInterfaceType import org.bukkit.Material import org.bukkit.Sound @@ -21,7 +22,6 @@ import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.BlockBreakEvent -import org.bukkit.persistence.PersistentDataType import java.util.* class BlockBreakListener : Listener { @@ -50,22 +50,23 @@ class BlockBreakListener : Listener { player.sendMessagePrefixed("Successfully removed ${ networkInterfaceType.name.lowercase(Locale.getDefault()) - .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}.") + .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } + }." + ) player.sendSuccessSound() } private fun removeDiskDrive(block: Block, player: Player) { if (block.type != Material.CHISELED_BOOKSHELF) return - if (!block.persistentDataContainer.has(DISK_DRIVE_ID_NAMESPACE)) return - val systemUUID = block.persistentDataContainer[DISK_DRIVE_ID_NAMESPACE, PersistentDataType.STRING] ?: return - val system = DiskDriveCache.getDiskDriveByUUID(UUID.fromString(systemUUID)) ?: DiskDrive(UUID.fromString(systemUUID)) + if (!block.persistentDataContainer.has(NETWORK_INTERFACE_ID_NAMESPACE)) return + val diskDrive = getNetworkInterfaceFromBlock(block) - for (drive in system.disks) { + for (drive in diskDrive.disks) { player.world.dropItemNaturally(block.location, drive.createDiskItem()) drive.diskDriveUUID = null drive.saveToDB() } - system.disks.clear() - DiskDriveCache.addDiskDrive(system) + diskDrive.disks.clear() + NetworkInterfaceCache.addNetworkInterface(diskDrive) } } \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt index a536d62..c08c75b 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt @@ -1,8 +1,8 @@ package com.liamxsage.energeticstorage.listeners -import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE -import com.liamxsage.energeticstorage.cache.DiskDriveCache +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache import com.liamxsage.energeticstorage.extensions.* import com.liamxsage.energeticstorage.model.DiskDrive import com.liamxsage.energeticstorage.network.NetworkInterfaceType @@ -31,7 +31,7 @@ class BlockPlaceListener : Listener { block.persistentDataContainer[NETWORK_INTERFACE_NAMESPACE, PersistentDataType.BOOLEAN] = true try { - updateNetworkCoreWithConnectedInterfaces(block, player) + updateNetworkCoreWithConnectedInterfaces(block) } catch (e: AssertionError) { player.sendMessagePrefixed("Multiple Cores detected. Please remove one.") player.sendDeniedSound() @@ -41,22 +41,27 @@ class BlockPlaceListener : Listener { player.sendMessagePrefixed("Successfully placed ${ networkInterfaceType.name.lowercase(Locale.getDefault()) - .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}.") + .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } + }." + ) player.sendSuccessSound() } private fun placeDiskDrive(block: Block, itemInHand: ItemStack) { if (block.type != Material.CHISELED_BOOKSHELF) return - if (!itemInHand.hasKey(DISK_DRIVE_ID_NAMESPACE)) return - val systemUUID = itemInHand.getKey(DISK_DRIVE_ID_NAMESPACE) ?: return - val system = DiskDriveCache.getDiskDriveByUUID(UUID.fromString(systemUUID)) ?: DiskDrive(UUID.fromString(systemUUID)) + if (!itemInHand.hasKey(NETWORK_INTERFACE_ID_NAMESPACE)) return + val diskDriveUUID = itemInHand.getKey(NETWORK_INTERFACE_ID_NAMESPACE) ?: return + val diskDrive = + NetworkInterfaceCache.getNetworkInterfaceByUUID(UUID.fromString(diskDriveUUID)) as? DiskDrive ?: DiskDrive( + UUID.fromString(diskDriveUUID) + ) val chiseledBookshelf = block.blockData as ChiseledBookshelf - val drives = system.disks.size + val drives = diskDrive.disks.size for (i in 0 until drives) { chiseledBookshelf.setSlotOccupied(i, true) } block.blockData = chiseledBookshelf - block.persistentDataContainer[DISK_DRIVE_ID_NAMESPACE, PersistentDataType.STRING] = systemUUID + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING] = diskDriveUUID } } \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/ItemClickListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/ItemClickListener.kt index 93b0d42..f255054 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/ItemClickListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/ItemClickListener.kt @@ -5,7 +5,7 @@ import org.bukkit.event.Listener import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.inventory.ItemStack -class ItemClickListener: Listener { +class ItemClickListener : Listener { companion object { val itemClickEvents: MutableMap Unit> = mutableMapOf() diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt index 7eb7b9f..35bfaf5 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt @@ -1,18 +1,16 @@ package com.liamxsage.energeticstorage.listeners -import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE import com.liamxsage.energeticstorage.DISK_ID_NAMESPACE +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.cache.DiskCache -import com.liamxsage.energeticstorage.cache.DiskDriveCache +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache import com.liamxsage.energeticstorage.database.saveToDB import com.liamxsage.energeticstorage.extensions.* import com.liamxsage.energeticstorage.gui.DiskDriveGui +import com.liamxsage.energeticstorage.model.Core import com.liamxsage.energeticstorage.model.DiskDrive import com.liamxsage.energeticstorage.model.Terminal -import com.liamxsage.energeticstorage.network.NetworkInterfaceType -import com.liamxsage.energeticstorage.network.getConnectedNetworkInterfaces -import com.liamxsage.energeticstorage.network.getNetworkInterface -import com.liamxsage.energeticstorage.network.getNetworkInterfaceType +import com.liamxsage.energeticstorage.network.* import org.bukkit.Material import org.bukkit.block.Block import org.bukkit.entity.Player @@ -22,7 +20,6 @@ import org.bukkit.event.block.Action import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.EquipmentSlot import org.bukkit.inventory.ItemStack -import org.bukkit.persistence.PersistentDataType import java.util.* class PlayerInteractListener : Listener { @@ -59,21 +56,24 @@ class PlayerInteractListener : Listener { NetworkInterfaceType.TERMINAL -> { val terminal = getNetworkInterface(block) as? Terminal ?: return - if (terminal.connectedCore == null) { + if (terminal.connectedCoreUUID == null) { player.sendMessagePrefixed("This terminal is not connected to a core.") player.sendDeniedSound() return } + val core = + NetworkInterfaceCache.getNetworkInterfaceByUUID(terminal.connectedCoreUUID!!) as? Core ?: return player.sendMessageBlock( "Network Information", - "Connected Terminals: ${terminal.connectedCore!!.connectedTerminals.size}", - "Connected DiskDrives: ${terminal.connectedCore!!.connectedDiskDrives.size}", - "Total Items: ${terminal.connectedCore!!.totalItems}/${terminal.connectedCore!!.totalSize}", - "Total Types: ${terminal.connectedCore!!.totalTypes}/${terminal.connectedCore!!.totalTypesSize}", - "Total Disks: ${terminal.connectedCore!!.totalDisks}/${terminal.connectedCore!!.connectedDiskDrives.size * 6}" + "Connected Terminals: ${core.connectedTerminals.size}", + "Connected DiskDrives: ${core.connectedDiskDrives.size}", + "Total Items: ${core.totalItems}/${core.totalSize}", + "Total Types: ${core.totalTypes}/${core.totalTypesSize}", + "Total Disks: ${core.totalDisks}/${core.connectedDiskDrives.size * 6}" ) } + else -> { /* Do nothing */ } } @@ -111,9 +111,8 @@ class PlayerInteractListener : Listener { * @param player The player interacting with the disk drive. */ private fun handleDiskDriveInteraction(block: Block, item: ItemStack?, player: Player) { - if (!block.persistentDataContainer.has(DISK_DRIVE_ID_NAMESPACE)) return - val diskDriveUUID = block.persistentDataContainer[DISK_DRIVE_ID_NAMESPACE, PersistentDataType.STRING] ?: return - val diskDrive = DiskDriveCache.getDiskDriveByUUID(UUID.fromString(diskDriveUUID)) ?: DiskDrive(UUID.fromString(diskDriveUUID)) + if (!block.persistentDataContainer.has(NETWORK_INTERFACE_ID_NAMESPACE)) return + val diskDrive = getNetworkInterfaceFromBlock(block) if (tryInsertingDiskIntoDrive(item, diskDrive, player, block)) return @@ -160,7 +159,7 @@ class PlayerInteractListener : Listener { drive.saveToDB() if (item.amount == 1) player.inventory.removeItem(item) else item.amount = item.amount.minus(1) - DiskDriveCache.addDiskDrive(diskDrive) + NetworkInterfaceCache.addNetworkInterface(diskDrive) diskDrive.updateBlock(block) player.sendMessagePrefixed("Successfully inserted drive.") diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt b/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt index ad4623b..fdfb24f 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/managers/RegisterManager.kt @@ -1,8 +1,8 @@ package com.liamxsage.energeticstorage.managers import com.google.common.reflect.ClassPath -import com.liamxsage.energeticstorage.PACKAGE_NAME import com.liamxsage.energeticstorage.EnergeticStorage +import com.liamxsage.energeticstorage.PACKAGE_NAME import com.liamxsage.energeticstorage.annotations.RegisterCommand import com.liamxsage.energeticstorage.customblockdata.BlockDataListener import com.liamxsage.energeticstorage.extensions.getLogger @@ -102,7 +102,7 @@ object RegisterManager { command.aliases = annotation.aliases.toList() command.description = annotation.description - if(annotation.permission.isEmpty()) { + if (annotation.permission.isEmpty()) { command.permission = null } else { command.permission = Permission(annotation.permission, annotation.permissionDefault).name diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Cable.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Cable.kt index 43652a8..6bd37ca 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Cable.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Cable.kt @@ -1,15 +1,29 @@ package com.liamxsage.energeticstorage.model +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.TEXT_GRAY +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache +import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.network.NetworkInterface import com.liamxsage.energeticstorage.network.NetworkInterfaceType +import org.bukkit.block.Block import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType +import java.util.* -class Cable : NetworkInterface { +class Cable(override val uuid: UUID = UUID.randomUUID()) : NetworkInterface { + + init { + NetworkInterfaceCache.addNetworkInterface(this) + } + + override fun setBlockUUID(block: Block): Cable { + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING] = uuid.toString() + return this + } /** * Creates an ItemStack representing a pipe item for the Network. diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt index 4af3202..8e055f3 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt @@ -1,19 +1,36 @@ package com.liamxsage.energeticstorage.model +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.TEXT_GRAY +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache +import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.network.NetworkInterface import com.liamxsage.energeticstorage.network.NetworkInterfaceType +import org.bukkit.block.Block import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType +import java.util.* -class Core : NetworkInterface { + +data class Core( + override val uuid: UUID = UUID.randomUUID() +) : NetworkInterface { val connectedDiskDrives = mutableListOf() val connectedTerminals = mutableListOf() + init { + NetworkInterfaceCache.addNetworkInterface(this) + } + + override fun setBlockUUID(block: Block): Core { + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING] = uuid.toString() + return this + } + /** * Creates an ItemStack representing the core item for the Network. * diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Disk.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Disk.kt index 52f78f2..4bc94cd 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Disk.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Disk.kt @@ -9,7 +9,7 @@ import kotlinx.serialization.Serializable import org.bukkit.Material import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack -import java.util.UUID +import java.util.* @Serializable data class Disk( diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt index 0bdc005..af7bfbd 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt @@ -1,10 +1,11 @@ package com.liamxsage.energeticstorage.model -import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.TEXT_GRAY -import com.liamxsage.energeticstorage.cache.DiskDriveCache +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache import com.liamxsage.energeticstorage.database.loadDisks +import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.network.NetworkInterface import kotlinx.serialization.Contextual @@ -15,18 +16,25 @@ import org.bukkit.block.data.type.ChiseledBookshelf import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType -import java.util.UUID +import java.util.* @Serializable data class DiskDrive( - @Contextual val uuid: UUID = UUID.randomUUID(), + @Contextual override val uuid: UUID = UUID.randomUUID(), val disks: MutableList = mutableListOf() ) : NetworkInterface { + constructor(uuid: UUID) : this(uuid, mutableListOf()) + init { loadDisks() - DiskDriveCache.addDiskDrive(this) + NetworkInterfaceCache.addNetworkInterface(this) + } + + override fun setBlockUUID(block: Block): NetworkInterface { + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING] = uuid.toString() + return this } /** @@ -43,7 +51,7 @@ data class DiskDrive( ) setGlinting(true) customModelData(1) - addPersistentData(DISK_DRIVE_ID_NAMESPACE, uuid.toString()) + addPersistentData(NETWORK_INTERFACE_ID_NAMESPACE, uuid.toString()) addPersistentData(NETWORK_INTERFACE_NAMESPACE, PersistentDataType.BOOLEAN, true) flag(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ADDITIONAL_TOOLTIP, ItemFlag.HIDE_ENCHANTS) }.build() diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt index f10d8e5..187cdf9 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt @@ -1,17 +1,31 @@ package com.liamxsage.energeticstorage.model +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_NAMESPACE import com.liamxsage.energeticstorage.TEXT_GRAY +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache +import com.liamxsage.energeticstorage.extensions.persistentDataContainer import com.liamxsage.energeticstorage.extensions.toItemBuilder import com.liamxsage.energeticstorage.network.NetworkInterface import com.liamxsage.energeticstorage.network.NetworkInterfaceType +import org.bukkit.block.Block import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType +import java.util.* -class Terminal : NetworkInterface { +class Terminal(override val uuid: UUID = UUID.randomUUID()) : NetworkInterface { - var connectedCore: Core? = null + var connectedCoreUUID: UUID? = null + + init { + NetworkInterfaceCache.addNetworkInterface(this) + } + + override fun setBlockUUID(block: Block): Terminal { + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING] = uuid.toString() + return this + } /** * Creates an ItemStack representing a terminal item for the Network. diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt index 820b003..3882661 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt @@ -1,21 +1,22 @@ package com.liamxsage.energeticstorage.network -import com.liamxsage.energeticstorage.DISK_DRIVE_ID_NAMESPACE import com.liamxsage.energeticstorage.MAX_NETWORK_LENGTH -import com.liamxsage.energeticstorage.cache.DiskDriveCache -import com.liamxsage.energeticstorage.extensions.* +import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE +import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache +import com.liamxsage.energeticstorage.extensions.getKey +import com.liamxsage.energeticstorage.extensions.isNetworkInterface +import com.liamxsage.energeticstorage.extensions.persistentDataContainer +import com.liamxsage.energeticstorage.model.Cable import com.liamxsage.energeticstorage.model.Core import com.liamxsage.energeticstorage.model.DiskDrive -import com.liamxsage.energeticstorage.model.Cable import com.liamxsage.energeticstorage.model.Terminal +import dev.fruxz.ascend.extension.forceCastOrNull import org.bukkit.block.Block import org.bukkit.block.BlockFace -import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType import java.util.* -interface NetworkInterface /** * Returns a map of connected network interfaces to the given block. @@ -43,7 +44,8 @@ fun getConnectedNetworkInterfaces( val networkInterface = getNetworkInterface(relativeBlock) ?: continue connectedInterfaces[relativeBlock] = networkInterface - val iteratedInterfaces = getConnectedNetworkInterfaces(relativeBlock, face.oppositeFace, iteration + 1, visitedBlocks) + val iteratedInterfaces = + getConnectedNetworkInterfaces(relativeBlock, face.oppositeFace, iteration + 1, visitedBlocks) // Merge iteratedInterfaces into connectedInterfaces iteratedInterfaces.forEach { (key, value) -> @@ -59,7 +61,7 @@ fun getConnectedNetworkInterfaces( * * @param block The block to check for connected network interfaces. */ -fun updateNetworkCoreWithConnectedInterfaces(block: Block, player: Player) { +fun updateNetworkCoreWithConnectedInterfaces(block: Block) { val connectedInterfaces = getConnectedNetworkInterfaces(block) if (connectedInterfaces.isEmpty()) return @@ -76,18 +78,18 @@ fun updateNetworkCoreWithConnectedInterfaces(block: Block, player: Player) { when (networkInterface) { is DiskDrive -> { core.connectedDiskDrives.add(networkInterface) - player.sendMessagePrefixed("Connected disk drive to core.") - player.sendInfoSound() } + is Terminal -> { core.connectedTerminals.add(networkInterface) - networkInterface.connectedCore = core - player.sendMessagePrefixed("Connected terminal to core.") - player.sendInfoSound() + networkInterface.connectedCoreUUID = core.uuid + } + + else -> { /* Do nothing */ } - else -> { /* Do nothing */ } } } + NetworkInterfaceCache.addNetworkInterface(core) } @@ -131,18 +133,30 @@ fun getNetworkInterfaceType(itemStack: ItemStack): NetworkInterfaceType? { */ fun getNetworkInterface(block: Block): NetworkInterface? { return when (getNetworkInterfaceType(block)) { - NetworkInterfaceType.DISK_DRIVE -> { - if (!block.persistentDataContainer.has(DISK_DRIVE_ID_NAMESPACE)) return null - val diskDriveUUID = block.persistentDataContainer[DISK_DRIVE_ID_NAMESPACE, PersistentDataType.STRING] ?: return null - return DiskDriveCache.getDiskDriveByUUID(UUID.fromString(diskDriveUUID)) ?: DiskDrive(UUID.fromString(diskDriveUUID)) - } - NetworkInterfaceType.TERMINAL -> Terminal() - NetworkInterfaceType.CORE -> Core() - NetworkInterfaceType.CABLE -> Cable() + NetworkInterfaceType.DISK_DRIVE -> getNetworkInterfaceFromBlock(block) + NetworkInterfaceType.TERMINAL -> getNetworkInterfaceFromBlock(block) + NetworkInterfaceType.CORE -> getNetworkInterfaceFromBlock(block) + NetworkInterfaceType.CABLE -> getNetworkInterfaceFromBlock(block) else -> null } } +/** + * Retrieves the network interface of the given block. + * + * @param block The block to retrieve the network interface from. + * @return The network interface associated with the block. + */ +inline fun getNetworkInterfaceFromBlock(block: Block): T { + val networkInterfaceUUID = + block.persistentDataContainer[NETWORK_INTERFACE_ID_NAMESPACE, PersistentDataType.STRING]?.let { + UUID.fromString(it) + } ?: UUID.randomUUID() + return NetworkInterfaceCache.getNetworkInterfaceByUUID(networkInterfaceUUID)?.forceCastOrNull() + ?: T::class.java.getDeclaredConstructor(UUID::class.java).newInstance(networkInterfaceUUID) + .setBlockUUID(block) as T +} + /** * Retrieves the NetworkInterface associated with the given ItemStack. * @@ -151,14 +165,23 @@ fun getNetworkInterface(block: Block): NetworkInterface? { */ fun getNetworkInterface(itemStack: ItemStack): NetworkInterface? { return when (getNetworkInterfaceType(itemStack)) { - NetworkInterfaceType.DISK_DRIVE -> { - if (!itemStack.hasKey(DISK_DRIVE_ID_NAMESPACE)) return null - val diskDriveUUID = itemStack.getKey(DISK_DRIVE_ID_NAMESPACE) ?: return null - return DiskDriveCache.getDiskDriveByUUID(UUID.fromString(diskDriveUUID)) ?: DiskDrive(UUID.fromString(diskDriveUUID)) - } - NetworkInterfaceType.TERMINAL -> Terminal() - NetworkInterfaceType.CORE -> Core() - NetworkInterfaceType.CABLE -> Cable() + NetworkInterfaceType.DISK_DRIVE -> getNetworkInterfaceFromItemStack(itemStack) + NetworkInterfaceType.TERMINAL -> getNetworkInterfaceFromItemStack(itemStack) + NetworkInterfaceType.CORE -> getNetworkInterfaceFromItemStack(itemStack) + NetworkInterfaceType.CABLE -> getNetworkInterfaceFromItemStack(itemStack) else -> null } } + +/** + * Retrieves the network interface of the given block. + * + * @param block The block to retrieve the network interface from. + * @return The network interface associated with the block. + */ +inline fun getNetworkInterfaceFromItemStack(itemStack: ItemStack): T { + val networkInterfaceUUID = + itemStack.getKey(NETWORK_INTERFACE_ID_NAMESPACE)?.let { UUID.fromString(it) } ?: UUID.randomUUID() + return NetworkInterfaceCache.getNetworkInterfaceByUUID(networkInterfaceUUID)?.forceCastOrNull() + ?: T::class.java.getDeclaredConstructor(UUID::class.java).newInstance(networkInterfaceUUID) as T +} diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkInterface.kt b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkInterface.kt new file mode 100644 index 0000000..ee03fd6 --- /dev/null +++ b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkInterface.kt @@ -0,0 +1,10 @@ +package com.liamxsage.energeticstorage.network + +import org.bukkit.block.Block +import java.util.* + +interface NetworkInterface { + val uuid: UUID + + fun setBlockUUID(block: Block): NetworkInterface +} \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt index 99909c3..84c3c39 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/serialization/ItemStackConverter.kt @@ -159,7 +159,7 @@ object ItemStackConverter { val dataInput = BukkitObjectInputStream(inputStream) var invSize = dataInput.readInt() val initialSize = invSize - if(invSize % 9 != 0) { + if (invSize % 9 != 0) { invSize += (9 - invSize % 9) } val inventory = Bukkit.getServer().createInventory(null, invSize)