| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 5 | import com.mojang.brigadier.tree.LiteralCommandNode;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.MessageArgument;
|
| 10 | import net.minecraft.network.chat.ChatType;
|
| 11 | import net.minecraft.network.chat.ClickEvent;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.chat.HoverEvent;
|
| 14 | import net.minecraft.network.chat.OutgoingChatMessage;
|
| 15 | import net.minecraft.network.chat.PlayerChatMessage;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.server.players.PlayerList;
|
| 19 | import net.minecraft.world.entity.Entity;
|
| 20 | import net.minecraft.world.scores.PlayerTeam;
|
| 21 |
|
| 22 | public class TeamMsgCommand {
|
| 23 | private static final Style SUGGEST_STYLE = Style.EMPTY
|
| 24 | .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.type.team.hover")))
|
| 25 | .withClickEvent(new ClickEvent.SuggestCommand("/teammsg "));
|
| 26 | private static final SimpleCommandExceptionType ERROR_NOT_ON_TEAM = new SimpleCommandExceptionType(Component.translatable("commands.teammsg.failed.noteam"));
|
| 27 |
|
| 28 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 29 | LiteralCommandNode<CommandSourceStack> msg = dispatcher.register(
|
| 30 | Commands.literal("teammsg")
|
| 31 | .then(
|
| 32 | Commands.argument("message", MessageArgument.message())
|
| 33 | .executes(
|
| 34 | c -> {
|
| 35 | CommandSourceStack source = c.getSource();
|
| 36 | Entity entity = source.getEntityOrException();
|
| 37 | PlayerTeam team = entity.getTeam();
|
| 38 | if (team == null) {
|
| 39 | throw ERROR_NOT_ON_TEAM.create();
|
| 40 | }
|
| 41 |
|
| 42 | List<ServerPlayer> receivers = source.getServer()
|
| 43 | .getPlayerList()
|
| 44 | .getPlayers()
|
| 45 | .stream()
|
| 46 | .filter(receiver -> receiver == entity || receiver.getTeam() == team)
|
| 47 | .toList();
|
| 48 | if (!receivers.isEmpty()) {
|
| 49 | MessageArgument.resolveChatMessage(c, "message", message -> sendMessage(source, entity, team, receivers, message));
|
| 50 | }
|
| 51 |
|
| 52 | return receivers.size();
|
| 53 | }
|
| 54 | )
|
| 55 | )
|
| 56 | );
|
| 57 | dispatcher.register(Commands.literal("tm").redirect(msg));
|
| 58 | }
|
| 59 |
|
| 60 | private static void sendMessage(
|
| 61 | final CommandSourceStack source, final Entity entity, final PlayerTeam team, final List<ServerPlayer> receivers, final PlayerChatMessage message
|
| 62 | ) {
|
| 63 | Component teamName = team.getFormattedDisplayName().withStyle(SUGGEST_STYLE);
|
| 64 | ChatType.Bound incomingChatType = ChatType.bind(ChatType.TEAM_MSG_COMMAND_INCOMING, source).withTargetName(teamName);
|
| 65 | ChatType.Bound outgoingChatType = ChatType.bind(ChatType.TEAM_MSG_COMMAND_OUTGOING, source).withTargetName(teamName);
|
| 66 | OutgoingChatMessage tracked = OutgoingChatMessage.create(message);
|
| 67 | boolean wasFullyFiltered = false;
|
| 68 |
|
| 69 | for (ServerPlayer teamPlayer : receivers) {
|
| 70 | ChatType.Bound chatType = teamPlayer == entity ? outgoingChatType : incomingChatType;
|
| 71 | boolean filtered = source.shouldFilterMessageTo(teamPlayer);
|
| 72 | teamPlayer.sendChatMessage(tracked, filtered, chatType);
|
| 73 | wasFullyFiltered |= filtered && message.isFullyFiltered();
|
| 74 | }
|
| 75 |
|
| 76 | if (wasFullyFiltered) {
|
| 77 | source.sendSystemMessage(PlayerList.CHAT_FILTERED_FULL);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|