File size: 8,983 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package net.minecraft.commands.arguments;

import com.google.common.collect.Lists;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandSigningContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.commands.arguments.selector.EntitySelectorParser;
import net.minecraft.network.chat.ChatDecorator;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.FilteredText;

public class MessageArgument implements SignedArgument<MessageArgument.Message> {
    private static final Collection<String> EXAMPLES = Arrays.asList("Hello world!", "foo", "@e", "Hello @p :)");
    static final Dynamic2CommandExceptionType TOO_LONG = new Dynamic2CommandExceptionType(
        (p_325588_, p_325589_) -> Component.translatableEscape("argument.message.too_long", p_325588_, p_325589_)
    );

    public static MessageArgument message() {
        return new MessageArgument();
    }

    public static Component getMessage(CommandContext<CommandSourceStack> p_96836_, String p_96837_) throws CommandSyntaxException {
        MessageArgument.Message messageargument$message = p_96836_.getArgument(p_96837_, MessageArgument.Message.class);
        return messageargument$message.resolveComponent(p_96836_.getSource());
    }

    public static void resolveChatMessage(CommandContext<CommandSourceStack> p_249433_, String p_248718_, Consumer<PlayerChatMessage> p_249460_) throws CommandSyntaxException {
        MessageArgument.Message messageargument$message = p_249433_.getArgument(p_248718_, MessageArgument.Message.class);
        CommandSourceStack commandsourcestack = p_249433_.getSource();
        Component component = messageargument$message.resolveComponent(commandsourcestack);
        CommandSigningContext commandsigningcontext = commandsourcestack.getSigningContext();
        PlayerChatMessage playerchatmessage = commandsigningcontext.getArgument(p_248718_);
        if (playerchatmessage != null) {
            resolveSignedMessage(p_249460_, commandsourcestack, playerchatmessage.withUnsignedContent(component));
        } else {
            resolveDisguisedMessage(p_249460_, commandsourcestack, PlayerChatMessage.system(messageargument$message.text).withUnsignedContent(component));
        }
    }

    private static void resolveSignedMessage(Consumer<PlayerChatMessage> p_250000_, CommandSourceStack p_252335_, PlayerChatMessage p_249420_) {
        MinecraftServer minecraftserver = p_252335_.getServer();
        CompletableFuture<FilteredText> completablefuture = filterPlainText(p_252335_, p_249420_);
        Component component = minecraftserver.getChatDecorator().decorate(p_252335_.getPlayer(), p_249420_.decoratedContent());
        p_252335_.getChatMessageChainer().append(completablefuture, p_296325_ -> {
            PlayerChatMessage playerchatmessage = p_249420_.withUnsignedContent(component).filter(p_296325_.mask());
            p_250000_.accept(playerchatmessage);
        });
    }

    private static void resolveDisguisedMessage(Consumer<PlayerChatMessage> p_249162_, CommandSourceStack p_248759_, PlayerChatMessage p_252332_) {
        ChatDecorator chatdecorator = p_248759_.getServer().getChatDecorator();
        Component component = chatdecorator.decorate(p_248759_.getPlayer(), p_252332_.decoratedContent());
        p_249162_.accept(p_252332_.withUnsignedContent(component));
    }

    private static CompletableFuture<FilteredText> filterPlainText(CommandSourceStack p_252063_, PlayerChatMessage p_251184_) {
        ServerPlayer serverplayer = p_252063_.getPlayer();
        return serverplayer != null && p_251184_.hasSignatureFrom(serverplayer.getUUID())
            ? serverplayer.getTextFilter().processStreamMessage(p_251184_.signedContent())
            : CompletableFuture.completedFuture(FilteredText.passThrough(p_251184_.signedContent()));
    }

    public MessageArgument.Message parse(StringReader p_96834_) throws CommandSyntaxException {
        return MessageArgument.Message.parseText(p_96834_, true);
    }

    public <S> MessageArgument.Message parse(StringReader p_345550_, @Nullable S p_345556_) throws CommandSyntaxException {
        return MessageArgument.Message.parseText(p_345550_, EntitySelectorParser.allowSelectors(p_345556_));
    }

    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }

    public static record Message(String text, MessageArgument.Part[] parts) {
        Component resolveComponent(CommandSourceStack p_232197_) throws CommandSyntaxException {
            return this.toComponent(p_232197_, EntitySelectorParser.allowSelectors(p_232197_));
        }

        public Component toComponent(CommandSourceStack p_96850_, boolean p_96851_) throws CommandSyntaxException {
            if (this.parts.length != 0 && p_96851_) {
                MutableComponent mutablecomponent = Component.literal(this.text.substring(0, this.parts[0].start()));
                int i = this.parts[0].start();

                for (MessageArgument.Part messageargument$part : this.parts) {
                    Component component = messageargument$part.toComponent(p_96850_);
                    if (i < messageargument$part.start()) {
                        mutablecomponent.append(this.text.substring(i, messageargument$part.start()));
                    }

                    mutablecomponent.append(component);
                    i = messageargument$part.end();
                }

                if (i < this.text.length()) {
                    mutablecomponent.append(this.text.substring(i));
                }

                return mutablecomponent;
            } else {
                return Component.literal(this.text);
            }
        }

        public static MessageArgument.Message parseText(StringReader p_96847_, boolean p_96848_) throws CommandSyntaxException {
            if (p_96847_.getRemainingLength() > 256) {
                throw MessageArgument.TOO_LONG.create(p_96847_.getRemainingLength(), 256);
            } else {
                String s = p_96847_.getRemaining();
                if (!p_96848_) {
                    p_96847_.setCursor(p_96847_.getTotalLength());
                    return new MessageArgument.Message(s, new MessageArgument.Part[0]);
                } else {
                    List<MessageArgument.Part> list = Lists.newArrayList();
                    int i = p_96847_.getCursor();

                    while (true) {
                        int j;
                        EntitySelector entityselector;
                        while (true) {
                            if (!p_96847_.canRead()) {
                                return new MessageArgument.Message(s, list.toArray(new MessageArgument.Part[0]));
                            }

                            if (p_96847_.peek() == '@') {
                                j = p_96847_.getCursor();

                                try {
                                    EntitySelectorParser entityselectorparser = new EntitySelectorParser(p_96847_, true);
                                    entityselector = entityselectorparser.parse();
                                    break;
                                } catch (CommandSyntaxException commandsyntaxexception) {
                                    if (commandsyntaxexception.getType() != EntitySelectorParser.ERROR_MISSING_SELECTOR_TYPE
                                        && commandsyntaxexception.getType() != EntitySelectorParser.ERROR_UNKNOWN_SELECTOR_TYPE) {
                                        throw commandsyntaxexception;
                                    }

                                    p_96847_.setCursor(j + 1);
                                }
                            } else {
                                p_96847_.skip();
                            }
                        }

                        list.add(new MessageArgument.Part(j - i, p_96847_.getCursor() - i, entityselector));
                    }
                }
            }
        }
    }

    public static record Part(int start, int end, EntitySelector selector) {
        public Component toComponent(CommandSourceStack p_96861_) throws CommandSyntaxException {
            return EntitySelector.joinNames(this.selector.findEntities(p_96861_));
        }
    }
}