Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status Feature #4

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package live.einfachgustaf.mods.smp.mixins.feature.status;

import live.einfachgustaf.mods.smp.Entrypoint;
import live.einfachgustaf.mods.smp.features.StatusFeature;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerPlayer.class)
public class MixinServerPlayer {

@Inject(at = @At("RETURN"), method = "getTabListDisplayName", cancellable = true)
private void getTabListDisplayName(CallbackInfoReturnable<Component> cir) {
StatusFeature.INSTANCE.injectTablist((ServerPlayer) (Object) this, cir);
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/live/einfachgustaf/mods/smp/Entrypoint.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package live.einfachgustaf.mods.smp

import live.einfachgustaf.mods.smp.features.StatusFeature
import net.fabricmc.api.DedicatedServerModInitializer
import net.fabricmc.api.ModInitializer
import org.apache.logging.log4j.LogManager
Expand All @@ -9,7 +10,7 @@ object Entrypoint: ModInitializer, DedicatedServerModInitializer {
val logger = LogManager.getLogger("smp")

override fun onInitialize() {
// Common initialization
StatusFeature
}

override fun onInitializeServer() {
Expand Down
117 changes: 117 additions & 0 deletions src/main/kotlin/live/einfachgustaf/mods/smp/features/Status.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package live.einfachgustaf.mods.smp.features

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import live.einfachgustaf.mods.smp.Entrypoint.logger
import live.einfachgustaf.mods.smp.features.StatusRegistry.playerStatusMap
import live.einfachgustaf.mods.smp.features.StatusRegistry.readFromFile
import live.einfachgustaf.mods.smp.features.StatusRegistry.statusList
import live.einfachgustaf.mods.smp.utils.dataFolder
import live.einfachgustaf.mods.smp.utils.json
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.silkmc.silk.commands.command
import net.silkmc.silk.core.text.literalText
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
import java.io.File

object StatusRegistry {
val statusFile = File(dataFolder, "status.json")
val statusList = arrayListOf<Status>()
val playerStatusMap = hashMapOf<ServerPlayer, Status>()

fun add(status: Status) {
readFromFile()
statusList.add(status)
statusFile.writeText(json.encodeToString<List<Status>>(statusList))
}

fun remove(status: Status) {
readFromFile()
statusList.remove(status)
statusFile.writeText(json.encodeToString<List<Status>>(statusList))
}

fun readFromFile(): List<Status> {
if (!statusFile.exists()) {
statusFile.writeText(json.encodeToString<List<Status>>(listOf(
Status("ONLINE", 0x00FF00)
)))
return listOf()
}

kotlin.runCatching {
json.decodeFromString<List<Status>>(statusFile.readText()).forEach {
if (!statusList.contains(it)) {
statusList.add(it)
}
}
}

return statusList
}
}

val statusCommand = command("status") {
runs {
source.sendSystemMessage(literalText("Available: ${statusList.joinToString(", ") { it.name }}") { }) //TODO: make it prettier
}

literal("add") {
requiresPermissionLevel(2) //TODO: Optional LuckPerms Implementation
argument<String>("status") { statusArgument ->
suggestList {
statusList.filter { !it.name.contains(" ") }.map { it.name }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Just set the argument to StringArgumentType.greedyString() in order to deal with spaces

}
}
}

literal("remove") {
requiresPermissionLevel(2) //TODO: Optional LuckPerms Implementation
argument<String>("status") { statusArgument ->
suggestList {
statusList.filter { !it.name.contains(" ") }.map { it.name }
}
}
}

argument<String>("status") { statusArgument ->
suggestList {
statusList.filter { !it.name.contains(" ") }.map { it.name }
}

runs {
if (!statusList.map { it.name }.contains(statusArgument())) return@runs
playerStatusMap[source.playerOrException] = statusList.first { it.name == statusArgument() }
source.sendSystemMessage(Component.literal("Your status is now '${statusArgument()}'.")) // TODO: translateable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using the same component builder in all places.

}
}
}

object StatusFeature {

fun injectTablist(
serverPlayer: ServerPlayer,
cir: CallbackInfoReturnable<Component>
) {
logger.info("LOL CALLED")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either make it a logger.debug() or remove it

val status = playerStatusMap[serverPlayer] ?: return
cir.returnValue = literalText {
text("[")
text(status.name) { color = status.color }
text("] ")
text(serverPlayer.name)
}
}

init {
readFromFile()
statusCommand
}
}

@Serializable
data class Status(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a HashMap instead.

val name: String,
val color: Int
)
18 changes: 18 additions & 0 deletions src/main/kotlin/live/einfachgustaf/mods/smp/utils/Global.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package live.einfachgustaf.mods.smp.utils

import kotlinx.serialization.json.Json
import java.io.File

val json = Json {
prettyPrint = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may want to add isLenient = true

ignoreUnknownKeys = true
}

val dataFolder: File get() {
val file = File("config/einfachgustaf-smp/")
if (!file.exists()) {
file.mkdirs()
}

return file
}
2 changes: 1 addition & 1 deletion src/main/resources/smp.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"defaultRequire": 0
},
"mixins": [

"feature.status.MixinServerPlayer"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't register server side mixins on the common side

]
}