Fix hook retrieval not working properly

This commit is contained in:
William278 committed 2023-03-22 15:58:50 +00:00
1 parent c316d290c5
commit 42adc13425
6 files changed
+66 -86

No files matched your search

@@ -166,38 +166,21 @@ public interface HuskHomes extends TaskRunner, EventDispatcher {
* @return the {@link Set} of active {@link Hook}s
*/
@NotNull
Set<Hook> getPluginHooks();
List<Hook> getHooks();
/**
* Finds the {@link Hook} of the given class instance from the set of active {@link Hook}s
*
* @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 Hook> Optional<H> getHook(@NotNull Class<H> hookClass) {
return getPluginHooks().stream()
.filter(hook -> hook.getClass() == hookClass)
.findFirst()
.map(hookClass::cast);
default <T extends Hook> Optional<T> getHook(@NotNull Class<T> hookClass) {
return getHooks().stream()
.filter(hook -> hookClass.isAssignableFrom(hook.getClass()))
.map(hookClass::cast)
.findFirst();
}
/**
* Gets the {@link MapHook} being used to display public homes and warps on a web map, if there is one, and it is enabled
*
* @return the {@link MapHook} optionally being used
*/
default Optional<MapHook> getMapHook() {
return getSettings().doMapHook() ? getHook(MapHook.class) : Optional.empty();
}
/**
* Gets the {@link EconomyHook} being used to charge players for commands, if there is one, and it is enabled
*
* @return the {@link EconomyHook} optionally being used
*/
default Optional<EconomyHook> getEconomyHook() {
return getSettings().doEconomy() ? getHook(EconomyHook.class) : Optional.empty();
return getHook(EconomyHook.class);
}
default Optional<MapHook> getMapHook() {
return getHook(MapHook.class);
}
/**
@@ -483,11 +483,11 @@ public class Settings {
return type;
}
public boolean isPublicHomesOnMap() {
public boolean doPublicHomesOnMap() {
return publicHomesOnMap;
}
public boolean isWarpsOnMap() {
public boolean doWarpsOnMap() {
return warpsOnMap;
}
@@ -10,6 +10,7 @@ import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.util.ThrowingConsumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -21,7 +22,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
/**
@@ -43,7 +43,7 @@ public class BlueMapHook extends MapHook {
this.warpsMarkerSets = new HashMap<>();
for (World world : plugin.getWorlds()) {
getMapWorld(world).ifPresent(mapWorld -> {
this.editMapWorld(world, (mapWorld -> {
final MarkerSet publicHomeMarkers = MarkerSet.builder().label(getPublicHomesMarkerSetName()).build();
final MarkerSet warpsMarkers = MarkerSet.builder().label(getWarpsMarkerSetName()).build();
@@ -54,7 +54,7 @@ public class BlueMapHook extends MapHook {
publicHomesMarkerSets.put(world.getName(), publicHomeMarkers);
warpsMarkerSets.put(world.getName(), warpsMarkers);
});
}));
}
this.populateMap();
@@ -67,33 +67,30 @@ public class BlueMapHook extends MapHook {
return;
}
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)
.icon(getIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), 25, 25)
.build());
});
this.editPublicHomesMarkerSet(home.getWorld(), (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)
.icon(getIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), 25, 25)
.build());
}));
}
@Override
public void removeHome(@NotNull Home home) {
getPublicHomesMarkerSet(home.getWorld())
.ifPresent(markerSet -> markerSet.remove(home.getOwner().getUuid() + ":" + home.getUuid()));
this.editPublicHomesMarkerSet(home.getWorld(), markerSet -> markerSet
.remove(home.getOwner().getUuid() + ":" + home.getUuid()));
}
@Override
public void clearHomes(@NotNull User user) {
if (publicHomesMarkerSets != null) {
for (MarkerSet markerSet : publicHomesMarkerSets.values()) {
markerSet.getMarkers().keySet()
.removeIf(markerId -> markerId.startsWith(user.getUuid().toString()));
}
publicHomesMarkerSets.values().forEach(markerSet -> markerSet.getMarkers().keySet()
.removeIf(markerId -> markerId.startsWith(user.getUuid().toString())));
}
}
@@ -103,32 +100,27 @@ public class BlueMapHook extends MapHook {
return;
}
getWarpsMarkerSet(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)
.icon(getIcon(WARP_MARKER_IMAGE_NAME), 25, 25)
.build());
});
this.editWarpsMarkerSet(warp.getWorld(), (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)
.icon(getIcon(WARP_MARKER_IMAGE_NAME), 25, 25)
.build());
}));
}
@Override
public void removeWarp(@NotNull Warp warp) {
getWarpsMarkerSet(warp.getWorld())
.ifPresent(markerSet -> markerSet.remove(warp.getUuid().toString()));
editWarpsMarkerSet(warp.getWorld(), markerSet -> markerSet.remove(warp.getUuid().toString()));
}
@Override
public void clearWarps() {
if (warpsMarkerSets != null) {
for (MarkerSet markerSet : warpsMarkerSets.values()) {
markerSet.getMarkers().keySet()
.forEach(markerSet::remove);
}
warpsMarkerSets.values().forEach(markerSet -> markerSet.getMarkers().clear());
}
}
@@ -157,19 +149,24 @@ public class BlueMapHook extends MapHook {
}).orElse(null);
}
@NotNull
private Optional<MarkerSet> getPublicHomesMarkerSet(@NotNull World world) {
return publicHomesMarkerSets == null ? Optional.empty() : Optional.ofNullable(publicHomesMarkerSets.get(world.getName()));
private void editPublicHomesMarkerSet(@NotNull World world, @NotNull ThrowingConsumer<MarkerSet> editor) {
editMapWorld(world, (mapWorld -> {
if (publicHomesMarkerSets != null) {
editor.accept(publicHomesMarkerSets.get(world.getName()));
}
}));
}
@NotNull
private Optional<MarkerSet> getWarpsMarkerSet(@NotNull World world) {
return warpsMarkerSets == null ? Optional.empty() : Optional.ofNullable(warpsMarkerSets.get(world.getName()));
private void editWarpsMarkerSet(@NotNull World world, @NotNull ThrowingConsumer<MarkerSet> editor) {
editMapWorld(world, (mapWorld -> {
if (warpsMarkerSets != null) {
editor.accept(warpsMarkerSets.get(world.getName()));
}
}));
}
@NotNull
private Optional<BlueMapWorld> getMapWorld(@NotNull World world) {
return BlueMapAPI.getInstance().flatMap(api -> api.getWorld(world.getName()));
private void editMapWorld(@NotNull World world, @NotNull ThrowingConsumer<BlueMapWorld> editor) {
BlueMapAPI.getInstance().flatMap(api -> api.getWorld(world.getName())).ifPresent(editor);
}
}
@@ -40,12 +40,12 @@ public class DynmapHook extends MapHook {
public void apiEnabled(@NotNull DynmapCommonAPI dynmapCommonAPI) {
dynmapApi = dynmapCommonAPI;
if (plugin.getSettings().isPublicHomesOnMap()) {
if (plugin.getSettings().doPublicHomesOnMap()) {
getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME).orElseThrow();
dynmapApi.getMarkerAPI().createMarkerSet(getPublicHomesKey(),
getPublicHomesMarkerSetName(), dynmapApi.getMarkerAPI().getMarkerIcons(), false);
}
if (plugin.getSettings().isWarpsOnMap()) {
if (plugin.getSettings().doWarpsOnMap()) {
getMarkerIcon(WARP_MARKER_IMAGE_NAME).orElseThrow();
dynmapApi.getMarkerAPI().createMarkerSet(getWarpsKey(),
getWarpsMarkerSetName(), dynmapApi.getMarkerAPI().getMarkerIcons(), false);
@@ -23,12 +23,12 @@ public abstract class MapHook extends Hook {
* Populate the map with public homes and warps
*/
protected void populateMap() {
if (plugin.getSettings().isPublicHomesOnMap()) {
if (plugin.getSettings().doPublicHomesOnMap()) {
plugin.getDatabase()
.getLocalPublicHomes(plugin)
.forEach(this::updateHome);
}
if (plugin.getSettings().isWarpsOnMap()) {
if (plugin.getSettings().doWarpsOnMap()) {
plugin.getDatabase()
.getLocalWarps(plugin)
.forEach(this::updateWarp);
@@ -83,14 +83,14 @@ public abstract class MapHook extends Hook {
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
protected final boolean isValidPosition(@NotNull SavedPosition position) {
if (position instanceof Warp && !plugin.getSettings().isWarpsOnMap()) {
if (position instanceof Warp && !plugin.getSettings().doWarpsOnMap()) {
return false;
}
if (position instanceof Home && !plugin.getSettings().isPublicHomesOnMap()) {
if (position instanceof Home && !plugin.getSettings().doPublicHomesOnMap()) {
return false;
}
return position.getServer().equals(plugin.getServerName());
return !plugin.getSettings().isCrossServer() || position.getServer().equals(plugin.getServerName());
}
@NotNull