Skip to content

Commit b56e34f

Browse files
committed
[1.1.0] Lots of improvements, create good documentation
- Broken TextParser tags in an safe and all categories, - Gradients now work with Unicode Emojis - Added TextParser.parseSafe(String) for simpler parsing of user input, - Added multiple new aliases to tags, - Added `style` and `raw` tags, which allow usage of json style and json text, - Marked it as library in ModMenu, - Added logo to mod (and replaced the old one) - Created a documentation,
1 parent 785c756 commit b56e34f

28 files changed

+874
-38
lines changed

.github/workflows/docs.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/setup-python@v2
11+
with:
12+
python-version: 3.x
13+
- run: pip install mkdocs-material
14+
- run: mkdocs gh-deploy --force

.github/workflows/release.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ jobs:
4141
with:
4242
files: 'build/libs/*.jar;!build/libs/*-sources.jar;!build/libs/*-dev.jar'
4343
repo-token: ${{ secrets.GITHUB_TOKEN }}
44+
45+
- uses: actions/setup-python@v2
46+
with:
47+
python-version: 3.x
48+
- run: pip install mkdocs-material
49+
- run: mkdocs gh-deploy --force

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Fabric Placeholder API
1+
# About Placeholder API
22
It's a small, jij-able API that allows creation and parsing placeholders within strings and Minecraft Text Components.
33
Placeholders use simple format of `%modid:type%` or `%modid:type/data%`.
4+
It also includes simple, general usage text format indented for simplifying user input in configs/chats/etc.
45

