How can I parse string by placeholder

I want to parse string with placeholder.

String message = “&eMy prefix is &f{{nucleus:prefix}} &eWow!!”
String parse = ???

Hwo can I parse that message to placeholder string ?

Please help me.

Due to sponge using Text (API 7) or Component (API 8) to send messages, the placeholder API is connected into that, not a String

So you have two options. Either convert your string into a component (your example uses the old legacy magic characters - so im guessing its a message) and then use the sponge placeholder api.

Or

You create your own placeholder api.

I actually have one that I needed to create for my cross platform plugins. Its tailored to my needs, but im sure it can spark some inspiration

Docs on placeholder api

https://docs.spongepowered.org/stable/en-GB/plugin/placeholders/index.html

My placeholder api

1 Like

Thank for you care!

If using Component, then How can I parse component message and replace by placeholder ?

Component message = Component.text(“&eMy prefix is &f{{nucleus:prefix}} &eWow!!”)

Component parse = ???

Could you please send me an example code to parse that message ?

I’ve tried to make it but It doesn’t work. this is my code

        ServerPlayer player = context.one(CommandParameters.PLAYER).get();
        String message = context.one(CommandParameters.MESSAGE).get();
        PlaceholderParser parser = PlaceholderParsers.NAME.get();
        PlaceholderContext contx = PlaceholderContext.builder()
                .argumentString(message)
                .associatedObject(player)
                .build();
        Component text = parser.parse(contx);
        source.sendMessage(text);

It just show player name. I don’t know why.

Personally I haven’t used the placeholder api in either api 7 or 8. Ill take a look now

1 Like

[09:39:35] [Server thread/INFO] [spongeexample]: Component: hello minecraft:overworld

You were actually close. I read the docs and found that the placeholder api in sponge doesn’t do everything I thought it did (aka replace %world name% with the worlds name) but for good reason (said below)

Here is my code

        Component originalMessage = Component.text("hello %world name%");
        PlaceholderContext placeholderContext = PlaceholderContext.builder().associatedObject(Sponge.server().worldManager().worlds().iterator().next()).build();
        Component worldName = PlaceholderParsers.CURRENT_WORLD.get().parse(placeholderContext);
        Component message = originalMessage.replaceText(TextReplacementConfig.builder().match("%world name%").replacement(worldName).build());

        String messagePlain = PlainTextComponentSerializer.plainText().serialize(message);
        this.logger.info("Component: " + messagePlain);

The difference is the extra line where I manually replace the world name with the component version of the world name given from the placeholder api.

Anyway the reason for why it doesn’t do the %% stuff is if a server has two plugins. One registers %player name% to be the players registered minecraft name and another plugin registers %player name% to a players nickname, then the two would conflict.

However by not having that part registered it means that both plugins will not conflict and work as intented.

Not entirly sure why the ability to register them is a thing as if im wanting to use another plugins placeholder, I personally would go into the plugins api and get it myself. My guess is some people prefer using the plugins ids for ease of soft dependencies … but oh well

1 Like

Thank for you care!