your current location:首页 > news>[Anno Note Library] Add new annotations and parsers Anno Minecraft Game

[Anno Note Library] Add new annotations and parsers Anno Minecraft Game

2024-12-10 17:54:20|Myriagame |source:minecraft skins

This tutorial is set by the author to use the CC by-NC protocol.

annotation

Note category indexing

When the annotation classes require the life cycle to run, there must be @RETENTION (RetentionPolicy.runtime);

Generally, its use range is only for fields, that is, @Target (Elementtype.field);

In order to determine the type of the target field, Anno added a new annotation @Targettype (class <?> [] value)

For example: registered fuel should limit the type to ITEM, then the @Targettype of the registered fuel should be @Targettype ({item.class}).If the field is required to be repeatedly marked (such as @lang), you can Baidu @repeatable annotation.

Note attribute

If you are free, the attributes require specific analysis, please ensure that you will create new annotations in Java.

Example (fuel annotation)

@targettype (item.class)

@Target (ElementType.field)

@RETENTION (RetentionPolicy.runtime)

public @Interface Burnable {

Int value ();

}

Parser

The parser is used to handle the field instance and annotation, and the specific implementation is large in different cases.


We take fuel annotations as an example.

Exemplary example

 Public Class FuelresolVer IMPLEMEREMENTS Resolver  {

@Override

Public Void Process (target target, class <?> registration) {

If (target.Object () Instanceof item item) {{

Int Time = target.field (). Getannotation (burnable.class). Value ();

Fuelregitive.instance.add (item, time);

}

}

@Override

Public class Annoclass () {{) {

Return Burnable.class;

}

@Override

Public string name () {return "fuel";

}

}

illustrate

The implementation of the parser is also very simple. TARGET is a record class, which packages instances and fields.

PROCESS (ARG ...) is the specific processing process. The process of adding fuel here is as follows:

First judge the type of the instance, and use the new fuel registration of the Fabric API after the conversion.

Annoclass () only needs to return to the annotation class.

In MINECRAFT -.21.1 and its subsequent versions, we also need to implement name (), which should not be the same as other parsers.

register

The annotation does not need to register. The parser should be registered with Annoresolvers.regiter (ARG ...) at the top of the main class of the module.

Exemplary example

 Public Class Anno Implements Modinitializer {

Public Static Final String Mod_id = "Anno";

@Override

Public void oninitialize () {

Annoresolvers.register (ID ("fuel"), new fuelresolver ());

Annoresolvers.Resolve (annoregistration.class);

}

Public Static Identifier ID (String Path) {{

Return Identifier.of (mod_id, PATH);

}

}

Precautions

If you need the dataGeneration related parser, you can refer to the official implementation of the Anno.

The resolution of the same ID will be registered first.