|
1 | 1 | package com.eternalcode.combat.bridge.placeholder; |
2 | 2 |
|
| 3 | +import com.eternalcode.combat.config.implementation.PlaceholderSettings; |
| 4 | +import com.eternalcode.combat.config.implementation.PluginConfig; |
3 | 5 | import com.eternalcode.combat.fight.FightManager; |
4 | 6 | import com.eternalcode.combat.fight.FightTag; |
5 | 7 | import com.eternalcode.combat.util.DurationUtil; |
|
14 | 16 |
|
15 | 17 | public class FightTagPlaceholder extends PlaceholderExpansion { |
16 | 18 |
|
| 19 | + private static final String IDENTIFIER = "eternalcombat"; |
| 20 | + |
| 21 | + private final PlaceholderSettings placeholderSettings; |
17 | 22 | private final FightManager fightManager; |
18 | 23 | private final Server server; |
19 | 24 | private final Plugin plugin; |
20 | | - private static final String IDENTIFIER = "eternalcombat"; |
21 | 25 |
|
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; |
23 | 28 | this.fightManager = fightManager; |
24 | 29 | this.server = server; |
25 | 30 | this.plugin = plugin; |
26 | 31 | } |
27 | 32 |
|
28 | 33 | @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 | + }; |
31 | 44 | } |
32 | 45 |
|
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(""); |
37 | 50 | } |
38 | 51 |
|
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(""); |
42 | 56 | } |
43 | 57 |
|
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(""); |
47 | 62 | } |
48 | 63 |
|
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 | + } |
62 | 69 |
|
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 | + } |
68 | 73 |
|
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 | + } |
74 | 79 |
|
75 | | - return null; |
| 80 | + private boolean isPlayerInCombat(OfflinePlayer player) { |
| 81 | + Player onlinePlayer = player.getPlayer(); |
| 82 | + return onlinePlayer != null && this.fightManager.isInCombat(onlinePlayer.getUniqueId()); |
76 | 83 | } |
77 | 84 |
|
78 | 85 | private @NotNull Optional<Player> getTagger(OfflinePlayer player) { |
79 | 86 | 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); |
82 | 89 | } |
83 | 90 |
|
84 | 91 | private Optional<FightTag> getFightTag(OfflinePlayer player) { |
85 | 92 | Player onlinePlayer = player.getPlayer(); |
86 | 93 |
|
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(); |
93 | 96 | } |
94 | 97 |
|
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); |
96 | 114 | } |
97 | 115 |
|
| 116 | + @Override |
| 117 | + public @NotNull String getVersion() { |
| 118 | + return this.plugin.getDescription().getVersion(); |
| 119 | + } |
98 | 120 | } |
0 commit comments