generated from devOS-Sanity-Edition/fabric-nautical-template-kt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
239 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/one/devos/nautical/exposeplayers/mixin/PlayerListMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package one.devos.nautical.exposeplayers.mixin; | ||
|
||
import net.minecraft.server.players.PlayerList; | ||
import net.minecraft.stats.ServerStatsCounter; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Mixin(PlayerList.class) | ||
public interface PlayerListMixin { | ||
|
||
@Accessor("stats") | ||
default Map<UUID, ServerStatsCounter> getStats() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
22 changes: 0 additions & 22 deletions
22
src/main/java/one/devos/nautical/template/mixin/ExampleMixin.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/one/devos/nautical/exposeplayers/ExposePlayers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package one.devos.nautical.exposeplayers | ||
|
||
import gay.asoji.fmw.FMW | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.netty.* | ||
import net.fabricmc.api.ModInitializer | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents | ||
import one.devos.nautical.exposeplayers.plugins.configureRouting | ||
import one.devos.nautical.exposeplayers.plugins.configureSerialization | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
object ExposePlayers : ModInitializer { | ||
val MOD_ID: String = "exposeplayers" | ||
val LOGGER: Logger = LoggerFactory.getLogger(MOD_ID) | ||
val MOD_NAME: String = FMW.getName(MOD_ID) | ||
|
||
private var server: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>? = null | ||
|
||
override fun onInitialize() { | ||
LOGGER.info("[${MOD_NAME}] Starting up ExposePlayers") | ||
|
||
ServerLifecycleEvents.SERVER_STARTED.register { server -> | ||
this.server?.stop() | ||
this.server = embeddedServer(Netty, port = 64589, host = "0.0.0.0", module = { | ||
configureRouting(server) | ||
configureSerialization() | ||
}).start(wait = false) | ||
} | ||
|
||
ServerLifecycleEvents.SERVER_STOPPED.register { server -> | ||
this.server?.stop() | ||
this.server = null | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/one/devos/nautical/exposeplayers/plugins/Routing.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package one.devos.nautical.exposeplayers.plugins | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.Serializable | ||
import net.minecraft.core.registries.BuiltInRegistries | ||
import net.minecraft.server.MinecraftServer | ||
import net.minecraft.stats.Stats | ||
import one.devos.nautical.exposeplayers.utils.UUIDSerializer | ||
import one.devos.nautical.exposeplayers.utils.getPlayerStatsByUuid | ||
import java.util.UUID | ||
|
||
fun Application.configureRouting(server: MinecraftServer) { | ||
routing { | ||
get { | ||
call.respondText("Hello world!") | ||
} | ||
|
||
get("/players") { | ||
call.respond(PlayersEndpointResponse( | ||
server.playerCount, | ||
server.playerList.players.map { player -> | ||
PlayerInfo(player.uuid, player.name.string) | ||
} | ||
)) | ||
} | ||
|
||
get("/players/stats/{player_name}") { | ||
val playerName = call.parameters["player_name"] | ||
if (playerName == null) { | ||
call.respond(HttpStatusCode.BadRequest) | ||
return@get | ||
} | ||
|
||
val playerUuid = server.profileCache?.get(playerName)?.get()?.id | ||
if (playerUuid == null) { | ||
call.respond(HttpStatusCode.BadRequest) | ||
return@get | ||
} | ||
|
||
val statsCounter = server.playerList.getPlayerStatsByUuid(playerUuid, playerName) | ||
// call.respond(BuiltInRegistries.STAT_TYPE.map { statisticType -> { | ||
// statisticType.flatMap | ||
// }) | ||
|
||
|
||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
private data class PlayerInfo( | ||
@Serializable(with = UUIDSerializer::class) val uuid: UUID, | ||
val name: String | ||
) | ||
|
||
@Serializable | ||
private data class PlayersEndpointResponse( | ||
val count: Int, | ||
val players: List<@Contextual PlayerInfo> | ||
) | ||
|
||
@Serializable | ||
private data class PlayerStatistic( | ||
val displayName: String, | ||
val value: Any | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/one/devos/nautical/exposeplayers/plugins/Serialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package one.devos.nautical.exposeplayers.plugins | ||
|
||
import io.ktor.serialization.kotlinx.json.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.util.reflect.* | ||
|
||
fun Application.configureSerialization() { | ||
install(ContentNegotiation) { | ||
json() | ||
} | ||
|
||
// routing { | ||
// get("/json/kotlinx-serialization") { | ||
// call.respond( | ||
// mapOf("hello" to "world") | ||
// ) | ||
// } | ||
// } | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/one/devos/nautical/exposeplayers/utils/UUIDSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package one.devos.nautical.exposeplayers.utils | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import java.util.UUID | ||
|
||
object UUIDSerializer : KSerializer<UUID> { | ||
|
||
override val descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING) | ||
|
||
override fun deserialize(decoder: Decoder): UUID { | ||
return UUID.fromString(decoder.decodeString()) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: UUID) { | ||
encoder.encodeString(value.toString()) | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/one/devos/nautical/exposeplayers/utils/extensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package one.devos.nautical.exposeplayers.utils | ||
|
||
import net.minecraft.FileUtil | ||
import net.minecraft.server.players.PlayerList | ||
import net.minecraft.stats.ServerStatsCounter | ||
import net.minecraft.world.level.storage.LevelResource | ||
import one.devos.nautical.exposeplayers.mixin.PlayerListMixin | ||
import java.io.File | ||
import java.util.UUID | ||
|
||
val PlayerList.stats: MutableMap<UUID, ServerStatsCounter> | ||
get() = (this as PlayerListMixin).stats | ||
|
||
fun PlayerList.getPlayerStatsByUuid(uuid: UUID, name: String): ServerStatsCounter { | ||
var statsCounter = stats[uuid] | ||
if (statsCounter == null) { | ||
val directory = this.server.getWorldPath(LevelResource.PLAYER_STATS_DIR).toFile() | ||
val uuidFile = File(directory, "$uuid.json") | ||
if (!uuidFile.exists()) { | ||
val playerNameFile = File(directory, "$name.json") | ||
val path = playerNameFile.toPath() | ||
if (FileUtil.isPathNormalized(path) && FileUtil.isPathPortable(path) && path.startsWith(directory.path) && playerNameFile.isFile) { | ||
playerNameFile.renameTo(uuidFile); | ||
} | ||
} | ||
|
||
statsCounter = ServerStatsCounter(this.server, uuidFile) | ||
stats[uuid] = statsCounter | ||
} | ||
|
||
return statsCounter!! | ||
} |
39 changes: 0 additions & 39 deletions
39
src/main/kotlin/one/devos/nautical/template/TemplateMod.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/main/kotlin/one/devos/nautical/template/client/TemplateModClient.kt
This file was deleted.
Oops, something went wrong.
File renamed without changes
Oops, something went wrong.
d49f2e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my computer was dying so hard while pushing this help @BluSpring @Deftu