Skip to content

Commit

Permalink
add a codespace check in the codeclient placing
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingPig525 committed Jun 6, 2024
1 parent 20d6a7a commit eb3282f
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions KotlinFire/src/main/kotlin/io/github/flyingpig525/base/Template.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,39 +130,49 @@ open class Template<T>(
install(WebSockets)
}

fun <T> codeClientPlaceTemplate(template: Template<T>) where T : Item, T : JsonData {
fun <T> codeClientPlaceTemplate(template: Template<T>) where T : Item, T : JsonData = codeClientPlaceMultipleTemplates(listOf(template))

fun <T> codeClientPlaceMultipleTemplates(templates: List<Template<T>>, ignoreSizeWarning: Boolean = false) where T : Item, T : JsonData {
runBlocking {
codeClientHttp.webSocket(
method = HttpMethod.Get,
host = "localhost",
port = 31375
) {
val inc = incoming.receive()
if ("auth" in String(inc.data)) {
send("place ${template.getTemplateString()}")
send("place go")
if ("auth" !in String(incoming.receive().data)) {
close()
return@webSocket
}
incoming.receive()
close(CloseReason(CloseReason.Codes.NORMAL, "Function done."))
}
}
}

fun <T> codeClientPlaceMultipleTemplates(templates: List<Template<T>>) where T : Item, T : JsonData {
runBlocking {
codeClientHttp.webSocket(
method = HttpMethod.Get,
host = "localhost",
port = 31375
) {
val inc = incoming.receive()
if ("auth" in String(inc.data)) {
send("place swap")
for (temp in templates) {
send("place ${temp.getTemplateString()}")
send("size")
val size = String(incoming.receive().data)

val sizeNum = when(size) {
"BASIC" -> 50
"LARGE" -> 100
"MASSIVE" -> 300
else -> 0
}
if (sizeNum == 0) {
close()
return@webSocket
}
for (temp in templates) {
if (temp.blocks.size*2 > sizeNum && !ignoreSizeWarning) {
println("TEMPLATE PLACE ERROR\n"
+ "Template ${temp.name} may not fit in codespace, cancelling\n"
+ "Disable this by passing `true` for the `ignoreSizeWarning` parameter"
)
close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, "Template may not fit in codespace"))
return@webSocket
}
send("place go")
}

send("place swap")
for (temp in templates) {
send("place ${temp.getTemplateString()}")
}
send("place go")
incoming.receive()
close(CloseReason(CloseReason.Codes.NORMAL, "Function done."))
}
Expand Down

0 comments on commit eb3282f

Please sign in to comment.