|
| 1 | +package com.github.itsempa.nautilus.commands.brigadier.arguments |
| 2 | + |
| 3 | +import at.hannibal2.skyhanni.utils.NeuInternalName |
| 4 | +import com.github.itsempa.nautilus.commands.brigadier.BrigadierUtils |
| 5 | +import com.github.itsempa.nautilus.commands.brigadier.BrigadierUtils.readOptionalDoubleQuotedString |
| 6 | +import com.mojang.brigadier.LiteralMessage |
| 7 | +import com.mojang.brigadier.StringReader |
| 8 | +import com.mojang.brigadier.arguments.ArgumentType |
| 9 | +import com.mojang.brigadier.context.CommandContext |
| 10 | +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType |
| 11 | +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType |
| 12 | +import com.mojang.brigadier.suggestion.Suggestions |
| 13 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder |
| 14 | +import java.util.concurrent.CompletableFuture |
| 15 | + |
| 16 | +private typealias ParsingFail = BrigadierUtils.ItemParsingFail |
| 17 | + |
| 18 | +// TODO: Maybe add greedy argument support |
| 19 | +sealed class InternalNameArgumentType : ArgumentType<NeuInternalName> { |
| 20 | + |
| 21 | + protected open val showWhenEmpty: Boolean = false |
| 22 | + |
| 23 | + private val unknownValueException = DynamicCommandExceptionType { input -> |
| 24 | + LiteralMessage("Unknown item '$input'.") |
| 25 | + } |
| 26 | + |
| 27 | + private val disallowedValueException = DynamicCommandExceptionType { input -> |
| 28 | + LiteralMessage("Disallowed item '$input'.") |
| 29 | + } |
| 30 | + |
| 31 | + private val emptyValueException = SimpleCommandExceptionType { "Empty item name provided." } |
| 32 | + |
| 33 | + protected fun parseString(input: String, reader: StringReader): NeuInternalName { |
| 34 | + val result = BrigadierUtils.parseItem(input, isValidItem = ::isValidItem) |
| 35 | + return when (result) { |
| 36 | + is NeuInternalName -> result |
| 37 | + ParsingFail.DISALLOWED_ITEM -> throw disallowedValueException.createWithContext(reader, input) |
| 38 | + ParsingFail.UNKNOWN_ITEM -> throw unknownValueException.createWithContext(reader, input) |
| 39 | + ParsingFail.EMPTY -> throw emptyValueException.createWithContext(reader) |
| 40 | + else -> throw IllegalArgumentException("Unexpected item parsing result: $result") |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + protected open fun isValidItem(item: NeuInternalName): Boolean = true |
| 45 | + |
| 46 | + private open class ItemName : InternalNameArgumentType() { |
| 47 | + override fun parse(reader: StringReader): NeuInternalName { |
| 48 | + val input = reader.readOptionalDoubleQuotedString() |
| 49 | + return parseString(input, reader) |
| 50 | + } |
| 51 | + |
| 52 | + override fun <S : Any?> listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): CompletableFuture<Suggestions> { |
| 53 | + return BrigadierUtils.parseItemNameTabComplete( |
| 54 | + builder.remainingLowerCase, |
| 55 | + builder, |
| 56 | + showWhenEmpty = showWhenEmpty, |
| 57 | + isValidItem = ::isValidItem, |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private open class InternalName : InternalNameArgumentType() { |
| 63 | + override fun parse(reader: StringReader): NeuInternalName { |
| 64 | + val input = reader.readOptionalDoubleQuotedString() |
| 65 | + return parseString(input, reader) |
| 66 | + } |
| 67 | + |
| 68 | + override fun <S : Any?> listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): CompletableFuture<Suggestions> { |
| 69 | + return BrigadierUtils.parseInternalNameTabComplete( |
| 70 | + builder.remaining, |
| 71 | + builder, |
| 72 | + showWhenEmpty = showWhenEmpty, |
| 73 | + isValidItem = ::isValidItem, |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Suppress("unused") |
| 79 | + companion object { |
| 80 | + fun itemName(): InternalNameArgumentType = ItemName() |
| 81 | + |
| 82 | + fun itemName(isValid: (NeuInternalName) -> Boolean): InternalNameArgumentType { |
| 83 | + return object : ItemName() { |
| 84 | + private val isValid: (NeuInternalName) -> Boolean = isValid |
| 85 | + override fun isValidItem(item: NeuInternalName): Boolean = this.isValid(item) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + fun itemName(showWhenEmpty: Boolean, isValid: (NeuInternalName) -> Boolean): InternalNameArgumentType { |
| 90 | + return object : ItemName() { |
| 91 | + override val showWhenEmpty: Boolean = showWhenEmpty |
| 92 | + private val isValid: (NeuInternalName) -> Boolean = isValid |
| 93 | + override fun isValidItem(item: NeuInternalName): Boolean = this.isValid(item) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fun itemName(allowed: Collection<NeuInternalName>, showWhenEmpty: Boolean = false): InternalNameArgumentType { |
| 98 | + return object : ItemName() { |
| 99 | + override val showWhenEmpty: Boolean = showWhenEmpty |
| 100 | + private val set = allowed.toSet() |
| 101 | + override fun isValidItem(item: NeuInternalName): Boolean = item in set |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fun internalName(): InternalNameArgumentType = InternalName() |
| 106 | + |
| 107 | + fun internalName(isValid: (NeuInternalName) -> Boolean): InternalNameArgumentType { |
| 108 | + return object : InternalName() { |
| 109 | + private val isValid: (NeuInternalName) -> Boolean = isValid |
| 110 | + override fun isValidItem(item: NeuInternalName): Boolean = this.isValid(item) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fun internalName(showWhenEmpty: Boolean, isValid: (NeuInternalName) -> Boolean): InternalNameArgumentType { |
| 115 | + return object : InternalName() { |
| 116 | + override val showWhenEmpty: Boolean = showWhenEmpty |
| 117 | + private val isValid: (NeuInternalName) -> Boolean = isValid |
| 118 | + override fun isValidItem(item: NeuInternalName): Boolean = this.isValid(item) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + fun internalName(allowed: Collection<NeuInternalName>, showWhenEmpty: Boolean = false): InternalNameArgumentType { |
| 123 | + return object : InternalName() { |
| 124 | + override val showWhenEmpty: Boolean = showWhenEmpty |
| 125 | + private val set = allowed.toSet() |
| 126 | + override fun isValidItem(item: NeuInternalName): Boolean = item in set |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments