Skip to content

Commit a0bd849

Browse files
P1otrullaCitralFloJakubk15
authored
GH-253 Add configurable placeholder settings and new placeholders (#253)
* Add configurable placeholder settings and new placeholders Introduces PlaceholderSettings to allow customization of placeholder output, specifically for isInCombat_formatted. Adds new placeholders isInCombat and isInCombat_formatted to FightTagPlaceholder, updates BridgeService and PluginConfig to support the new settings, and adjusts command permission annotation placement. Also updates build scripts and dependencies. * rollback version * Add papi plugin to runServer * remove unused import * follow gemini's suggestion * Add more comments for configuration, merge time combat placeholders Took 14 minutes * change download source to spigot - got 503 on download from github during tests Took 10 minutes * follow Rollczi's suggestion -> refactored placeholder registry * empty string -> null (papi, when receiving null in the code, marks it and sends an empty string) * Update eternalcombat-plugin/src/main/java/com/eternalcode/combat/bridge/placeholder/FightTagPlaceholder.java Co-authored-by: Jakub Kędziora <[email protected]> * Refactor FightTagPlaceholder and update build script - Simplified conversion to string in handleIsInCombat. - Adjusted build script to use Modrinth URLs for plugin downloads and rolled back Minecraft version to 1.21. --------- Co-authored-by: Michał Wojtas <[email protected]> Co-authored-by: Jakub Kędziora <[email protected]>
1 parent 08cfe7c commit a0bd849

File tree

5 files changed

+92
-49
lines changed

5 files changed

+92
-49
lines changed

eternalcombat-plugin/src/main/java/com/eternalcode/combat/EternalCombatReloadCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.bukkit.command.CommandSender;
1515

1616
@Command(name = "combatlog", aliases = "combat")
17-
@Permission("eternalcombat.reload")
1817
public class EternalCombatReloadCommand {
1918

2019
private static final Notice RELOAD_MESSAGE = BukkitNotice.builder()
@@ -31,6 +30,7 @@ public EternalCombatReloadCommand(ConfigService configService, NoticeService not
3130

3231
@Async
3332
@Execute(name = "reload")
33+
@Permission("eternalcombat.reload")
3434
void execute(@Context CommandSender sender) {
3535
Stopwatch stopwatch = Stopwatch.createStarted();
3636
this.configService.reload();

eternalcombat-plugin/src/main/java/com/eternalcode/combat/bridge/BridgeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void init(Server server) {
6969

7070
initialize(
7171
"PlaceholderAPI",
72-
() -> new FightTagPlaceholder(this.fightManager, server, this.plugin).register(),
72+
() -> new FightTagPlaceholder(this.config, this.fightManager, server, this.plugin).register(),
7373
() -> this.logger.warning("PlaceholderAPI not found; skipping placeholders.")
7474
);
7575
}
Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.eternalcode.combat.bridge.placeholder;
22

3+
import com.eternalcode.combat.config.implementation.PlaceholderSettings;
4+
import com.eternalcode.combat.config.implementation.PluginConfig;
35
import com.eternalcode.combat.fight.FightManager;
46
import com.eternalcode.combat.fight.FightTag;
57
import com.eternalcode.combat.util.DurationUtil;
@@ -14,85 +16,105 @@
1416

1517
public class FightTagPlaceholder extends PlaceholderExpansion {
1618

19+
private static final String IDENTIFIER = "eternalcombat";
20+
21+
private final PlaceholderSettings placeholderSettings;
1722
private final FightManager fightManager;
1823
private final Server server;
1924
private final Plugin plugin;
20-
private static final String IDENTIFIER = "eternalcombat";
2125

22-
public FightTagPlaceholder(FightManager fightManager, Server server, Plugin plugin) {
26+
public FightTagPlaceholder(PluginConfig pluginConfig, FightManager fightManager, Server server, Plugin plugin) {
27+
this.placeholderSettings = pluginConfig.placeholders;
2328
this.fightManager = fightManager;
2429
this.server = server;
2530
this.plugin = plugin;
2631
}
2732

2833
@Override
29-
public boolean canRegister() {
30-
return true;
34+
public String onRequest(OfflinePlayer player, String identifier) {
35+
return switch (identifier) {
36+
case "remaining_millis" -> this.handleRemainingMillis(player);
37+
case "remaining_seconds" -> this.handleRemainingSeconds(player);
38+
case "opponent" -> this.handleOpponent(player);
39+
case "opponent_health" -> this.handleOpponentHealth(player);
40+
case "isInCombat" -> this.handleIsInCombat(player);
41+
case "isInCombat_formatted" -> this.handleIsInCombatFormatted(player);
42+
default -> null;
43+
};
3144
}
3245

33-
34-
@Override
35-
public @NotNull String getIdentifier() {
36-
return IDENTIFIER;
46+
private String handleRemainingMillis(OfflinePlayer player) {
47+
return this.getFightTag(player)
48+
.map(tag -> DurationParser.TIME_UNITS.format(tag.getRemainingDuration()))
49+
.orElse("");
3750
}
3851

39-
@Override
40-
public @NotNull String getAuthor() {
41-
return this.plugin.getDescription().getAuthors().get(0);
52+
private String handleRemainingSeconds(OfflinePlayer player) {
53+
return this.getFightTag(player)
54+
.map(tag -> DurationUtil.format(tag.getRemainingDuration()))
55+
.orElse("");
4256
}
4357

44-
@Override
45-
public @NotNull String getVersion() {
46-
return this.plugin.getDescription().getVersion();
58+
private String handleOpponent(OfflinePlayer player) {
59+
return this.getTagger(player)
60+
.map(Player::getName)
61+
.orElse("");
4762
}
4863

49-
@Override
50-
public String onRequest(OfflinePlayer player, String identifier) {
51-
if (identifier.equals("remaining_seconds")) {
52-
return this.getFightTag(player)
53-
.map(fightTagInter -> DurationUtil.format(fightTagInter.getRemainingDuration()))
54-
.orElse("");
55-
}
56-
57-
if (identifier.equals("remaining_millis")) {
58-
return this.getFightTag(player)
59-
.map(fightTag -> DurationParser.TIME_UNITS.format(fightTag.getRemainingDuration()))
60-
.orElse("");
61-
}
64+
private String handleOpponentHealth(OfflinePlayer player) {
65+
return this.getTagger(player)
66+
.map(tagger -> String.format("%.2f", tagger.getHealth()))
67+
.orElse("");
68+
}
6269

63-
if (identifier.equals("opponent")) {
64-
return this.getTagger(player)
65-
.map(tagger -> tagger.getName())
66-
.orElse("");
67-
}
70+
private String handleIsInCombat(OfflinePlayer player) {
71+
return String.valueOf(this.isPlayerInCombat(player));
72+
}
6873

69-
if (identifier.equals("opponent_health")) {
70-
return this.getTagger(player)
71-
.map(tagger -> String.format("%.2f", tagger.getHealth()))
72-
.orElse("");
73-
}
74+
private String handleIsInCombatFormatted(OfflinePlayer player) {
75+
return this.isPlayerInCombat(player)
76+
? this.placeholderSettings.isInCombatFormattedTrue
77+
: this.placeholderSettings.isInCombatFormattedFalse;
78+
}
7479

75-
return null;
80+
private boolean isPlayerInCombat(OfflinePlayer player) {
81+
Player onlinePlayer = player.getPlayer();
82+
return onlinePlayer != null && this.fightManager.isInCombat(onlinePlayer.getUniqueId());
7683
}
7784

7885
private @NotNull Optional<Player> getTagger(OfflinePlayer player) {
7986
return this.getFightTag(player)
80-
.map(fightTagInter -> fightTagInter.getTagger())
81-
.map(taggerId -> this.server.getPlayer(taggerId));
87+
.map(FightTag::getTagger)
88+
.map(this.server::getPlayer);
8289
}
8390

8491
private Optional<FightTag> getFightTag(OfflinePlayer player) {
8592
Player onlinePlayer = player.getPlayer();
8693

87-
if (onlinePlayer != null) {
88-
if (!this.fightManager.isInCombat(onlinePlayer.getUniqueId())) {
89-
return Optional.empty();
90-
}
91-
92-
return Optional.of(this.fightManager.getTag(onlinePlayer.getUniqueId()));
94+
if (onlinePlayer == null || !this.fightManager.isInCombat(onlinePlayer.getUniqueId())) {
95+
return Optional.empty();
9396
}
9497

95-
return Optional.empty();
98+
return Optional.of(this.fightManager.getTag(onlinePlayer.getUniqueId()));
99+
}
100+
101+
@Override
102+
public boolean canRegister() {
103+
return true;
104+
}
105+
106+
@Override
107+
public @NotNull String getIdentifier() {
108+
return IDENTIFIER;
109+
}
110+
111+
@Override
112+
public @NotNull String getAuthor() {
113+
return this.plugin.getDescription().getAuthors().get(0);
96114
}
97115

116+
@Override
117+
public @NotNull String getVersion() {
118+
return this.plugin.getDescription().getVersion();
119+
}
98120
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.eternalcode.combat.config.implementation;
2+
3+
import eu.okaeri.configs.OkaeriConfig;
4+
import eu.okaeri.configs.annotation.Comment;
5+
6+
public class PlaceholderSettings extends OkaeriConfig {
7+
8+
@Comment("Text returned by %eternalcombat_isInCombat_formatted% placeholder when the player is in combat")
9+
public String isInCombatFormattedTrue = "In Combat";
10+
11+
@Comment("Text returned by %eternalcombat_isInCombat_formatted% placeholder when the player is out of combat")
12+
public String isInCombatFormattedFalse = "Not In Combat";
13+
14+
}

eternalcombat-plugin/src/main/java/com/eternalcode/combat/config/implementation/PluginConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ public class PluginConfig extends OkaeriConfig {
105105
})
106106
public CombatSettings combat = new CombatSettings();
107107

108+
@Comment({
109+
" ",
110+
"# Settings related to placeholders used in the plugin.",
111+
"# Configure default values returned by placeholders"
112+
})
113+
public PlaceholderSettings placeholders = new PlaceholderSettings();
114+
108115
@Comment({
109116
" ",
110117
"# Customize the messages displayed by the plugin.",

0 commit comments

Comments
 (0)