Skip to content

Using the model

Wesley1808 edited this page Nov 15, 2023 · 4 revisions

1. Loading the model

To load a model file into the actual game, you can use the utility methods in the AjLoader class.
Models required in mods should be retrieved by ID, but its also possible to load files from the server.
Note that these models are not cached by Nylon. They are safe to reuse and you should cache them yourself.

public static final AjModel MODEL_FROM_ID = AjLoader.require(new ResourceLocation("namespace", "path"));
public static final AjModel MODEL_FROM_FILEPATH = AjLoader.require("config/models/example.json");

These AjModels can be used with Nylon's custom implementations of Polymer's ElementHolder.
Anything extending AbstractAjHolder supports it.

2. Entity Attachments

Nylon has a lot of built-in support for using models attached to entities.
Writing a custom entity with model works mostly the same as writing one normally.
You just have to implement AjEntity and initialize a EntityHolder in the constructor with the model.

Here's a simplified example of the Redstone Golem in the Test Mod. Note that you will also have to register custom mobs.

public class RedstoneGolem extends Monster implements AjEntity {
    public static final ResourceLocation ID = new ResourceLocation("provim", "redstone_golem");
    public static final AjModel MODEL = AjLoader.require(ID);
    private final EntityHolder<RedstoneGolem> holder;

    public RedstoneGolem(EntityType<? extends Monster> type, Level level) {
        super(type, level);

        // Creates model holder with out of the box support for most LivingEntity features.
        // Note that it is always possible to write your own or override some of their methods.
        this.holder = new LivingEntityHolder<>(this, MODEL);

        // Attaches the holder to this entity in Polymer.
        // Make sure that ticking is enabled, as it is required for model updates.
        EntityAttachment.ofTicking(this.holder, this);
    }

    @Override
    public EntityHolder<RedstoneGolem> getHolder() {
        return this.holder;
    }
}

3. Other Attachments

While Nylon is mostly targeted on entities, it is possible to attach model holders to other things.
Polymer provides a few of its own other attachments, like ChunkAttachment and BlockBoundAttachment.
You can attach your model holder to all of these aswell.

4. Variants and Animations

One thing Nylon can't automatically do for you is decide when certain animations or variants should be used.
To do this, every holder provides an Animator and VariantController, which can for example be used like this:

Animator animator = holder.getAnimator();

// Completely stops the animation named 'idle'.
animator.stopAnimation("idle");

// Pauses the animation named 'walk'.
animator.pauseAnimation("walk");

// Plays the melee animation with priority 10.
animator.playAnimation("melee", 10); 

VariantController controller = holder.getVariantController();

// Sets the variant to the variant named 'hurt' if the current variant is default.
if (controller.isDefaultVariant()) {
    controller.setVariant("hurt");
}