TeamMsgCommand.java

net.minecraft.server.commands.TeamMsgCommand

信息

  • 全限定名:net.minecraft.server.commands.TeamMsgCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/TeamMsgCommand.java
  • 起始行号:L22
  • 职责:

    TODO

字段/常量

  • SUGGEST_STYLE

    • 类型: Style
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • ERROR_NOT_ON_TEAM

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L28

  • 方法名:register
  • 源码定位:L28
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static void sendMessage(CommandSourceStack source, Entity entity, PlayerTeam team, List<ServerPlayer> receivers, PlayerChatMessage message) @ L60

  • 方法名:sendMessage
  • 源码定位:L60
  • 返回类型:void
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • entity: Entity
  • team: PlayerTeam
  • receivers: List
  • message: PlayerChatMessage

说明:

TODO

代码

public class TeamMsgCommand {
    private static final Style SUGGEST_STYLE = Style.EMPTY
        .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.type.team.hover")))
        .withClickEvent(new ClickEvent.SuggestCommand("/teammsg "));
    private static final SimpleCommandExceptionType ERROR_NOT_ON_TEAM = new SimpleCommandExceptionType(Component.translatable("commands.teammsg.failed.noteam"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        LiteralCommandNode<CommandSourceStack> msg = dispatcher.register(
            Commands.literal("teammsg")
                .then(
                    Commands.argument("message", MessageArgument.message())
                        .executes(
                            c -> {
                                CommandSourceStack source = c.getSource();
                                Entity entity = source.getEntityOrException();
                                PlayerTeam team = entity.getTeam();
                                if (team == null) {
                                    throw ERROR_NOT_ON_TEAM.create();
                                } else {
                                    List<ServerPlayer> receivers = source.getServer()
                                        .getPlayerList()
                                        .getPlayers()
                                        .stream()
                                        .filter(receiver -> receiver == entity || receiver.getTeam() == team)
                                        .toList();
                                    if (!receivers.isEmpty()) {
                                        MessageArgument.resolveChatMessage(c, "message", message -> sendMessage(source, entity, team, receivers, message));
                                    }
 
                                    return receivers.size();
                                }
                            }
                        )
                )
        );
        dispatcher.register(Commands.literal("tm").redirect(msg));
    }
 
    private static void sendMessage(CommandSourceStack source, Entity entity, PlayerTeam team, List<ServerPlayer> receivers, PlayerChatMessage message) {
        Component teamName = team.getFormattedDisplayName().withStyle(SUGGEST_STYLE);
        ChatType.Bound incomingChatType = ChatType.bind(ChatType.TEAM_MSG_COMMAND_INCOMING, source).withTargetName(teamName);
        ChatType.Bound outgoingChatType = ChatType.bind(ChatType.TEAM_MSG_COMMAND_OUTGOING, source).withTargetName(teamName);
        OutgoingChatMessage tracked = OutgoingChatMessage.create(message);
        boolean wasFullyFiltered = false;
 
        for (ServerPlayer teamPlayer : receivers) {
            ChatType.Bound chatType = teamPlayer == entity ? outgoingChatType : incomingChatType;
            boolean filtered = source.shouldFilterMessageTo(teamPlayer);
            teamPlayer.sendChatMessage(tracked, filtered, chatType);
            wasFullyFiltered |= filtered && message.isFullyFiltered();
        }
 
        if (wasFullyFiltered) {
            source.sendSystemMessage(PlayerList.CHAT_FILTERED_FULL);
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.literal()
  • MessageArgument

    • 引用位置: 方法调用
    • 关联成员: MessageArgument.message(), MessageArgument.resolveChatMessage()
  • ChatType

    • 引用位置: 方法调用
    • 关联成员: ChatType.bind()
  • ClickEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: ClickEvent.SuggestCommand(), SuggestCommand()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable()
  • HoverEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: HoverEvent.ShowText(), ShowText()
  • OutgoingChatMessage

    • 引用位置: 方法调用
    • 关联成员: OutgoingChatMessage.create()
  • PlayerChatMessage

    • 引用位置: 参数
  • Style

    • 引用位置: 字段
  • ServerPlayer

    • 引用位置: 参数
  • Entity

    • 引用位置: 参数
  • PlayerTeam

    • 引用位置: 参数