-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MapPlatformObservers API to handle platform reloads appropriately. (
#65)
- Loading branch information
1 parent
dcf8ae2
commit b99cde9
Showing
11 changed files
with
628 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
maptowny-api/src/main/java/me/silverwolfg11/maptowny/platform/MapPlatformObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
118 changes: 118 additions & 0 deletions
118
...emap/src/main/java/me/silverwolfg11/maptowny/platform/bluemap/BlueMapObserverHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.