mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +00:00
Internalize PAPI hook, cache SavedUser data
This commit is contained in:
1 parent
d4be673581
commit
15227ee702
14 files changed
+193
-67
No files matched your search
@@ -29,6 +29,7 @@ import net.william278.huskhomes.random.RandomTeleportEngine;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bstats.charts.SimplePie;
|
||||
@@ -58,6 +59,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
* Metrics ID for <a href="https://bstats.org/plugin/bukkit/HuskHomes/8430">HuskHomes on Bukkit</a>.
|
||||
*/
|
||||
private static final int METRICS_ID = 8430;
|
||||
private Set<SavedUser> savedUsers;
|
||||
private Settings settings;
|
||||
private Locales locales;
|
||||
private Database database;
|
||||
@@ -91,6 +93,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
public void onEnable() {
|
||||
// Create adventure audience
|
||||
this.audiences = BukkitAudiences.create(this);
|
||||
this.savedUsers = new HashSet<>();
|
||||
this.globalPlayerList = new HashMap<>();
|
||||
this.currentlyOnWarmup = new HashSet<>();
|
||||
this.validator = new Validator(this);
|
||||
@@ -185,6 +188,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
|
||||
hooks.add(new PlanHook(this));
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
hooks.add(new PlaceholderAPIHook(this));
|
||||
}
|
||||
|
||||
if (hooks.size() > 0) {
|
||||
hooks.forEach(Hook::initialize);
|
||||
@@ -233,6 +239,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return Bukkit.getOnlinePlayers().stream().map(player -> (OnlineUser) BukkitUser.adapt(player)).toList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<SavedUser> getSavedUsers() {
|
||||
return savedUsers;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Settings getSettings() {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PlaceholderAPIHook extends Hook {
|
||||
|
||||
public PlaceholderAPIHook(@NotNull HuskHomes plugin) {
|
||||
super(plugin, "PlaceholderAPI");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
new HuskHomesExpansion(plugin).register();
|
||||
}
|
||||
|
||||
public static class HuskHomesExpansion extends PlaceholderExpansion {
|
||||
|
||||
@NotNull
|
||||
private final HuskHomes plugin;
|
||||
|
||||
private HuskHomesExpansion(@NotNull HuskHomes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String onRequest(OfflinePlayer offlinePlayer, @NotNull String params) {
|
||||
if (offlinePlayer == null || !offlinePlayer.isOnline() || offlinePlayer.getPlayer() == null) {
|
||||
return "Player not online";
|
||||
}
|
||||
|
||||
// Return the requested data
|
||||
final OnlineUser player = BukkitUser.adapt(offlinePlayer.getPlayer());
|
||||
return switch (params) {
|
||||
case "homes_count" -> String.valueOf(plugin.getManager().homes()
|
||||
.getUserHomes().get(player.getUsername())
|
||||
.size());
|
||||
case "max_homes" -> String.valueOf(player.getMaxHomes(
|
||||
plugin.getSettings().getMaxHomes(),
|
||||
plugin.getSettings().doStackPermissionLimits()
|
||||
));
|
||||
case "max_public_homes" -> String.valueOf(player.getMaxPublicHomes(
|
||||
plugin.getSettings().getMaxPublicHomes(),
|
||||
plugin.getSettings().doStackPermissionLimits()
|
||||
));
|
||||
case "free_home_slots" -> String.valueOf(player.getFreeHomes(
|
||||
plugin.getSettings().getFreeHomeSlots(),
|
||||
plugin.getSettings().doStackPermissionLimits()
|
||||
));
|
||||
case "home_slots" -> String.valueOf(plugin.getSavedUser(player)
|
||||
.map(SavedUser::getHomeSlots)
|
||||
.orElse(0));
|
||||
case "homes_list" -> String.join(", ", plugin.getManager()
|
||||
.homes().getUserHomes()
|
||||
.get(player.getUsername()));
|
||||
case "public_homes_count" -> String.valueOf(plugin.getManager().homes()
|
||||
.getPublicHomes().get(player.getUsername())
|
||||
.size());
|
||||
case "public_homes_list" -> String.join(", ", plugin.getManager().homes()
|
||||
.getPublicHomeNames());
|
||||
case "ignoring_tp_requests" -> getBooleanValue(plugin.getManager().requests()
|
||||
.isIgnoringRequests(player));
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getIdentifier() {
|
||||
return "huskhomes";
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getAuthor() {
|
||||
return "William278";
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getVersion() {
|
||||
return plugin.getVersion().toStringWithoutMetadata();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getBooleanValue(final boolean bool) {
|
||||
return bool ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user