|
| 1 | +# Parsing placeholders |
| 2 | +There are few ways (and types) of placeholders you can parse with PlaceholderAPI. |
| 3 | +So depending on your use case some of these will be more useful than others. |
| 4 | + |
| 5 | +## Parsing global placeholders |
| 6 | +Parsing global placeholders is really simple, as long as you have access to ServerPlayerEntity |
| 7 | +or MinecraftServer object. You just need to simply import `eu.pb4.placeholders.PlaceholderAPI` and call |
| 8 | +`parseText`. This method will return fully parsed Text, which can be displayed to the user. |
| 9 | + |
| 10 | +Example |
| 11 | +``` |
| 12 | +// for ServerPlayerEntity |
| 13 | +Text message = PlaceholderAPI.parseText(textInput, player); |
| 14 | +
|
| 15 | +// for Server |
| 16 | +Text message2 = PlaceholderAPI.parseText(textInput, server); |
| 17 | +``` |
| 18 | + |
| 19 | +Placeholders itself will use default formatting of `%category:placeholder%`. |
| 20 | +If you want to use other formatting for them (which is recommended), you can use |
| 21 | +`parseTextAlt(Text, ServerPlayerEntity or MinecraftServer)` for `{category:placeholder}`. |
| 22 | + |
| 23 | +## Parsing own/custom/predefined placeholders |
| 24 | +If you want to parse your own placeholders, you can do this in 2 ways. |
| 25 | + |
| 26 | +### Static placeholders |
| 27 | +To parse static placeholders you need to create a Map with `String` as a key and `Text` as a value. |
| 28 | +You also need a Pattern object (which can be taken from predefined ones). Then it's as simple as calling |
| 29 | +a `parsePredefinedText` static method on `PlaceholderAPI` class. |
| 30 | + |
| 31 | +Example |
| 32 | +``` |
| 33 | +ServerPlayerEntity player = something.getPlayer(); // MinecraftServer server = something.getServer() |
| 34 | +
|
| 35 | +Text inputText = new LiteralText("Hello! ${player}"); |
| 36 | +Map<String, Text> placeholders = Map.of("player", new LiteralText("You are a player!")); |
| 37 | +Pattern pattern = PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN; |
| 38 | +
|
| 39 | +Text output = PlaceholderAPI.parsePredefinedText(inputText, pattern, placeholders); |
| 40 | +``` |
| 41 | + |
| 42 | +### Dynamic placeholders |
| 43 | +In case where you want to parse placeholder with a context similar to global one, you need to |
| 44 | +create a Map with `Identifier` (with `/` being disallowed) as a key and `PlaceholderHandler` as a value (same as adding global ones). |
| 45 | +You also will need a pattern object, which is the same as with static ones. |
| 46 | + |
| 47 | +As opposite to global ones, you don't need to define namespace/category as it can default to minecraft one (for simpler user input). |
| 48 | +Then you just parse it with `parseTextCustom(Text, ServerPlayerEntity or MinecraftServer, Map<String, PlaceholderHandler>, Pattern)`. |
| 49 | + |
| 50 | +Example |
| 51 | +``` |
| 52 | +ServerPlayerEntity player = something.getPlayer(); // MinecraftServer server = something.getServer() |
| 53 | +
|
| 54 | +Text inputText = new LiteralText("Hello! ${player/blue}"); |
| 55 | +Map<Identifier, Text> placeholders = Map.of(new Identifier("player"), |
| 56 | + (ctx) -> { |
| 57 | + if (ctx.hasPlayer()) { |
| 58 | + return PlaceholderResult.value(new LiteralText("You are a player!") |
| 59 | + .setStyle(Style.EMPTY.withColor(TextColor.parse(ctx.getArgument())))); |
| 60 | + } else { |
| 61 | + return PlaceholderResult.value(new LiteralText("You are a server!") |
| 62 | + .setStyle(Style.EMPTY.withColor(TextColor.parse(ctx.getArgument())))); |
| 63 | + } |
| 64 | + }); |
| 65 | +Pattern pattern = PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN; |
| 66 | +
|
| 67 | +Text output = PlaceholderAPI.parseTextCustom(inputText, player, pattern, placeholders); |
| 68 | +// Text output = PlaceholderAPI.parseTextCustom(inputText, server, pattern, placeholders); |
| 69 | +``` |
| 70 | + |
| 71 | +### Preferred Patterns for static |
| 72 | +PlaceholderAPI has few Patterns you can use, which are accessible as static objects on `PlaceholderAPI` class. |
| 73 | + |
| 74 | +* `PREDEFINED_PLACEHOLDER_PATTERN` (`${placeholder}`) - works the best in most cases, doesn't collide with other ones. |
| 75 | +* `ALT_PLACEHOLDER_PATTERN_CUSTOM` (`{placeholder}`) - second best, but have more chance of colliding with user formatting. |
| 76 | + |
| 77 | +There are other ones, which usage is allowed, but they might work worse. |
| 78 | + |
| 79 | +* `PLACEHOLDER_PATTERN_CUSTOM` (`%placeholder%`) - is the same as default one, but doesn't require `:`. |
| 80 | +* `PLACEHOLDER_PATTERN` (`%category:placeholder%`) - used by default global placeholders (requires category). |
| 81 | +* `PLACEHOLDER_PATTERN_ALT` (`{category:placeholder}`) - used as alternative formatting for global ones (requires category). |
| 82 | + |
0 commit comments