Skip to content

Commit 11707a5

Browse files
committed
feat(nav): ✨ Fully Reload BlueMap markers when smt changes + make it opt out
Closes #74.
1 parent 0af5630 commit 11707a5

5 files changed

Lines changed: 140 additions & 24 deletions

File tree

src/main/java/net/buildtheearth/buildteamtools/modules/navigation/NavigationModule.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.buildtheearth.buildteamtools.modules.navigation;
22

33
import lombok.Getter;
4+
import net.buildtheearth.buildteamtools.BuildTeamTools;
45
import net.buildtheearth.buildteamtools.modules.Module;
56
import net.buildtheearth.buildteamtools.modules.navigation.components.bluemap.BluemapComponent;
67
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.NavigatorComponent;
@@ -17,6 +18,8 @@
1718
import net.buildtheearth.buildteamtools.modules.navigation.components.warps.listeners.WarpJoinListener;
1819
import net.buildtheearth.buildteamtools.modules.network.NetworkModule;
1920
import net.buildtheearth.buildteamtools.utils.WikiLinks;
21+
import net.buildtheearth.buildteamtools.utils.io.ConfigPaths;
22+
import net.buildtheearth.buildteamtools.utils.io.ConfigUtil;
2023
import org.bukkit.Bukkit;
2124

