Skip to content

Commit

Permalink
feat: Network connections
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Jun 11, 2024
1 parent cf882ba commit 07dbe37
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() }}.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
}
}
Expand Down
58 changes: 58 additions & 0 deletions src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import org.bukkit.persistence.PersistentDataType

class Core : NetworkInterface {

val connectedDiskDrives = mutableListOf<DiskDrive>()
val connectedTerminals = mutableListOf<Terminal>()

/**
* Creates an ItemStack representing the core item for the Network.
*
Expand All @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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 */ }
}
}
}


/**
Expand Down

0 comments on commit 07dbe37

Please sign in to comment.