mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Rewrite map hooks, use more modern APIs
This commit is contained in:
1 parent
167d5d6c7a
commit
98fa779046
15 files changed
+457
-463
No files matched your search
@@ -68,7 +68,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
private RandomTeleportEngine randomTeleportEngine;
|
||||
private Spawn serverSpawn;
|
||||
private UnsafeBlocks unsafeBlocks;
|
||||
private Set<PluginHook> pluginHooks;
|
||||
private Set<Hook> hooks;
|
||||
private List<Command> commands;
|
||||
private Map<String, List<String>> globalPlayerList;
|
||||
private Set<UUID> currentlyOnWarmup;
|
||||
@@ -168,12 +168,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
private void loadHooks() {
|
||||
this.pluginHooks = new HashSet<>();
|
||||
this.hooks = new HashSet<>();
|
||||
if (settings.doEconomy()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("RedisEconomy") != null) {
|
||||
pluginHooks.add(new RedisEconomyHook(this));
|
||||
hooks.add(new RedisEconomyHook(this));
|
||||
} else if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
|
||||
pluginHooks.add(new VaultEconomyHook(this));
|
||||
hooks.add(new VaultEconomyHook(this));
|
||||
}
|
||||
}
|
||||
if (settings.doMapHook()) {
|
||||
@@ -181,24 +181,24 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
case DYNMAP -> {
|
||||
final Plugin dynmapPlugin = Bukkit.getPluginManager().getPlugin("Dynmap");
|
||||
if (dynmapPlugin != null) {
|
||||
pluginHooks.add(new DynMapHook(this, dynmapPlugin));
|
||||
hooks.add(new DynmapHook(this));
|
||||
}
|
||||
}
|
||||
case BLUEMAP -> {
|
||||
if (Bukkit.getPluginManager().getPlugin("BlueMap") != null) {
|
||||
pluginHooks.add(new BlueMapHook(this));
|
||||
hooks.add(new BlueMapHook(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
|
||||
pluginHooks.add(new PlanHook(this));
|
||||
hooks.add(new PlanHook(this));
|
||||
}
|
||||
|
||||
if (pluginHooks.size() > 0) {
|
||||
pluginHooks.forEach(PluginHook::initialize);
|
||||
log(Level.INFO, "Registered " + pluginHooks.size() + " plugin hooks: " + pluginHooks.stream()
|
||||
.map(PluginHook::getHookName)
|
||||
if (hooks.size() > 0) {
|
||||
hooks.forEach(Hook::initialize);
|
||||
log(Level.INFO, "Registered " + hooks.size() + " plugin hooks: " + hooks.stream()
|
||||
.map(Hook::getName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
}
|
||||
@@ -314,8 +314,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Set<PluginHook> getPluginHooks() {
|
||||
return pluginHooks;
|
||||
public Set<Hook> getPluginHooks() {
|
||||
return hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -441,7 +441,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
metrics.addCustomChart(new SimplePie("using_economy", () -> Boolean.toString(getSettings().doEconomy())));
|
||||
metrics.addCustomChart(new SimplePie("using_map", () -> Boolean.toString(getSettings().doMapHook())));
|
||||
if (getSettings().doMapHook()) {
|
||||
metrics.addCustomChart(new SimplePie("map_type", () -> getSettings().getMappingPlugin().displayName));
|
||||
metrics.addCustomChart(new SimplePie("map_type", () -> getSettings().getMappingPlugin().getDisplayName()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log(Level.WARNING, "Failed to register metrics", e);
|
||||
@@ -484,6 +484,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
// Super constructor for unit testing
|
||||
@SuppressWarnings("unused")
|
||||
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file) {
|
||||
super(loader, description, dataFolder, file);
|
||||
}
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.command.BukkitCommand;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.dynmap.DynmapAPI;
|
||||
import org.dynmap.markers.Marker;
|
||||
import org.dynmap.markers.MarkerAPI;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Hook to display warps and public homes on Dynmap
|
||||
*/
|
||||
public class DynMapHook extends MapHook {
|
||||
|
||||
@NotNull
|
||||
private final DynmapAPI dynmapAPI;
|
||||
|
||||
public DynMapHook(@NotNull HuskHomes implementor, @NotNull Plugin dynmapPlugin) {
|
||||
super(implementor, "Dynmap");
|
||||
this.dynmapAPI = (DynmapAPI) dynmapPlugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Void> initializeMap() {
|
||||
final CompletableFuture<Void> initializedFuture = new CompletableFuture<>();
|
||||
plugin.runAsync(() -> {
|
||||
final MarkerAPI markerAPI = dynmapAPI.getMarkerAPI();
|
||||
if (plugin.getSettings().isPublicHomesOnMap()) {
|
||||
markerAPI.getMarkerIcons().stream()
|
||||
.filter(markerIcon -> markerIcon.getMarkerIconID().equals(PUBLIC_HOME_MARKER_IMAGE_NAME))
|
||||
.findFirst()
|
||||
.ifPresent(markerIcon -> markerAPI.getMarkerIcons().remove(markerIcon));
|
||||
markerAPI.createMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME, "Public Home",
|
||||
((BukkitHuskHomes) plugin).getResource("markers/16x/" + PUBLIC_HOME_MARKER_IMAGE_NAME + ".png"));
|
||||
markerAPI.createMarkerSet(PUBLIC_HOMES_MARKER_SET_ID,
|
||||
"Public Homes", dynmapAPI.getMarkerAPI().getMarkerIcons(), false);
|
||||
}
|
||||
if (plugin.getSettings().isWarpsOnMap()) {
|
||||
markerAPI.getMarkerIcons().stream()
|
||||
.filter(markerIcon -> markerIcon.getMarkerIconID().equals(WARP_MARKER_IMAGE_NAME))
|
||||
.findFirst()
|
||||
.ifPresent(markerIcon -> markerAPI.getMarkerIcons().remove(markerIcon));
|
||||
markerAPI.createMarkerIcon(WARP_MARKER_IMAGE_NAME, "Warp",
|
||||
((BukkitHuskHomes) plugin).getResource("markers/16x/" + WARP_MARKER_IMAGE_NAME + ".png"));
|
||||
markerAPI.createMarkerSet(WARPS_MARKER_SET_ID,
|
||||
"Warps", dynmapAPI.getMarkerAPI().getMarkerIcons(), false);
|
||||
}
|
||||
initializedFuture.complete(null);
|
||||
});
|
||||
return initializedFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> updateHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
|
||||
|
||||
final CompletableFuture<Void> updatedFuture = new CompletableFuture<>();
|
||||
removeHome(home).thenRun(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).createMarker(
|
||||
home.getOwner().getUuid() + ":" + home.getUuid(), home.getMeta().getName(), home.getWorld().getName(),
|
||||
home.getX(), home.getY(), home.getZ(), dynmapAPI.getMarkerAPI().getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), false)
|
||||
.setDescription(MarkerInformationPopup.create(home.getMeta().getName())
|
||||
.setThumbnailMarker(PUBLIC_HOME_MARKER_IMAGE_NAME)
|
||||
.addField("Owner", home.getOwner().getUsername())
|
||||
.addField("Description", plugin.getLocales().wrapText(home.getMeta().getDescription(), 60))
|
||||
.addField("Command", "/" + BukkitCommand.Type.PUBLIC_HOME_COMMAND.getCommand().getName() + " " + home.getMeta().getName())
|
||||
.toHtml());
|
||||
updatedFuture.complete(null);
|
||||
}));
|
||||
return updatedFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
|
||||
|
||||
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(home.getOwner().getUuid() + ":" + home.getUuid()))
|
||||
.findFirst()
|
||||
.ifPresent(Marker::deleteMarker);
|
||||
removedFuture.complete(null);
|
||||
});
|
||||
return removedFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearHomes(@NotNull User user) {
|
||||
final CompletableFuture<Void> clearedFuture = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().startsWith(user.getUuid().toString()))
|
||||
.forEach(Marker::deleteMarker);
|
||||
clearedFuture.complete(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||
|
||||
final CompletableFuture<Void> updatedFuture = new CompletableFuture<>();
|
||||
removeWarp(warp).thenRun(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).createMarker(
|
||||
warp.getUuid().toString(), warp.getMeta().getName(), warp.getWorld().getName(),
|
||||
warp.getX(), warp.getY(), warp.getZ(), dynmapAPI.getMarkerAPI().getMarkerIcon(WARP_MARKER_IMAGE_NAME), false)
|
||||
.setDescription(MarkerInformationPopup.create(warp.getMeta().getName())
|
||||
.setThumbnailMarker(WARP_MARKER_IMAGE_NAME)
|
||||
.addField("Description", plugin.getLocales().wrapText(warp.getMeta().getDescription(), 60))
|
||||
.addField("Command", "/" + BukkitCommand.Type.WARP_COMMAND.getCommand().getName() + " " + warp.getMeta().getName())
|
||||
.toHtml());
|
||||
updatedFuture.complete(null);
|
||||
}));
|
||||
return updatedFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||
|
||||
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
||||
CompletableFuture.runAsync(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(warp.getUuid().toString()))
|
||||
.findFirst()
|
||||
.ifPresent(Marker::deleteMarker);
|
||||
removedFuture.complete(null);
|
||||
}));
|
||||
return removedFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarps() {
|
||||
final CompletableFuture<Void> clearedFuture = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||
dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers()
|
||||
.forEach(Marker::deleteMarker);
|
||||
clearedFuture.complete(null);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HTML Dynmap marker information popup widget
|
||||
*/
|
||||
private static class MarkerInformationPopup {
|
||||
@NotNull
|
||||
private final String title;
|
||||
|
||||
@Nullable
|
||||
private String thumbnailMarkerId;
|
||||
|
||||
@NotNull
|
||||
private final Map<String, String> fields;
|
||||
|
||||
private MarkerInformationPopup(@NotNull String title) {
|
||||
this.title = title;
|
||||
this.fields = new HashMap<>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static DynMapHook.MarkerInformationPopup create(@NotNull String title) {
|
||||
return new MarkerInformationPopup(title);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private DynMapHook.MarkerInformationPopup setThumbnailMarker(@NotNull String thumbnailMarkerId) {
|
||||
this.thumbnailMarkerId = thumbnailMarkerId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private DynMapHook.MarkerInformationPopup addField(@NotNull String key, @NotNull String value) {
|
||||
fields.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String toHtml() {
|
||||
final StringBuilder html = new StringBuilder();
|
||||
html.append("<div class=\"infowindow\">");
|
||||
if (thumbnailMarkerId != null) {
|
||||
html.append("<img src=\"/tiles/_markers_/")
|
||||
.append(thumbnailMarkerId)
|
||||
.append(".png\" class=\"thumbnail\"/>")
|
||||
.append(" ");
|
||||
}
|
||||
html.append("<span style=\"font-weight: bold;\">")
|
||||
.append(StringEscapeUtils.escapeHtml(title))
|
||||
.append("</span><br/>");
|
||||
fields.forEach((key, value) -> html.append("<span style=\"font-weight: bold;\">")
|
||||
.append(StringEscapeUtils.escapeHtml(key))
|
||||
.append(": </span><span>")
|
||||
.append(StringEscapeUtils.escapeHtml(value))
|
||||
.append("</span><br/>"));
|
||||
return html.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,14 +11,14 @@ public class RedisEconomyHook extends VaultEconomyHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialize() {
|
||||
public void initialize() {
|
||||
RedisEconomyAPI api = RedisEconomyAPI.getAPI();
|
||||
if (api != null) {
|
||||
economy = api.getCurrencyByName(plugin.getSettings().getRedisEconomyName());
|
||||
if (economy != null) {
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
return super.initialize();
|
||||
super.initialize();
|
||||
}
|
||||
}
|
||||
@@ -21,14 +21,12 @@ public class VaultEconomyHook extends EconomyHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialize() {
|
||||
public void initialize() {
|
||||
final RegisteredServiceProvider<Economy> economyProvider = ((BukkitHuskHomes) plugin).getServer()
|
||||
.getServicesManager().getRegistration(Economy.class);
|
||||
if (economyProvider != null) {
|
||||
economy = economyProvider.getProvider();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user