Skip to content

Dynamic Hitboxes

ANightDazingZoroark edited this page Jul 5, 2025 · 4 revisions

Steps

Adding dynamic hitboxes to your entity requires the following steps:

  1. Creating your hitbox.json file
  2. Linking to your Entity
  3. Creating a Hitbox Linker class
  4. Registering the Hitbox Linker class

Creating the hitbox.json file

This step requires you to install the Blockbench plugin to help visualize the hitboxes.

The first step is to create a locator. Make sure its name starts with "hitbox_", otherwise, it will not be recognized by the plugin.

Next step, click on "Tools > Edit Hitboxes". It will then open the following popup:

image

  • Hitbox refers to the hitbox to be edited
  • Size refers to the width and height of the hitbox
  • Damage Multiplier refers to by how much will the damage received be multiplied if the entity is hit by a player there
  • Affected by Animation refers to if the hitbox can or cannot be moved from animations

The positions of the hitboxes will depend on the positions of the locators.

Here's what editing them should look like:

image

Once you're happy with your hitboxes, make sure to export them by clicking on "Tools > Export Hitboxes" to the hitboxes folder in your resource pack.

If you want to edit your hitboxes further, click on "Tools > Import Hitboxes"

Linking to your Entity

  1. The first step to linking hitboxes to your entity is to make them implement IMultiHitboxUser and IAnimatable.

  2. In the constructor, add this.initializeHitboxes(this). This method from IMultiHitboxUser is what links the hitboxes to the entity.

  3. Add a global Entity[] variable. The entitys hitboxes will be stored in this array.

  4. In a method that updates every tick, preferrably onUpdate() or onLivingUpdate(), add this.updateParts(). This method from IMultiHitboxUser updates the positions of the hitboxes every tick based on the parent entitys position and look direction, as well as delete the hitboxes when the parent dies.

  5. Finally, make getMultiHitboxUser() return the entity, getParts() return the global Entity[] variable mentioned in step 3, setParts(Entity[] hitboxes) contain this.hitboxes = hitboxes, and getWorld() return this.world. They may seem redundant, but they're crucial parts of how hitboxes are managed.

Here's an example class that shows the result:

public class ExampleEntity extends EntityLiving implements IAnimatable, IMultiHitboxUser {
        private final AnimationFactory factory = new AnimationFactory(this);
        private Entity[] hitboxes = {};

        public ExampleEntity(World world) {
            super(world);
            this.setSize(1f, 1f);
            this.initializeHitboxes(this);
        }

        @Override
        public void onLivingUpdate() {
            super.onLivingUpdate();
            this.updateParts();
        }

        @Override
        public Entity getMultiHitboxUser() {
            return this;
        }

        @Override
        public Entity[] getParts() {
            return this.hitboxes;
        }

        @Override
        public void setParts(Entity[] hitboxes) {
            this.hitboxes = hitboxes;
        }

        @Override
        public World getWorld() {
            return this.world;
        }
    
        @Override
        public void registerControllers(AnimationData data) {}
    
        @Override
        public AnimationFactory getFactory() {
            return this.factory;
        }
}

Creating a Hitbox Linker class

A Hitbox Linker is where the hitbox is linked to the entity. It must extend EntityHitboxLinker<T extends IAnimatable & IMultiHitboxUser>. It must implement the following methods:

getModelLocation(T entity)

This method should return the ResourceLocation path to your .geo.json model file for this animatable.

getHitboxFileLocation(T entity)

This method should return the ResourceLocation path to your .json hitboxe file for this animatable.

Here's an example class that shows the result:

public class ExampleEntityHitboxLinker extends EntityHitboxLinker<ExampleEntity> {
    @Override
    public ResourceLocation getModelLocation(ExampleEntity entity) {
        return new ResourceLocation(RiftLib.ModID, "geo/example.geo.json");
    }

    @Override
    public ResourceLocation getHitboxFileLocation(ExampleEntity entity) {
        return new ResourceLocation(RiftLib.ModID, "hitboxes/example.json");
    }
}

Registering the Hitbox Linker class

In the FMLPreInitializationEvent event, add this method:

RiftLibLinkerRegistry.registerEntityHitboxLinker(ExampleEntity.class, new ExampleEntityHitboxLinker());

This is where the hitbox linker class is registered.

Results

image

If you followed the above steps properly, you should get an entity whose hitboxes move dynamically based on animations!

HWYLA Compat

By default, if HWYLA is in your mods folder, it will also show information about the parent entity, but the information shown will be similar to how info for vanilla mobs is displayed. If your entity has a custom HWYLA handler and you want the hitboxes to be similar to its custom HWYLA handler, you can override the hitboxUseHWYLA() method in the entity class and make it return false.

Clone this wiki locally