Skip to content

Commit

Permalink
Add MapPlatformObservers API to handle platform reloads appropriately. (
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwolfg11 authored Oct 28, 2024
1 parent dcf8ae2 commit b99cde9
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public interface MapPlatform {
* </ul>
*
* @param callback Callback to execute
* @deprecated 3.0.0+ - Use {@link MapPlatformObserver} instead.
*/
default void onFirstInitialize(Runnable callback) {
callback.run();
Expand All @@ -67,11 +68,29 @@ default void onFirstInitialize(Runnable callback) {
* </ul>
*
* @param callback Callback to execute.
* @deprecated 3.0.0+ - Use {@link MapPlatformObserver} instead.
*/
default void onInitialize(Runnable callback) {
callback.run();
}

/**
* Register an observer to listen to platform events.
* Registering an already registered observer has no effect
* and will report as a registration failure.
*
* @return if registration was successful.
* @since 3.0.0
*/
boolean registerObserver(@NotNull MapPlatformObserver observer);

/**
* Unregister an observer.
* @return if unregistering was successful.
* @since 3.0.0
*/
boolean unregisterObserver(@NotNull MapPlatformObserver observer);

/**
* Check if the map plugin renders the specific world.
*
Expand Down Expand Up @@ -134,6 +153,8 @@ default void onInitialize(Runnable callback) {
* Indicate to the platform that it should switch to shutdown mode.
*
* The platform will still be able to process all operations during this period.
* @deprecated 3.0.0 - Platform cleanup does not need to be split into two phases.
*
*/
default void startShutdown() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Silverwolfg11
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.silverwolfg11.maptowny.platform;

/**
* Provides an observer interface for map-platform events.
* These functions may be called on any thread.
* <br><b>Note: Platforms may not implement all events.</b>
*
* @since 3.0.0
*/
public interface MapPlatformObserver {
/**
* Run initial logic for the observer.
* <br>
* Called immediately if the map-platform is already enabled
* or delayed until the map-platform is enabled for the first time.
* Only runs once after the observer is registered (not re-run on platform re-enable).
*/
default void onObserverSetup() {}

/**
* Called when the map-platform is enabling.
* <br><br>
* If the map-platform is already enabled when the observer is registered, then this is not called.
* If {@link #onObserverSetup()} has not been called when the platform is enabling,
* then {@link #onObserverSetup()} will be called and this will not.
* <br><br>
* At this stage, some platforms may not have started marker processing
* and may not have process queues, so to be safe avoid
* performing platform processing logic in this event.
*/
default void onPlatformEnabled() {}

/**
* Called when the map-platform is disabling.
* <br>
* At this stage, some platforms may have already stopped marker processing,
* so to be safe, avoid performing platform processing logic in this event.
*/
default void onPlatformDisabled() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2024 Silverwolfg11
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.silverwolfg11.maptowny.platform.bluemap;

import de.bluecolored.bluemap.api.BlueMapAPI;
import me.silverwolfg11.maptowny.platform.MapPlatformObserver;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

public class BlueMapObserverHandler {
private final CopyOnWriteArrayList<MapPlatformObserver> observers = new CopyOnWriteArrayList<>();
private final List<MapPlatformObserver> unsetupObservers = new ArrayList<>();

private final Consumer<BlueMapAPI> enableConsumer;
private final Consumer<BlueMapAPI> disableConsumer;

public BlueMapObserverHandler() {
enableConsumer = (BlueMapAPI) -> this.onBlueMapEnabled();
disableConsumer = (BlueMapAPI) -> this.onBlueMapDisabled();

BlueMapAPI.onEnable(enableConsumer);
BlueMapAPI.onDisable(disableConsumer);
}

public boolean registerObserver(MapPlatformObserver observer) {
if (observers.contains(observer)) {
return false;
}

synchronized (unsetupObservers) {
if (unsetupObservers.contains(observer)) {
return false;
}
}

setupObserver(observer);
return true;
}

public boolean unregisterObserver(MapPlatformObserver observer) {
if (observers.remove(observer)) {
return true;
}

synchronized (unsetupObservers) {
if (unsetupObservers.remove(observer)) {
return true;
}
}

return false;
}

public void disableObservers() {
BlueMapAPI.unregisterListener(enableConsumer);
BlueMapAPI.unregisterListener(disableConsumer);
}

private void setupObserver(MapPlatformObserver observer) {
if (BlueMapAPI.getInstance().isPresent()) {
observer.onObserverSetup();
observers.add(observer);
}
else {
synchronized (unsetupObservers) {
unsetupObservers.add(observer);
}
}
}

private void onBlueMapEnabled() {
for (MapPlatformObserver observer : observers) {
observer.onPlatformEnabled();
}

List<MapPlatformObserver> tmpObservers;
synchronized (unsetupObservers) {
tmpObservers = new ArrayList<>(unsetupObservers);
unsetupObservers.clear();
}

// Setup first-time observers
for (MapPlatformObserver observer : tmpObservers) {
observer.onObserverSetup();
}

observers.addAll(tmpObservers);
}

private void onBlueMapDisabled() {
for (MapPlatformObserver observer : observers) {
observer.onPlatformDisabled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@

import de.bluecolored.bluemap.api.BlueMapAPI;
import me.silverwolfg11.maptowny.platform.MapPlatform;
import me.silverwolfg11.maptowny.platform.MapPlatformObserver;
import me.silverwolfg11.maptowny.platform.MapWorld;
import me.silverwolfg11.maptowny.platform.bluemap.objects.WorldIdentifier;
import me.silverwolfg11.maptowny.schedulers.MapTownyScheduler;
import me.silverwolfg11.maptowny.schedulers.MapTownySchedulerFactory;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.image.BufferedImage;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class BlueMapPlatform implements MapPlatform {

private final JavaPlugin plugin;
private final BlueMapIconMapper iconMapper;
private final BlueMapObserverHandler observerHandler;

public BlueMapPlatform(JavaPlugin plugin) {
this.plugin = plugin;
this.iconMapper = new BlueMapIconMapper(plugin.getLogger());
observerHandler = new BlueMapObserverHandler();
}

@Override
Expand All @@ -54,23 +51,38 @@ public BlueMapPlatform(JavaPlugin plugin) {

@Override
public void onFirstInitialize(Runnable callback) {
// Use the future to unregister the listener
CompletableFuture<Void> future = new CompletableFuture<>();
Consumer<BlueMapAPI> apiConsumer = (api) -> {
callback.run();
// Have to delay it, otherwise it will end up concurrently modifying
// the onEnablers list.
MapTownySchedulerFactory.create(plugin).scheduleTask(() -> future.complete(null));
};
future.thenRun(() -> BlueMapAPI.unregisterListener(apiConsumer));

BlueMapAPI.onEnable(apiConsumer);
observerHandler.registerObserver(new MapPlatformObserver() {
@Override
public void onObserverSetup() {
callback.run();
}
});
}

@Override
public void onInitialize(final Runnable callback) {
// BlueMap API asynchronously initializes after the server starts ticking.
BlueMapAPI.onEnable(api -> callback.run());
// Implement both setup and enabled to maintain old behavior
observerHandler.registerObserver(new MapPlatformObserver() {
@Override
public void onObserverSetup() {
callback.run();
}

@Override
public void onPlatformEnabled() {
callback.run();
}
});
}

@Override
public boolean registerObserver(@NotNull MapPlatformObserver observer) {
return observerHandler.registerObserver(observer);
}

@Override
public boolean unregisterObserver(@NotNull MapPlatformObserver observer) {
return observerHandler.unregisterObserver(observer);
}

@Override
Expand Down Expand Up @@ -107,4 +119,9 @@ public boolean hasIcon(@NotNull String iconKey) {
public boolean unregisterIcon(@NotNull String iconKey) {
return iconMapper.unregisterIcon(iconKey);
}

@Override
public void shutdown() {
observerHandler.disableObservers();
}
}
Loading

0 comments on commit b99cde9

Please sign in to comment.