Skip to content

Commit

Permalink
fix: Remember waypoint ids to re-access them later
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jun 24, 2024
1 parent add4a85 commit 65f8fe3
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class JourneyMapIntegration implements IClientPlugin {
private WaypointGroup waystonesGroup;
private WaypointGroup sharestonesGroup;
private boolean journeyMapReady;
private final Map<UUID, String> waystoneToWaypoint = new HashMap<>();

private final List<Runnable> scheduledJobsWhenReady = new ArrayList<>();

private static JourneyMapIntegration instance;
Expand Down Expand Up @@ -101,7 +103,7 @@ public void onWaystoneUpdateReceived(WaystoneUpdateReceivedEvent event) {

public void onWaystoneRemoveReceived(WaystoneRemoveReceivedEvent event) {
if (shouldManageWaypoints() && isSupportedWaystoneType(event.getWaystoneType())) {
runWhenJourneyMapIsReady(() -> removeWaypoint(event.getWaystoneType(), event.getWaystoneId()));
runWhenJourneyMapIsReady(() -> removeWaypoint(event.getWaystoneId()));
}
}

Expand All @@ -117,7 +119,7 @@ private void updateAllWaypoints(ResourceLocation waystoneType, List<Waystone> wa
final var idPrefix = waystoneType.getPath() + ":";
final var stillExistingIds = new HashSet<String>();
for (final var waystone : waystones) {
stillExistingIds.add(getPrefixedWaystoneId(waystone));
stillExistingIds.add(waystone.getWaystoneUid().toString());
updateWaypoint(waystone);
}

Expand All @@ -129,18 +131,20 @@ private void updateAllWaypoints(ResourceLocation waystoneType, List<Waystone> wa
}
}

private void removeWaypoint(ResourceLocation waystoneType, UUID waystoneId) {
final var prefixedId = getPrefixedWaystoneId(waystoneType, waystoneId);
final var waypoint = api.getWaypoint(Waystones.MOD_ID, prefixedId);
if (waypoint != null) {
api.removeWaypoint(Waystones.MOD_ID, waypoint);
private void removeWaypoint(UUID waystoneId) {
final var waypointId = waystoneToWaypoint.get(waystoneId);
if (waypointId != null) {
final var waypoint = api.getWaypoint(Waystones.MOD_ID, waypointId);
if (waypoint != null) {
api.removeWaypoint(Waystones.MOD_ID, waypoint);
}
}
}

private void updateWaypoint(Waystone waystone) {
try {
final var prefixedId = getPrefixedWaystoneId(waystone);
final var oldWaypoint = api.getWaypoint(Waystones.MOD_ID, prefixedId);
final var waypointId = waystoneToWaypoint.get(waystone.getWaystoneUid());
final var oldWaypoint = api.getWaypoint(Waystones.MOD_ID, waypointId);
final var waystoneName = waystone.hasName() ? waystone.getName() : Component.translatable("waystones.map.untitled_waystone");
final var waypoint = WaypointFactory.createClientWaypoint(Waystones.MOD_ID,
waystone.getPos(),
Expand All @@ -154,6 +158,7 @@ private void updateWaypoint(Waystone waystone) {
api.removeWaypoint(Waystones.MOD_ID, oldWaypoint);
}
api.addWaypoint(Waystones.MOD_ID, waypoint);
waystoneToWaypoint.put(waystone.getWaystoneUid(), waypoint.getId());

final var group = getWaystoneGroup(waystone);
if (group != null) {
Expand All @@ -171,12 +176,4 @@ private WaypointGroup getWaystoneGroup(Waystone waystone) {
return waystonesGroup;
}
}

private String getPrefixedWaystoneId(Waystone waystone) {
return getPrefixedWaystoneId(waystone.getWaystoneType(), waystone.getWaystoneUid());
}

private String getPrefixedWaystoneId(ResourceLocation waystoneType, UUID waystoneId) {
return waystoneType.getPath() + ":" + waystoneId.toString();
}
}

0 comments on commit 65f8fe3

Please sign in to comment.