2225
/**
@@ -56,7 +59,14 @@ public void enable() {
5659
warpsComponent = new WarpsComponent();
5760
navigatorComponent = new NavigatorComponent();
5861
tpllComponent = new TpllComponent();
59-
if (Bukkit.getPluginManager().isPluginEnabled("BlueMap")) bluemapComponent = new BluemapComponent();
62+
63+
// Check if BlueMap plugin is enabled and config allows BlueMap integration
64+
boolean bluemapConfigEnabled = BuildTeamTools.getInstance().getConfig(ConfigUtil.NAVIGATION)
65+
.getBoolean(ConfigPaths.Navigation.BLUEMAP_ENABLED, true);
66+
67+
if (Bukkit.getPluginManager().isPluginEnabled("BlueMap") && bluemapConfigEnabled) {
68+
bluemapComponent = new BluemapComponent();
69+
}
6070

6171
super.enable();
6272
}

src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/bluemap/BluemapComponent.java

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.WarpGroup;
1212
import net.buildtheearth.buildteamtools.modules.network.NetworkModule;
1313
import net.buildtheearth.buildteamtools.utils.geo.CoordinateConversion;
14+
import net.buildtheearth.buildteamtools.utils.io.ConfigPaths;
15+
import net.buildtheearth.buildteamtools.utils.io.ConfigUtil;
1416
import net.kyori.adventure.text.Component;
1517
import net.kyori.adventure.text.format.NamedTextColor;
1618
import org.bukkit.Bukkit;
@@ -34,20 +36,38 @@
3436
* If a world doesn't exist or isn't loaded in BlueMap, its warps are simply skipped without
3537
* causing errors.
3638
* </p>
39+
* <p>
40+
* This component can be disabled via configuration by setting {@code bluemap.enabled} to false
41+
* in the navigation config. Live updates are supported - when warps or warp groups change,
42+
* all markers are cleared and re-registered.
43+
* </p>
3744
*/
3845
public class BluemapComponent extends ModuleComponent {
3946

4047
/**
4148
* Initializes the BlueMap component and registers warp markers.
4249
* <p>
43-
* Checks if BlueMapAPI is available before proceeding. If not, the component is disabled.
44-
* Once BlueMap is ready, all warp groups from the current BuildTeam are processed and
45-
* their warps are registered as markers, organized by world.
50+
* Checks if BlueMapAPI is available and if the component is enabled in config.
51+
* If either check fails, the component is disabled. Once BlueMap is ready, all warp groups
52+
* from the current BuildTeam are processed and their warps are registered as markers,
53+
* organized by world.
4654
* </p>
4755
*/
4856
public BluemapComponent() {
4957
super("BlueMap");
5058

59+
// Check if BlueMap integration is enabled in config
60+
boolean isEnabled = BuildTeamTools.getInstance().getConfig(ConfigUtil.NAVIGATION)
61+
.getBoolean(ConfigPaths.Navigation.BLUEMAP_ENABLED, true);
62+
63+
if (!isEnabled) {
64+
BuildTeamTools.getInstance().getComponentLogger().info(
65+
Component.text("BlueMap integration is disabled in configuration.", NamedTextColor.YELLOW)
66+
);
67+
disable();
68+
return;
69+
}
70+
5171
Optional<BlueMapAPI> optionalApi = BlueMapAPI.getInstance();
5272
if (optionalApi.isEmpty()) {
5373
disable();
@@ -57,8 +77,7 @@ public BluemapComponent() {
5777
BuildTeamTools.getInstance().getComponentLogger().info(Component.text("Loading BlueMap integration for WarpGroups & Warps...", NamedTextColor.GREEN));
5878

5979
// Register marker loading when BlueMap is ready
60-
BlueMapAPI.onEnable(api -> NetworkModule.getInstance().getBuildTeam().getWarpGroups()
61-
.forEach(warpGroup -> registerWarpGroupMarkers(api, warpGroup)));
80+
BlueMapAPI.onEnable(api -> refreshAllMarkers());
6281
}
6382

6483
/**
@@ -137,4 +156,55 @@ private void addWarpMarker(@NotNull MarkerSet markerSet, @NotNull Warp warp) {
137156
// Add marker to the marker set
138157
markerSet.getMarkers().put(warp.getId().toString(), marker);
139158
}
159+
160+
/**
161+
* Refreshes all BlueMap markers by clearing existing ones and re-registering all warp groups.
162+
* <p>
163+
* This method should be called whenever warps or warp groups are added, updated, or removed
164+
* to ensure the BlueMap display stays in sync with the current state.
165+
* </p>
166+
*/
167+
public void refreshAllMarkers() {
168+
if (!isEnabled()) {
169+
return;
170+
}
171+
172+
Optional<BlueMapAPI> optionalApi = BlueMapAPI.getInstance();
173+
if (optionalApi.isEmpty()) {
174+
return;
175+
}
176+
177+
BlueMapAPI api = optionalApi.get();
178+
179+
// Clear all existing warp group markers
180+
clearAllMarkers(api);
181+
182+
// Re-register all warp groups
183+
NetworkModule.getInstance().getBuildTeam().getWarpGroups()
184+
.forEach(warpGroup -> registerWarpGroupMarkers(api, warpGroup));
185+
186+
BuildTeamTools.getInstance().getComponentLogger().info(
187+
Component.text("Refreshed BlueMap markers for all warp groups.", NamedTextColor.GREEN)
188+
);
189+
}
190+
191+
/**
192+
* Clears all warp group markers from all BlueMap worlds.
193+
* <p>
194+
* This method iterates through all loaded worlds and removes all marker sets
195+
* that were created by this component for warp groups.
196+
* </p>
197+
*
198+
* @param api the BlueMapAPI instance
199+
*/
200+
private void clearAllMarkers(@NotNull BlueMapAPI api) {
201+
// Get all warp group IDs that need to be cleared
202+
List<String> warpGroupIds = NetworkModule.getInstance().getBuildTeam().getWarpGroups()
203+
.stream()
204+
.map(warpGroup -> warpGroup.getId().toString())
205+
.toList();
206+
207+
// Clear markers from all worlds
208+
api.getWorlds().forEach(world -> world.getMaps().forEach(map -> warpGroupIds.forEach(id -> map.getMarkerSets().remove(id))));
209+
}
140210
}

src/main/java/net/buildtheearth/buildteamtools/modules/network/model/BuildTeam.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.alpsbte.alpslib.utils.ChatHelper;
44
import lombok.Getter;
5+
import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule;
56
import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.Warp;
67
import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.WarpGroup;
78
import net.buildtheearth.buildteamtools.modules.network.NetworkModule;
@@ -70,9 +71,11 @@ public void createWarp(Player creator, Warp warp){
7071
@Override
7172
public void onResponse(String response) {
7273
// Update the cache
73-
NetworkModule.getInstance().updateCache().thenRun(() ->
74-
ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp %s!", warp.getName())
75-
);
74+
NetworkModule.getInstance().updateCache().thenRun(() -> {
75+
ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp %s!", warp.getName());
76+
// Refresh BlueMap markers
77+
refreshBluemapMarkers();
78+
});
7679
}
7780

7881
@Override
@@ -94,9 +97,11 @@ public void createWarpGroup(Player creator, WarpGroup warpGroup){
9497
@Override
9598
public void onResponse(String response) {
9699
// Update the cache
97-
NetworkModule.getInstance().updateCache().thenRun(() ->
98-
ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp group %s!", warpGroup.getName())
99-
);
100+
NetworkModule.getInstance().updateCache().thenRun(() -> {
101+
ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp group %s!", warpGroup.getName());
102+
// Refresh BlueMap markers
103+
refreshBluemapMarkers();
104+
});
100105
}
101106

102107
@Override
@@ -119,9 +124,11 @@ public void updateWarp(Player updater, Warp warp){
119124
@Override
120125
public void onResponse(String response) {
121126
// Update the cache
122-
NetworkModule.getInstance().updateCache().thenRun(() ->
123-
ChatHelper.sendSuccessfulMessage(updater, "Successfully updated the warp %s!", warp.getName())
124-
).exceptionally(e -> {
127+
NetworkModule.getInstance().updateCache().thenRun(() -> {
128+
ChatHelper.sendSuccessfulMessage(updater, "Successfully updated the warp %s!", warp.getName());
129+
// Refresh BlueMap markers
130+
refreshBluemapMarkers();
131+
}).exceptionally(e -> {
125132
updater.sendMessage(ChatHelper.getErrorString("Something went wrong while updating the warp %s! Please take a look at the console.", warp.getName()));
126133
e.printStackTrace();
127134
return null;
@@ -147,9 +154,11 @@ public void updateWarpGroup(Player updater, WarpGroup warpGroup){
147154
@Override
148155
public void onResponse(String response) {
149156
// Update the cache
150-
NetworkModule.getInstance().updateCache().thenRun(() ->
151-
ChatHelper.sendSuccessfulMessage(updater, "Successfully updated the warp group %s!", warpGroup.getName())
152-
).exceptionally(e -> {
157+
NetworkModule.getInstance().updateCache().thenRun(() -> {
158+
ChatHelper.sendSuccessfulMessage(updater, "Successfully updated the warp group %s!", warpGroup.getName());
159+
// Refresh BlueMap markers
160+
refreshBluemapMarkers();
161+
}).exceptionally(e -> {
153162
updater.sendMessage(ChatHelper.getErrorString("Something went wrong while updating the warp group %s! Please take a look at the console.", warpGroup.getName()));
154163
e.printStackTrace();
155164
return null;
@@ -175,9 +184,11 @@ public void deleteWarp(Player deleter, Warp warp){
175184
@Override
176185
public void onResponse(String response) {
177186
// Update the cache
178-
NetworkModule.getInstance().updateCache().thenRun(() ->
179-
ChatHelper.sendSuccessfulMessage(deleter, "Successfully deleted the warp %s!", warp.getName())
180-
);
187+
NetworkModule.getInstance().updateCache().thenRun(() -> {
188+
ChatHelper.sendSuccessfulMessage(deleter, "Successfully deleted the warp %s!", warp.getName());
189+
// Refresh BlueMap markers
190+
refreshBluemapMarkers();
191+
});
181192
}
182193

183194
@Override
@@ -199,9 +210,11 @@ public void deleteWarpGroup(Player deleter, WarpGroup warpGroup){
199210
@Override
200211
public void onResponse(String response) {
201212
// Update the cache
202-
NetworkModule.getInstance().updateCache().thenRun(() ->
203-
ChatHelper.sendSuccessfulMessage(deleter, "Successfully deleted the warp group %s!", warpGroup.getName())
204-
);
213+
NetworkModule.getInstance().updateCache().thenRun(() -> {
214+
ChatHelper.sendSuccessfulMessage(deleter, "Successfully deleted the warp group %s!", warpGroup.getName());
215+
// Refresh BlueMap markers
216+
refreshBluemapMarkers();
217+
});
205218
}
206219

207220
@Override
@@ -212,4 +225,18 @@ public void onFailure(IOException e) {
212225
});
213226
}
214227

228+
/**
229+
* Refreshes BlueMap markers for all warp groups.
230+
* <p>
231+
* This method is called after any warp or warp group is created, updated, or deleted
232+
* to ensure the BlueMap display stays in sync with the current state.
233+
* </p>
234+
*/
235+
private void refreshBluemapMarkers() {
236+
if (NavigationModule.getInstance().getBluemapComponent() != null
237+
&& NavigationModule.getInstance().getBluemapComponent().isEnabled()) {
238+
NavigationModule.getInstance().getBluemapComponent().refreshAllMarkers();
239+
}
240+
}
241+
215242
}

src/main/java/net/buildtheearth/buildteamtools/utils/io/ConfigPaths.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static class Navigation {
4141
// Navigator.Warps
4242
private static final String NAVIGATOR_WARPS = "warps.";
4343
public static final String WARPS_GROUP_SORTING_MODE = NAVIGATOR_WARPS + "sorting-mode";
44+
45+
// BlueMap Integration
46+
private static final String BLUEMAP = "bluemap.";
47+
public static final String BLUEMAP_ENABLED = BLUEMAP + "enabled";
4448
}
4549

4650
public static class PlotSystem {

src/main/resources/modules/navigation/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ warps:
4747
# Possible values: default (same as the creation order), name
4848
sorting-mode: default
4949

50+
# Configures the BlueMap integration
51+
bluemap:
52+
# Enables or disables the BlueMap integration for displaying warps [true|false]
53+
enabled: true
54+
5055
# NOTE: Do not change
5156
config-version: 1.6

0 commit comments

Comments
 (0)