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
@@ -13,7 +13,7 @@ import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.event.EventDispatcher;
|
||||
import net.william278.huskhomes.hook.EconomyHook;
|
||||
import net.william278.huskhomes.hook.MapHook;
|
||||
import net.william278.huskhomes.hook.PluginHook;
|
||||
import net.william278.huskhomes.hook.Hook;
|
||||
import net.william278.huskhomes.manager.Manager;
|
||||
import net.william278.huskhomes.network.Broker;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
@@ -161,21 +161,21 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
|
||||
void setServerSpawn(@NotNull Location location);
|
||||
|
||||
/**
|
||||
* Set of active {@link PluginHook}s running on the server
|
||||
* Set of active {@link Hook}s running on the server
|
||||
*
|
||||
* @return the {@link Set} of active {@link PluginHook}s
|
||||
* @return the {@link Set} of active {@link Hook}s
|
||||
*/
|
||||
@NotNull
|
||||
Set<PluginHook> getPluginHooks();
|
||||
Set<Hook> getPluginHooks();
|
||||
|
||||
/**
|
||||
* Finds the {@link PluginHook} of the given class instance from the set of active {@link PluginHook}s
|
||||
* Finds the {@link Hook} of the given class instance from the set of active {@link Hook}s
|
||||
*
|
||||
* @param hookClass the class of the {@link PluginHook} to get
|
||||
* @param <H> the type of the {@link PluginHook}
|
||||
* @return the {@link PluginHook} instance, or an empty {@link Optional} if not found
|
||||
* @param hookClass the class of the {@link Hook} to get
|
||||
* @param <H> the type of the {@link Hook}
|
||||
* @return the {@link Hook} instance, or an empty {@link Optional} if not found
|
||||
*/
|
||||
default <H extends PluginHook> Optional<H> getHook(@NotNull Class<H> hookClass) {
|
||||
default <H extends Hook> Optional<H> getHook(@NotNull Class<H> hookClass) {
|
||||
return getPluginHooks().stream()
|
||||
.filter(hook -> hook.getClass().isInstance(hookClass))
|
||||
.findFirst()
|
||||
|
||||
@@ -249,7 +249,7 @@ public class Settings {
|
||||
private boolean doMapHook = false;
|
||||
|
||||
@YamlKey("map_hook.map_plugin")
|
||||
private MapHook.MappingPlugin mappingPlugin = MapHook.MappingPlugin.DYNMAP;
|
||||
private MapHook.Plugin type = MapHook.Plugin.DYNMAP;
|
||||
|
||||
@YamlKey("map_hook.show_public_homes")
|
||||
private boolean publicHomesOnMap = true;
|
||||
@@ -479,8 +479,8 @@ public class Settings {
|
||||
return doMapHook;
|
||||
}
|
||||
|
||||
public MapHook.MappingPlugin getMappingPlugin() {
|
||||
return mappingPlugin;
|
||||
public MapHook.Plugin getMappingPlugin() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public boolean isPublicHomesOnMap() {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import com.flowpowered.math.vector.Vector2i;
|
||||
import de.bluecolored.bluemap.api.BlueMapAPI;
|
||||
import de.bluecolored.bluemap.api.BlueMapMap;
|
||||
import de.bluecolored.bluemap.api.BlueMapWorld;
|
||||
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
||||
import de.bluecolored.bluemap.api.markers.POIMarker;
|
||||
@@ -12,161 +12,131 @@ import net.william278.huskhomes.position.World;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Hook to display warps and public homes on <a href="https://github.com/BlueMap-Minecraft/BlueMap">BlueMap</a> maps
|
||||
*/
|
||||
public class BlueMapHook extends MapHook {
|
||||
|
||||
private String publicHomeMarkerIconPath;
|
||||
private String warpMarkerIconPath;
|
||||
private Map<String, MarkerSet> publicHomesMarkerSets;
|
||||
private Map<String, MarkerSet> warpsMarkerSets;
|
||||
|
||||
public BlueMapHook(@NotNull HuskHomes implementor) {
|
||||
super(implementor, "BlueMap");
|
||||
public BlueMapHook(@NotNull HuskHomes plugin) {
|
||||
super(plugin, Plugin.BLUEMAP);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Void> initializeMap() {
|
||||
final CompletableFuture<Void> initializedFuture = new CompletableFuture<>();
|
||||
plugin.runAsync(() -> BlueMapAPI.onEnable(blueMapAPI -> {
|
||||
// Create marker sets
|
||||
plugin.getWorlds().forEach(world -> blueMapAPI.getWorld(world.getUuid())
|
||||
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(map -> {
|
||||
if (plugin.getSettings().isPublicHomesOnMap()) {
|
||||
map.getMarkerSets().put(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID,
|
||||
MarkerSet.builder().label("Public Homes").build());
|
||||
}
|
||||
if (plugin.getSettings().isWarpsOnMap()) {
|
||||
map.getMarkerSets().put(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID,
|
||||
MarkerSet.builder().label("Warps").build());
|
||||
}
|
||||
})));
|
||||
public void initialize() {
|
||||
BlueMapAPI.onEnable(api -> {
|
||||
this.publicHomesMarkerSets = new HashMap<>();
|
||||
this.warpsMarkerSets = new HashMap<>();
|
||||
|
||||
// Create marker icons
|
||||
try {
|
||||
publicHomeMarkerIconPath = blueMapAPI.getWebApp().createImage(
|
||||
ImageIO.read(Objects.requireNonNull(plugin.getResource("markers/50x/" + PUBLIC_HOME_MARKER_IMAGE_NAME + ".png"))),
|
||||
"huskhomes/" + PUBLIC_HOMES_MARKER_SET_ID + ".png");
|
||||
warpMarkerIconPath = blueMapAPI.getWebApp().createImage(
|
||||
ImageIO.read(Objects.requireNonNull(plugin.getResource("markers/50x/" + WARP_MARKER_IMAGE_NAME + ".png"))),
|
||||
"huskhomes/" + WARP_MARKER_IMAGE_NAME + ".png");
|
||||
} catch (IOException e) {
|
||||
plugin.log(Level.SEVERE, "Failed to create warp marker image", e);
|
||||
for (World world : plugin.getWorlds()) {
|
||||
getMapWorld(world).ifPresent(mapWorld -> {
|
||||
final MarkerSet publicHomeMarkers = MarkerSet.builder().label("Public Homes").build();
|
||||
final MarkerSet warpsMarkers = MarkerSet.builder().label("Warps").build();
|
||||
|
||||
for (BlueMapMap map : mapWorld.getMaps()) {
|
||||
map.getMarkerSets().put(plugin.getKey(map.getId()).toString(), publicHomeMarkers);
|
||||
map.getMarkerSets().put(plugin.getKey(map.getId()).toString(), warpsMarkers);
|
||||
}
|
||||
|
||||
publicHomesMarkerSets.put(world.getName(), publicHomeMarkers);
|
||||
warpsMarkerSets.put(world.getName(), warpsMarkers);
|
||||
});
|
||||
}
|
||||
|
||||
initializedFuture.complete(null);
|
||||
}));
|
||||
return initializedFuture;
|
||||
this.populateMap();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> updateHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
|
||||
public void updateHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return removeHome(home).thenRun(() -> BlueMapAPI.getInstance().flatMap(
|
||||
blueMapAPI -> getBlueMapWorld(blueMapAPI, home.getWorld())).ifPresent(blueMapWorld -> blueMapWorld.getMaps()
|
||||
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().put(home.getOwner().getUuid() + ":" + home.getUuid(),
|
||||
POIMarker.toBuilder()
|
||||
.label("/phome" + home.getOwner().getUsername() + "." + home.getMeta().getName())
|
||||
.position((int) home.getX(), (int) home.getY(), (int) home.getZ())
|
||||
.icon(publicHomeMarkerIconPath, Vector2i.from(25, 25))
|
||||
.maxDistance(5000)
|
||||
.build());
|
||||
return markerSet;
|
||||
}))));
|
||||
getPublicHomesMarkerSet(home.getWorld())
|
||||
.ifPresent(markerSet -> {
|
||||
final String markerId = home.getOwner().getUuid() + ":" + home.getUuid();
|
||||
markerSet.remove(markerId);
|
||||
markerSet.put(markerId, POIMarker.builder()
|
||||
.label("/phome " + home.getOwner().getUsername() + "." + home.getName())
|
||||
.position(home.getX(), home.getY(), home.getZ())
|
||||
.maxDistance(5000)
|
||||
.defaultIcon() // todo: custom icon
|
||||
.build());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
|
||||
public void removeHome(@NotNull Home home) {
|
||||
getPublicHomesMarkerSet(home.getWorld())
|
||||
.ifPresent(markerSet -> markerSet.remove(home.getOwner().getUuid() + ":" + home.getUuid()));
|
||||
|
||||
BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, home.getWorld()))
|
||||
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().remove(home.getOwner().getUuid() + ":" + home.getUuid());
|
||||
return markerSet;
|
||||
})));
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearHomes(@NotNull User user) {
|
||||
BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds()
|
||||
.forEach(blueMapWorld -> blueMapWorld.getMaps()
|
||||
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().keySet().removeIf(key -> key.startsWith(user.getUuid().toString()));
|
||||
return markerSet;
|
||||
}))));
|
||||
|
||||
CompletableFuture.completedFuture(null);
|
||||
if (publicHomesMarkerSets != null) {
|
||||
for (MarkerSet markerSet : publicHomesMarkerSets.values()) {
|
||||
markerSet.getMarkers().keySet()
|
||||
.removeIf(markerId -> markerId.startsWith(user.getUuid().toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||
public void updateWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return removeWarp(warp).thenRun(() -> BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, warp.getWorld()))
|
||||
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().put(warp.getUuid().toString(),
|
||||
POIMarker.toBuilder()
|
||||
.label("/warp " + warp.getMeta().getName())
|
||||
.position((int) warp.getX(), (int) warp.getY(), (int) warp.getZ())
|
||||
.icon(warpMarkerIconPath, Vector2i.from(25, 25))
|
||||
.maxDistance(10000)
|
||||
.build());
|
||||
return markerSet;
|
||||
}))));
|
||||
getPublicHomesMarkerSet(warp.getWorld())
|
||||
.ifPresent(markerSet -> {
|
||||
final String markerId = warp.getUuid().toString();
|
||||
markerSet.remove(markerId);
|
||||
markerSet.put(markerId, POIMarker.builder()
|
||||
.label("/warp " + warp.getName())
|
||||
.position(warp.getX(), warp.getY(), warp.getZ())
|
||||
.maxDistance(5000)
|
||||
.defaultIcon() // todo: custom icon
|
||||
.build());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> removeWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||
|
||||
BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, warp.getWorld()))
|
||||
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().remove(warp.getUuid().toString());
|
||||
return markerSet;
|
||||
})));
|
||||
return CompletableFuture.completedFuture(null);
|
||||
public void removeWarp(@NotNull Warp warp) {
|
||||
getWarpsMarkerSet(warp.getWorld())
|
||||
.ifPresent(markerSet -> markerSet.remove(warp.getUuid().toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarps() {
|
||||
BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds()
|
||||
.forEach(blueMapWorld -> blueMapWorld.getMaps()
|
||||
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||
.computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
|
||||
markerSet.getMarkers().clear();
|
||||
return markerSet;
|
||||
}))));
|
||||
|
||||
CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link BlueMapWorld} for a world
|
||||
*
|
||||
* @param world The {@link World} to get the {@link BlueMapWorld} for
|
||||
* @return The {@link BlueMapWorld} of the world
|
||||
*/
|
||||
@NotNull
|
||||
private Optional<BlueMapWorld> getBlueMapWorld(@NotNull BlueMapAPI blueMapAPI, @NotNull World world) {
|
||||
if (world.getUuid().equals(new UUID(0, 0))) {
|
||||
return blueMapAPI.getWorld(world.getName());
|
||||
} else {
|
||||
return blueMapAPI.getWorld(world.getUuid());
|
||||
if (warpsMarkerSets != null) {
|
||||
for (MarkerSet markerSet : warpsMarkerSets.values()) {
|
||||
markerSet.getMarkers().keySet()
|
||||
.forEach(markerSet::remove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Optional<MarkerSet> getPublicHomesMarkerSet(@NotNull World world) {
|
||||
return publicHomesMarkerSets == null ? Optional.empty() : Optional.ofNullable(publicHomesMarkerSets.get(world.getName()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Optional<MarkerSet> getWarpsMarkerSet(@NotNull World world) {
|
||||
return warpsMarkerSets == null ? Optional.empty() : Optional.ofNullable(warpsMarkerSets.get(world.getName()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Optional<BlueMapWorld> getMapWorld(@NotNull World world) {
|
||||
return BlueMapAPI.getInstance().flatMap(api -> api.getWorld(world.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.dynmap.DynmapCommonAPI;
|
||||
import org.dynmap.DynmapCommonAPIListener;
|
||||
import org.dynmap.markers.Marker;
|
||||
import org.dynmap.markers.MarkerIcon;
|
||||
import org.dynmap.markers.MarkerSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Hook to display warps and public homes on Dynmap
|
||||
*/
|
||||
public class DynmapHook extends MapHook {
|
||||
|
||||
@Nullable
|
||||
private DynmapCommonAPI dynmapApi;
|
||||
@Nullable
|
||||
private MarkerSet publicHomesMarkers;
|
||||
@Nullable
|
||||
private MarkerSet warpsMarkers;
|
||||
|
||||
public DynmapHook(@NotNull HuskHomes plugin) {
|
||||
super(plugin, Plugin.DYNMAP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
DynmapCommonAPIListener.register(new DynmapCommonAPIListener() {
|
||||
@Override
|
||||
public void apiEnabled(@NotNull DynmapCommonAPI dynmapCommonAPI) {
|
||||
dynmapApi = dynmapCommonAPI;
|
||||
populateMap();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void populateMap() {
|
||||
getDynmap().ifPresent(api -> {
|
||||
if (plugin.getSettings().isPublicHomesOnMap()) {
|
||||
getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME).orElseThrow();
|
||||
api.getMarkerAPI().createMarkerSet(getPublicHomesKey(),
|
||||
"Public Homes", api.getMarkerAPI().getMarkerIcons(), false);
|
||||
}
|
||||
if (plugin.getSettings().isWarpsOnMap()) {
|
||||
getMarkerIcon(WARP_MARKER_IMAGE_NAME).orElseThrow();
|
||||
api.getMarkerAPI().createMarkerSet(getPublicHomesKey(),
|
||||
"Public Homes", api.getMarkerAPI().getMarkerIcons(), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateHome(@NotNull Home home) {
|
||||
if (!isValidPosition(home)) {
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.runSync(() -> {
|
||||
final String markerId = home.getOwner().getUuid() + ":" + home.getUuid();
|
||||
getPublicHomesMarkerSet().ifPresent(markerSet -> {
|
||||
markerSet.getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(markerId))
|
||||
.forEach(Marker::deleteMarker);
|
||||
markerSet.createMarker(markerId, home.getName(), home.getWorld().getName(),
|
||||
home.getX(), home.getY(), home.getZ(),
|
||||
getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME).orElseThrow(), false)
|
||||
.setDescription(MarkerInformationPopup.create(home.getName())
|
||||
.thumbnail(PUBLIC_HOME_MARKER_IMAGE_NAME)
|
||||
.field("Owner", home.getOwner().getUsername())
|
||||
.field("Description", plugin.getLocales().wrapText(home.getMeta().getDescription(), 60))
|
||||
.field("Command", "/phome " + home.getName())
|
||||
.toHtml());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeHome(@NotNull Home home) {
|
||||
plugin.runSync(() -> {
|
||||
final String markerId = home.getOwner().getUuid() + ":" + home.getUuid();
|
||||
getPublicHomesMarkerSet().ifPresent(markerSet -> markerSet.getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(markerId))
|
||||
.forEach(Marker::deleteMarker));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearHomes(@NotNull User user) {
|
||||
plugin.runSync(() -> getPublicHomesMarkerSet().ifPresent(markerSet -> markerSet.getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().startsWith(user.getUuid().toString()))
|
||||
.forEach(Marker::deleteMarker)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateWarp(@NotNull Warp warp) {
|
||||
if (!isValidPosition(warp)) {
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.runSync(() -> {
|
||||
final String markerId = warp.getUuid().toString();
|
||||
getWarpsMarkerSet().ifPresent(markerSet -> {
|
||||
markerSet.getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(markerId))
|
||||
.forEach(Marker::deleteMarker);
|
||||
markerSet.createMarker(markerId, warp.getName(), warp.getWorld().getName(),
|
||||
warp.getX(), warp.getY(), warp.getZ(),
|
||||
getMarkerIcon(WARP_MARKER_IMAGE_NAME).orElseThrow(), false)
|
||||
.setDescription(MarkerInformationPopup.create(warp.getName())
|
||||
.thumbnail(WARP_MARKER_IMAGE_NAME)
|
||||
.field("Description", plugin.getLocales().wrapText(warp.getMeta().getDescription(), 60))
|
||||
.field("Command", "/phome " + warp.getName())
|
||||
.toHtml());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeWarp(@NotNull Warp warp) {
|
||||
plugin.runSync(() -> {
|
||||
final String markerId = warp.getUuid().toString();
|
||||
getWarpsMarkerSet().ifPresent(markerSet -> markerSet.getMarkers().stream()
|
||||
.filter(marker -> marker.getMarkerID().equals(markerId))
|
||||
.forEach(Marker::deleteMarker));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarps() {
|
||||
plugin.runSync(() -> getWarpsMarkerSet().ifPresent(markerSet -> markerSet.getMarkers()
|
||||
.forEach(Marker::deleteMarker)));
|
||||
}
|
||||
|
||||
private Optional<DynmapCommonAPI> getDynmap() {
|
||||
return Optional.ofNullable(dynmapApi);
|
||||
}
|
||||
|
||||
private Optional<MarkerSet> getWarpsMarkerSet() {
|
||||
return getDynmap().map(api -> {
|
||||
warpsMarkers = api.getMarkerAPI().getMarkerSet(getWarpsKey());
|
||||
if (warpsMarkers == null) {
|
||||
warpsMarkers = api.getMarkerAPI().createMarkerSet(getWarpsKey(), "Warps",
|
||||
api.getMarkerAPI().getMarkerIcons(), false);
|
||||
} else {
|
||||
warpsMarkers.setMarkerSetLabel("Warps");
|
||||
}
|
||||
return warpsMarkers;
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<MarkerSet> getPublicHomesMarkerSet() {
|
||||
return getDynmap().map(api -> {
|
||||
publicHomesMarkers = api.getMarkerAPI().getMarkerSet(getPublicHomesKey());
|
||||
if (publicHomesMarkers == null) {
|
||||
publicHomesMarkers = api.getMarkerAPI().createMarkerSet(getPublicHomesKey(), "Public Homes",
|
||||
api.getMarkerAPI().getMarkerIcons(), false);
|
||||
} else {
|
||||
publicHomesMarkers.setMarkerSetLabel("Public Homes");
|
||||
}
|
||||
return publicHomesMarkers;
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<MarkerIcon> getMarkerIcon(@NotNull String imageName) {
|
||||
return getDynmap().map(api -> {
|
||||
MarkerIcon icon = api.getMarkerAPI().getMarkerIcon(imageName);
|
||||
if (icon == null) {
|
||||
icon = api.getMarkerAPI().createMarkerIcon(imageName, imageName,
|
||||
plugin.getResource("markers/16x/" + imageName + ".png"));
|
||||
}
|
||||
return icon;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 thumbnail(@NotNull String thumbnailMarkerId) {
|
||||
this.thumbnailMarkerId = thumbnailMarkerId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private DynmapHook.MarkerInformationPopup field(@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.escapeHtml4(title))
|
||||
.append("</span><br/>");
|
||||
fields.forEach((key, value) -> html.append("<span style=\"font-weight: bold;\">")
|
||||
.append(StringEscapeUtils.escapeHtml4(key))
|
||||
.append(": </span><span>")
|
||||
.append(StringEscapeUtils.escapeHtml4(value))
|
||||
.append("</span><br/>"));
|
||||
return html.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* A hook that provides economy features
|
||||
*/
|
||||
public abstract class EconomyHook extends PluginHook {
|
||||
public abstract class EconomyHook extends Hook {
|
||||
|
||||
protected EconomyHook(@NotNull HuskHomes implementor, @NotNull String hookName) {
|
||||
super(implementor, hookName);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* A plugin hook, where HuskHomes hooks into another plugin
|
||||
*/
|
||||
public abstract class Hook {
|
||||
|
||||
/**
|
||||
* The plugin that this hook is for
|
||||
*/
|
||||
protected final HuskHomes plugin;
|
||||
|
||||
/**
|
||||
* The name of the hook
|
||||
*/
|
||||
protected final String name;
|
||||
|
||||
/**
|
||||
* Construct a new {@link Hook}
|
||||
*
|
||||
* @param plugin the {@link HuskHomes} instance
|
||||
*/
|
||||
protected Hook(@NotNull HuskHomes plugin, @NotNull String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the hook and return {@code true} if it could be enabled
|
||||
*/
|
||||
public abstract void initialize() ;
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.SavedPosition;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.position.World;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -13,55 +12,44 @@ import java.util.concurrent.CompletableFuture;
|
||||
/**
|
||||
* A hook for a mapping plugin, such as Dynmap
|
||||
*/
|
||||
public abstract class MapHook extends PluginHook {
|
||||
public abstract class MapHook extends Hook {
|
||||
|
||||
protected static final String PUBLIC_HOMES_MARKER_SET_ID = "huskhomes-public-homes";
|
||||
protected static final String WARPS_MARKER_SET_ID = "huskhomes-warps";
|
||||
protected static final String WARP_MARKER_IMAGE_NAME = "warp";
|
||||
protected static final String PUBLIC_HOME_MARKER_IMAGE_NAME = "public-home";
|
||||
|
||||
protected MapHook(@NotNull HuskHomes implementor, @NotNull String hookName) {
|
||||
super(implementor, hookName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean initialize() {
|
||||
initializeMap().thenRun(() -> {
|
||||
if (plugin.getSettings().isPublicHomesOnMap()) {
|
||||
plugin.getDatabase()
|
||||
.getLocalPublicHomes(plugin)
|
||||
.forEach(this::updateHome);
|
||||
}
|
||||
if (plugin.getSettings().isWarpsOnMap()) {
|
||||
plugin.getDatabase()
|
||||
.getLocalWarps(plugin)
|
||||
.forEach(this::updateWarp);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
protected MapHook(@NotNull HuskHomes plugin, @NotNull MapHook.Plugin type) {
|
||||
super(plugin, type.getDisplayName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the map plugin for adding homes to
|
||||
*
|
||||
* @return a {@link CompletableFuture} that completes when the map plugin is ready
|
||||
* Populate the map with public homes and warps
|
||||
*/
|
||||
protected abstract CompletableFuture<Void> initializeMap();
|
||||
protected void populateMap() {
|
||||
if (plugin.getSettings().isPublicHomesOnMap()) {
|
||||
plugin.getDatabase()
|
||||
.getLocalPublicHomes(plugin)
|
||||
.forEach(this::updateHome);
|
||||
}
|
||||
if (plugin.getSettings().isWarpsOnMap()) {
|
||||
plugin.getDatabase()
|
||||
.getLocalWarps(plugin)
|
||||
.forEach(this::updateWarp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a home, adding it to the map if it exists, or updating it on the map if it doesn't
|
||||
*
|
||||
* @param home the home to update
|
||||
*/
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public abstract CompletableFuture<Void> updateHome(@NotNull Home home);
|
||||
public abstract void updateHome(@NotNull Home home);
|
||||
|
||||
/**
|
||||
* Removes a home from the map
|
||||
*
|
||||
* @param home the home to remove
|
||||
*/
|
||||
public abstract CompletableFuture<Void> removeHome(@NotNull Home home);
|
||||
public abstract void removeHome(@NotNull Home home);
|
||||
|
||||
/**
|
||||
* Clears homes owned by a player from the map
|
||||
@@ -75,15 +63,14 @@ public abstract class MapHook extends PluginHook {
|
||||
*
|
||||
* @param warp the warp to update
|
||||
*/
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public abstract CompletableFuture<Void> updateWarp(@NotNull Warp warp);
|
||||
public abstract void updateWarp(@NotNull Warp warp);
|
||||
|
||||
/**
|
||||
* Removes a warp from the map
|
||||
*
|
||||
* @param warp the warp to remove
|
||||
*/
|
||||
public abstract CompletableFuture<Void> removeWarp(@NotNull Warp warp);
|
||||
public abstract void removeWarp(@NotNull Warp warp);
|
||||
|
||||
/**
|
||||
* Clears all warps from the map
|
||||
@@ -98,30 +85,43 @@ public abstract class MapHook extends PluginHook {
|
||||
*/
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
protected final boolean isValidPosition(@NotNull SavedPosition position) {
|
||||
if (position instanceof Warp && !plugin.getSettings().isWarpsOnMap()) return false;
|
||||
if (position instanceof Home && !plugin.getSettings().isPublicHomesOnMap()) return false;
|
||||
|
||||
try {
|
||||
return position.getServer().equals(plugin.getServerName());
|
||||
} catch (IllegalStateException e) {
|
||||
return plugin.getWorlds().stream()
|
||||
.map(World::getUuid)
|
||||
.anyMatch(uuid -> uuid.equals(position.getWorld().getUuid()));
|
||||
if (position instanceof Warp && !plugin.getSettings().isWarpsOnMap()) {
|
||||
return false;
|
||||
}
|
||||
if (position instanceof Home && !plugin.getSettings().isPublicHomesOnMap()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return position.getServer().equals(plugin.getServerName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final String getPublicHomesKey() {
|
||||
return plugin.getKey(getName().toLowerCase(), "public_homes", "markers").toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final String getWarpsKey() {
|
||||
return plugin.getKey(getName().toLowerCase(), "warps", "markers").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies types of supported Map plugins
|
||||
*/
|
||||
public enum MappingPlugin {
|
||||
public enum Plugin {
|
||||
DYNMAP("Dynmap"),
|
||||
BLUEMAP("BlueMap");
|
||||
|
||||
@NotNull
|
||||
public final String displayName;
|
||||
private final String displayName;
|
||||
|
||||
MappingPlugin(@NotNull String displayName) {
|
||||
Plugin(@NotNull String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import java.util.logging.Level;
|
||||
/**
|
||||
* Hooks into Plan to provide the {@link PlanDataExtension} with stats on the web panel
|
||||
*/
|
||||
public class PlanHook extends PluginHook {
|
||||
public class PlanHook extends Hook {
|
||||
|
||||
private final HuskHomes plugin;
|
||||
|
||||
@@ -30,13 +30,12 @@ public class PlanHook extends PluginHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialize() {
|
||||
public void initialize() {
|
||||
if (!areAllCapabilitiesAvailable()) {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
registerDataExtension();
|
||||
handlePlanReload();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* A plugin hook, where HuskHomes hooks into another plugin
|
||||
*/
|
||||
public abstract class PluginHook {
|
||||
|
||||
/**
|
||||
* The plugin that this hook is for
|
||||
*/
|
||||
protected final HuskHomes plugin;
|
||||
|
||||
/**
|
||||
* The name of the hook
|
||||
*/
|
||||
protected final String hookName;
|
||||
|
||||
/**
|
||||
* Construct a new {@link PluginHook}
|
||||
*
|
||||
* @param implementor the {@link HuskHomes} instance
|
||||
*/
|
||||
protected PluginHook(@NotNull HuskHomes implementor, @NotNull String hookName) {
|
||||
this.plugin = implementor;
|
||||
this.hookName = hookName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the hook and return {@code true} if it could be enabled
|
||||
*
|
||||
* @return {@code true} if the hook could be enabled
|
||||
*/
|
||||
public abstract boolean initialize() ;
|
||||
|
||||
@NotNull
|
||||
public String getHookName() {
|
||||
return hookName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user