|
| 1 | +package com.eternalcode.core.litecommand.argument; |
| 2 | + |
| 3 | +import com.eternalcode.core.injector.annotations.Inject; |
| 4 | +import com.eternalcode.core.injector.annotations.lite.LiteArgument; |
| 5 | +import com.eternalcode.core.translation.Translation; |
| 6 | +import com.eternalcode.core.translation.TranslationManager; |
| 7 | +import dev.rollczi.litecommands.argument.Argument; |
| 8 | +import dev.rollczi.litecommands.argument.parser.ParseResult; |
| 9 | +import dev.rollczi.litecommands.invocation.Invocation; |
| 10 | +import dev.rollczi.litecommands.suggestion.SuggestionContext; |
| 11 | +import dev.rollczi.litecommands.suggestion.SuggestionResult; |
| 12 | +import java.util.Arrays; |
| 13 | +import org.bukkit.Material; |
| 14 | +import org.bukkit.command.CommandSender; |
| 15 | + |
| 16 | +@LiteArgument(type = Material.class) |
| 17 | +public class MaterialArgument extends AbstractViewerArgument<Material> { |
| 18 | + |
| 19 | + private static final SuggestionResult CACHED_SUGGESTIONS = Arrays.stream(Material.values()) |
| 20 | + .map(Material::name) |
| 21 | + .map(String::toLowerCase) |
| 22 | + .collect(SuggestionResult.collector()); |
| 23 | + |
| 24 | + @Inject |
| 25 | + MaterialArgument(TranslationManager translationManager) { |
| 26 | + super(translationManager); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ParseResult<Material> parse(Invocation<CommandSender> invocation, String argument, Translation translation) { |
| 31 | + Material material = Material.matchMaterial(argument); |
| 32 | + if (material == null) { |
| 33 | + return ParseResult.failure(translation.argument().noMaterial()); |
| 34 | + } |
| 35 | + return ParseResult.success(material); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public SuggestionResult suggest( |
| 40 | + Invocation<CommandSender> invocation, |
| 41 | + Argument<Material> argument, |
| 42 | + SuggestionContext context |
| 43 | + ) { |
| 44 | + return CACHED_SUGGESTIONS; |
| 45 | + } |
| 46 | +} |
0 commit comments