Creating a custom option
Creating a Custom Option
This section explains how to create a custom Option type for unique settings.
Steps
Extend
Option:Create a subclass of
Option<T>, specifying the value typeT.Implement a constructor with
name,getter, andsetter.
Define Behavior:
Override input methods (
mouseClicked, etc.) for custom interactions.Set
width,height, andpositionin the renderer.
Create a Renderer:
Implement a
SkinRendererfor 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.
Last updated
Was this helpful?