Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[versions]
kotlin = "2.1.0"
shadow = "9.0.0-beta4"
simplecloud-controller = "0.0.30-dev.16f91aa"
paper = "1.21.4-R0.1-SNAPSHOT"

simplecloud-controller = "0.0.30-dev.16f91aa"
simplecloud-plugin-api = "0.0.1-dev.e7b12c9"

cloud-core = "2.0.0"
cloud-commands = "2.0.0-beta.10"
cloud-confirmation-processors = "1.0.0-rc.1"
Expand All @@ -27,6 +29,7 @@ cloud-paper = { module = "org.incendo:cloud-paper", version.ref = "cloud-command
cloud-processors-confirmation = { module = "org.incendo:cloud-processors-confirmation", version.ref = "cloud-confirmation-processors" }

simplecloud-controller-api = { module = "app.simplecloud.controller:controller-api", version.ref = "simplecloud-controller" }
simplecloud-plugin-api = { module = "app.simplecloud.plugin:plugin-shared", version.ref = "simplecloud-plugin-api" }

coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutine" }
coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutine" }
Expand All @@ -39,6 +42,7 @@ cloud-core = ["cloud-core", "cloud-kotlin-coroutines", "cloud-kotlin-extensions"
cloud-paper = ["cloud-paper", "cloud-minecraft-extras", "cloud-processors-confirmation"]
coroutine = ["coroutines-core", "coroutines-android"]
adventure = ["adventure", "adventure-text-minimessage"]
simplecloud = ["simplecloud-controller-api", "simplecloud-plugin-api"]

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app.simplecloud.plugin.sign.paper

import app.simplecloud.plugin.sign.paper.dispatcher.PaperPlatformDispatcher
import app.simplecloud.plugin.sign.shared.command.SignStateManager
import app.simplecloud.plugin.sign.shared.config.location.SignLocation
import app.simplecloud.plugin.sign.shared.config.location.SignLocationConfig
import kotlinx.coroutines.withContext
import net.kyori.adventure.text.Component
import org.bukkit.Location
Expand Down Expand Up @@ -32,7 +32,7 @@ class PaperSignStateManager(
}
}

override suspend fun updateSign(location: SignLocation, lines: List<Component>) {
override suspend fun updateSign(location: SignLocationConfig, lines: List<Component>) {
withContext(dispatcher.getDispatcher()) {
val mappedLocation = bootstrap.signManager.map(location)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ package app.simplecloud.plugin.sign.paper

import app.simplecloud.controller.api.ControllerApi
import app.simplecloud.plugin.sign.paper.dispatcher.PaperPlatformDispatcher
import app.simplecloud.plugin.sign.paper.rule.PaperRuleRegistry
import app.simplecloud.plugin.sign.paper.rule.PlayerRuleContext
import app.simplecloud.plugin.sign.paper.sender.PaperCommandSender
import app.simplecloud.plugin.sign.paper.sender.PaperCommandSenderMapper
import app.simplecloud.plugin.sign.paper.service.PaperSignService
import app.simplecloud.plugin.sign.shared.CloudSign
import app.simplecloud.plugin.sign.shared.SignManager
import app.simplecloud.plugin.sign.shared.command.SignCommand
import app.simplecloud.plugin.sign.shared.config.layout.FrameConfig
import app.simplecloud.plugin.sign.shared.rule.impl.RuleContext
import app.simplecloud.plugin.sign.shared.utils.MatcherUtil
import io.papermc.paper.plugin.bootstrap.BootstrapContext
import io.papermc.paper.plugin.bootstrap.PluginBootstrap
import io.papermc.paper.plugin.bootstrap.PluginProviderContext
import kotlinx.coroutines.runBlocking
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import org.bukkit.Location
import org.bukkit.block.Sign
import org.bukkit.block.sign.Side
Expand All @@ -35,6 +31,8 @@ class PaperSignsPluginBootstrap : PluginBootstrap {
private val controllerApi = ControllerApi.createCoroutineApi()
private val miniMessage = MiniMessage.miniMessage()

private var isEnabled = false

lateinit var signManager: SignManager<Location>
private set
lateinit var commandManager: PaperCommandManager.Bootstrapped<PaperCommandSender>
Expand All @@ -48,6 +46,8 @@ class PaperSignsPluginBootstrap : PluginBootstrap {

override fun bootstrap(context: BootstrapContext) {
try {
isEnabled = true

initializeSignManager(context)
initializeCommandManager(context)
registerCommands()
Expand All @@ -59,12 +59,12 @@ class PaperSignsPluginBootstrap : PluginBootstrap {
}
}


private fun initializeSignManager(context: BootstrapContext) {
signManager = SignManager(
controllerApi,
context.dataDirectory,
PaperSignService(this),
PaperRuleRegistry()
) { cloudSign, frameConfig ->
updateSign(cloudSign, frameConfig)
}
Expand All @@ -89,7 +89,9 @@ class PaperSignsPluginBootstrap : PluginBootstrap {
}

override fun createPlugin(context: PluginProviderContext): JavaPlugin = plugin

fun disable() {
isEnabled = false
runBlocking {
try {
signCommand?.cleanup()
Expand All @@ -102,36 +104,39 @@ class PaperSignsPluginBootstrap : PluginBootstrap {
}

private fun updateSign(cloudSign: CloudSign<Location>, frameConfig: FrameConfig) {
if (!isEnabled || !plugin.isEnabled) {
logger.debug("Skipping sign update - plugin is disabled")
return
}

val location = cloudSign.location
plugin.server.scheduler.runTask(plugin, Runnable {
try {
val sign = location.block.state as? Sign ?: return@Runnable

val onlinePlayers = plugin.server.onlinePlayers

onlinePlayers.forEach { player ->
val playerRuleContext = PlayerRuleContext(
server = cloudSign.server,
serverState = cloudSign.server?.state,
player
)

updateSignLines(sign, frameConfig, cloudSign, playerRuleContext)
}

updateSignLines(sign, frameConfig, cloudSign)
} catch (e: Exception) {
logger.error("Failed to update sign at location: $location", e)
}
})
}

private fun updateSignLines(sign: Sign, frameConfig: FrameConfig, cloudSign: CloudSign<*>, context: RuleContext) {
private fun updateSignLines(sign: Sign, frameConfig: FrameConfig, cloudSign: CloudSign<*>) {
clearSignLines(sign)

frameConfig.lines.forEachIndexed { index, line ->
val resolvedLine = miniMessage.deserialize(line, *getPlaceholders(cloudSign, context).toTypedArray())
sign.getSide(Side.FRONT).line(index, resolvedLine)
sign.getSide(Side.BACK).line(index, resolvedLine)
val resolvedLine = miniMessage.deserialize(MatcherUtil.resolveAllPlaceholders(line, cloudSign.server))
when (frameConfig.sides.uppercase()) {
"FRONT" -> sign.getSide(Side.FRONT).line(index, resolvedLine)
"BACK" -> sign.getSide(Side.BACK).line(index, resolvedLine)

"BOTH" -> {
sign.getSide(Side.FRONT).line(index, resolvedLine)
sign.getSide(Side.BACK).line(index, resolvedLine)
}

else -> sign.getSide(Side.FRONT).line(index, resolvedLine)
}
}

sign.update()
Expand All @@ -142,25 +147,4 @@ class PaperSignsPluginBootstrap : PluginBootstrap {
sign.getSide(Side.FRONT).lines().replaceAll { emptyComponent }
sign.getSide(Side.BACK).lines().replaceAll { emptyComponent }
}

private fun getPlaceholders(cloudSign: CloudSign<*>, context: RuleContext): List<TagResolver.Single> = buildList {
with(cloudSign.server) {
add(Placeholder.parsed("group", this?.group ?: "unknown"))
add(Placeholder.parsed("numerical-id", this?.numericalId?.toString() ?: "0"))
add(Placeholder.parsed("type", this?.type?.toString() ?: "unknown"))
add(Placeholder.parsed("host", this?.host ?: "unknown"))
add(Placeholder.parsed("ip", this?.ip ?: "unknown"))
add(Placeholder.parsed("port", this?.port?.toString() ?: "0"))
add(Placeholder.parsed("min-memory", this?.minMemory?.toString() ?: "0"))
add(Placeholder.parsed("max-memory", this?.maxMemory?.toString() ?: "0"))
add(Placeholder.parsed("max-players", this?.maxPlayers?.toString() ?: "0"))
add(Placeholder.parsed("player-count", this?.playerCount?.toString() ?: "0"))
add(Placeholder.parsed("state", this?.state?.toString() ?: "unknown"))
}

if (context is PlayerRuleContext) {
add(Placeholder.parsed("player_name", context.player.name))
add(Placeholder.parsed("player_sender", context.player.server.name))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
package app.simplecloud.plugin.sign.paper.listener

import app.simplecloud.plugin.sign.paper.PaperSignsPlugin
import app.simplecloud.plugin.sign.paper.rule.PlayerRuleContext
import app.simplecloud.plugin.sign.shared.rule.impl.ServerRuleContext
import org.bukkit.block.Block
import org.bukkit.block.Sign
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.player.PlayerInteractEvent

data class SignListener(private val plugin: PaperSignsPlugin) : Listener {
class SignListener(private val plugin: PaperSignsPlugin) : Listener {

//TODO: Add Click Catching for Different Rules e.g handle click on sign that is staring/offline/current_server

@EventHandler
@EventHandler(priority = EventPriority.HIGH)
fun handleSignInteract(event: PlayerInteractEvent) {
if (event.action != Action.RIGHT_CLICK_BLOCK || event.clickedBlock == null) return
if (!isValidSignInteraction(event)) return

val sign = (event.clickedBlock?.state as? Sign) ?: return
val sign = event.clickedBlock?.state as? Sign ?: return
val cloudSign = plugin.bootstrap.signManager.getCloudSign(sign.location) ?: return

plugin.bootstrap.signManager.getCloudSign(sign.location)?.let { cloudSign ->
event.isCancelled = true
event.isCancelled = true

val playerRuleContext = PlayerRuleContext(
server = cloudSign.server,
serverState = cloudSign.server?.state,
event.player
)
cloudSign.server?.let { server ->
val serverRuleContext = ServerRuleContext(server = server)
val serverName = plugin.bootstrap.signManager.getLayout(serverRuleContext)
.constructName(server)

cloudSign.server?.let { server ->
val serverName = plugin.bootstrap.signManager.getLayout(playerRuleContext).constructName(server)
plugin.sendPlayerToServer(event.player, serverName)
}
plugin.sendPlayerToServer(event.player, serverName)
}
}

@EventHandler
@EventHandler(priority = EventPriority.HIGH)
fun handleSignBreak(event: BlockBreakEvent) {
val sign = (event.block.state as? Sign) ?: return
if (!isSign(event.block)) return

plugin.bootstrap.signManager.getCloudSign(sign.location)?.let {
val sign = event.block.state as Sign
if (plugin.bootstrap.signManager.getCloudSign(sign.location) != null) {
event.isCancelled = true
}
}

private fun isValidSignInteraction(event: PlayerInteractEvent): Boolean {
return event.action == Action.RIGHT_CLICK_BLOCK &&
event.clickedBlock != null &&
isSign(event.clickedBlock!!)
}

private fun isSign(block: Block): Boolean = block.state is Sign

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.simplecloud.plugin.sign.paper.sender

import app.simplecloud.plugin.sign.paper.PaperSignsPlugin
import app.simplecloud.plugin.sign.shared.config.location.SignLocation
import app.simplecloud.plugin.sign.shared.config.location.SignLocationConfig
import app.simplecloud.plugin.sign.shared.sender.SignCommandSender
import app.simplecloud.plugin.sign.shared.utils.SignCommandMessages
import io.papermc.paper.command.brigadier.CommandSourceStack
Expand All @@ -20,7 +20,7 @@ class PaperCommandSender(
override fun sendMessage(component: Component) =
sourceStack.sender.sendMessage(component)

override suspend fun getTargetBlock(maxDistance: Int): SignLocation? {
override suspend fun getTargetBlock(maxDistance: Int): SignLocationConfig? {
val player = sourceStack.sender as? Player ?: return null

return withContext(PaperSignsPlugin.instance.bootstrap.platformDispatcher.getDispatcher()) {
Expand All @@ -32,7 +32,7 @@ class PaperCommandSender(
return@withContext null
}

SignLocation(
SignLocationConfig(
world = targetBlock.world.name,
x = targetBlock.x.toDouble(),
y = targetBlock.y.toDouble(),
Expand Down
Loading