mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Pl3xMap: update to latest API, fabric platform support (#382)
This commit is contained in:
1 parent
0606e49d02
commit
3db08349cd
8 files changed
+148
-114
No files matched your search
@@ -359,6 +359,8 @@ public interface HuskHomes extends TaskRunner, EventDispatcher, SafetyResolver {
|
||||
getHooks().add(new DynmapHook(this));
|
||||
} else if (isDependencyLoaded("BlueMap")) {
|
||||
getHooks().add(new BlueMapHook(this));
|
||||
} else if (isDependencyLoaded("Pl3xMap")) {
|
||||
getHooks().add(new Pl3xMapHook(this));
|
||||
}
|
||||
}
|
||||
if (isDependencyLoaded("Plan")) {
|
||||
|
||||
@@ -23,7 +23,6 @@ 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;
|
||||
@@ -32,8 +31,6 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -41,6 +38,7 @@ import java.util.Optional;
|
||||
*/
|
||||
public class DynmapHook extends MapHook {
|
||||
|
||||
private static final String ICON_PATH = "/tiles/_markers_/";
|
||||
@Nullable
|
||||
private DynmapCommonAPI dynmapApi;
|
||||
@Nullable
|
||||
@@ -90,12 +88,9 @@ public class DynmapHook extends MapHook {
|
||||
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.getIdentifier())
|
||||
.toHtml());
|
||||
.setDescription(MarkerInformationPopup.publicHome(
|
||||
home, ICON_PATH + PUBLIC_HOME_MARKER_IMAGE_NAME, plugin
|
||||
).toHtml());
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -132,11 +127,9 @@ public class DynmapHook extends MapHook {
|
||||
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", "/warp " + warp.getName())
|
||||
.toHtml());
|
||||
.setDescription(MarkerInformationPopup.warp(
|
||||
warp, ICON_PATH + WARP_MARKER_IMAGE_NAME, plugin
|
||||
).toHtml());
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -198,61 +191,4 @@ public class DynmapHook extends MapHook {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,12 @@ import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.SavedPosition;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A hook for a mapping plugin, such as Dynmap
|
||||
@@ -134,4 +139,79 @@ public abstract class MapHook extends Hook {
|
||||
.orElse("Warps");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HTML Dynmap marker information popup widget
|
||||
*/
|
||||
protected static class MarkerInformationPopup {
|
||||
@NotNull
|
||||
private final String title;
|
||||
|
||||
@Nullable
|
||||
private String thumbnail;
|
||||
|
||||
@NotNull
|
||||
private final Map<String, String> fields;
|
||||
|
||||
private MarkerInformationPopup(@NotNull String title) {
|
||||
this.title = title;
|
||||
this.fields = new HashMap<>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static DynmapHook.MarkerInformationPopup warp(@NotNull Warp warp, @NotNull String thumbnail, @NotNull HuskHomes plugin) {
|
||||
return MarkerInformationPopup.create(warp.getName())
|
||||
.thumbnail(thumbnail)
|
||||
.field("Description", plugin.getLocales().wrapText(warp.getMeta().getDescription(), 60))
|
||||
.field("Location", warp.toString())
|
||||
.field("Command", "/warp " + warp.getSafeIdentifier());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static DynmapHook.MarkerInformationPopup publicHome(@NotNull Home home, @NotNull String thumbnail, @NotNull HuskHomes plugin) {
|
||||
return MarkerInformationPopup.create(home.getName())
|
||||
.thumbnail(thumbnail)
|
||||
.field("Owner", home.getOwner().getUsername())
|
||||
.field("Description", plugin.getLocales().wrapText(home.getMeta().getDescription(), 60))
|
||||
.field("Location", home.toString())
|
||||
.field("Command", "/phome " + home.getSafeIdentifier());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static DynmapHook.MarkerInformationPopup create(@NotNull String title) {
|
||||
return new MarkerInformationPopup(title);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected DynmapHook.MarkerInformationPopup thumbnail(@NotNull String thumbnail) {
|
||||
this.thumbnail = thumbnail;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected DynmapHook.MarkerInformationPopup field(@NotNull String key, @NotNull String value) {
|
||||
fields.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String toHtml() {
|
||||
final StringBuilder html = new StringBuilder();
|
||||
html.append("<div class=\"infowindow\">");
|
||||
if (thumbnail != null) {
|
||||
html.append("<img src=\"")
|
||||
.append(thumbnail)
|
||||
.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* This file is part of HuskHomes, licensed under the Apache License 2.0.
|
||||
*
|
||||
* Copyright (c) William278 <[email protected]>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.william278.huskhomes.hook;
|
||||
|
||||
import net.pl3x.map.core.Pl3xMap;
|
||||
import net.pl3x.map.core.event.EventHandler;
|
||||
import net.pl3x.map.core.event.EventListener;
|
||||
import net.pl3x.map.core.event.server.ServerLoadedEvent;
|
||||
import net.pl3x.map.core.event.world.WorldLoadedEvent;
|
||||
import net.pl3x.map.core.event.world.WorldUnloadedEvent;
|
||||
import net.pl3x.map.core.image.IconImage;
|
||||
import net.pl3x.map.core.markers.Point;
|
||||
import net.pl3x.map.core.markers.layer.SimpleLayer;
|
||||
import net.pl3x.map.core.markers.marker.Icon;
|
||||
import net.pl3x.map.core.markers.marker.Marker;
|
||||
import net.pl3x.map.core.markers.option.Options;
|
||||
import net.pl3x.map.core.markers.option.Popup;
|
||||
import net.pl3x.map.core.markers.option.Tooltip;
|
||||
import net.pl3x.map.core.world.World;
|
||||
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.user.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Pl3xMapHook extends MapHook implements EventListener {
|
||||
|
||||
private static final String ICON_PATH = "/images/icon/registered/";
|
||||
private static final String WARPS_LAYER = "warp_markers";
|
||||
private static final String PUBLIC_HOMES_LAYER = "public_home_markers";
|
||||
private final ConcurrentLinkedQueue<Home> publicHomes = new ConcurrentLinkedQueue<>();
|
||||
private final ConcurrentLinkedQueue<Warp> warps = new ConcurrentLinkedQueue<>();
|
||||
|
||||
public Pl3xMapHook(@NotNull HuskHomes plugin) {
|
||||
super(plugin, "Pl3xMap");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
Pl3xMap.api().getEventRegistry().register(this);
|
||||
|
||||
if (plugin.getSettings().doWarpsOnMap()) {
|
||||
this.registerIcon(WARPS_LAYER, "markers/16x/warp.png");
|
||||
}
|
||||
if (plugin.getSettings().doPublicHomesOnMap()) {
|
||||
this.registerIcon(PUBLIC_HOMES_LAYER, "markers/16x/public-home.png");
|
||||
}
|
||||
|
||||
plugin.runAsync(() -> {
|
||||
plugin.getDatabase().getLocalPublicHomes(plugin).forEach(this::updateHome);
|
||||
plugin.getDatabase().getLocalWarps(plugin).forEach(this::updateWarp);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateHome(@NotNull Home home) {
|
||||
publicHomes.remove(home);
|
||||
if (isValidPosition(home)) {
|
||||
publicHomes.add(home);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeHome(@NotNull Home home) {
|
||||
publicHomes.remove(home);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearHomes(@NotNull User user) {
|
||||
publicHomes.removeIf(home -> home.getOwner().equals(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateWarp(@NotNull Warp warp) {
|
||||
warps.remove(warp);
|
||||
if (isValidPosition(warp)) {
|
||||
warps.add(warp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeWarp(@NotNull Warp warp) {
|
||||
warps.remove(warp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWarps() {
|
||||
warps.clear();
|
||||
}
|
||||
|
||||
private void registerIcon(@NotNull String key, @NotNull String iconFileName) {
|
||||
try (InputStream iconStream = plugin.getResource(iconFileName)) {
|
||||
if (iconStream == null) {
|
||||
plugin.log(Level.WARNING, "Failed to load Pl3xMap icon (" + key + "): icon file not found");
|
||||
return;
|
||||
}
|
||||
Pl3xMap.api().getIconRegistry().register(new IconImage(key, ImageIO.read(iconStream), "png"));
|
||||
} catch (IOException e) {
|
||||
plugin.log(Level.WARNING, "Failed to load Pl3xMap icon (" + key + "): " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerLayers(@NotNull World world) {
|
||||
if (plugin.getSettings().doWarpsOnMap()) {
|
||||
WarpsLayer layer = new WarpsLayer(this, world);
|
||||
world.getLayerRegistry().register(layer);
|
||||
}
|
||||
if (plugin.getSettings().doPublicHomesOnMap()) {
|
||||
PublicHomesLayer layer = new PublicHomesLayer(this, world);
|
||||
world.getLayerRegistry().register(layer);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler
|
||||
public void onServerLoaded(@NotNull ServerLoadedEvent event) {
|
||||
Pl3xMap.api().getWorldRegistry().forEach(this::registerLayers);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler
|
||||
public void onWorldLoaded(@NotNull WorldLoadedEvent event) {
|
||||
registerLayers(event.getWorld());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@EventHandler
|
||||
public void onWorldUnloaded(@NotNull WorldUnloadedEvent event) {
|
||||
event.getWorld().getLayerRegistry().unregister(WARPS_LAYER);
|
||||
event.getWorld().getLayerRegistry().unregister(PUBLIC_HOMES_LAYER);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Options getMarkerOptions(@NotNull SavedPosition position) {
|
||||
return Options.builder()
|
||||
.tooltip(new Tooltip(position.getIdentifier()))
|
||||
.popup(new Popup(position instanceof Home home ? MarkerInformationPopup.publicHome(
|
||||
home, ICON_PATH + WARPS_LAYER, plugin
|
||||
).toHtml() : position instanceof Warp warp ? MarkerInformationPopup.warp(
|
||||
warp, ICON_PATH + PUBLIC_HOMES_LAYER, plugin
|
||||
).toHtml() : ""))
|
||||
.build();
|
||||
}
|
||||
|
||||
public static class WarpsLayer extends SimpleLayer {
|
||||
|
||||
private final Pl3xMapHook hook;
|
||||
private final World mapWorld;
|
||||
|
||||
public WarpsLayer(@NotNull Pl3xMapHook hook, @NotNull World mapWorld) {
|
||||
super(WARPS_LAYER, hook::getWarpsMarkerSetName);
|
||||
this.hook = hook;
|
||||
this.mapWorld = mapWorld;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Collection<Marker<?>> getMarkers() {
|
||||
return hook.warps.stream()
|
||||
.filter(warp -> warp.getWorld().getName().equals(mapWorld.getName()))
|
||||
.map(warp -> Icon.of(
|
||||
hook.plugin.getKey("warp_" + warp.getUuid()).asString(),
|
||||
Point.of(warp.getX(), warp.getZ()),
|
||||
WARPS_LAYER
|
||||
).setOptions(hook.getMarkerOptions(warp)))
|
||||
.collect(Collectors.toCollection(LinkedList::new));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class PublicHomesLayer extends SimpleLayer {
|
||||
|
||||
private final Pl3xMapHook hook;
|
||||
private final World mapWorld;
|
||||
|
||||
public PublicHomesLayer(@NotNull Pl3xMapHook hook, @NotNull World mapWorld) {
|
||||
super(PUBLIC_HOMES_LAYER, hook::getPublicHomesMarkerSetName);
|
||||
this.hook = hook;
|
||||
this.mapWorld = mapWorld;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Collection<Marker<?>> getMarkers() {
|
||||
return hook.publicHomes.stream()
|
||||
.filter(home -> home.getWorld().getName().equals(mapWorld.getName()))
|
||||
.map(home -> Marker.icon(
|
||||
hook.plugin.getKey("public_home_" + home.getUuid()).asString(),
|
||||
Point.of(home.getX(), home.getZ()),
|
||||
PUBLIC_HOMES_LAYER
|
||||
).setOptions(hook.getMarkerOptions(home)))
|
||||
.collect(Collectors.toCollection(LinkedList::new));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user