Skip to content

Commit

Permalink
Re-added holohook
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite committed Dec 9, 2023
1 parent 93fbdef commit d176e35
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/dev/efnilite/ip/IP.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.efnilite.ip.config.Config;
import dev.efnilite.ip.config.Locales;
import dev.efnilite.ip.config.Option;
import dev.efnilite.ip.hook.HoloHook;
import dev.efnilite.ip.hook.PAPIHook;
import dev.efnilite.ip.mode.DefaultMode;
import dev.efnilite.ip.mode.Modes;
Expand Down Expand Up @@ -75,6 +76,12 @@ public void enable() {
Modes.init();
Menu.init(this);

// hook with hd / papi after gamemode leaderboards have initialized
if (getServer().getPluginManager().isPluginEnabled("HolographicDisplays")) {
logging.info("Connecting with Holographic Displays...");
HoloHook.init();
}

if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
logging.info("Connecting with PlaceholderAPI...");
placeholderHook = new PAPIHook();
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/dev/efnilite/ip/hook/HoloHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package dev.efnilite.ip.hook;

import dev.efnilite.ip.IP;
import dev.efnilite.ip.api.Registry;
import dev.efnilite.ip.leaderboard.Leaderboard;
import dev.efnilite.ip.leaderboard.Score;
import dev.efnilite.ip.mode.Mode;
import me.filoghost.holographicdisplays.api.HolographicDisplaysAPI;

public class HoloHook {

/**
* Initializes this hook.
*/
public static void init() {
try {
Class.forName("me.filoghost.holographicdisplays.api.HolographicDisplaysAPI");
} catch (Exception ex) {
IP.logging().warn("##");
IP.logging().warn("## IP only supports Holographic Displays v3.0.0 or higher!");
IP.logging().warn("## This hook will now be disabled.");
IP.logging().warn("##");
return;
}

HolographicDisplaysAPI.get(IP.getPlugin()).registerGlobalPlaceholder("ip_leaderboard", 100, argument -> {

if (argument == null) {
return "?";
}

// {ip_leaderboard: default, score, #1}
String[] split = argument.replace(" ", "").split(",");

Mode mode = Registry.getMode(split[0].toLowerCase());

if (mode == null) {
return "?";
}

Leaderboard leaderboard = mode.getLeaderboard();

String type = split[1].toLowerCase();
String rank = split[2].replace("#", "");

Score score = leaderboard.getScoreAtRank(Integer.parseInt(rank));

if (score == null) {
return "?";
}

return switch (type) {
case "score" -> Integer.toString(score.score());
case "name" -> score.name();
case "time" -> score.time();
case "difficulty" -> score.difficulty();
case "difficulty_string" -> parseDifficulty(score.difficulty());
default -> "?";
};
});
}

private static String parseDifficulty(String string) {
if (string.contains("?")) {
return "?";
}

double difficulty = Double.parseDouble(string);
if (difficulty <= 0.25) {
return "easy";
} else if (difficulty <= 0.5) {
return "medium";
} else if (difficulty <= 0.75) {
return "hard";
} else if (difficulty <= 1) {
return "very hard";
}
return "?";
}
}

0 comments on commit d176e35

Please sign in to comment.