-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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 } | ||
} | ||
} | ||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either make it a |
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a HashMap instead. |
||
val name: String, | ||
val color: Int | ||
) |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may want to add |
||
ignoreUnknownKeys = true | ||
} | ||
|
||
val dataFolder: File get() { | ||
val file = File("config/einfachgustaf-smp/") | ||
if (!file.exists()) { | ||
file.mkdirs() | ||
} | ||
|
||
return file | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ | |
"defaultRequire": 0 | ||
}, | ||
"mixins": [ | ||
|
||
"feature.status.MixinServerPlayer" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't register server side mixins on the common side |
||
] | ||
} |
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.
Why? Just set the argument to
StringArgumentType.greedyString()
in order to deal with spaces