User Tools

Site Tools


tutorial:command_argument_types

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
Next revisionBoth sides next revision
tutorial:command_argument_types [2023/11/18 12:59] – [Specifying suggestions] solidblocktutorial:command_argument_types [2024/04/15 07:03] – [Custom argument serializer] solidblock
Line 90: Line 90:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-ArgumentTypeRegistry.registerArgumentType(new Identifier("tutorial", "uuid"), UuidArgumentType.class, ConstantArgumentSerializer.of(UuidArgumentType::uuid)); +ArgumentTypeRegistry.registerArgumentType( 
 +  new Identifier("tutorial", "uuid"), 
 +  UuidArgumentType.class, ConstantArgumentSerializer.of(UuidArgumentType::uuid)); 
 // The argument should be what will create the ArgumentType. // The argument should be what will create the ArgumentType.
 </code> </code>
Line 251: Line 253:
  
       @Override       @Override
-      public ArgumentSerializer<ExampleArgumentType, ?> getSerializer() { +      public ArgumentSerializer<ExampleArgumentType, Serializer.Properties> getSerializer() { 
-        return new Serializer();+        // Do not create a new ''Serializer'' object here. 
 +        return Serializer.this;
       }       }
     }     }
Line 258: Line 261:
 } }
 </code> </code>
 +
 +And now you can register it like this:
 +<code java>
 +ArgumentTypeRegistry.registerArgumentType(new Identifier("tutorial", "example"), ExampleArgumentType.class, new ExampleArgumentType.Serializer());
 +</code>
 +
 +==== Another possible way to define serializer ====
  
 If the argument does not require ''CommandRegistryAccess'', it itself may extend ''ArgumentSerializer.ArgumentTypeProperties'': If the argument does not require ''CommandRegistryAccess'', it itself may extend ''ArgumentSerializer.ArgumentTypeProperties'':
Line 275: Line 285:
   @Override   @Override
   public ArgumentSerializer<ExampleArgumentType, ?> getSerializer() {   public ArgumentSerializer<ExampleArgumentType, ?> getSerializer() {
-    return new Serializer();+    // always return a same object here to avoid failing to serialize. 
 +    return Serializer.INSTANCE;
   }   }
  
   public static class Serializer implements ArgumentSerializer<ExampleArgumentType, ExampleArgumentType> {   public static class Serializer implements ArgumentSerializer<ExampleArgumentType, ExampleArgumentType> {
 +    public static final Serializer INSTANCE = new Serializer();
 +    private Serializer() {}
 +  
     @Override     @Override
     public void writePacket(ExampleArgumentType properties, PacketByteBuf buf) {     public void writePacket(ExampleArgumentType properties, PacketByteBuf buf) {
Line 301: Line 315:
   }   }
 } }
 +</code>
 +
 +And now you can register it like this:
 +<code java>
 +ArgumentTypeRegistry.registerArgumentType(new Identifier("tutorial", "example"), ExampleArgumentType.class, ExampleArgumentType.Serializer.INSTANCE);
 </code> </code>
tutorial/command_argument_types.txt · Last modified: 2024/04/15 07:21 by solidblock