Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] Text Color Formatting


NullDev
 Share

Recommended Posts

I am having some issues with text formatting. I format my text like this:

player.sendStatusMessage(new StringTextComponent(ticksRemaining/20 + " §f| §4" + redstoneAdded + " §f| §a" + emeraldAdded + " §f| §b" + diamondAdded + " §f| §0" + coalAdded), true)

and it appears like this in the game chat:

spacer.png2021-03-04_11_41_12.thumb.png.aa2777d86dc3d14defabd427c2f1d642.pngspacer.pngspacer.pngspacer.pngspacer.pngspacer.png

Edited by NullDev
Link to comment
Share on other sites

Do not use the § based formatting codes, use IFormattableTextComponent#mergeStyle(TextFormatting) to set the formatting of a text component.

Additionally it looks like your character encoding is not configured correctly. Make sure your files are encoded in UTF-8 and that Javac is configured to use UTF-8 (it does not do this by default).

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

IFormattableTextComponent#mergeStyle(TextFormatting)

I've done that like this, but now it just appears all black

 

IFormattableTextComponent toSend = new StringTextComponent("Info: ");
        toSend.func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(ticksRemaining/20 + " | "))
                .func_240701_a_(TextFormatting.RED)
                .func_230529_a_(new StringTextComponent(Integer.toString(redstoneAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.GREEN)
                .func_230529_a_(new StringTextComponent(Integer.toString(emeraldAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.AQUA)
                .func_230529_a_(new StringTextComponent(Integer.toString(diamondAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.BLACK)
                .func_230529_a_(new StringTextComponent(Integer.toString(coalAdded)));
player.sendStatusMessage(toSend, true);

 

Link to comment
Share on other sites

@diesieben07

I could not understand, how do we send mixed colored messages?
For an example, I wanna send a message: 

Quote

SKIL_NAME level: N

Where N (is any number, got through a method)  and it's color is DARK_PURPLE and everything else is GOLD.

How do I achieve that? Since using .append and .mergeStyle in a sequence doesn't work, as @NullDev tried.

Link to comment
Share on other sites

1 hour ago, Toktom said:

Since using .append and .mergeStyle in a sequence doesn't work, as @NullDev tried.

NullDev's issue was something else.

You can use append with an already styled component. For example:
 

text = new StringTextComponent("foo").mergeStyle(RED).append(
  new StringTextComponent("bar").mergeStyle(BLUE)
)

 

Link to comment
Share on other sites

5 hours ago, diesieben07 said:

ou completely ignore the result of the methods you call and just use the original, unformatted, component.

I changed it to this and it is still all black:

StringTextComponent toSend = new StringTextComponent("Info: ");
        
player.sendStatusMessage(toSend.func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(ticksRemaining/20 + " | "))
                .func_240701_a_(TextFormatting.RED)
                .func_230529_a_(new StringTextComponent(Integer.toString(redstoneAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.GREEN)
                .func_230529_a_(new StringTextComponent(Integer.toString(emeraldAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.AQUA)
                .func_230529_a_(new StringTextComponent(Integer.toString(diamondAdded)))
                .func_240701_a_(TextFormatting.WHITE)
                .func_230529_a_(new StringTextComponent(" | "))
                .func_240701_a_(TextFormatting.BLACK)
                .func_230529_a_(new StringTextComponent(Integer.toString(coalAdded))), true);

 

Link to comment
Share on other sites

You keep calling mergeStyle on the outer text component with a color. Every time you do that you overwrite the previously set color. Hence the "Info: " part is black, because that's the last color you set for it. The appended components all have the default style, which is also black.

Link to comment
Share on other sites

8 hours ago, diesieben07 said:

You keep calling mergeStyle on the outer text component with a color

For anyone else with this issue, I figured it out. Do this for each new StringTextComponent:

toReturn.func_230529_a_(
	new StringTextComponent("New Text With New Color").func_240701_a_(TextFormatting.WHITE)
	)

I call the mergestyle on the new StringTextComponent that I am adding with the appendText function.

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I've been trying to get a server running on my Raspberry Pi, but run.sh won't function properly! I've used chmod, but it fails every time I try to run it. Here's the output: pi@raspberrypi:~/🅱️orge $ sudo chmod +x run.sh pi@raspberrypi:~/🅱️orge $ ./run.sh Exception in thread "main" java.lang.ExceptionInInitializerError at cpw.mods.securejarhandler@1.0.3/cpw.mods.jarhandling.SecureJar.from(SecureJar.java:58) at cpw.mods.securejarhandler@1.0.3/cpw.mods.jarhandling.SecureJar.from(SecureJar.java:50) at cpw.mods.securejarhandler@1.0.3/cpw.mods.jarhandling.SecureJar.from(SecureJar.java:42) at cpw.mods.bootstraplauncher@1.0.0/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:93) Caused by: java.lang.IllegalArgumentException: Error decoding percent encoded characters at java.base/sun.net.www.ParseUtil.decode(ParseUtil.java:218) at java.base/jdk.internal.loader.URLClassPath$FileLoader.<init>(URLClassPath.java:1219) at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:488) at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:482) at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:481) at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:449) at java.base/jdk.internal.loader.URLClassPath$1.next(URLClassPath.java:345) at java.base/jdk.internal.loader.URLClassPath$1.hasMoreElements(URLClassPath.java:356) at java.base/jdk.internal.loader.BuiltinClassLoader$1.hasNext(BuiltinClassLoader.java:408) at java.base/jdk.internal.loader.BuiltinClassLoader$1.hasMoreElements(BuiltinClassLoader.java:416) at java.base/java.lang.CompoundEnumeration.next(ClassLoader.java:2725) at java.base/java.lang.CompoundEnumeration.hasMoreElements(ClassLoader.java:2734) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1210) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1228) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1273) at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1309) at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1393) at java.base/java.nio.file.spi.FileSystemProvider.loadInstalledProviders(FileSystemProvider.java:156) at java.base/java.nio.file.spi.FileSystemProvider$1.run(FileSystemProvider.java:207) at java.base/java.nio.file.spi.FileSystemProvider$1.run(FileSystemProvider.java:204) at java.base/java.security.AccessController.doPrivileged(AccessController.java:318) at java.base/java.nio.file.spi.FileSystemProvider.installedProviders(FileSystemProvider.java:204) at cpw.mods.securejarhandler@1.0.3/cpw.mods.jarhandling.impl.Jar.<clinit>(Jar.java:29) ... 4 more (Yes, the 🅱️s are intentional)
    • when i press install on any version it says "adfoc.us redirected you too many times" and i cant install any version. please help.
    • I've got flywheel installed but it still gives me the exit on error
    • https://www.dropbox.com/s/zrit7mgov31left/2022-08-14-7.log?dl=0
  • Topics

×
×
  • Create New...

Important Information

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