Skip to content

Commit a34f3ed

Browse files
refactor: port the latest proxy plugin version to BungeeCord
1 parent b1cff2f commit a34f3ed

File tree

8 files changed

+271
-99
lines changed

8 files changed

+271
-99
lines changed

proxy-bungeecord/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ dependencies {
88
implementation(rootProject.libs.adventure.bungeecord.platform)
99

1010
compileOnly(rootProject.libs.simplecloud.event.wrapper.bungeecord)
11+
12+
implementation(rootProject.libs.command.cloud.core)
13+
implementation(rootProject.libs.command.cloud.bungeecord)
1114
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package app.simplecloud.plugin.proxy.bungeecord
2+
3+
import app.simplecloud.plugin.proxy.shared.handler.command.CommandSender
4+
import net.kyori.adventure.text.Component
5+
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer
6+
import net.md_5.bungee.api.chat.BaseComponent
7+
8+
class BungeeCordCommandSender(private val commandSender: net.md_5.bungee.api.CommandSender
9+
) : CommandSender {
10+
11+
fun getCommandSender(): net.md_5.bungee.api.CommandSender {
12+
return commandSender
13+
}
14+
15+
override fun sendMessage(message: String) {
16+
commandSender.sendMessage(message)
17+
}
18+
}
19+
20+
fun Component.toBaseComponent(): BaseComponent {
21+
return BungeeComponentSerializer.get().serialize(this)[0]
22+
}

proxy-bungeecord/src/main/kotlin/app/simplecloud/plugin/proxy/bungeecord/ProxyBungeeCordPlugin.kt

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,74 @@ package app.simplecloud.plugin.proxy.bungeecord
22

33
import app.simplecloud.plugin.proxy.bungeecord.event.ConfigureTagResolversEvent
44
import app.simplecloud.plugin.proxy.bungeecord.handler.TabListHandler
5-
import app.simplecloud.plugin.proxy.bungeecord.listener.ConfigureTagResolversListener
6-
import app.simplecloud.plugin.proxy.bungeecord.listener.ProxyPingListener
7-
import app.simplecloud.plugin.proxy.bungeecord.listener.TabListListener
5+
import app.simplecloud.plugin.proxy.bungeecord.listener.*
6+
import app.simplecloud.plugin.proxy.shared.ProxyPlugin
87
import app.simplecloud.plugin.proxy.shared.config.YamlConfig
98
import app.simplecloud.plugin.proxy.shared.config.placeholder.PlaceHolderConfiguration
109
import app.simplecloud.plugin.proxy.shared.config.tablis.TabListConfiguration
1110
import app.simplecloud.plugin.proxy.shared.handler.MotdLayoutHandler
11+
import app.simplecloud.plugin.proxy.shared.handler.command.CommandSender
12+
import app.simplecloud.plugin.proxy.shared.handler.command.ProxyCommandHandler
1213
import net.kyori.adventure.platform.bungeecord.BungeeAudiences
1314
import net.kyori.adventure.text.Component
1415
import net.kyori.adventure.text.minimessage.MiniMessage
1516
import net.md_5.bungee.api.connection.ProxiedPlayer
1617
import net.md_5.bungee.api.plugin.Plugin
18+
import org.incendo.cloud.SenderMapper
19+
import org.incendo.cloud.bungee.BungeeCommandManager
20+
import org.incendo.cloud.execution.ExecutionCoordinator
1721

1822

1923
class ProxyBungeeCordPlugin: Plugin() {
2024

21-
lateinit var generalConfiguration: GeneralConfig
22-
lateinit var tabListConfiguration: TabListConfiguration
23-
lateinit var placeHolderConfiguration: PlaceHolderConfiguration
25+
val proxyPlugin = ProxyPlugin(this.dataFolder.path)
26+
2427

25-
val config = YamlConfig(this.dataFolder.path)
2628
val tabListHandler = TabListHandler(this)
27-
val motdLayoutHandler = MotdLayoutHandler(config, generalConfiguration)
29+
30+
private lateinit var commandManager: BungeeCommandManager<CommandSender>
2831

2932
private var adventure: BungeeAudiences? = null
3033

3134
private val miniMessage = MiniMessage.miniMessage()
3235

3336
override fun onEnable() {
34-
val config = YamlConfig(this.dataFolder.path)
35-
36-
this.generalConfiguration = config.load<GeneralConfig>("general")!!
37-
config.save("general", this.generalConfiguration)
38-
39-
this.tabListConfiguration = config.load<TabListConfiguration>("tablist")!!
40-
config.save("tablist", this.tabListConfiguration)
37+
this.proxyPlugin.config.save("tablist", this.proxyPlugin.tabListConfiguration)
38+
this.proxyPlugin.config.save("placeholder", this.proxyPlugin.placeHolderConfiguration)
39+
this.proxyPlugin.config.save("messages", this.proxyPlugin.messagesConfiguration)
4140

42-
this.placeHolderConfiguration = config.load<PlaceHolderConfiguration>("placeholder")!!
43-
config.save("placeholder", this.placeHolderConfiguration)
44-
45-
this.motdLayoutHandler.loadMotdLayouts()
41+
this.proxyPlugin.motdLayoutHandler.loadMotdLayouts()
4642

4743
this.adventure = BungeeAudiences.create(this);
44+
this.proxy.pluginManager.registerListener(this, ProxyPingListener(this))
45+
this.proxy.pluginManager.registerListener(this, ConfigureTagResolversListener(this))
46+
this.proxy.pluginManager.registerListener(this, CloudListener(this))
47+
this.proxy.pluginManager.registerListener(this, ServerPreConnectListener(this))
4848

49-
if (this.tabListConfiguration.tabListUpdateTime > 0)
49+
if (this.proxyPlugin.tabListConfiguration.tabListUpdateTime > 0)
5050
this.tabListHandler.startTabListTask()
5151
else
5252
this.logger.info("Tablist update time is set to 0, tablist will not be updated automatically")
5353

54-
this.proxy.pluginManager.registerListener(this, TabListListener(this))
55-
this.proxy.pluginManager.registerListener(this, ProxyPingListener(this))
56-
this.proxy.pluginManager.registerListener(this, ConfigureTagResolversListener(this))
54+
val executionCoordinator = ExecutionCoordinator.simpleCoordinator<CommandSender>()
55+
56+
val senderMapper = SenderMapper.create<net.md_5.bungee.api.CommandSender, CommandSender>(
57+
{ commandSender -> BungeeCordCommandSender(commandSender) },
58+
{ cloudSender -> (cloudSender as BungeeCordCommandSender).getCommandSender() }
59+
)
60+
61+
commandManager = BungeeCommandManager(
62+
this,
63+
executionCoordinator,
64+
senderMapper
65+
)
66+
67+
val proxyCommandHandler = ProxyCommandHandler(commandManager, this.proxyPlugin)
68+
proxyCommandHandler.loadCommands()
69+
70+
System.getenv("SIMPLECLOUD_MAINTENANCE")?.let {
71+
this.proxyPlugin.maintenance = it == "true"
72+
}
5773
}
5874

5975
override fun onDisable() {

proxy-bungeecord/src/main/kotlin/app/simplecloud/plugin/proxy/bungeecord/handler/TabListHandler.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import net.md_5.bungee.api.connection.ProxiedPlayer
77
import net.md_5.bungee.api.scheduler.ScheduledTask
88
import java.util.concurrent.TimeUnit
99

10-
1110
class TabListHandler(
1211
private val plugin: ProxyBungeeCordPlugin
1312
) {
@@ -24,7 +23,7 @@ class TabListHandler(
2423
this.tabListIndex.forEach { (key, value) ->
2524
this.tabListIndex[key] = value + 1
2625
}
27-
}, 1, this.plugin.tabListConfiguration.tabListUpdateTime, TimeUnit.MILLISECONDS)
26+
}, 1, this.plugin.proxyPlugin.tabListConfiguration.tabListUpdateTime, TimeUnit.MILLISECONDS)
2827
}
2928

