99import com .mojang .brigadier .arguments .ArgumentType ;
1010import com .mojang .brigadier .context .CommandContext ;
1111import com .mojang .brigadier .exceptions .CommandSyntaxException ;
12- import com .mojang .brigadier .exceptions .DynamicCommandExceptionType ;
12+ import com .mojang .brigadier .exceptions .SimpleCommandExceptionType ;
1313import com .mojang .brigadier .suggestion .Suggestions ;
1414import com .mojang .brigadier .suggestion .SuggestionsBuilder ;
1515import net .minecraft .enchantment .Enchantment ;
2121import java .util .concurrent .CompletableFuture ;
2222
2323public class EnchantmentLevelArgumentType implements ArgumentType <Integer > {
24+ private static final SimpleCommandExceptionType INVALID_LEVEL = new SimpleCommandExceptionType (Text .literal ("Level must be at least 1" ));
2425 private final String enchantmentArgName ;
2526
26-
2727 public EnchantmentLevelArgumentType (String enchantmentArgName ) {
2828 this .enchantmentArgName = enchantmentArgName ;
2929 }
@@ -32,18 +32,14 @@ public static EnchantmentLevelArgumentType enchantmentLevel(String enchantmentAr
3232 return new EnchantmentLevelArgumentType (enchantmentArgName );
3333 }
3434
35-
3635 @ Override
3736 public Integer parse (StringReader reader ) throws CommandSyntaxException {
3837 int start = reader .getCursor ();
3938 int level = reader .readInt ();
4039
4140 if (level < 1 ) {
4241 reader .setCursor (start );
43- throw new CommandSyntaxException (
44- new DynamicCommandExceptionType (obj -> Text .literal ("Level must be at least 1" )),
45- Text .literal ("Level must be at least 1" )
46- );
42+ throw INVALID_LEVEL .createWithContext (reader );
4743 }
4844
4945 return level ;
@@ -59,27 +55,28 @@ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> cont
5955 int maxLevel = enchantment .value ().getMaxLevel ();
6056 String enchantName = enchantment .value ().description ().getString ();
6157
62- // Build suggestions based on max level
63- // Suggest 1 through maxLevel
64- for (int i = 1 ; i <= Math .min (maxLevel , 10 ); i ++) {
65- builder .suggest (i );
66- }
67-
68- // TODO: this isn't working, only the above suggestions show up; overengineering?
6958 // Add a tooltip showing the valid range
7059 String remaining = builder .getRemaining ();
7160 if (!remaining .isEmpty ()) {
7261 try {
7362 int typedLevel = Integer .parseInt (remaining );
7463 if (typedLevel > maxLevel ) {
7564 // Show error in suggestions
76- builder .suggest (maxLevel , Text .literal ("§c" + enchantName + " max: " + maxLevel ));
65+ builder .suggest (maxLevel , Text .literal ("§c" + enchantName + " max level : " + maxLevel ));
7766 }
67+
68+ return builder .buildFuture ();
7869 } catch (NumberFormatException ignored ) {
7970 // Command handler highlights invalid input
8071 }
8172 }
8273
74+ // Build suggestions based on max level
75+ // Suggest 1 through maxLevel
76+ for (int i = 1 ; i <= Math .min (maxLevel , 10 ); i ++) {
77+ builder .suggest (i );
78+ }
79+
8380 return builder .buildFuture ();
8481 } catch (Exception e ) {
8582 return Suggestions .empty ();
0 commit comments