Skip to content

Commit 170e8d1

Browse files
author
pybsh
committed
⚡ Remove duplicates
1 parent 8d5e579 commit 170e8d1

3 files changed

Lines changed: 34 additions & 127 deletions

File tree

src/main/kotlin/cc/dashify/plugin/manager/PlayerManager.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ object PlayerManager {
2121
fun getPlayerList(): HashMap<String, Any> {
2222
val players = arrayListOf<HashMap<String, Any>>()
2323
plugin.server.onlinePlayers.forEach { players.add(hashMapOf("uuid" to it.uniqueId, "name" to it.name)) }
24-
2524
return hashMapOf("players" to players)
2625
}
2726

@@ -34,9 +33,8 @@ object PlayerManager {
3433
fun getPlayerInfo(playerUuid: String): HashMap<String, Any?> {
3534
val result = HashMap<String, Any?>()
3635

37-
try {
38-
UUID.fromString(playerUuid)
39-
} catch (e: IllegalArgumentException) {
36+
try { UUID.fromString(playerUuid) }
37+
catch (e: IllegalArgumentException) {
4038
result["statusCode"] = HttpStatusCode.BadRequest
4139
result["error"] = "invalid UUID"
4240
return result
@@ -69,9 +67,8 @@ object PlayerManager {
6967
suspend fun managePlayer(type: String, playerUid: String, reasonContext: String?): HashMap<String, Any> {
7068
val result = HashMap<String, Any>()
7169

72-
try {
73-
UUID.fromString(playerUid)
74-
} catch (e: IllegalArgumentException) {
70+
try { UUID.fromString(playerUid) }
71+
catch (e: IllegalArgumentException) {
7572
result["statusCode"] = HttpStatusCode.BadRequest
7673
result["error"] = "invalid UUID"
7774
return result

src/main/kotlin/cc/dashify/plugin/manager/WorldManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ object WorldManager {
1111
fun getWorldsList(): HashMap<String, Any> {
1212
val worlds = arrayListOf<HashMap<String, Any>>()
1313
plugin.server.worlds.forEach { worlds.add(hashMapOf("uuid" to it.uid, "name" to it.name)) }
14-
1514
return hashMapOf("worlds" to worlds)
1615
}
1716

src/main/kotlin/cc/dashify/plugin/router/Dashify.kt

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import io.ktor.http.*
1010
import io.ktor.serialization.jackson.*
1111
import io.ktor.server.application.*
1212
import io.ktor.server.plugins.contentnegotiation.*
13-
import io.ktor.server.plugins.cors.routing.*
1413
import io.ktor.server.request.*
1514
import io.ktor.server.response.*
1615
import io.ktor.server.routing.*
@@ -36,62 +35,45 @@ fun Application.dashify() {
3635
jackson {}
3736
}
3837

39-
routing {
40-
get("/") {
41-
if (!checkIsEnabled()) return@get call.respond(
38+
suspend fun checkError(call: ApplicationCall): Boolean {
39+
if (!checkIsEnabled()) {
40+
call.respond(
4241
HttpStatusCode.fromValue(418),
4342
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
4443
)
45-
val authHeader = call.request.headers["Authorization"]
46-
authHeader ?: return@get call.respond(
44+
return true
45+
}
46+
val authHeader = call.request.headers["Authorization"]
47+
if (authHeader == null) {
48+
call.respond(
4749
HttpStatusCode.Unauthorized,
4850
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
4951
)
50-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
51-
return@get call.respond(
52-
HttpStatusCode.Unauthorized,
53-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
54-
)
55-
}
52+
return true
53+
}
54+
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
55+
call.respond(
56+
HttpStatusCode.Unauthorized,
57+
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
58+
)
59+
return true
60+
}
5661

62+
return false
63+
}
64+
65+
routing {
66+
get("/") {
67+
if(checkError(call)) { return@get }
5768
call.respond(HttpStatusCode.OK, hashMapOf("status" to "ok"))
5869
}
5970

6071
get("/worlds") {
61-
if (!checkIsEnabled()) return@get call.respond(
62-
HttpStatusCode.fromValue(418),
63-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
64-
)
65-
val authHeader = call.request.headers["Authorization"]
66-
authHeader ?: return@get call.respond(
67-
HttpStatusCode.Unauthorized,
68-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
69-
)
70-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
71-
return@get call.respond(
72-
HttpStatusCode.Unauthorized,
73-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
74-
)
75-
}
76-
72+
if(checkError(call)) { return@get }
7773
call.respond(HttpStatusCode.OK, WorldManager.getWorldsList())
7874
}
7975
get("/worlds/{uuid}") {
80-
if (!checkIsEnabled()) return@get call.respond(
81-
HttpStatusCode.fromValue(418),
82-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
83-
)
84-
val authHeader = call.request.headers["Authorization"]
85-
authHeader ?: return@get call.respond(
86-
HttpStatusCode.Unauthorized,
87-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
88-
)
89-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
90-
return@get call.respond(
91-
HttpStatusCode.Unauthorized,
92-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
93-
)
94-
}
76+
if(checkError(call)) { return@get }
9577

9678
val result = WorldManager.getWorldInfo(call.parameters["uuid"]!!)
9779
if (result["error"] == null) {
@@ -102,40 +84,11 @@ fun Application.dashify() {
10284
}
10385

10486
get("/players") {
105-
if (!checkIsEnabled()) return@get call.respond(
106-
HttpStatusCode.fromValue(418),
107-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
108-
)
109-
val authHeader = call.request.headers["Authorization"]
110-
authHeader ?: return@get call.respond(
111-
HttpStatusCode.Unauthorized,
112-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
113-
)
114-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
115-
return@get call.respond(
116-
HttpStatusCode.Unauthorized,
117-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
118-
)
119-
}
120-
87+
if(checkError(call)) { return@get }
12188
call.respond(HttpStatusCode.OK, PlayerManager.getPlayerList())
12289
}
12390
get("/players/{uuid}") {
124-
if (!checkIsEnabled()) return@get call.respond(
125-
HttpStatusCode.fromValue(418),
126-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
127-
)
128-
val authHeader = call.request.headers["Authorization"]
129-
authHeader ?: return@get call.respond(
130-
HttpStatusCode.Unauthorized,
131-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
132-
)
133-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
134-
return@get call.respond(
135-
HttpStatusCode.Unauthorized,
136-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
137-
)
138-
}
91+
if(checkError(call)) { return@get }
13992

14093
val result = PlayerManager.getPlayerInfo(call.parameters["uuid"]!!)
14194
if (result["error"] == null) {
@@ -145,21 +98,7 @@ fun Application.dashify() {
14598
}
14699
}
147100
post("/players/{uuid}/kick") {
148-
if (!checkIsEnabled()) return@post call.respond(
149-
HttpStatusCode.fromValue(418),
150-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
151-
)
152-
val authHeader = call.request.headers["Authorization"]
153-
authHeader ?: return@post call.respond(
154-
HttpStatusCode.Unauthorized,
155-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
156-
)
157-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
158-
return@post call.respond(
159-
HttpStatusCode.Unauthorized,
160-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
161-
)
162-
}
101+
if(checkError(call)) { return@post }
163102

164103
val result = PlayerManager.managePlayer("kick", call.parameters["uuid"]!!, call.receiveText())
165104
if (result["error"] == null) {
@@ -169,21 +108,7 @@ fun Application.dashify() {
169108
}
170109
}
171110
post("/players/{uuid}/ban") {
172-
if (!checkIsEnabled()) return@post call.respond(
173-
HttpStatusCode.fromValue(418),
174-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
175-
)
176-
val authHeader = call.request.headers["Authorization"]
177-
authHeader ?: return@post call.respond(
178-
HttpStatusCode.Unauthorized,
179-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
180-
)
181-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
182-
return@post call.respond(
183-
HttpStatusCode.Unauthorized,
184-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
185-
)
186-
}
111+
if(checkError(call)) { return@post }
187112

188113
val result = PlayerManager.managePlayer("ban", call.parameters["uuid"]!!, call.receiveText())
189114
if (result["error"] == null) {
@@ -193,21 +118,7 @@ fun Application.dashify() {
193118
}
194119
}
195120
get("/stats") {
196-
if (!checkIsEnabled()) return@get call.respond(
197-
HttpStatusCode.fromValue(418),
198-
hashMapOf("status" to "I'm a tea pot :3", "detail" to "server disabled plugin.")
199-
)
200-
val authHeader = call.request.headers["Authorization"]
201-
authHeader ?: return@get call.respond(
202-
HttpStatusCode.Unauthorized,
203-
hashMapOf("status" to "Unauthorized", "detail" to "Authorization header not found.")
204-
)
205-
if (authHeader != "Bearer ${ConfigHandler["key"].toString()}") {
206-
return@get call.respond(
207-
HttpStatusCode.Unauthorized,
208-
hashMapOf("status" to "Unauthorized", "detail" to "Invalid key.")
209-
)
210-
}
121+
if(checkError(call)) { return@get }
211122

212123
val stats = SystemManager.getSysInfo()
213124
stats["jvm"] = RuntimeManager.getMemory()

0 commit comments

Comments
 (0)