3029
fun stopTabListTask() {
@@ -37,7 +36,7 @@ class TabListHandler(
3736
}
3837

3938
fun updateTabListForPlayer(player: ProxiedPlayer) {
40-
val configuration = plugin.tabListConfiguration
39+
val configuration = plugin.proxyPlugin.tabListConfiguration
4140

4241
val serviceName = player.server.info.name
4342

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package app.simplecloud.plugin.proxy.bungeecord.listener
2+
3+
import app.simplecloud.event.bungeecord.mapping.CloudServerUpdateEvent
4+
import app.simplecloud.plugin.proxy.bungeecord.ProxyBungeeCordPlugin
5+
import app.simplecloud.plugin.proxy.shared.handler.MotdLayoutHandler
6+
import net.md_5.bungee.api.plugin.Listener
7+
import net.md_5.bungee.event.EventHandler
8+
import java.util.logging.Logger
9+
10+
class CloudListener(
11+
private val plugin: ProxyBungeeCordPlugin
12+
) : Listener {
13+
14+
private val logger = Logger.getLogger(CloudListener::class.java.name)
15+
16+
@EventHandler
17+
fun test(event: CloudServerUpdateEvent) {
18+
19+
if (event.getTo().uniqueId != System.getenv("SIMPLECLOUD_UNIQUE_ID")) return
20+
21+
checkMaintenanceChance(event)
22+
checkLayoutMaintenanceChance(event)
23+
checkLayoutChance(event)
24+
}
25+
26+
private fun checkMaintenanceChance(event: CloudServerUpdateEvent) {
27+
val isMaintenance = event.getTo().properties["maintenance"]
28+
29+
if (isMaintenance == event.getFrom().properties["maintenance"]) return
30+
31+
val newMaintenanceState = isMaintenance == "true"
32+
33+
if (this.plugin.proxyPlugin.maintenance == newMaintenanceState) return
34+
35+
this.plugin.proxyPlugin.maintenance = newMaintenanceState
36+
37+
this.logger.info("Maintenance mode has been toggled to $newMaintenanceState")
38+
}
39+
40+
private fun checkLayoutMaintenanceChance(event: CloudServerUpdateEvent) {
41+
val layout = event.getTo().properties[MotdLayoutHandler.CURRENT_MAINTENANCE_LAYOUT_KEY]
42+
43+
if (layout == event.getFrom().properties[MotdLayoutHandler.CURRENT_MAINTENANCE_LAYOUT_KEY]) return
44+
45+
val newLayout = layout ?: MotdLayoutHandler.DEFAULT_MAINTENANCE_LAYOUT_NAME
46+
47+
if (MotdLayoutHandler.CURRENT_MAINTENANCE_LAYOUT_KEY == newLayout) return
48+
49+
MotdLayoutHandler.CURRENT_MAINTENANCE_LAYOUT_KEY = newLayout
50+
51+
this.logger.info("Layout has been changed to $newLayout")
52+
}
53+
54+
private fun checkLayoutChance(event: CloudServerUpdateEvent) {
55+
val layout = event.getTo().properties[MotdLayoutHandler.CURRENT_LAYOUT_KEY]
56+
57+
if (layout == event.getFrom().properties[MotdLayoutHandler.CURRENT_LAYOUT_KEY]) return
58+
59+
val newLayout = layout ?: MotdLayoutHandler.DEFAULT_LAYOUT_NAME
60+
61+
if (MotdLayoutHandler.CURRENT_LAYOUT_KEY == newLayout) return
62+
63+
MotdLayoutHandler.CURRENT_LAYOUT_KEY = newLayout
64+
65+
this.logger.info("Layout has been changed to $newLayout")
66+
}
67+
}

proxy-bungeecord/src/main/kotlin/app/simplecloud/plugin/proxy/bungeecord/listener/ConfigureTagResolversListener.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,40 @@ package app.simplecloud.plugin.proxy.bungeecord.listener
33
import app.simplecloud.plugin.proxy.bungeecord.ProxyBungeeCordPlugin
44
import app.simplecloud.plugin.proxy.bungeecord.event.ConfigureTagResolversEvent
55
import app.simplecloud.plugin.proxy.shared.resolver.TagResolverHelper
6+
import kotlinx.coroutines.runBlocking
67
import net.md_5.bungee.api.plugin.Listener
78
import net.md_5.bungee.event.EventHandler
89
import net.md_5.bungee.event.EventPriority
10+
import kotlin.jvm.optionals.getOrNull
911

1012
class ConfigureTagResolversListener(
1113
private val plugin: ProxyBungeeCordPlugin
1214
) : Listener {
1315

1416
@EventHandler(priority = EventPriority.LOWEST)
1517
fun onConfigureTagResolvers(event: ConfigureTagResolversEvent) {
16-
val player = event.player
17-
val serverName = player?.server?.info?.name ?: "unknown"
18-
19-
val ping = player?.ping?.toLong() ?: -1
20-
val pingColors = plugin.placeHolderConfiguration.pingColors
21-
22-
val onlinePlayers = this.plugin.proxy.players.size
23-
val realMaxPlayers = this.plugin.proxy.config.listeners.sumOf { it.maxPlayers }
24-
25-
event.withTagResolvers(
26-
TagResolverHelper.getDefaultTagResolvers(
27-
serverName,
28-
ping,
29-
pingColors,
30-
onlinePlayers,
31-
realMaxPlayers,
32-
this.plugin.motdLayoutHandler.getCurrentMotdLayout()
18+
19+
runBlocking {
20+
val player = event.player
21+
val serverName = player?.server?.info?.name ?: "unknown"
22+
23+
val ping = player?.ping ?: -1
24+
val pingColors = plugin.proxyPlugin.placeHolderConfiguration.pingColors
25+
26+
val onlinePlayers = plugin.proxy.players.size
27+
val realMaxPlayers = plugin.proxy.config.playerLimit
28+
29+
event.withTagResolvers(
30+
TagResolverHelper.getDefaultTagResolvers(
31+
serverName,
32+
ping.toLong(),
33+
pingColors,
34+
onlinePlayers,
35+
realMaxPlayers,
36+
plugin.proxyPlugin.motdLayoutHandler.getCurrentMotdLayout()
37+
)
3338
)
34-
)
39+
}
3540
}
3641

3742
}

0 commit comments

Comments
 (0)