Skip to content

Commit daebd9a

Browse files
committed
Add Renderer-API and API-Listener
1 parent 982f01c commit daebd9a

File tree

7 files changed

+494
-0
lines changed

7 files changed

+494
-0
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ repositories {
1010
}
1111
}
1212

13+
dependencies {
14+
compile 'com.flowpowered:flow-math:1.0.3'
15+
}
16+
1317
apply plugin: 'java'
1418

1519
group = 'de.bluecolored.bluemap.api'
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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}&lt;{@link BlueMapAPI}&gt;
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
public interface BlueMapAPIListener {
28+
29+
/**
30+
* Called when BlueMap has been loaded and started and the API is ready to use.<br>
31+
* If {@link BlueMapAPI} is already enabled when this listener is registered this method will be called immediately <i>(on the same thread)</i>!
32+
* <p><i>(Note: This method will likely be called asynchronously, <b>not</b> on the server-thread!</i><p>
33+
* @param blueMapApi the {@link BlueMapAPI}
34+
*/
35+
default void onEnable(BlueMapAPI blueMapApi) {}
36+
37+
/**
38+
* Called <b>before</b> BlueMap is being unloaded and stopped, after this method returns the API is no longer usable!<br>
39+
* Unlike {@link BlueMapAPIListener#onEnable(BlueMapAPI)}, if {@link BlueMapAPI} is not enabled when this listener is registered this method will <b>not</b> be called immediately.
40+
* <p><i>(Note: This method will likely be called asynchronously, <b>not</b> on the server-thread!</i><p>
41+
* @param blueMapApi
42+
*/
43+
default void onDisable(BlueMapAPI blueMapApi) {}
44+
45+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.renderer;
26+
27+
import com.flowpowered.math.vector.Vector2i;
28+
import com.flowpowered.math.vector.Vector3d;
29+
import com.flowpowered.math.vector.Vector3i;
30+
31+
public interface BlueMapMap {
32+
33+
/**
34+
* Returns this maps id, this is equal to the id configured in bluemap's config for this map.
35+
* @return the id of this map
36+
*/
37+
String getId();
38+
39+
/**
40+
* Returns this maps display-name, this is equal to the name configured in bluemap's config for this map.
41+
* @return the name of this map
42+
*/
43+
String getName();
44+
45+
/**
46+
* Getter for the {@link BlueMapWorld} of this map.
47+
* @return the {@link BlueMapWorld} of this map
48+
*/
49+
BlueMapWorld getWorld();
50+
51+
/**
52+
* Getter for the size of all tiles on this map in blocks.
53+
* @return the tile-size in blocks
54+
*/
55+
Vector2i getTileSize();
56+
57+
/**
58+
* Getter for the offset of the tile-grid on this map.<br>
59+
* E.g. an offset of (2|-1) would mean that the tile (0|0) has block (2|0|-1) at it's min-corner.
60+
* @return the tile-offset in blocks
61+
*/
62+
Vector2i getTileOffset();
63+
64+
/**
65+
* Converts a block-position to a map-tile-coordinate for this map
66+
*/
67+
default Vector2i posToTile(double blockX, double blockZ){
68+
Vector2i offset = getTileOffset();
69+
Vector2i size = getTileSize();
70+
71+
return Vector2i.from(
72+
(int) Math.floor((blockX - offset.getX()) / size.getX()),
73+
(int) Math.floor((blockZ - offset.getY()) / size.getY())
74+
);
75+
}
76+
77+
/**
78+
* Converts a block-position to a map-tile-coordinate for this map
79+
*/
80+
default Vector2i posToTile(Vector3i pos){
81+
return posToTile(pos.getX(), pos.getZ());
82+
}
83+
84+
/**
85+
* Converts a block-position to a map-tile-coordinate for this map
86+
*/
87+
default Vector2i posToTile(Vector3d pos){
88+
return posToTile(pos.getX(), pos.getZ());
89+
}
90+
91+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.renderer;
26+
27+
import java.nio.file.Path;
28+
import java.util.Collection;
29+
import java.util.UUID;
30+
31+
public interface BlueMapWorld {
32+
33+
/**
34+
* <p>Getter for the {@link UUID} of the world.</p>
35+
* <p>
36+
* The {@link UUID} of this world is <b>not</b> guaranteed to be consistent across reloads/restarts!
37+
* </p>
38+
* <p>
39+
* <b>Implementation notes:</b><br>
40+
* The used UUID highly depends on the implementation
41+
* <table>
42+
* <tr><th>Sponge</th><td>The UUID is equal to the returned UUID by World-Instances of the Sponge-API, so you can just use <code>spongeWorld.getUniqueId()</code><td><tr>
43+
* <tr><th>Bukkit</th><td>The UUID is equal to the returned UUID by World-Instances of the Bukkit-API, so you can just use <code>bukkitWorld.getUID()</code><td><tr>
44+
* <tr><th>Forge</th><td>The UUID is randomly generated, and changes on each reload/restart</code><td><tr>
45+
* <tr><th>CLI</th><td>The UUID is randomly generated, and changes on each reload/restart</code><td><tr>
46+
* </table>
47+
*
48+
* @return the {@link UUID} of the world
49+
*/
50+
UUID getUuid();
51+
52+
/**
53+
* Getter for the {@link Path} of this world's save-files (folder). This matches the folder configured in bluemap's config for this map ( <code>world:</code> ).
54+
* @return the save-folder of this world.
55+
*/
56+
Path getSaveFolder();
57+
58+
/**
59+
* Getter for all {@link BlueMapMap}s for this world
60+
* @return a {@link Collection} of all {@link BlueMapMap}s for this world
61+
*/
62+
Collection<BlueMapMap> getMaps();
63+
64+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.renderer;
26+
27+
import java.util.function.Consumer;
28+
29+
import com.flowpowered.math.vector.Vector2i;
30+
31+
public interface RenderTicket {
32+
33+
/**
34+
* <p>Getter for the {@link BlueMapMap} this ticket belongs to.</p>
35+
* @return the map for this ticket
36+
*/
37+
BlueMapMap getMap();
38+
39+
/**
40+
* <p>Getter for the tile coordinates that this ticket renders.</p>
41+
* @return the tile this ticket renders
42+
*/
43+
Vector2i getTile();
44+
45+
/**
46+
* The state of the ticket. This also returns <code>true</code> if the ticket has been processed but has failed to render the tile.
47+
* @return <code>true</code> if this ticket has been processed, <code>false</code> otherwise.
48+
*/
49+
boolean isCompleted();
50+
51+
/**
52+
* Every listener registered with this method will be called (in an undefined order) after this ticket has been completed.
53+
* <p>
54+
* <b>Important:</b><br>
55+
* The callback-method will likely (but is not guaranteed to) be called <b>asynchronously, on the render-thread!</b><br>
56+
* You'll need to handle any further threading, scheduling or synchronizing yourself.
57+
* </p>
58+
* @param callback the method to be called on completion
59+
*/
60+
void onCompletion(Consumer<RenderTicket> callback);
61+
62+
}

0 commit comments

Comments
 (0)