From ce3332a24ab1d673e8fca7790656947244564a94 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Fri, 15 Mar 2024 20:49:45 +0800 Subject: [PATCH] update code --- .../cn/rtast/viewdimension/ViewDimension.kt | 50 +++++++++++++++++++ .../mixin/ServerPlayNetworkMixin.java | 45 +++++++++++++++++ .../mixin/ServerPlayerMixin.java | 42 ++++++++++++++++ src/main/resources/fabric.mod.json | 10 ++-- src/main/resources/viewdimension.mixins.json | 15 ++++++ 5 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/rtast/viewdimension/ViewDimension.kt create mode 100644 src/main/java/cn/rtast/viewdimension/mixin/ServerPlayNetworkMixin.java create mode 100644 src/main/java/cn/rtast/viewdimension/mixin/ServerPlayerMixin.java create mode 100644 src/main/resources/viewdimension.mixins.json diff --git a/src/main/java/cn/rtast/viewdimension/ViewDimension.kt b/src/main/java/cn/rtast/viewdimension/ViewDimension.kt new file mode 100644 index 0000000..6f5c7bc --- /dev/null +++ b/src/main/java/cn/rtast/viewdimension/ViewDimension.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2024 RTAkland + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package cn.rtast.viewdimension + +import net.fabricmc.api.ModInitializer +import net.minecraft.server.network.ServerPlayerEntity +import net.minecraft.text.Text +import net.minecraft.util.Formatting + +class ViewDimension : ModInitializer { + + companion object { + + fun replacePlayerName(player: ServerPlayerEntity): Text { + val dimension = player.world.dimension.effects.path + val oldName = player.name.string + val dimensionText = when (dimension) { + "overworld" -> Text.literal(" ") + .styled { it.withColor(Formatting.GREEN).withItalic(true) } + "the_nether" -> Text.literal(" ") + .styled { it.withColor(Formatting.DARK_RED).withItalic(true) } + "the_end" -> Text.literal(" ") + .styled { it.withColor(Formatting.DARK_PURPLE).withItalic(true) } + else -> Text.literal(" <$dimension>") + .styled { it.withColor(Formatting.GRAY).withItalic(true) } + } + return Text.literal(oldName).append(dimensionText) + } + + } + + override fun onInitialize() { + println("ViewDimension loaded!") + } +} \ No newline at end of file diff --git a/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayNetworkMixin.java b/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayNetworkMixin.java new file mode 100644 index 0000000..75c4075 --- /dev/null +++ b/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayNetworkMixin.java @@ -0,0 +1,45 @@ +/* + * Copyright 2024 RTAkland + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package cn.rtast.viewdimension.mixin; + +import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerPlayNetworkHandler.class) +public abstract class ServerPlayNetworkMixin { + + @Shadow + public ServerPlayerEntity player; + + @Shadow public abstract void tick(); + + @Inject(method = "tick", at = @At("HEAD")) + public void tick(CallbackInfo ci) { + this.player.server + .getPlayerManager() + .sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.UPDATE_DISPLAY_NAME, player)); + } + +} diff --git a/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayerMixin.java b/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayerMixin.java new file mode 100644 index 0000000..ba5eacc --- /dev/null +++ b/src/main/java/cn/rtast/viewdimension/mixin/ServerPlayerMixin.java @@ -0,0 +1,42 @@ +/* + * Copyright 2024 RTAkland + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package cn.rtast.viewdimension.mixin; + +import cn.rtast.viewdimension.ViewDimension; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(ServerPlayerEntity.class) +public class ServerPlayerMixin { + + @Unique + public ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; + + + @Inject(method = "getPlayerListName", at = @At("RETURN"), cancellable = true) + public void replacePlayerName(CallbackInfoReturnable cir) { + Text newName = ViewDimension.Companion.replacePlayerName(this.player); + cir.setReturnValue(newName); + } + +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c2ee50c..2d83952 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -12,19 +12,21 @@ }, "license": "Apache-2.0", "icon": "assets/viewdimension/icon.png", - "environment": "server", + "environment": "*", "entrypoints": { - "server": [ + "main": [ { "adapter": "kotlin", "value": "cn.rtast.viewdimension.ViewDimension" } ] }, + "mixins": [ + "viewdimension.mixins.json" + ], "depends": { "fabricloader": ">=${loader_version}", "fabric": "*", - "minecraft": "${minecraft_version}", - "java": "17" + "minecraft": "${minecraft_version}" } } diff --git a/src/main/resources/viewdimension.mixins.json b/src/main/resources/viewdimension.mixins.json new file mode 100644 index 0000000..1c7c5f6 --- /dev/null +++ b/src/main/resources/viewdimension.mixins.json @@ -0,0 +1,15 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "cn.rtast.viewdimension.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "ServerPlayerMixin", + "ServerPlayNetworkMixin" + ], + "client": [ + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file