diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt index c327322..d5a7c8b 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/commands/ESGiveCommand.kt @@ -37,7 +37,7 @@ class ESGiveCommand : CommandExecutor, TabExecutor { } val itemStack = when (item) { - "system" -> { + "diskdrive" -> { DiskDrive().createDiskDriveItem() } @@ -81,7 +81,7 @@ class ESGiveCommand : CommandExecutor, TabExecutor { return when (args.size) { 1 -> { listOf( - "system", + "diskdrive", "core", "cable", "terminal", diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt index e614b0c..0b26e54 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockBreakListener.kt @@ -33,8 +33,10 @@ class BlockBreakListener : Listener { val networkInterface = getNetworkInterface(block) ?: return@with isDropItems = false - if (networkInterfaceType == NetworkInterfaceType.DISK_DRIVE) { - removeDiskDrive(block, player) + when (networkInterfaceType) { + NetworkInterfaceType.DISK_DRIVE -> removeDiskDrive(block, player) + NetworkInterfaceType.TERMINAL -> removeTerminal(block) + else -> { /* Do nothing */ } } val itemStack = when (networkInterface) { @@ -56,9 +58,13 @@ class BlockBreakListener : Listener { player.sendSuccessSound() } + /** + * Removes the disk drive and drops any disks it contains. + * + * @param block The block representing the disk drive. + * @param player The player who triggered the removal. + */ private fun removeDiskDrive(block: Block, player: Player) { - if (block.type != Material.CHISELED_BOOKSHELF) return - if (!block.persistentDataContainer.has(NETWORK_INTERFACE_ID_NAMESPACE)) return val diskDrive = getNetworkInterfaceFromBlock(block) for (drive in diskDrive.disks) { @@ -68,5 +74,28 @@ class BlockBreakListener : Listener { } diskDrive.disks.clear() NetworkInterfaceCache.addNetworkInterface(diskDrive) + + if (diskDrive.connectedCoreUUID == null) return + val core = + NetworkInterfaceCache.getNetworkInterfaceByUUID(diskDrive.connectedCoreUUID!!) as? Core ?: return + + core.connectedDiskDrives.remove(diskDrive) + NetworkInterfaceCache.addNetworkInterface(core) + } + + /** + * Removes the terminal from the network. + * + * @param block The block containing the terminal. + */ + private fun removeTerminal(block: Block) { + val terminal = getNetworkInterfaceFromBlock(block) + + if (terminal.connectedCoreUUID == null) return + val core = + NetworkInterfaceCache.getNetworkInterfaceByUUID(terminal.connectedCoreUUID!!) as? Core ?: return + + core.connectedTerminals.remove(terminal) + NetworkInterfaceCache.addNetworkInterface(core) } } \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt index 35bfaf5..1edf83a 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt @@ -2,6 +2,7 @@ package com.liamxsage.energeticstorage.listeners import com.liamxsage.energeticstorage.DISK_ID_NAMESPACE import com.liamxsage.energeticstorage.NETWORK_INTERFACE_ID_NAMESPACE +import com.liamxsage.energeticstorage.TEXT_GRAY import com.liamxsage.energeticstorage.cache.DiskCache import com.liamxsage.energeticstorage.cache.NetworkInterfaceCache import com.liamxsage.energeticstorage.database.saveToDB @@ -56,7 +57,8 @@ class PlayerInteractListener : Listener { NetworkInterfaceType.TERMINAL -> { val terminal = getNetworkInterface(block) as? Terminal ?: return - if (terminal.connectedCoreUUID == null) { + + if (terminal.connectedCoreUUID == null && updateNetworkCoreWithConnectedInterfaces(block) == null) { player.sendMessagePrefixed("This terminal is not connected to a core.") player.sendDeniedSound() return @@ -64,14 +66,10 @@ class PlayerInteractListener : Listener { val core = NetworkInterfaceCache.getNetworkInterfaceByUUID(terminal.connectedCoreUUID!!) as? Core ?: return - player.sendMessageBlock( - "Network Information", - "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}" - ) + val disks = core.getAllDisksInSystem() + player.sendMessagePrefixed("Connected Disks: ${disks.size} with a total size of ${core.totalSize}") + + // Todo: Open Gui } else -> { /* Do nothing */ @@ -89,18 +87,31 @@ class PlayerInteractListener : Listener { */ private fun PlayerInteractEvent.sendDebugInfo(networkInterfaceType: NetworkInterfaceType) { // The following code is only for debugging purposes + val clickedInterface = getNetworkInterface(clickedBlock!!) ?: return val connectedInterfaced = getConnectedNetworkInterfaces(clickedBlock!!) val interfacesSummedByType = connectedInterfaced.values.groupBy { it::class.java }.mapValues { it.value.size } player.sendMessagePrefixed("Clicked Network Interface: ${ networkInterfaceType.name.lowercase(Locale.getDefault()) .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } - }" - ) + }") + player.sendMessagePrefixed("InterfaceUUID: ${clickedInterface.uuid}") player.sendMessagePrefixed("Network Interfaces found:") interfacesSummedByType.forEach { (type, amount) -> - player.sendMessagePrefixed("${type.simpleName}: $amount") + player.sendMessagePrefixed("${type.simpleName}: $amount") } player.sendInfoSound() + + val core = connectedInterfaced.filter { it.value is Core }.values.firstOrNull() as? Core + if (core == null) return + + player.sendMessageBlock( + "${TEXT_GRAY}Network Information", + "${TEXT_GRAY}Connected Terminals: ${core.connectedTerminals.size}", + "${TEXT_GRAY}Connected DiskDrives: ${core.connectedDiskDrives.size}", + "${TEXT_GRAY}Total Items: ${core.totalItems}/${core.totalSize}", + "${TEXT_GRAY}Total Types: ${core.totalTypes}/${core.totalTypesSize}", + "${TEXT_GRAY}Total Disks: ${core.totalDisks}/${core.connectedDiskDrives.size * 6}" + ) } /** diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt index 8e055f3..918e56b 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt @@ -49,6 +49,14 @@ data class Core( flag(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ADDITIONAL_TOOLTIP, ItemFlag.HIDE_ENCHANTS) }.build() + /** + * Returns a list of all disks in the system. + * + * @return A list of [Disk] objects representing the disks in the system. + */ + fun getAllDisksInSystem(): List { + return connectedDiskDrives.map { it.disks }.flatten() + } /** * Represents the total number of disks in the system. diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt index af7bfbd..fec587d 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/DiskDrive.kt @@ -24,6 +24,9 @@ data class DiskDrive( val disks: MutableList = mutableListOf() ) : NetworkInterface { + @Contextual + var connectedCoreUUID: UUID? = null + constructor(uuid: UUID) : this(uuid, mutableListOf()) init { diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt index 3882661..0f90034 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt @@ -6,10 +6,7 @@ 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.Terminal +import com.liamxsage.energeticstorage.model.* import dev.fruxz.ascend.extension.forceCastOrNull import org.bukkit.block.Block import org.bukkit.block.BlockFace @@ -61,12 +58,12 @@ fun getConnectedNetworkInterfaces( * * @param block The block to check for connected network interfaces. */ -fun updateNetworkCoreWithConnectedInterfaces(block: Block) { +fun updateNetworkCoreWithConnectedInterfaces(block: Block): Core? { val connectedInterfaces = getConnectedNetworkInterfaces(block) - if (connectedInterfaces.isEmpty()) return + if (connectedInterfaces.isEmpty()) return null val cores = connectedInterfaces.filter { it.value is Core } - if (cores.isEmpty()) return + if (cores.isEmpty()) return null assert(cores.size == 1) { "Multiple cores found in network." } @@ -78,6 +75,7 @@ fun updateNetworkCoreWithConnectedInterfaces(block: Block) { when (networkInterface) { is DiskDrive -> { core.connectedDiskDrives.add(networkInterface) + networkInterface.connectedCoreUUID = core.uuid } is Terminal -> { @@ -90,6 +88,7 @@ fun updateNetworkCoreWithConnectedInterfaces(block: Block) { } } NetworkInterfaceCache.addNetworkInterface(core) + return core } @@ -184,4 +183,4 @@ inline fun getNetworkInterfaceFromItemStack(itemS 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 -} +} \ No newline at end of file