Creating a custom option

Creating a Custom Option

This section explains how to create a custom Option type for unique settings.

Steps

  1. Extend Option:

    • Create a subclass of Option<T>, specifying the value type T.

    • Implement a constructor with name, getter, and setter.

  2. Define Behavior:

    • Override input methods (mouseClicked, etc.) for custom interactions.

    • Set width, height, and position in the renderer.

  3. Create a Renderer:

    • Implement a SkinRenderer for the custom option.

    • Register it in the skin’s constructor.

Example Custom Option

public class StringOption extends Option<String> {
    public StringOption(Text name, Supplier<String> getter, Consumer<String> setter) {
        super(name, getter, setter);
    }

    @Override
    public boolean charTyped(char c, int modifiers) {
        if (Character.isLetterOrDigit(c)) {
            set(get() + c);
            return true;
        }
        return false;
    }
}

// Register in skin (check in creating a custom skin page)
public class SimpleSkin extends Skin {
    public SimpleSkin() {
        addRenderer(StringOption.class, SimpleStringRenderer::new);
    }
    
    // Renderer in a skin
    public class SimpleStringRenderer implements SkinRenderer<StringOption> {
        @Override
        public void render(DrawContext drawContext, StringOption option, int x, int y, int mouseX, int mouseY) {
             option.setPosition(x, y);
             option.setHeight(10);
             option.setWidth(mc.textRenderer.getWidth(option.get()) + 10);
             drawContext.drawText(mc.textRenderer, option.get(), x, y, 0xFFFFFF, false);
        }
    }
    // ... other methods
}

Common Pitfalls

  • Missing Renderer: The skin must register a renderer for the custom option.

    • Fix: Add the renderer in the skin’s constructor.

  • Input Handling: Failing to override input methods can make the option non-interactive.

    • Fix: Implement relevant input methods for user interaction.

Use description and withComplexity to enhance usability and control visibility.

Last updated

Was this helpful?