|
8 | 8 | import java.util.stream.Collectors;
|
9 | 9 |
|
10 | 10 | import org.bukkit.World;
|
| 11 | +import org.bukkit.Material; |
11 | 12 | import org.eclipse.jdt.annotation.Nullable;
|
| 13 | +import org.bukkit.Bukkit; |
12 | 14 |
|
13 | 15 | import world.bentobox.bentobox.BentoBox;
|
14 | 16 | import world.bentobox.bentobox.api.addons.GameModeAddon;
|
@@ -97,6 +99,87 @@ protected void registerPlaceholders(GameModeAddon gm) {
|
97 | 99 | // Personal rank
|
98 | 100 | bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_rank_value",
|
99 | 101 | u -> getRankValue(gm.getOverWorld(), u));
|
| 102 | + |
| 103 | + // Register mainhand placeholders |
| 104 | + bpm.registerPlaceholder(addon, |
| 105 | + gm.getDescription().getName().toLowerCase() + "_island_value_mainhand", |
| 106 | + user -> { |
| 107 | + if (user.getPlayer() == null || !user.getPlayer().getInventory().getItemInMainHand().getType().isBlock()) { |
| 108 | + return "0"; |
| 109 | + } |
| 110 | + String blockName = user.getPlayer().getInventory().getItemInMainHand().getType().getKey().getKey(); |
| 111 | + return String.valueOf(Objects.requireNonNullElse( |
| 112 | + addon.getBlockConfig().getValue(gm.getOverWorld(), blockName), |
| 113 | + 0 |
| 114 | + )); |
| 115 | + } |
| 116 | + ); |
| 117 | + |
| 118 | + bpm.registerPlaceholder(addon, |
| 119 | + gm.getDescription().getName().toLowerCase() + "_island_count_mainhand", |
| 120 | + user -> { |
| 121 | + if (user.getPlayer() == null || !user.getPlayer().getInventory().getItemInMainHand().getType().isBlock()) { |
| 122 | + return "0"; |
| 123 | + } |
| 124 | + Material material = user.getPlayer().getInventory().getItemInMainHand().getType(); |
| 125 | + return getBlockCount(gm, user, material); |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + // Register looking at block placeholders |
| 130 | + bpm.registerPlaceholder(addon, |
| 131 | + gm.getDescription().getName().toLowerCase() + "_island_value_looking", |
| 132 | + user -> { |
| 133 | + if (user.getPlayer() == null) return "0"; |
| 134 | + var targetBlock = user.getPlayer().getTargetBlock(null, 5); |
| 135 | + if (targetBlock != null && !targetBlock.getType().isAir()) { |
| 136 | + String blockName = targetBlock.getType().getKey().getKey(); |
| 137 | + return String.valueOf(Objects.requireNonNullElse( |
| 138 | + addon.getBlockConfig().getValue(gm.getOverWorld(), blockName), |
| 139 | + 0 |
| 140 | + )); |
| 141 | + } |
| 142 | + return "0"; |
| 143 | + } |
| 144 | + ); |
| 145 | + |
| 146 | + bpm.registerPlaceholder(addon, |
| 147 | + gm.getDescription().getName().toLowerCase() + "_island_count_looking", |
| 148 | + user -> { |
| 149 | + if (user.getPlayer() == null) return "0"; |
| 150 | + var targetBlock = user.getPlayer().getTargetBlock(null, 5); |
| 151 | + if (targetBlock != null && !targetBlock.getType().isAir()) { |
| 152 | + return getBlockCount(gm, user, targetBlock.getType()); |
| 153 | + } |
| 154 | + return "0"; |
| 155 | + } |
| 156 | + ); |
| 157 | + |
| 158 | + // Register placeholders for all block materials |
| 159 | + if (Bukkit.getServer() != null) { |
| 160 | + // Get all materials from the block config |
| 161 | + addon.getBlockConfig().getBlockValues().keySet().forEach(blockName -> { |
| 162 | + String formattedName = blockName.replace(':', '_').toLowerCase(); |
| 163 | + |
| 164 | + // Register value placeholder |
| 165 | + bpm.registerPlaceholder(addon, |
| 166 | + gm.getDescription().getName().toLowerCase() + "_island_value_" + formattedName, |
| 167 | + user -> String.valueOf(Objects.requireNonNullElse( |
| 168 | + addon.getBlockConfig().getValue(gm.getOverWorld(), blockName), |
| 169 | + 0 |
| 170 | + )) |
| 171 | + ); |
| 172 | + |
| 173 | + // Register count placeholder |
| 174 | + bpm.registerPlaceholder(addon, |
| 175 | + gm.getDescription().getName().toLowerCase() + "_island_count_" + formattedName, |
| 176 | + user -> { |
| 177 | + Material material = Material.valueOf(blockName.toUpperCase()); |
| 178 | + return getBlockCount(gm, user, material); |
| 179 | + } |
| 180 | + ); |
| 181 | + }); |
| 182 | + } |
100 | 183 | }
|
101 | 184 |
|
102 | 185 | /**
|
@@ -220,4 +303,43 @@ String getVisitedIslandLevel(GameModeAddon gm, User user) {
|
220 | 303 | .orElse("0");
|
221 | 304 | }
|
222 | 305 |
|
| 306 | + /** |
| 307 | + * Gets the block count for a specific material in a user's island |
| 308 | + * @param gm GameModeAddon |
| 309 | + * @param user User requesting the count |
| 310 | + * @param material Material to count |
| 311 | + * @return String representation of the count |
| 312 | + */ |
| 313 | + private String getBlockCount(GameModeAddon gm, User user, Object material) { |
| 314 | + if (user == null) { |
| 315 | + return "0"; |
| 316 | + } |
| 317 | + return getBlockCountForUser(gm, user, material); |
| 318 | + } |
| 319 | + |
| 320 | + /** |
| 321 | + * Gets the block count for a specific material in a user's island |
| 322 | + * @param gm GameModeAddon |
| 323 | + * @param user User to get count for |
| 324 | + * @param material Material to count |
| 325 | + * @return String representation of the count |
| 326 | + */ |
| 327 | + private String getBlockCountForUser(GameModeAddon gm, User user, Object material) { |
| 328 | + // Get the island for the user |
| 329 | + Island island = addon.getIslands().getIsland(gm.getOverWorld(), user); |
| 330 | + if (island == null) { |
| 331 | + return "0"; |
| 332 | + } |
| 333 | + |
| 334 | + // Get the level data for the island |
| 335 | + IslandLevels data = addon.getManager().getLevelsData(island); |
| 336 | + if (data == null) { |
| 337 | + return "0"; |
| 338 | + } |
| 339 | + |
| 340 | + // Get the total count from both above sea level and underwater |
| 341 | + int count = data.getMdCount().getOrDefault(material, 0) + data.getUwCount().getOrDefault(material, 0); |
| 342 | + return String.valueOf(count); |
| 343 | + } |
| 344 | + |
223 | 345 | }
|
0 commit comments