Jump to content

[Solved] How to Create my own Style Instances


Jimmeh

Recommended Posts

Hi there. I'm trying to create my own Style instances so I can apply custom colors those to MutableComponents. For instance, lets say I have

public static final int RED_ORANGE = FastColor.ARGB32.color(255, 247, 69, 37);

And I wanted to apply this custom color to a MutableComponent with MutableComponent#withStyle. I notice the Style constructor isn't public, so I'm doing some reflection like so:

final Constructor<Style> constructor = Style.class.getDeclaredConstructor(TextColor.class,
                    Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class,
                    ClickEvent.class, HoverEvent.class,
                    String.class,
                    ResourceLocation.class
            );

final Style style = constructor.newInstance(TextColor.fromRgb(color.color()),
    false, false, false, false, false,
    null, null,
    null,
    null
);

This, however, doesn't work. Is there a way to create custom Styles? Thanks!

 

Edit: I needed to "constructor.setAccessible(true);"

Edited by Jimmeh
Solved
Link to comment
Share on other sites

  • Jimmeh changed the title to [Solved] How to Create my own Style Instances

yikes!

ok, first general advice: if it feels like you are pushing a wall (trying to move it) - you are probably on the wrong path. i am serious and this is an important advice. walls have holes in them - doors. stop and look around. don't just move forward and push.

now to your topic.

first of all, delete all that crap up there.

text components are usually created in two ways:
    Component.translatable("some.key.from.lang.files")               or
    Component.literal("space dash space or something like that")

styles are often added to components in anonymous manner, especially if the components are reused: 
    private final Component messageSevere = Component.translatable("message.something.lang.file").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xc84030)));

if you recognize the need to store a style and apply it to newly created components without creating needless identical style objects (points for you), do it like i do - start from empty style, make yours, store it for later and apply it to components with withStyle method like above.

Link to comment
Share on other sites

5 hours ago, Potato5 said:

yikes!

ok, first general advice: if it feels like you are pushing a wall (trying to move it) - you are probably on the wrong path. i am serious and this is an important advice. walls have holes in them - doors. stop and look around. don't just move forward and push.

now to your topic.

first of all, delete all that crap up there.

text components are usually created in two ways:
    Component.translatable("some.key.from.lang.files")               or
    Component.literal("space dash space or something like that")

styles are often added to components in anonymous manner, especially if the components are reused: 
    private final Component messageSevere = Component.translatable("message.something.lang.file").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xc84030)));

if you recognize the need to store a style and apply it to newly created components without creating needless identical style objects (points for you), do it like i do - start from empty style, make yours, store it for later and apply it to components with withStyle method like above.

I didn't realize 

Style.EMPTY.withColor()

was a thing. Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.