Minecraft 1122 Forge 代替玩家发送消息全体都看得见
要在Minecraft 1.12.2 Forge中代替玩家发送可见于全体的消息,你可以使用以下代码:
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.server.MinecraftServer;
import net.minecraft.entity.player.EntityPlayerMP;
// 发送全体消息
public void sendGlobalMessage(String message) {
TextComponentString textComponentString = new TextComponentString(TextFormatting.GREEN + "[全体消息] " + TextFormatting.RESET + message);
MinecraftServer.getServer().getPlayerList().sendMessage(textComponentString);
}
// 在玩家加入服务器时发送消息给全体玩家
public void sendGlobalMessageOnPlayerJoin(EntityPlayerMP player) {
String playerName = player.getName();
String message = playerName + " 加入了服务器!";
sendGlobalMessage(message);
}
在你的mod中,你可以使用sendGlobalMessage(message)方法发送全体消息。你可以在适当的时间调用该方法,例如当玩家加入服务器时调用sendGlobalMessageOnPlayerJoin(player)方法。
请确保你的mod已正确安装并运行,并且你已将以上代码正确地放置在你的mod中
原文地址: https://www.cveoy.top/t/topic/iRh7 著作权归作者所有。请勿转载和采集!