Posted August 26, 20223 yr 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 August 26, 20223 yr by Jimmeh Solved
August 26, 20223 yr 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.
August 26, 20223 yr 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.