| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 5 | import net.minecraft.commands.CommandSourceStack;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 | import org.jspecify.annotations.Nullable;
|
| 8 |
|
| 9 | public class CommandResponseTracker<Element> {
|
| 10 | private int totalValue;
|
| 11 | private @Nullable Element onlyElement;
|
| 12 | private int elementCount;
|
| 13 | private @Nullable Element onlyNonZeroElement;
|
| 14 | private int nonZeroElementCount;
|
| 15 |
|
| 16 | public static <T> CommandResponseTracker<T> create() {
|
| 17 | return new CommandResponseTracker();
|
| 18 | }
|
| 19 |
|
| 20 | public void track(final Element element, final int value) {
|
| 21 | this.totalValue += value;
|
| 22 | if (++this.elementCount == 1) {
|
| 23 | this.onlyElement = element;
|
| 24 | } else {
|
| 25 | this.onlyElement = null;
|
| 26 | }
|
| 27 |
|
| 28 | if (value != 0) {
|
| 29 | if (++this.nonZeroElementCount == 1) {
|
| 30 | this.onlyNonZeroElement = element;
|
| 31 | } else {
|
| 32 | this.onlyNonZeroElement = null;
|
| 33 | }
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public void track(final Element element, final boolean value) {
|
| 38 | this.track(element, value ? 1 : 0);
|
| 39 | }
|
| 40 |
|
| 41 | public void track(final Element element) {
|
| 42 | this.track(element, 1);
|
| 43 | }
|
| 44 |
|
| 45 | private @Nullable Element firstElement(final CommandResponseTracker.ElementType type) {
|
| 46 | return (Element)(switch (type) {
|
| 47 | case ANY -> this.onlyElement;
|
| 48 | case NON_ZERO -> this.onlyNonZeroElement;
|
| 49 | });
|
| 50 | }
|
| 51 |
|
| 52 | private int elementCount(final CommandResponseTracker.ElementType type) {
|
| 53 | return switch (type) {
|
| 54 | case ANY -> this.elementCount;
|
| 55 | case NON_ZERO -> this.nonZeroElementCount;
|
| 56 | };
|
| 57 | }
|
| 58 |
|
| 59 | public int totalValue() {
|
| 60 | return this.totalValue;
|
| 61 | }
|
| 62 |
|
| 63 | public static <Element> CommandResponseTracker.Messages<Element> messages(
|
| 64 | final CommandResponseTracker.SingleHandler<Component, Element> onSingle, final CommandResponseTracker.MultipleHandler<Component> onMultiple
|
| 65 | ) {
|
| 66 | return new CommandResponseTracker.Messages<>(null, new CommandResponseTracker.Dispatch<>(onSingle, onMultiple));
|
| 67 | }
|
| 68 |
|
| 69 | public static <Element> CommandResponseTracker.Messages<Element> messages(
|
| 70 | final CommandResponseTracker.ErrorHandler onZero,
|
| 71 | final CommandResponseTracker.SingleHandler<Component, Element> onSingle,
|
| 72 | final CommandResponseTracker.MultipleHandler<Component> onMultiple
|
| 73 | ) {
|
| 74 | return new CommandResponseTracker.Messages<>(onZero, new CommandResponseTracker.Dispatch<>(onSingle, onMultiple));
|
| 75 | }
|
| 76 |
|
| 77 | public static <Element> CommandResponseTracker.Messages<Element> messages(
|
| 78 | final SimpleCommandExceptionType onZero,
|
| 79 | final CommandResponseTracker.SingleHandler<Component, Element> onSingle,
|
| 80 | final CommandResponseTracker.MultipleHandler<Component> onMultiple
|
| 81 | ) {
|
| 82 | return new CommandResponseTracker.Messages<>(onZero::create, new CommandResponseTracker.Dispatch<>(onSingle, onMultiple));
|
| 83 | }
|
| 84 |
|
| 85 | public <Result> Result dispatch(final CommandResponseTracker.ElementType elementType, final CommandResponseTracker.Dispatch<Result, ? super Element> dispatch) {
|
| 86 | Element firstElement = this.firstElement(elementType);
|
| 87 | return firstElement != null
|
| 88 | ? dispatch.onSingle().create(firstElement, this.totalValue)
|
| 89 | : dispatch.onMultiple().create(this.elementCount(elementType), this.totalValue);
|
| 90 | }
|
| 91 |
|
| 92 | public int sendFeedback(
|
| 93 | final CommandSourceStack sourceStack,
|
| 94 | final boolean broadcast,
|
| 95 | final CommandResponseTracker.ElementType elementType,
|
| 96 | final CommandResponseTracker.Messages<? super Element> messages
|
| 97 | ) throws CommandSyntaxException {
|
| 98 | messages.throwIfZero(this.elementCount(elementType));
|
| 99 | sourceStack.sendSuccess(() -> this.dispatch(elementType, messages.onSuccess()), broadcast);
|
| 100 | return this.totalValue;
|
| 101 | }
|
| 102 |
|
| 103 | public int sendFeedback(final CommandSourceStack sourceStack, final boolean broadcast, final CommandResponseTracker.Messages<? super Element> messages) throws CommandSyntaxException {
|
| 104 | return this.sendFeedback(sourceStack, broadcast, CommandResponseTracker.ElementType.NON_ZERO, messages);
|
| 105 | }
|
| 106 |
|
| 107 | public static <Element, Arg> CommandResponseTracker.MessagesWithArg<Element, Arg> messages(
|
| 108 | final CommandResponseTracker.SingleHandlerWithArg<Component, Element, Arg> onSingle,
|
| 109 | final CommandResponseTracker.MultipleHandlerWithArg<Component, Arg> onMultiple
|
| 110 | ) {
|
| 111 | return new CommandResponseTracker.MessagesWithArg<>(null, new CommandResponseTracker.DispatchWithArg<>(onSingle, onMultiple));
|
| 112 | }
|
| 113 |
|
| 114 | public static <Element, Arg> CommandResponseTracker.MessagesWithArg<Element, Arg> messages(
|
| 115 | final CommandResponseTracker.ErrorHandlerWithArg<Arg> onZero,
|
| 116 | final CommandResponseTracker.SingleHandlerWithArg<Component, Element, Arg> onSingle,
|
| 117 | final CommandResponseTracker.MultipleHandlerWithArg<Component, Arg> onMultiple
|
| 118 | ) {
|
| 119 | return new CommandResponseTracker.MessagesWithArg<>(onZero, new CommandResponseTracker.DispatchWithArg<>(onSingle, onMultiple));
|
| 120 | }
|
| 121 |
|
| 122 | public static <Element, Arg> CommandResponseTracker.MessagesWithArg<Element, Arg> messages(
|
| 123 | final SimpleCommandExceptionType onZero,
|
| 124 | final CommandResponseTracker.SingleHandlerWithArg<Component, Element, Arg> onSingle,
|
| 125 | final CommandResponseTracker.MultipleHandlerWithArg<Component, Arg> onMultiple
|
| 126 | ) {
|
| 127 | return new CommandResponseTracker.MessagesWithArg<>(var1 -> onZero.create(), new CommandResponseTracker.DispatchWithArg<>(onSingle, onMultiple));
|
| 128 | }
|
| 129 |
|
| 130 | public <Result, Arg> Result dispatch(
|
| 131 | final CommandResponseTracker.ElementType elementType, final CommandResponseTracker.DispatchWithArg<Result, ? super Element, Arg> dispatch, final Arg argument
|
| 132 | ) {
|
| 133 | Element firstElement = this.firstElement(elementType);
|
| 134 | return firstElement != null
|
| 135 | ? dispatch.onSingle().create(firstElement, this.totalValue, argument)
|
| 136 | : dispatch.onMultiple().create(this.elementCount(elementType), this.totalValue, argument);
|
| 137 | }
|
| 138 |
|
| 139 | public <Arg> int sendFeedback(
|
| 140 | final CommandSourceStack sourceStack,
|
| 141 | final boolean broadcast,
|
| 142 | final CommandResponseTracker.ElementType elementType,
|
| 143 | final CommandResponseTracker.MessagesWithArg<? super Element, Arg> messages,
|
| 144 | final Arg argument
|
| 145 | ) throws CommandSyntaxException {
|
| 146 | messages.throwIfZero(this.elementCount(elementType), argument);
|
| 147 | sourceStack.sendSuccess(() -> this.dispatch(elementType, messages.onSuccess(), argument), broadcast);
|
| 148 | return this.totalValue;
|
| 149 | }
|
| 150 |
|
| 151 | public <Arg> int sendFeedback(
|
| 152 | final CommandSourceStack sourceStack,
|
| 153 | final boolean broadcast,
|
| 154 | final CommandResponseTracker.MessagesWithArg<? super Element, Arg> messages,
|
| 155 | final Arg argument
|
| 156 | ) throws CommandSyntaxException {
|
| 157 | return this.sendFeedback(sourceStack, broadcast, CommandResponseTracker.ElementType.NON_ZERO, messages, argument);
|
| 158 | }
|
| 159 |
|
| 160 | public static <Element, Arg0, Arg1> CommandResponseTracker.MessagesWithArgs<Element, Arg0, Arg1> messages(
|
| 161 | final CommandResponseTracker.SingleHandlerWithArgs<Component, Element, Arg0, Arg1> onSingle,
|
| 162 | final CommandResponseTracker.MultipleHandlerWithArgs<Component, Arg0, Arg1> onMultiple
|
| 163 | ) {
|
| 164 | return new CommandResponseTracker.MessagesWithArgs<>(null, new CommandResponseTracker.DispatchWithArgs<>(onSingle, onMultiple));
|
| 165 | }
|
| 166 |
|
| 167 | public static <Element, Arg0, Arg1> CommandResponseTracker.MessagesWithArgs<Element, Arg0, Arg1> messages(
|
| 168 | final CommandResponseTracker.ErrorHandlerWithArgs<Arg0, Arg1> onZero,
|
| 169 | final CommandResponseTracker.SingleHandlerWithArgs<Component, Element, Arg0, Arg1> onSingle,
|
| 170 | final CommandResponseTracker.MultipleHandlerWithArgs<Component, Arg0, Arg1> onMultiple
|
| 171 | ) {
|
| 172 | return new CommandResponseTracker.MessagesWithArgs<>(onZero, new CommandResponseTracker.DispatchWithArgs<>(onSingle, onMultiple));
|
| 173 | }
|
| 174 |
|
| 175 | public static <Element, Arg0, Arg1> CommandResponseTracker.MessagesWithArgs<Element, Arg0, Arg1> messages(
|
| 176 | final SimpleCommandExceptionType onZero,
|
| 177 | final CommandResponseTracker.SingleHandlerWithArgs<Component, Element, Arg0, Arg1> onSingle,
|
| 178 | final CommandResponseTracker.MultipleHandlerWithArgs<Component, Arg0, Arg1> onMultiple
|
| 179 | ) {
|
| 180 | return new CommandResponseTracker.MessagesWithArgs<>((var1, var2) -> onZero.create(), new CommandResponseTracker.DispatchWithArgs<>(onSingle, onMultiple));
|
| 181 | }
|
| 182 |
|
| 183 | public <Result, Arg0, Arg1> Result dispatch(
|
| 184 | final CommandResponseTracker.ElementType elementType,
|
| 185 | final CommandResponseTracker.DispatchWithArgs<Result, ? super Element, Arg0, Arg1> messages,
|
| 186 | final Arg0 argument0,
|
| 187 | final Arg1 argument1
|
| 188 | ) {
|
| 189 | Element firstElement = this.firstElement(elementType);
|
| 190 | return firstElement != null
|
| 191 | ? messages.onSingle().create(firstElement, this.totalValue, argument0, argument1)
|
| 192 | : messages.onMultiple().create(this.elementCount(elementType), this.totalValue, argument0, argument1);
|
| 193 | }
|
| 194 |
|
| 195 | public <Arg0, Arg1> int sendFeedback(
|
| 196 | final CommandSourceStack sourceStack,
|
| 197 | final boolean broadcast,
|
| 198 | final CommandResponseTracker.ElementType elementType,
|
| 199 | final CommandResponseTracker.MessagesWithArgs<? super Element, Arg0, Arg1> messages,
|
| 200 | final Arg0 argument0,
|
| 201 | final Arg1 argument1
|
| 202 | ) throws CommandSyntaxException {
|
| 203 | messages.throwIfZero(this.elementCount(elementType), argument0, argument1);
|
| 204 | sourceStack.sendSuccess(() -> this.dispatch(elementType, messages.onSuccess(), argument0, argument1), broadcast);
|
| 205 | return this.totalValue;
|
| 206 | }
|
| 207 |
|
| 208 | public <Arg0, Arg1> int sendFeedback(
|
| 209 | final CommandSourceStack sourceStack,
|
| 210 | final boolean broadcast,
|
| 211 | final CommandResponseTracker.MessagesWithArgs<? super Element, Arg0, Arg1> messages,
|
| 212 | final Arg0 argument0,
|
| 213 | final Arg1 argument1
|
| 214 | ) throws CommandSyntaxException {
|
| 215 | return this.sendFeedback(sourceStack, broadcast, CommandResponseTracker.ElementType.NON_ZERO, messages, argument0, argument1);
|
| 216 | }
|
| 217 |
|
| 218 | public record Dispatch<Result, Element>(
|
| 219 | CommandResponseTracker.SingleHandler<Result, Element> onSingle, CommandResponseTracker.MultipleHandler<Result> onMultiple
|
| 220 | ) {
|
| 221 | }
|
| 222 |
|
| 223 | public record DispatchWithArg<Result, Element, Arg>(
|
| 224 | CommandResponseTracker.SingleHandlerWithArg<Result, Element, Arg> onSingle, CommandResponseTracker.MultipleHandlerWithArg<Result, Arg> onMultiple
|
| 225 | ) {
|
| 226 | }
|
| 227 |
|
| 228 | public record DispatchWithArgs<Result, Element, Arg0, Arg1>(
|
| 229 | CommandResponseTracker.SingleHandlerWithArgs<Result, Element, Arg0, Arg1> onSingle,
|
| 230 | CommandResponseTracker.MultipleHandlerWithArgs<Result, Arg0, Arg1> onMultiple
|
| 231 | ) {
|
| 232 | }
|
| 233 |
|
| 234 | public enum ElementType {
|
| 235 | ANY,
|
| 236 | NON_ZERO;
|
| 237 | }
|
| 238 |
|
| 239 | @FunctionalInterface
|
| 240 | public interface ErrorHandler {
|
| 241 | CommandSyntaxException get();
|
| 242 | }
|
| 243 |
|
| 244 | @FunctionalInterface
|
| 245 | public interface ErrorHandlerWithArg<Arg> {
|
| 246 | CommandSyntaxException get(Arg argument);
|
| 247 | }
|
| 248 |
|
| 249 | @FunctionalInterface
|
| 250 | public interface ErrorHandlerWithArgs<Arg0, Arg1> {
|
| 251 | CommandSyntaxException get(Arg0 argument0, Arg1 argument1);
|
| 252 | }
|
| 253 |
|
| 254 | public record Messages<Element>(CommandResponseTracker.@Nullable ErrorHandler onZero, CommandResponseTracker.Dispatch<Component, Element> onSuccess) {
|
| 255 | public void throwIfZero(final int value) throws CommandSyntaxException {
|
| 256 | if (this.onZero != null && value == 0) {
|
| 257 | throw this.onZero.get();
|
| 258 | }
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | public record MessagesWithArg<Element, Arg>(
|
| 263 | CommandResponseTracker.@Nullable ErrorHandlerWithArg<Arg> onZero, CommandResponseTracker.DispatchWithArg<Component, Element, Arg> onSuccess
|
| 264 | ) {
|
| 265 | public void throwIfZero(final int value, final Arg argument) throws CommandSyntaxException {
|
| 266 | if (this.onZero != null && value == 0) {
|
| 267 | throw this.onZero.get(argument);
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | public record MessagesWithArgs<Element, Arg0, Arg1>(
|
| 273 | CommandResponseTracker.@Nullable ErrorHandlerWithArgs<Arg0, Arg1> onZero, CommandResponseTracker.DispatchWithArgs<Component, Element, Arg0, Arg1> onSuccess
|
| 274 | ) {
|
| 275 | public void throwIfZero(final int value, final Arg0 argument0, final Arg1 argument1) throws CommandSyntaxException {
|
| 276 | if (this.onZero != null && value == 0) {
|
| 277 | throw this.onZero.get(argument0, argument1);
|
| 278 | }
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | @FunctionalInterface
|
| 283 | public interface MultipleHandler<Result> {
|
| 284 | Result create(int elementCount, int totalValue);
|
| 285 | }
|
| 286 |
|
| 287 | @FunctionalInterface
|
| 288 | public interface MultipleHandlerWithArg<Result, Arg> {
|
| 289 | Result create(int elementCount, int totalValue, Arg argument);
|
| 290 | }
|
| 291 |
|
| 292 | @FunctionalInterface
|
| 293 | public interface MultipleHandlerWithArgs<Result, Arg0, Arg1> {
|
| 294 | Result create(int elementCount, int totalValue, Arg0 argument0, Arg1 argument1);
|
| 295 | }
|
| 296 |
|
| 297 | @FunctionalInterface
|
| 298 | public interface SingleHandler<Result, Element> {
|
| 299 | Result create(Element element, int totalValue);
|
| 300 | }
|
| 301 |
|
| 302 | @FunctionalInterface
|
| 303 | public interface SingleHandlerWithArg<Result, Element, Arg> {
|
| 304 | Result create(Element element, int totalValue, Arg argument);
|
| 305 | }
|
| 306 |
|
| 307 | @FunctionalInterface
|
| 308 | public interface SingleHandlerWithArgs<Result, Element, Arg0, Arg1> {
|
| 309 | Result create(Element element, int totalValue, Arg0 argument0, Arg1 argument1);
|
| 310 | }
|
| 311 | }
|
| 312 |
|