Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Mar 15, 2024
1 parent 64d99a8 commit ce3332a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/main/java/cn/rtast/viewdimension/ViewDimension.kt
Original file line number Diff line number Diff line change
@@ -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(" <Overworld>")
.styled { it.withColor(Formatting.GREEN).withItalic(true) }
"the_nether" -> Text.literal(" <The Nether>")
.styled { it.withColor(Formatting.DARK_RED).withItalic(true) }
"the_end" -> Text.literal(" <The End>")
.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!")
}
}
Original file line number Diff line number Diff line change
@@ -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));
}

}
42 changes: 42 additions & 0 deletions src/main/java/cn/rtast/viewdimension/mixin/ServerPlayerMixin.java
Original file line number Diff line number Diff line change
@@ -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<Text> cir) {
Text newName = ViewDimension.Companion.replacePlayerName(this.player);
cir.setReturnValue(newName);
}

}
10 changes: 6 additions & 4 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}
15 changes: 15 additions & 0 deletions src/main/resources/viewdimension.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"required": true,
"minVersion": "0.8",
"package": "cn.rtast.viewdimension.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ServerPlayerMixin",
"ServerPlayNetworkMixin"
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit ce3332a

Please sign in to comment.