diff --git a/plugin-playground/src/main/kotlin/ChatboxHelmets/plugin.kt b/plugin-playground/src/main/kotlin/ChatboxHelmets/plugin.kt
index 0b473b8..ef18071 100644
--- a/plugin-playground/src/main/kotlin/ChatboxHelmets/plugin.kt
+++ b/plugin-playground/src/main/kotlin/ChatboxHelmets/plugin.kt
@@ -28,15 +28,13 @@ class plugin : Plugin() {
3 to "
", // UIM
)
- // Local storage for username matches to avoid API calls
@Exposed(description = "Username to account type mappings (0=Normal, 1=IM, 2=HCIM, 3=UIM)")
private var usernameMatches = HashMap()
private var ACCOUNT_TYPE = 0
-
private var component: Component? = null
- // See JagString.java parse(), There is special encoding
+ // See JagString.java parse() in the client, There is special encoding
// and this correctly resolves specials.
val SPECIAL_ESCAPES: Map = hashMapOf(
' ' to "(P",
@@ -245,38 +243,18 @@ class plugin : Plugin() {
HashMap()
}
}
- else -> {
- // No data or unexpected format
- HashMap()
- }
+ else -> HashMap()
}
}
- fun OnKondoValueUpdated() {
- println("OnKondoValueUpdated called - current usernameMatches: $usernameMatches")
- StoreData()
- OnPluginsReloaded() //refresh the ui
- }
-
- fun getCleanUserName(): String {
- return Player.usernameInput.toString().replace(" ", "_")
- }
-
override fun OnPluginsReloaded(): Boolean {
grabConfig()
return false
}
- private fun StoreData() {
- val jsonString = gson.toJson(usernameMatches)
- println("Storing ${jsonString}")
- API.StoreData(SETTINGS_KEY, jsonString)
- }
-
override fun Draw(timeDelta: Long) {
if (component != null && component?.id == TARGET_COMPONENT_ID) {
- val username = Player.usernameInput.toString().toLowerCase()
- val modifiedStr = replaceUsernameInBytes(component!!.text.chars, username)
+ val modifiedStr = replaceUsernameInBytes(component!!.text.chars, Player.usernameInput.toString())
component?.text = JagString.parse(modifiedStr)
}
}
@@ -287,8 +265,48 @@ class plugin : Plugin() {
}
}
+ override fun OnLogin() {
+ grabConfig()
+ }
+
+ // Allow setting from the chatbox
+ override fun ProcessCommand(commandStr: String?, args: Array?) {
+ super.ProcessCommand(commandStr, args)
+ when(commandStr) {
+ "::chathelm" -> {
+ if (args != null) {
+ if(args.isEmpty()) return
+ val type = args[0].toIntOrNull() ?: return
+ ACCOUNT_TYPE = type
+ usernameMatches[getCleanUserName()] = ACCOUNT_TYPE
+ storeData()
+ }
+ }
+ }
+ }
+
+ fun OnKondoValueUpdated() {
+ storeData()
+ OnPluginsReloaded() //refresh the ui
+ }
+
+ private fun getCleanUserName(): String {
+ return Player.usernameInput.toString().replace(" ", "_")
+ }
+
+ private fun grabConfig() {
+ // Check if we already have the account type for this user
+ val cleanUsername = getCleanUserName()
+ if (usernameMatches.containsKey(cleanUsername.toLowerCase())) {
+ ACCOUNT_TYPE = usernameMatches[cleanUsername]!!
+ return
+ }
+
+ // No match found, check the hiscores (live server)
+ fetchAccountTypeFromAPI()
+ }
+
private fun fetchAccountTypeFromAPI(){
- println("Fetching Iron status from API...")
val cleanUsername = getCleanUserName()
val apiUrl = "http://api.2009scape.org:3000/hiscores/playerSkills/1/${cleanUsername.toLowerCase()}"
Thread {
@@ -315,45 +333,10 @@ class plugin : Plugin() {
private fun updatePlayerData(jsonResponse: String, username: String) {
val hiscoresResponse = gson.fromJson(jsonResponse, HiscoresResponse::class.java)
ACCOUNT_TYPE = hiscoresResponse.info.iron_mode.toInt()
- println("Resolved you are account type: $ACCOUNT_TYPE")
// Store the result in our local cache
usernameMatches[username] = ACCOUNT_TYPE
- StoreData()
- }
-
- override fun OnLogin() {
- grabConfig()
- }
-
- private fun grabConfig() {
- // Check if we already have the account type for this user
- val cleanUsername = getCleanUserName()
- if (usernameMatches.containsKey(cleanUsername.toLowerCase())) {
- ACCOUNT_TYPE = usernameMatches[cleanUsername]!!
- println("Using cached account type: $ACCOUNT_TYPE for $cleanUsername")
- return
- }
-
- // The game server doesn't tell us what account type we are.
- // Requesting from API is the easier way to tell.
- fetchAccountTypeFromAPI()
- }
-
- // Allow setting from the chatbox
- override fun ProcessCommand(commandStr: String?, args: Array?) {
- super.ProcessCommand(commandStr, args)
- when(commandStr) {
- "::chathelm" -> {
- if (args != null) {
- if(args.isEmpty()) return
- val type = args[0].toIntOrNull() ?: return
- ACCOUNT_TYPE = type
- usernameMatches[getCleanUserName()] = ACCOUNT_TYPE
- StoreData()
- }
- }
- }
+ storeData()
}
private fun replaceUsernameInBytes(bytes: ByteArray, username: String): String {
@@ -369,6 +352,11 @@ class plugin : Plugin() {
return encodeToJagStr(text)
}
+ private fun storeData() {
+ val jsonString = gson.toJson(usernameMatches)
+ API.StoreData(SETTINGS_KEY, jsonString)
+ }
+
private fun encodeToJagStr(str: String): String {
val result = StringBuilder()
for (char in str) {