Minecraft 1.12.2 Forge Mod: 发送自定义消息给特定玩家
在 Minecraft 1.12.2 Forge 中,您可以使用以下代码以安装该 mod 的玩家的身份发送消息:
// 导入所需的类
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@Mod(modid = 'yourmodid', name = 'Your Mod Name', version = '1.0')
@EventBusSubscriber
public class YourModClass {
@Mod.EventHandler
public void init(FMLInitializationEvent event) {
// 初始化代码
}
@SubscribeEvent
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.player.getName().equals('YourUsername')) {
event.player.sendMessage(new TextComponentString('你是唯一安装了该 Mod 的玩家!'));
}
}
}
请确保将yourmodid替换为你的 mod 的唯一标识符,并将Your Mod Name替换为你的 mod 的名称。将YourUsername替换为你的 Minecraft 用户名。
这段代码将在玩家登录游戏时检查玩家的用户名是否与你的用户名匹配,如果匹配,则向该玩家发送一条消息。
原文地址: https://www.cveoy.top/t/topic/fQvE 著作权归作者所有。请勿转载和采集!