Replacers
Since 1.0.0
Replacers do what you might expect, they replace some text with some other text on a string, they exist to be used as an easy way to replace text, normally on multiple strings without having to specify multiple times what replacements you want. Creating a Replacer is easy, but there are two pretty important rules, firstly, Replacers can only have a number of replacements that is divisible by two, let's take a look at how to create a Replacer and you will quickly understand why that's a requirement.
The first "valid" Replacer will work because "%val%" has a replacement, "Test". However, the second Replacer, "invalid", will throw an exception because "%ShouldNotBeHere%" doesn't have a replacement, pretty obvious right? Now let's see how to actually use a Replacer.
Secondly, you can't really add two replacements for the same string as they will override each other in the order they were added, let's take a look at this.
Also, there is no need to use '%' for the text to replace, but it is the common way to identify placeholders, so yeah, try to keep it that way, at least for placeholders that the end user may use. Anyways, Replacers can also be used on string lists and as many times as you want.
Numeric support
Numeric support is a feature that replaces different text depending on a numeric value, let's see a real example to understand how it works.
If %points% gets replaced with either 1 or -1, the final string will be "You have 1 point", with any other number, for example 42, the final string will be "You have 42 points"
Player objects
Player objects are treated on a different way by replacers in order to make your code just a bit shorter, if you add a replacement with a player object, MCUtils assumes you want the player name.
Modifying replacements
Of course, you can add new replacements to an existing Replacer
Replacement
The replacement interface just adds one method, the asReplacement method. Every object implementing this interface will be recognized by a Replacer, which will use its asReplacement method instead of toString in order to display it. Here is an example with a useless player class.
This class could be used like this on a replacer
Last updated