|
| 1 | +/* |
| 2 | + * This file is part of BlueMap, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package de.bluecolored.bluemap.api; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +import de.bluecolored.bluemap.api.renderer.BlueMapMap; |
| 32 | +import de.bluecolored.bluemap.api.renderer.BlueMapWorld; |
| 33 | +import de.bluecolored.bluemap.api.renderer.Renderer; |
| 34 | + |
| 35 | +public abstract class BlueMapAPI { |
| 36 | + private static BlueMapAPI instance; |
| 37 | + private static Collection<BlueMapAPIListener> listener = new ArrayList<>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Getter for the {@link Renderer} instance. |
| 41 | + * @return the {@link Renderer} |
| 42 | + */ |
| 43 | + public abstract Renderer getRenderer(); |
| 44 | + |
| 45 | + /** |
| 46 | + * Getter for all {@link BlueMapMap}s loaded by BlueMap. |
| 47 | + * @return an immutable collection of all loaded {@link BlueMapMap}s |
| 48 | + */ |
| 49 | + public abstract Collection<BlueMapMap> getMaps(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Getter for all {@link BlueMapWorld}s loaded by BlueMap. |
| 53 | + * @return an immutable collection of all loaded {@link BlueMapWorld}s |
| 54 | + */ |
| 55 | + public abstract Collection<BlueMapWorld> getWorlds(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Getter for the installed BlueMap version |
| 59 | + * @return the version-string |
| 60 | + */ |
| 61 | + public abstract String getBlueMapVersion(); |
| 62 | + |
| 63 | + /** |
| 64 | + * Register a listener that will be called when the API enables/disables |
| 65 | + * @param listener the {@link BlueMapAPIListener} |
| 66 | + */ |
| 67 | + public static synchronized void registerListener(BlueMapAPIListener listener) { |
| 68 | + BlueMapAPI.listener.add(listener); |
| 69 | + if (BlueMapAPI.instance != null) listener.onEnable(BlueMapAPI.instance); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Removes/Unregisters a previously registered listener |
| 74 | + * @param listener the {@link BlueMapAPIListener} instance that has been registered previously |
| 75 | + * |
| 76 | + * @return <code>true</code> if a listener was removed as a result of this call |
| 77 | + */ |
| 78 | + public static synchronized boolean unregisterListener(BlueMapAPIListener listener) { |
| 79 | + return BlueMapAPI.listener.remove(listener); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns an instance of {@link BlueMapAPI} if it is currently enabled, else an empty {@link Optional} is returned. |
| 84 | + * @return an {@link Optional}<{@link BlueMapAPI}> |
| 85 | + */ |
| 86 | + public static synchronized Optional<BlueMapAPI> getInstance() { |
| 87 | + return Optional.ofNullable(instance); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Used by BlueMap to register the API and call the listeners properly. |
| 92 | + * @param instance {@link BlueMapAPI}-instance |
| 93 | + */ |
| 94 | + static synchronized void registerInstance(BlueMapAPI instance) { |
| 95 | + if (BlueMapAPI.instance != null) throw new IllegalStateException("There already is an API instance registered!"); |
| 96 | + |
| 97 | + BlueMapAPI.instance = instance; |
| 98 | + |
| 99 | + for (BlueMapAPIListener listener : BlueMapAPI.listener) { |
| 100 | + listener.onEnable(BlueMapAPI.instance); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Used by BlueMap to unregister the API and call the listeners properly. |
| 106 | + */ |
| 107 | + static synchronized void unregisterInstance() { |
| 108 | + if (BlueMapAPI.instance == null) throw new IllegalStateException("There is no API instance registered!"); |
| 109 | + |
| 110 | + for (BlueMapAPIListener listener : BlueMapAPI.listener) { |
| 111 | + listener.onDisable(BlueMapAPI.instance); |
| 112 | + } |
| 113 | + |
| 114 | + BlueMapAPI.instance = null; |
| 115 | + } |
| 116 | +} |
0 commit comments