User Tools

Site Tools


tutorial:command_exceptions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:command_exceptions [2023/11/18 11:38] solidblocktutorial:command_exceptions [2024/04/15 06:43] (current) solidblock
Line 5: Line 5:
 There are two type of command exceptions: There are two type of command exceptions:
   * **''CommandSyntaxException''**: provided by Brigadier. It should have an exception type, and created through ''create(...)'' or ''createWithContext(...)''. It does not belong to ''RuntimeException'', so when thrown, it must be caught properly, or added in the method signature.   * **''CommandSyntaxException''**: provided by Brigadier. It should have an exception type, and created through ''create(...)'' or ''createWithContext(...)''. It does not belong to ''RuntimeException'', so when thrown, it must be caught properly, or added in the method signature.
-  * **''<yarn class_2164>''**: provided in Minecraft, and used in less cases. It belongs to ''RuntimeException''. You can directly create it with a ''Text'' as parameter.+  * **''<yarn class_2164>'' (removed since 1.20.3)**: provided in Minecraft, and used in less cases. It belongs to ''RuntimeException''. You can directly create it with a ''Text'' as parameter.
  
 The two main types of ''CommandSyntaxException'' are dynamic and simple exception types, of which you can call ''create()'' to create the exception to throw it. These exceptions also allow you to specify the context in which the exception was thrown using ''createWithContext(ImmutableStringReader)'', which builds the error message to point to where on the inputted command line the error occured. The two main types of ''CommandSyntaxException'' are dynamic and simple exception types, of which you can call ''create()'' to create the exception to throw it. These exceptions also allow you to specify the context in which the exception was thrown using ''createWithContext(ImmutableStringReader)'', which builds the error message to point to where on the inputted command line the error occured.
Line 18: Line 18:
   
         if(random.nextBoolean()) { // If heads succeed.         if(random.nextBoolean()) { // If heads succeed.
-            ctx.getSource().sendFeedback(Text.translatable("coin.flip.heads"), true);+            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
             return Command.SINGLE_SUCCESS;             return Command.SINGLE_SUCCESS;
         }         }
Line 25: Line 25:
     }));     }));
 </code> </code>
 +:!: ''CommandException'' is removed since 1.20.3.
  
 ===== Using CommandSyntaxException ===== ===== Using CommandSyntaxException =====
Line 40: Line 41:
  
           if (random.nextBoolean()) { // If heads succeed.           if (random.nextBoolean()) { // If heads succeed.
-            ctx.getSource().sendFeedback(Text.translatable("coin.flip.heads"), true);+            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
             return Command.SINGLE_SUCCESS;             return Command.SINGLE_SUCCESS;
           }           }
Line 62: Line 63:
  
           if (random.nextBoolean()) { // If heads succeed.           if (random.nextBoolean()) { // If heads succeed.
-            ctx.getSource().sendFeedback(Text.translatable("coin.flip.heads"), true);+            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
             return Command.SINGLE_SUCCESS;             return Command.SINGLE_SUCCESS;
           }           }
tutorial/command_exceptions.1700307512.txt.gz · Last modified: 2023/11/18 11:38 by solidblock