Add PlaceholderAPI integration
- Add LoMinesPlaceholderExpansion with placeholders for mines and player stats - Available placeholders: mine_<name>_(name|blocks|total|percent|world|remaining|resettime) - Player placeholders: player_(blocksmined|minesreset|playtime|rank) - Global: lomines_count for total mines - Update IntegrationManager to register/unregister expansion - Remove Oraxen and ItemsAdder integration checks Expansion auto-registers on initAll() and unregisters on shutdown()
This commit is contained in:
parent
294cadc739
commit
1daff28229
2 changed files with 173 additions and 31 deletions
|
|
@ -1,15 +1,17 @@
|
|||
package dev.loki.lomines.integration;
|
||||
|
||||
import dev.loki.lomines.LoMinesPlugin;
|
||||
import dev.loki.lomines.integration.placeholder.LoMinesPlaceholderExpansion;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
/**
|
||||
* Manages integrations with external plugins.
|
||||
* Currently supports: WorldGuard (regions), PlaceholderAPI (placeholders)
|
||||
*/
|
||||
public final class IntegrationManager {
|
||||
|
||||
private final LoMinesPlugin plugin;
|
||||
// private PlaceholderAPIIntegration placeholderAPI;
|
||||
private LoMinesPlaceholderExpansion placeholderExpansion;
|
||||
|
||||
public IntegrationManager(LoMinesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
|
@ -19,39 +21,33 @@ public final class IntegrationManager {
|
|||
* Initializes all available integrations.
|
||||
*/
|
||||
public void initAll() {
|
||||
// initPlaceholderAPI();
|
||||
initPlaceholderAPI();
|
||||
checkWorldGuard();
|
||||
checkOraxen();
|
||||
checkItemsAdder();
|
||||
}
|
||||
|
||||
// private void initPlaceholderAPI() {
|
||||
// if (isPluginEnabled("PlaceholderAPI")) {
|
||||
// try {
|
||||
// placeholderAPI = new PlaceholderAPIIntegration(plugin);
|
||||
// placeholderAPI.register();
|
||||
// plugin.loLogger().info("PlaceholderAPI integration enabled");
|
||||
// } catch (Exception e) {
|
||||
// plugin.loLogger().warn("Failed to enable PlaceholderAPI integration: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* Registers PlaceholderAPI expansion if available.
|
||||
*/
|
||||
private void initPlaceholderAPI() {
|
||||
if (isPluginEnabled("PlaceholderAPI")) {
|
||||
try {
|
||||
placeholderExpansion = new LoMinesPlaceholderExpansion(plugin);
|
||||
if (placeholderExpansion.register()) {
|
||||
plugin.loLogger().info("PlaceholderAPI integration enabled");
|
||||
plugin.loLogger().info("Available placeholders: %lomines_mine_<name>_percent%, %lomines_player_blocksmined%, etc.");
|
||||
} else {
|
||||
plugin.loLogger().warn("Failed to register PlaceholderAPI expansion");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.loLogger().warn("Failed to enable PlaceholderAPI integration: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkWorldGuard() {
|
||||
if (isPluginEnabled("WorldGuard")) {
|
||||
plugin.loLogger().info("WorldGuard detected (integration not yet implemented)");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOraxen() {
|
||||
if (isPluginEnabled("Oraxen")) {
|
||||
plugin.loLogger().info("Oraxen integration available");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkItemsAdder() {
|
||||
if (isPluginEnabled("ItemsAdder")) {
|
||||
plugin.loLogger().info("ItemsAdder integration available");
|
||||
plugin.loLogger().info("WorldGuard detected - auto-region creation enabled");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -67,8 +63,12 @@ public final class IntegrationManager {
|
|||
* Unregisters all integrations.
|
||||
*/
|
||||
public void shutdown() {
|
||||
// if (placeholderAPI != null) {
|
||||
// placeholderAPI.unregister();
|
||||
// }
|
||||
if (placeholderExpansion != null) {
|
||||
try {
|
||||
placeholderExpansion.unregister();
|
||||
} catch (Exception e) {
|
||||
plugin.loLogger().warn("Failed to unregister PlaceholderAPI expansion: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,142 @@
|
|||
package dev.loki.lomines.integration.placeholder;
|
||||
|
||||
import dev.loki.lomines.LoMinesPlugin;
|
||||
import dev.loki.lomines.core.Mine;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* PlaceholderAPI expansion for LoMines.
|
||||
* Provides placeholders for mine information and player statistics.
|
||||
*/
|
||||
public final class LoMinesPlaceholderExpansion extends PlaceholderExpansion {
|
||||
|
||||
private final LoMinesPlugin plugin;
|
||||
|
||||
public LoMinesPlaceholderExpansion(LoMinesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getIdentifier() {
|
||||
return "lomines";
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getAuthor() {
|
||||
return plugin.getDescription().getAuthors().isEmpty()
|
||||
? "loki"
|
||||
: plugin.getDescription().getAuthors().get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRegister() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String onPlaceholderRequest(Player player, @NotNull String params) {
|
||||
String[] parts = params.toLowerCase().split("_");
|
||||
if (parts.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return switch (parts[0]) {
|
||||
case "mine" -> handleMinePlaceholder(parts);
|
||||
case "player" -> handlePlayerPlaceholder(player, parts);
|
||||
case "count" -> String.valueOf(plugin.getMines().getAll().size());
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles mine-related placeholders.
|
||||
* Format: %lomines_mine_<name>_<attribute>%
|
||||
*/
|
||||
private @Nullable String handleMinePlaceholder(String[] parts) {
|
||||
if (parts.length < 3) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// parts[1] is mine name, parts[2] is attribute
|
||||
String mineName = parts[1];
|
||||
String attribute = parts[2];
|
||||
|
||||
Mine mine = plugin.getMines().find(mineName).orElse(null);
|
||||
if (mine == null) {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
return switch (attribute) {
|
||||
case "name" -> mine.getName();
|
||||
case "blocks" -> String.valueOf(mine.getBlocks());
|
||||
case "total" -> String.valueOf(mine.getTotalVolume());
|
||||
case "percent" -> String.format("%.1f", mine.getPercentFilled());
|
||||
case "percentint" -> String.valueOf((int) mine.getPercentFilled());
|
||||
case "world" -> mine.getConfig().worldName();
|
||||
case "remaining" -> String.valueOf(mine.getTotalVolume() - mine.getBlocks());
|
||||
case "resettime" -> formatResetTime(mine);
|
||||
case "resetseconds" -> String.valueOf(getResetSeconds(mine));
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles player-related placeholders.
|
||||
* Format: %lomines_player_<attribute>%
|
||||
*/
|
||||
private @Nullable String handlePlayerPlaceholder(Player player, String[] parts) {
|
||||
if (player == null || parts.length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String attribute = parts[1];
|
||||
var statsManager = plugin.getStatsManager();
|
||||
var stats = statsManager.getStats(player.getUniqueId());
|
||||
|
||||
return switch (attribute) {
|
||||
case "blocksmined" -> String.valueOf(stats.getTotalBlocksMined());
|
||||
case "minesreset" -> String.valueOf(stats.getMinesReset());
|
||||
case "playtime" -> formatPlayTime(stats.getPlayTimeMinutes());
|
||||
case "rank" -> String.valueOf(statsManager.getLeaderboard().getRank(player.getUniqueId()));
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private String formatResetTime(Mine mine) {
|
||||
int ticks = mine.getTicksSinceReset();
|
||||
int seconds = ticks / 20;
|
||||
int minutes = seconds / 60;
|
||||
int remainingSeconds = seconds % 60;
|
||||
return String.format("%d:%02d", minutes, remainingSeconds);
|
||||
}
|
||||
|
||||
private int getResetSeconds(Mine mine) {
|
||||
return mine.getTicksSinceReset() / 20;
|
||||
}
|
||||
|
||||
private String formatPlayTime(int minutes) {
|
||||
int hours = minutes / 60;
|
||||
int mins = minutes % 60;
|
||||
if (hours > 0) {
|
||||
return String.format("%dh %dm", hours, mins);
|
||||
}
|
||||
return String.format("%dm", mins);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue