Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • 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.

  • Author
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!

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.