-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-125 expand default config and PlaceholderAPI support in internal p…
…laceholders (#125) * add PlaceholderAPI support in internal placeholders expand default config * more config description * literoofkey fix * second lang fix * Revert placeholder registry changes. * Move papi dependency from config class (see revert) to ConfiguredPlaceholderAPIStack class. * Fix bug, add unit tests. --------- Co-authored-by: Rollczi <[email protected]>
- Loading branch information
Showing
6 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...re/src/main/java/com/eternalcode/formatter/placeholder/ConfiguredPlaceholderAPIStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.eternalcode.formatter.placeholder; | ||
|
||
import com.eternalcode.formatter.config.PluginConfig; | ||
import java.util.Map; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import org.bukkit.entity.Player; | ||
|
||
public class ConfiguredPlaceholderAPIStack implements PlayerPlaceholderStack { | ||
|
||
private final PluginConfig pluginConfig; | ||
|
||
public ConfiguredPlaceholderAPIStack(PluginConfig pluginConfig) { | ||
this.pluginConfig = pluginConfig; | ||
} | ||
|
||
@Override | ||
public String apply(String text, Player target) { | ||
String value = text; | ||
|
||
for (Map.Entry<String, String> entry : this.pluginConfig.placeholders.entrySet()) { | ||
value = value.replace(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
value = PlaceholderAPI.setPlaceholders(target, value); | ||
|
||
return value; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...matter-core/src/test/java/com/eternalcode/formatter/placeholder/PapiPlaceholdersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.eternalcode.formatter.placeholder; | ||
|
||
import com.eternalcode.formatter.config.PluginConfig; | ||
import com.eternalcode.formatter.template.TemplateService; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import org.bukkit.entity.Player; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import org.mockito.MockedStatic; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
class PapiPlaceholdersTest { | ||
|
||
private static MockedStatic<PlaceholderAPI> mockStatic; | ||
|
||
private static PlaceholderRegistry registry; | ||
private static TemplateService templateService; | ||
private static PluginConfig pluginConfig; | ||
|
||
@BeforeAll | ||
static void setUpBeforeClass() { | ||
pluginConfig = new PluginConfig(); | ||
registry = new PlaceholderRegistry(); | ||
templateService = new TemplateService(pluginConfig); | ||
|
||
registry.playerStack(new ConfiguredPlaceholderAPIStack(pluginConfig)); | ||
registry.playerStack(new PlaceholderAPIStack()); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { // mock the PlaceholderAPI plugin (staic methods) | ||
mockStatic = mockStatic(PlaceholderAPI.class); | ||
mockStatic.when(() -> PlaceholderAPI.setPlaceholders(isNull(Player.class), anyString())).thenAnswer(invocation -> invocation.<String>getArgument(1) | ||
.replace("%vault_group%", "--VIP--") | ||
.replace("%player_first_join_date%", "2021-01-01") | ||
.replace("%player_health%", "19") | ||
.replace("%player_level%", "420")); | ||
} | ||
|
||
@Test | ||
void test() { | ||
String text = "Hello %player_name%, you joined on %player_first_join_date% and you are in the %vault_group% group. Your health is %player_health% and your level is %player_level%."; | ||
String result = registry.format(text, null); | ||
|
||
assertThat(result) | ||
.isEqualTo("Hello %player_name%, you joined on 2021-01-01 and you are in the --VIP-- group. Your health is 19 and your level is 420."); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"default", "vip", "mod", "admin", "owner"}) | ||
void testFormatFromConfig(String rank) { | ||
String rawFormat = pluginConfig.getRawFormat(rank); | ||
String formattedString = templateService.applyTemplates(rawFormat); | ||
String result = registry.format(formattedString, null); | ||
|
||
assertThat(result) | ||
.contains("19", "420", "2021-01-01", "--VIP--") | ||
.doesNotContain("%", "{", "}"); | ||
} | ||
|
||
|
||
@Test | ||
void testFormatFromConfigUnknown() { | ||
String rawFormat = pluginConfig.getRawFormat("unknown"); | ||
String formattedString = templateService.applyTemplates(rawFormat); | ||
String result = registry.format(formattedString, null); | ||
|
||
assertThat(result) | ||
.doesNotContain("%", "{", "}"); | ||
} | ||
|
||
@AfterEach | ||
void tearDownAfterClass() { | ||
mockStatic.close(); | ||
} | ||
|
||
} |