Skip to content

Commit

Permalink
Remove gson parse method
Browse files Browse the repository at this point in the history
 - Update 1.0.1
  • Loading branch information
patrick-choe committed Oct 27, 2020
1 parent b642171 commit 5e2488b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
18 changes: 15 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ plugins {
}

group = "com.github.patrick-mc"
version = "1.0"
version = "1.0.1"

repositories {
maven("https://repo.maven.apache.org/maven2/")
Expand All @@ -46,7 +46,6 @@ repositories {
dependencies {
compileOnly(kotlin("stdlib-jdk8"))
compileOnly("org.spigotmc:spigot-api:1.8-R0.1-SNAPSHOT")
compileOnly("com.google.code.gson:gson:2.8.6")
implementation("com.neovisionaries:nv-websocket-client:2.10")
}

Expand Down Expand Up @@ -96,7 +95,20 @@ tasks {

create<Copy>("distJar") {
from(shadowJar)
into("W:\\Servers\\1.16.3\\plugins\\update")

val fileName = "${project.name.capitalize()}.jar"
val pluginsDir = "W:\\Servers\\1.16.3\\plugins"
val updateDir = "$pluginsDir\\update"

rename {
fileName
}

if (file("$pluginsDir\\$fileName").exists()) {
into(updateDir)
} else {
into(pluginsDir)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TwipePlugin : JavaPlugin() {
* Called on startup
*/
override fun onLoad() {
TwipePlugin.logger = logger
twipeLogger = logger
timer = Timer()
saveDefaultConfig()
twipVersion = requireNotNull(config.getString("twip-version"))
Expand All @@ -49,7 +49,7 @@ class TwipePlugin : JavaPlugin() {
}

internal companion object {
lateinit var logger: Logger
lateinit var twipeLogger: Logger
private set

lateinit var twipVersion: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.github.patrick.twipe.event.AsyncTwipDonateEvent
import com.github.patrick.twipe.plugin.TwipePlugin
import com.github.patrick.twipe.plugin.TwipePlugin.Companion.timer
import com.github.patrick.twipe.plugin.TwipePlugin.Companion.twipVersion
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.neovisionaries.ws.client.WebSocket
import com.neovisionaries.ws.client.WebSocketAdapter
Expand Down Expand Up @@ -64,18 +63,20 @@ internal class TwipeSocketClient(streamer: String, key: String) {
}.createSocket(twipAddress.format(key, twipVersion)).apply {
addListener(object : WebSocketAdapter() {
override fun onConnected(websocket: WebSocket, headers: MutableMap<String, MutableList<String>>) {
TwipePlugin.logger.info("Connected to twip - $streamer")
TwipePlugin.twipeLogger.info("Connected to twip - $streamer")
}

override fun onDisconnected(websocket: WebSocket, serverCloseFrame: WebSocketFrame, clientCloseFrame: WebSocketFrame, closedByServer: Boolean) {
TwipePlugin.logger.warning("Disconnected from Twip - $streamer")
TwipePlugin.twipeLogger.warning("Disconnected from Twip - $streamer")
TwipeSocketClient(streamer, key)
}

override fun onTextMessage(websocket: WebSocket, text: String) {
when (text.substring(0, 1).toInt()) { // engine.io protocol
0 -> { // open
val interval = parse(text.substring(1)).asJsonObject.getAsJsonPrimitive("pingInterval").asLong
val json = parser.parse(text.substring(1)).asJsonObject
val interval = json.getAsJsonPrimitive("pingInterval").asLong

timer.schedule(object : TimerTask() {
override fun run() {
websocket.sendText("2")
Expand All @@ -85,7 +86,7 @@ internal class TwipeSocketClient(streamer: String, key: String) {
4 -> { // message
when (text.substring(1, 2).toInt()) { // socket.io protocol
2 -> { // event
val json = parse(text.substring(2)).asJsonArray
val json = parser.parse(text.substring(2)).asJsonArray
when (json[0].asJsonPrimitive.asString) { // socket.io event
"new donate" -> { // twip donation
val donation = json[1].asJsonObject
Expand All @@ -109,19 +110,8 @@ internal class TwipeSocketClient(streamer: String, key: String) {
private companion object {
private const val twipAddress = "wss://io.mytwip.net/socket.io/?alertbox_key=%s&version=%s&transport=websocket"

@Suppress("DEPRECATION")
private fun parse(json: String): JsonElement {
return requireNotNull(if (newGson) {
JsonParser.parseString(json)
} else {
JsonParser().parse(json)
})
}

private val newGson by lazy {
JsonParser::class.java.constructors.any { constructor ->
constructor.getAnnotation(Deprecated::class.java) != null
}
private val parser by lazy {
JsonParser()
}
}
}

0 comments on commit 5e2488b

Please sign in to comment.