Skip to content

Adding textures

mineLdiver edited this page Jul 25, 2023 · 2 revisions

Adding individual atlas textures

StationAPI overrides both /terrain.png and /gui/items.png atlases with its own extendable atlas which merges the former two together.

To register an individual block or item texture to this atlas, you simply need to put it under a discoverable folder. Such are src/main/resources/assets/my_modid/stationapi/textures/block/, src/main/resources/assets/my_modid/stationapi/textures/item/ and any subfolder of these.

Assigning textures to blocks and items

Since vanilla handles textures in integer IDs corresponding to their coordinates on their atlases, we have to use something to maintain an individual texture path -> integer ID relationship.

In StationAPI, we can use TextureRegisterEvent and Atlases classes, or a helper method .setTexture() in item class.

public class MyTextures {
    @Entrypoint.ModID
    final ModID MODID = Null.get();

    @EventListener
    void registerTextures(TextureRegisterEvent event) {
        MyItems.myItem.setTexture(MODID.id("item/my_item.png"));

        ExpandableAtlas terrain = Atlases.getTerrain();
        MyBlocks.myBlock.texture = terrain.addTexture(MODID.id("block/my_block.png")).index;
    }
}

Make sure to only call item.setTexture() and atlas.addTexture() within TextureRegisterEvent listeners. This is important, because this event is also called when the current texture pack is changed, reinitializing all textures.

But, texture indices can be saved to fields, for example, if you need to provide different textures for different block sides.

public class MyTextures {
    @Entrypoint.ModID
    final ModID MODID = Null.get();

    public static int myBlockBottom;
    public static int myBlockTop;
    public static int myBlockFront;

    @EventListener
    void registerTextures(TextureRegisterEvent event) {
        MyItems.myItem.setTexture(MODID.id("item/my_item.png"));

        ExpandableAtlas terrain = Atlases.getTerrain();
        MyBlocks.myBlock.texture = terrain.addTexture(MODID.id("block/my_block.png")).index;
        myBlockBottom = terrain.addTexture(MODID.id("block/my_block_bottom.png")).index;
        myBlockTop = terrain.addTexture(MODID.id("block/my_block_top.png")).index;
        myBlockFront = terrain.addTexture(MODID.id("block/my_block_front.png")).index;
    }
}