Website Integration Rewrite, Upgrades & Plugin Migration

- Remove synchronous website integration execution from the GameEngine 600ms tick loop
- Decouple website credentials and toggles from ConfigLoader, secrets.json, and Constants.WEBSITE_INTEGRATION
- Introduce modular "website" configuration group with independent toggles for players_online and registered_accounts
- Consolidate background tasks onto a single daemon ScheduledExecutorService with graceful lifecycle management via ServerStartupEvent and ServerShutdownEvent
- Optimize character directory counting with Java NIO streaming to prevent heap allocations
- Add UTF-8 URL encoding, User-Agent headers, safe stream handling, and network exception throttling
This commit is contained in:
Dark98 committed 2026-11-13 01:12:15 +00:00
1 parent 622b3264e1
commit e1efbb0fdf
12 files changed
+256 -101

No files matched your search

@@ -0,0 +1,50 @@
package plugin.integration.website
import com.rs2.Constants
import com.rs2.game.players.PlayerHandler
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
object PlayersOnlineWebsite {
private var task: ScheduledFuture<*>? = null
fun start() {
if (!WebsiteConfig.playersOnline) {
return
}
if (WebsiteConfig.password.isEmpty()) {
WebsiteService.warnMissingPassword()
return
}
stop()
task = WebsiteService.getScheduler().scheduleAtFixedRate({
try {
update()
} catch (e: Exception) {
if (Constants.SERVER_DEBUG) {
e.printStackTrace()
} else {
System.err.println("[WebsiteIntegration] Error in PlayersOnlineWebsite task: ${e.message}")
}
}
}, 10, 30, TimeUnit.SECONDS)
}
fun stop() {
task?.cancel(true)
task = null
}
fun update() {
val pass = WebsiteConfig.password
if (pass.isEmpty()) return
WebsiteService.sendGetRequest("playersonline.php", mapOf(
"pass" to pass,
"amount" to PlayerHandler.getPlayerCount(),
"world" to Constants.WORLD
))
}
}
@@ -0,0 +1,68 @@
package plugin.integration.website
import com.rs2.Constants
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
object RegisteredAccsWebsite {
private var task: ScheduledFuture<*>? = null
fun start() {
if (!WebsiteConfig.registeredAccounts) {
return
}
if (WebsiteConfig.password.isEmpty()) {
WebsiteService.warnMissingPassword()
return
}
stop()
task = WebsiteService.getScheduler().scheduleAtFixedRate({
try {
update()
} catch (e: Exception) {
if (Constants.SERVER_DEBUG) {
e.printStackTrace()
} else {
System.err.println("[WebsiteIntegration] Error in RegisteredAccsWebsite task: ${e.message}")
}
}
}, 15, 90, TimeUnit.SECONDS)
}
fun stop() {
task?.cancel(true)
task = null
}
fun update() {
val pass = WebsiteConfig.password
if (pass.isEmpty()) return
val charactersPath = Paths.get("data", "characters")
val amount = if (Files.exists(charactersPath) && Files.isDirectory(charactersPath)) {
try {
val stream = Files.list(charactersPath)
try {
stream.count().toInt()
} finally {
stream.close()
}
} catch (e: Exception) {
File("data/characters/").list()?.size ?: 0
}
} else {
0
}
WebsiteService.sendGetRequest("accountsregistered.php", mapOf(
"pass" to pass,
"amount" to amount,
"world" to Constants.WORLD
))
}
}
@@ -0,0 +1,26 @@
package plugin.integration.website
import com.rs2.event.EventContext
import com.rs2.event.EventSubscriber
import com.rs2.event.SubscribesTo
import com.rs2.event.impl.ServerConfigEvent
import com.rs2.game.players.Player
@SubscribesTo(ServerConfigEvent::class)
class WebsiteConfig : EventSubscriber<ServerConfigEvent> {
companion object {
var password: String = ""
var playersOnline: Boolean = false
var registeredAccounts: Boolean = false
}
override fun subscribe(context: EventContext, player: Player?, event: ServerConfigEvent) {
val legacyMaster = event.getBoolean("website_integration", false)
event.group("website") { g ->
g.registerString("password", password, "websitepass") { password = it }
g.registerBoolean("players_online", legacyMaster, "website_players_online") { playersOnline = it }
g.registerBoolean("registered_accounts", legacyMaster, "website_registered_accounts") { registeredAccounts = it }
}
}
}
@@ -0,0 +1,72 @@
package plugin.integration.website
import com.rs2.Constants
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
object WebsiteService {
private var scheduler: ScheduledExecutorService? = null
private var warnedNoPassword = false
@Synchronized
fun getScheduler(): ScheduledExecutorService {
val current = scheduler
if (current == null || current.isShutdown) {
val newExec = Executors.newSingleThreadScheduledExecutor { r ->
Thread(r, "WebsiteIntegrationWorker").apply { isDaemon = true }
}
scheduler = newExec
return newExec
}
return current
}
@Synchronized
fun stop() {
scheduler?.shutdownNow()
scheduler = null
}
fun warnMissingPassword() {
if (!warnedNoPassword) {
warnedNoPassword = true
println("No Website Password Set So Website Integration Tasks Stopped")
}
}
fun sendGetRequest(endpoint: String, params: Map<String, Any>): Boolean {
val baseUrl = Constants.WEBSITE_LINK.trimEnd('/')
if (baseUrl.isEmpty()) return false
val query = params.entries.joinToString("&") { (key, value) ->
val encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name())
"$key=$encodedValue"
}
val fullUrl = "$baseUrl/$endpoint?$query"
return try {
val url = URL(fullUrl)
val connection = url.openConnection() as? HttpURLConnection ?: return false
connection.apply {
requestMethod = "GET"
connectTimeout = 5000
readTimeout = 5000
setRequestProperty("User-Agent", "2006Scape-Server")
inputStream.use { it.readBytes() }
}
true
} catch (e: Exception) {
if (Constants.SERVER_DEBUG) {
e.printStackTrace()
} else {
System.err.println("[WebsiteIntegration] Failed to update $endpoint: ${e.message}")
}
false
}
}
}
@@ -0,0 +1,17 @@
package plugin.integration.website
import com.rs2.event.EventContext
import com.rs2.event.EventSubscriber
import com.rs2.event.SubscribesTo
import com.rs2.event.impl.ServerShutdownEvent
import com.rs2.game.players.Player
@SubscribesTo(ServerShutdownEvent::class)
class WebsiteShutdownSubscriber : EventSubscriber<ServerShutdownEvent> {
override fun subscribe(context: EventContext, player: Player?, event: ServerShutdownEvent) {
PlayersOnlineWebsite.stop()
RegisteredAccsWebsite.stop()
WebsiteService.stop()
}
}
@@ -0,0 +1,16 @@
package plugin.integration.website
import com.rs2.event.EventContext
import com.rs2.event.EventSubscriber
import com.rs2.event.SubscribesTo
import com.rs2.event.impl.ServerStartupEvent
import com.rs2.game.players.Player
@SubscribesTo(ServerStartupEvent::class)
class WebsiteStartupSubscriber : EventSubscriber<ServerStartupEvent> {
override fun subscribe(context: EventContext, player: Player?, event: ServerStartupEvent) {
PlayersOnlineWebsite.start()
RegisteredAccsWebsite.start()
}
}