Skip to content

Create a debug page

Qboi123 edited this page Mar 9, 2022 · 1 revision

This is follow-up of the Create an extension page.

Create a page class

The page class would extend the DebugPage class.
So here is the basic class:

/**
 * Test page class, rename class name to the name desired.
 * Although the name should end with 'Page'. And start with the name of the category.
 */
public class TestPage extends DebugPage {
    public TestPage() {
        super(Main.MOD_ID, "test"); // Change 'test' to the id for the page.
    }

    @Override
    public void render(PoseStack poseStack, IDebugRenderContext ctx) {
        // Render items here.
    }
}

You probably would see the super call, it's needed for making sure it's not getting overridden by another mod by accident.
The first parameter is the mod's id, and the second is the page's id.

Draw some entries

Now it's time to draw the entries on the screen on this page.
In the render method, we have the Debug Render Context with as parameter name ctx.
We have 3 options for drawing the entries; left, top and right.
Every time we draw something at the left, it will advance a row. The same happens with top and right.
We can draw only text or an entry (name and value(s)).
So, if we want to draw only text on the left side we can add this line:

ctx.left("Example empty left string.");

And if we want to also add values to it, we can add more arguments to it like this:

ctx.left("Example left entry.", "Text", 123, false, 456.789);

We can also replace ctx.left with ctx.right and then it will render it on the right.
While ctx.top also exist, it doesn't allow for adding values to it.

An example class:

public class TestPage extends DebugPage {
    public TestPage() {
        super(Main.MOD_ID, "test");
    }

    @Override
    public void render(PoseStack poseStack, IDebugRenderContext ctx) {
        ctx.left("Example empty left string.");
        ctx.left("Example left entry (string)", "String Value LOL");
        ctx.left("Example left entry (int)", 123456789);
        ctx.left("Example left entry (float)", 12345.6789f);
        ctx.left("Example left entry (double)", 12345.6789);
        ctx.left("Example left entry (item stack)", new ItemStack(Items.TORCH, 37));
        ctx.left();
        ctx.left("Example left entry (vec3i)", new Vec3i(123, 456, 789));
        ctx.left("Example left entry (vec3)", new Vec3(12.3, 45.6, 78.9));
        ctx.left();
        ctx.left("Example left entry (item)", Items.POTATO);
        ctx.left("Example left entry (pose)", Pose.SLEEPING);
        ctx.left("Example left entry (foo)", new Foo("Test", 8192));

        ctx.right("Example right entry (boolean 1)", true);
        ctx.right();
        ctx.right("Example right entry (boolean 2)", false);

        ctx.top("Example top text 1");
        ctx.top("Example top text 2");
        ctx.top();
        ctx.top("Example top text 3");
    }
}

Register the page

Now we need to register the page in the extension class.
The only thing we need to do here is adding this line in the initPages(...) method:

evt.register(new TestPage());

So this is the extension class with only a debug page:

@AdvDebugExt(modId = Main.MOD_ID) // We expect that 'Main.MOD_ID' refers to the id of the compatibilty mod.
public class AdvancedDebugExtension implements IAdvDebugExt {

    @Override
    public void initPages(IInitPagesEvent evt) {
        // Register a test page.
        evt.register(new TestPage());
    }

    @Override
    public void initFormatters(IFormatterRegistry registry) {
        // Initialize formatters.
    }
}

Download @ CurseForge and Modrinth.

Clone this wiki locally