Skip to content

Commit

Permalink
feat: Better networking
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Jun 11, 2024
1 parent 2ef1cc9 commit 3575b88
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ESGiveCommand : CommandExecutor, TabExecutor {
}

val itemStack = when (item) {
"system" -> {
"diskdrive" -> {
DiskDrive().createDiskDriveItem()
}

Expand Down Expand Up @@ -81,7 +81,7 @@ class ESGiveCommand : CommandExecutor, TabExecutor {
return when (args.size) {
1 -> {
listOf(
"system",
"diskdrive",
"core",
"cable",
"terminal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<DiskDrive>(block)

for (drive in diskDrive.disks) {
Expand All @@ -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<Terminal>(block)

if (terminal.connectedCoreUUID == null) return
val core =
NetworkInterfaceCache.getNetworkInterfaceByUUID(terminal.connectedCoreUUID!!) as? Core ?: return

core.connectedTerminals.remove(terminal)
NetworkInterfaceCache.addNetworkInterface(core)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,22 +57,19 @@ 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
}
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 */
Expand All @@ -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: <green>${
networkInterfaceType.name.lowercase(Locale.getDefault())
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}"
)
}")
player.sendMessagePrefixed("InterfaceUUID: <dark_green>${clickedInterface.uuid}")
player.sendMessagePrefixed("Network Interfaces found:")
interfacesSummedByType.forEach { (type, amount) ->
player.sendMessagePrefixed("${type.simpleName}: $amount")
player.sendMessagePrefixed("${type.simpleName}: <color:#dff9fb>$amount")
}
player.sendInfoSound()

val core = connectedInterfaced.filter { it.value is Core }.values.firstOrNull() as? Core
if (core == null) return

player.sendMessageBlock(
"<bold>${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}"
)
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/liamxsage/energeticstorage/model/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Disk> {
return connectedDiskDrives.map { it.disks }.flatten()
}

/**
* Represents the total number of disks in the system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ data class DiskDrive(
val disks: MutableList<Disk> = mutableListOf()
) : NetworkInterface {

@Contextual
var connectedCoreUUID: UUID? = null

constructor(uuid: UUID) : this(uuid, mutableListOf())

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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." }

Expand All @@ -78,6 +75,7 @@ fun updateNetworkCoreWithConnectedInterfaces(block: Block) {
when (networkInterface) {
is DiskDrive -> {
core.connectedDiskDrives.add(networkInterface)
networkInterface.connectedCoreUUID = core.uuid
}

is Terminal -> {
Expand All @@ -90,6 +88,7 @@ fun updateNetworkCoreWithConnectedInterfaces(block: Block) {
}
}
NetworkInterfaceCache.addNetworkInterface(core)
return core
}


Expand Down Expand Up @@ -184,4 +183,4 @@ inline fun <reified T : NetworkInterface> getNetworkInterfaceFromItemStack(itemS
itemStack.getKey(NETWORK_INTERFACE_ID_NAMESPACE)?.let { UUID.fromString(it) } ?: UUID.randomUUID()
return NetworkInterfaceCache.getNetworkInterfaceByUUID(networkInterfaceUUID)?.forceCastOrNull<T>()
?: T::class.java.getDeclaredConstructor(UUID::class.java).newInstance(networkInterfaceUUID) as T
}
}

0 comments on commit 3575b88

Please sign in to comment.