5-
For list of currently available placeholders, check [wiki](https://github.com/Patbox/FabricPlaceholderAPI/wiki).
6+
For list of currently available placeholders, check [wiki](https://github.com/Patbox/TextPlaceholderAPI/wiki).
67

78
## Usage:
89
Add it to your dependencies like this:
@@ -26,3 +27,5 @@ PlaceholderAPI.register(new Identifier("server", "name"), (ctx) -> PlaceholderRe
2627
If you want to parse placeholders, you need to use `PlaceholderAPI.parseString(String, MinecraftServer or ServerPlayerEntity)`
2728
for strings or `PlaceholderAPI.parseText(Text, MinecraftServer or ServerPlayerEntity)` for Vanilla Text Components.
2829
These function don't modify original one, instead they just return new String/Text.
30+
31+

docs/assets/logo.png

329 Bytes
Loading

docs/assets/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.md-header__button.md-logo img, .md-header__button.md-logo svg {
2+
width: auto;
3+
}

docs/dev/adding-placeholders.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Adding placeholders
2+
Creation of new placeholders is simple. You just need to import `eu.pb4.placeholders.PlaceholderAPI`
3+
and call static `register` method. You only need to provide 2 arguments:
4+
5+
- Identifier with your mod id as namespace and path as argument name (with one additional limitation being not allowed to use `/` in it).
6+
- A function (in form of lambda for example) that takes PlaceholderContext and returns PlaceholderResult,
7+
8+
Example
9+
```
10+
PlaceholderAPI.register(
11+
new Identifier("example", "placeholder"),
12+
(ctx) -> PlaceholderResult.value(new LiteralText("Hello World!"))
13+
);
14+
```
15+
16+
## Using the context
17+
PlaceholderContext object passed to placeholder contains allows getting player (if exist), server and argument value.
18+
It also includes few methods for checking if they are present.
19+
20+
Here is example for player only placeholder
21+
```
22+
PlaceholderAPI.register(new Identifier("player", "displayname"), (ctx) -> {
23+
if (ctx.hasPlayer()) {
24+
return PlaceholderResult.value(ctx.getPlayer().getDisplayName());
25+
} else {
26+
return PlaceholderResult.invalid("No player!");
27+
}
28+
});
29+
```
30+
31+
And one for usage of argument
32+
```
33+
PlaceholderAPI.register(new Identifier("server", "name_from_uuid"), (ctx) -> {
34+
if (ctx.hasArgument()) {
35+
return PlaceholderResult.value(ctx.getServer().getUserCache().getByUuid(UUID.fromString(ctx.getArgument())).get().getName()));
36+
} else {
37+
return PlaceholderResult.invalid("No argument!");
38+
}
39+
});
40+
```
41+
42+
## Returning correct value
43+
Placeholders need to return instance of PlaceholderResult. It can be created by usage of provided static methods on this class.
44+
45+
If it was successful:
46+
47+
* `PlaceholderResult.value(Text text)` - Creates a value with text
48+
* `PlaceholderResult.value(String text)` - Creates a value from string, by parsing it with TextParser
49+
50+
If it was invalid (no player or argument for example):
51+
52+
* `PlaceholderResult.invalid()` - Creates simple invalid result
53+
* `PlaceholderResult.invalid(String reason)` - Creates invalid result with a reason,
54+
which is currently unused, but can be implemented by other parsers

docs/dev/getting-started.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Getting Started
2+
3+
To begin, you need to add Nucleoid's maven to your build `build.gradle`.
4+
5+
```groovy
6+
repositories {
7+
// There might be other repos there too, just add it at the end
8+
maven { url "https://maven.nucleoid.xyz/" }
9+
}
10+
```
11+
12+
Then you just declare it as dependency!
13+
```groovy
14+
dependencies {
15+
// You will have other dependencies here too
16+
17+
modImplementation include("eu.pb4:placeholder-api:[VERSION]")
18+
}
19+
```
20+
This will also include it in yours mods, so users won't need to download it separately.
21+
22+
You just need to replace `[VERSION]` with version you want to use (which should be usually the latest available).
23+
For list of version names, you can check [maven](https://maven.nucleoid.xyz/eu/pb4/placeholder-api/)

docs/dev/parsing-placeholders.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+

docs/dev/text-format.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Using Simplified Text Format/TextParser
2+
[*You can read about format here!*](/user/text-format)
3+
4+
Usage of TextParser is simple and really customisable. You just need to import `eu.pb4.placeholders.TextParser`
5+
and call static `parse` method for admin provided configs or `parseSafe` for player provided ones.
6+
They both take only one String argument and output a Text object.
7+
8+
Example
9+
```
10+
String inputString = "<red>Hello <rb>World</rb>!"
11+
12+
Text output = TextParser.parse(inputString); // Text output = TextParser.parseSafe(inputString);
13+
```
14+
15+
## Parsing with only selected ones
16+
If you want to only use selected tags, you can simply get map of all with `TextParser.getRegisteredTags()`
17+
or `TextParser.getRegisteredSafeTags()`. Then you can copy these to your own Map with String keys
18+
and `TextParser.TextFormatterHandler` values.
19+
Then you just use them with `TextParser.parse(String, Map<String, TextFormatterHandler>)`.
20+
21+
Example
22+
```
23+
String inputString = "<red>Hello <blue>World</blue>!"
24+
25+
Map<String, TextParser.TextFormatterHandler> tags = Map.of("red", TextParser.getRegisteredTags().get("red"),
26+
"blue", TextParser.getRegisteredTags().get("yellow") // renames works too!
27+
);
28+
29+
Text output = TextParser.parse(inputString, tags);
30+
```

docs/index.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# About Placeholder API
2+
It's a small, jij-able API that allows creation and parsing placeholders within strings and Minecraft Text Components.
3+
Placeholders use simple format of `%modid:type%` or `%modid:type/data%`.
4+
It also includes simple, general usage text format indented for simplifying user input in configs/chats/etc.
5+
6+
## For users
7+
It allows users to configure multiple mods in similar way without losing compatibility between mods.
8+
Placeholders allow changing what and where any information is present within compatible mods.
9+
10+
Additionally, Simplified Text Format allows to style them in readable way without requirement of writing JSON manually or using
11+
generators.
12+
13+
* [Using placeholders](user/general)
14+
* [Default placeholder list](user/default-placeholders)
15+
* [Mod placeholder list](user/mod-placeholders)
16+
* [Simplified Text Format](user/text-format)
17+
18+
## For developers
19+
Usage of Placeholder API is a simple way to achieve good mod compatibility without having to implement
20+
multiple mod specific apis. Additionally, the placeholder parsing system can be used for replacing
21+
own static (or dynamic placeholders) in Text created by player or read from config. This with combination
22+
of Simplified Text Format allows creating great user/admin experience.
23+
24+
* [Getting Started](dev/getting-started)
25+
* [Adding placeholders](dev/adding-placeholders)
26+
* [Parsing placeholders](dev/parsing-placeholders)
27+
* [Using Simplified Text Format/TextParser](dev/text-format)

0 commit comments

Comments
 (0)