首页 > 解决方案 > 如何向服务器上的所有玩家发送消息?(服务器端)

问题描述

我如何向服务器上的所有玩家发送消息?(尝试这样做时,它只输出到控制台,我相信这是因为客户端没有安装mod。)

我一直在尝试为 1.7.10 服务器制作一个模组(放入 1.7.10 模组包),它可以在线向所有玩家发送消息。我已经查过这个问题,但没有找到任何答案。

@SideOnly(Side.SERVER)
@SubscribeEvent
public void onDeath(PlayerEvent.Clone event)
{
    if (event.wasDeath) {
        final String[] messages = {"Oh boiss we got a respawner O_O", "How dare ye respawn on me?", "GAAH! You died again!", "._. Just why...", "Was taht me or waas that you? -.-","Why isn't this in hardcore mode? It should be..."};
        Random random = new Random();
        int index = random.nextInt(messages.length);
        ChatComponentText text = new ChatComponentText(messages[index]);
        ChatStyle style = new ChatStyle();
        style.setColor(EnumChatFormatting.LIGHT_PURPLE);
        text.setChatStyle(style);
         FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().sendChatMsg(text);
    System.out.println("Respawned");
    }
}

我希望服务器会向所有人发送消息,但只会输出到控制台。

标签: minecraftgradlewminecraft-forge

解决方案


一个非常快速的处理方法是为玩家加入时创建一个 EventHandler。然后将它们添加到 ArrayList。然后当他们离开时(检查踢/退出事件)。从 ArrayList 中删除它们。通过拥有一个数组列表,您可以运行它并向每个玩家发送消息。


推荐阅读