From 07dbe37d4e7886c08b3fb284e6443649ffa16918 Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Tue, 11 Jun 2024 10:56:24 +0200 Subject: [PATCH] feat: Network connections --- .../listeners/BlockPlaceListener.kt | 10 ++++ .../listeners/PlayerInteractListener.kt | 20 ++++++- .../liamxsage/energeticstorage/model/Core.kt | 58 +++++++++++++++++++ .../energeticstorage/model/Terminal.kt | 2 + .../network/NetworkFunctions.kt | 41 +++++++++++-- 5 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt index f116125..a536d62 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/BlockPlaceListener.kt @@ -7,6 +7,7 @@ import com.liamxsage.energeticstorage.extensions.* import com.liamxsage.energeticstorage.model.DiskDrive import com.liamxsage.energeticstorage.network.NetworkInterfaceType import com.liamxsage.energeticstorage.network.getNetworkInterfaceType +import com.liamxsage.energeticstorage.network.updateNetworkCoreWithConnectedInterfaces import org.bukkit.Material import org.bukkit.block.Block import org.bukkit.block.data.type.ChiseledBookshelf @@ -29,6 +30,15 @@ class BlockPlaceListener : Listener { } block.persistentDataContainer[NETWORK_INTERFACE_NAMESPACE, PersistentDataType.BOOLEAN] = true + try { + updateNetworkCoreWithConnectedInterfaces(block, player) + } catch (e: AssertionError) { + player.sendMessagePrefixed("Multiple Cores detected. Please remove one.") + player.sendDeniedSound() + isCancelled = true + return + } + player.sendMessagePrefixed("Successfully placed ${ networkInterfaceType.name.lowercase(Locale.getDefault()) .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}.") diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt index a221494..7eb7b9f 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/listeners/PlayerInteractListener.kt @@ -8,8 +8,10 @@ import com.liamxsage.energeticstorage.database.saveToDB import com.liamxsage.energeticstorage.extensions.* import com.liamxsage.energeticstorage.gui.DiskDriveGui 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 org.bukkit.Material import org.bukkit.block.Block @@ -55,7 +57,23 @@ class PlayerInteractListener : Listener { player ) - NetworkInterfaceType.TERMINAL -> player.sendMessagePrefixed("Terminal") + NetworkInterfaceType.TERMINAL -> { + val terminal = getNetworkInterface(block) as? Terminal ?: return + if (terminal.connectedCore == null) { + player.sendMessagePrefixed("This terminal is not connected to a core.") + player.sendDeniedSound() + 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}" + ) + } else -> { /* Do nothing */ } } diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt index d44107d..4af3202 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt @@ -11,6 +11,9 @@ import org.bukkit.persistence.PersistentDataType class Core : NetworkInterface { + val connectedDiskDrives = mutableListOf() + val connectedTerminals = mutableListOf() + /** * Creates an ItemStack representing the core item for the Network. * @@ -29,4 +32,59 @@ class Core : NetworkInterface { flag(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ADDITIONAL_TOOLTIP, ItemFlag.HIDE_ENCHANTS) }.build() + + /** + * Represents the total number of disks in the system. + * + * This variable is calculated by summing up the number of disks in each connected disk drive. + * + * @return The total number of disks. + */ + val totalDisks: Int + get() = connectedDiskDrives.sumOf { it.disks.size } + + /** + * Represents the total number of items stored in the connected disk drives. + * + * This property is calculated by summing up the totalItems property of each DiskDrive + * in the connectedDiskDrives list. + * @see DiskDrive + * + * @property totalItems The total number of items stored in the connected disk drives. + */ + val totalItems: Long + get() = connectedDiskDrives.sumOf { it.totalItems } + + /** + * Represents the total number of types of items stored in the disk drives of a network system. + * + * This variable is calculated by summing up the totalTypes property of each connected disk drive in the network. + * + * @see Core + * @see DiskDrive + */ + val totalTypes: Int + get() = connectedDiskDrives.sumOf { it.totalTypes } + + /** + * Represents the possible total size of types stored in connected disk drives. + * + * This variable is calculated by summing up the totalTypesSize property of each connected disk drive + * in the connectedDiskDrives list. + * + * @see DiskDrive + */ + val totalTypesSize: Int + get() = connectedDiskDrives.sumOf { it.totalTypesSize } + + /** + * Represents the possible total size of all connected disk drives in the system. + * + * This variable is calculated by summing up the totalSize property of each connected disk drive. + * + * @see connectedDiskDrives + * @see DiskDrive + */ + val totalSize: Long + get() = connectedDiskDrives.sumOf { it.totalSize } } \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt b/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt index 52f5bab..f10d8e5 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/model/Terminal.kt @@ -11,6 +11,8 @@ import org.bukkit.persistence.PersistentDataType class Terminal : NetworkInterface { + var connectedCore: Core? = null + /** * 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 9cbc9d4..820b003 100644 --- a/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt +++ b/src/main/kotlin/com/liamxsage/energeticstorage/network/NetworkFunctions.kt @@ -3,16 +3,14 @@ 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.getKey -import com.liamxsage.energeticstorage.extensions.hasKey -import com.liamxsage.energeticstorage.extensions.isNetworkInterface -import com.liamxsage.energeticstorage.extensions.persistentDataContainer +import com.liamxsage.energeticstorage.extensions.* 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 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.* @@ -56,6 +54,41 @@ fun getConnectedNetworkInterfaces( return connectedInterfaces } +/** + * Updates the network core with the connected network interfaces. + * + * @param block The block to check for connected network interfaces. + */ +fun updateNetworkCoreWithConnectedInterfaces(block: Block, player: Player) { + val connectedInterfaces = getConnectedNetworkInterfaces(block) + + if (connectedInterfaces.isEmpty()) return + val cores = connectedInterfaces.filter { it.value is Core } + if (cores.isEmpty()) return + + assert(cores.size == 1) { "Multiple cores found in network." } + + val core = cores.entries.first().value as Core + core.connectedDiskDrives.clear() + core.connectedTerminals.clear() + + connectedInterfaces.forEach { (_, networkInterface) -> + 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() + } + else -> { /* Do nothing */ } + } + } +} /**