Skip to content

Adding an Attribute Page

CleverNucleus edited this page Jun 22, 2021 · 2 revisions

Adding an Attribute Page

This page covers how to add your own attribute page to display our newly created attribute, Max Mana.

Firstly, we need to create our attribute page - we'll call it MagicPage.java:

@Environment(EnvType.CLIENT)
public class MagicPage extends PageScreen {
    public MagicPage(HandledScreen<?> parent, ScreenHandler handler, PlayerInventory inventory) {
        // The super constructor includes the Heading displayed at the top left of the page, and the tab icon
        super(parent, handler, inventory, new TranslatableText("gui.examplemod.page.magic"), new ItemStack(Items.DIAMOND));
    }
    
    @Override
    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
        // This is where we would draw text
        // If you have tooltips, you would draw them here (after text)
    }
    
    @Override
    public void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
        // This is where we would draw textures - the background is already drawn
        // We would also draw buttons here (after textures)
    }
    
    @Override
    protected void init() {
        // if you choose to override this init() method, you need to call the super first
	super.init();
        
        // Here you would add buttons
    }
}

Secondly, we need a client entry point; we'll call it ClientExampleMod.java:

public final class ClientExampleMod implements ClientModInitializer {
    // Our page key
    public static final Identifier MAGIC_PAGE = new Identifier(ExampleMod.MODID, "magic_page");
    
    @Override
    public void onInitializeClient() {
        // Register our page
        PageRegistry.register(MAGIC_PAGE, MagicPage::new);
    }
}