|
| 1 | +/* |
| 2 | + * Copyright 2023 The Quilt Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Preserve binary compatibility when moving extensions between files |
| 19 | + */ |
| 20 | +@file:JvmMultifileClass |
| 21 | +@file:JvmName("ArgumentsKt") |
| 22 | + |
| 23 | +package com.lambda.brigadier.argument |
| 24 | + |
| 25 | +import com.lambda.brigadier.* |
| 26 | +import com.lambda.brigadier.assumeSourceNotUsed |
| 27 | +import com.mojang.brigadier.arguments.ArgumentType |
| 28 | +import com.mojang.brigadier.arguments.IntegerArgumentType |
| 29 | + |
| 30 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder |
| 31 | +import net.minecraft.command.argument.TimeArgumentType |
| 32 | +import net.minecraft.command.argument.UuidArgumentType |
| 33 | +import java.util.* |
| 34 | + |
| 35 | +/** |
| 36 | + * Descriptor for a literal argument. |
| 37 | + * |
| 38 | + * Separate from [DefaultArgumentDescriptor] |
| 39 | + * to stand out more in type hints. |
| 40 | + */ |
| 41 | +object LiteralDescriptor : ArgumentDescriptor<ArgumentType<*>> |
| 42 | + |
| 43 | +/** |
| 44 | + * Reads the integer value in ticks from the |
| 45 | + * argument in the receiver [ArgumentReader]. |
| 46 | + */ |
| 47 | +@JvmName("valueTimeArg") |
| 48 | +@BrigadierDsl |
| 49 | +fun DefaultArgumentReader<TimeArgumentType>.value(): Int { |
| 50 | + return IntegerArgumentType.getInteger(context, name) |
| 51 | +} // TimeArgumentType does not provide an accessor, defaulting to int |
| 52 | + |
| 53 | +/** |
| 54 | + * Reads the [UUID] value from the |
| 55 | + * argument in the receiver [ArgumentReader]. |
| 56 | + * |
| 57 | + * @see UuidArgumentType.getUuid |
| 58 | + */ |
| 59 | +@JvmName("valueUuidArg") |
| 60 | +@BrigadierDsl |
| 61 | +fun DefaultArgumentReader<UuidArgumentType>.value(): UUID { |
| 62 | + return UuidArgumentType.getUuid(context.assumeSourceNotUsed(), name) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Creates a time argument with [name] as the parameter name. |
| 67 | + * |
| 68 | + * @see TimeArgumentType.time |
| 69 | + */ |
| 70 | +@BrigadierDsl |
| 71 | +fun <S> time( |
| 72 | + name: String, |
| 73 | + minimumTicks: Int = 0 |
| 74 | +): DefaultArgumentConstructor<S, TimeArgumentType> { |
| 75 | + return argument(name, TimeArgumentType.time(minimumTicks)) |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Creates a UUID argument with [name] as the parameter name. |
| 80 | + */ |
| 81 | +@BrigadierDsl |
| 82 | +fun <S> uuid( |
| 83 | + name: String |
| 84 | +): RequiredArgumentConstructor< |
| 85 | + S, |
| 86 | + DefaultArgumentDescriptor< |
| 87 | + UuidArgumentType> |
| 88 | + > { |
| 89 | + return argument(name, UuidArgumentType.uuid()) |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Creates a literal argument with [name] as the literal. |
| 94 | + * |
| 95 | + * Like other arguments, literal arguments create accessors, |
| 96 | + * which can be checked for presence of [optional] literals. |
| 97 | + * However, [ArgumentReader]s produced from those accessors |
| 98 | + * serve no purpose due to the argument not having a value. |
| 99 | + */ |
| 100 | +@BrigadierDsl |
| 101 | +fun <S> literal( |
| 102 | + name: String |
| 103 | +): ArgumentConstructor< |
| 104 | + S, |
| 105 | + LiteralArgumentBuilder<S>, |
| 106 | + LiteralDescriptor |
| 107 | + > { |
| 108 | + return ArgumentConstructor(LiteralArgumentBuilder.literal(name), name, LiteralDescriptor) |
| 109 | +} |
0 commit comments