Skip to content

Commit

Permalink
feat: Update to JourneyMap API v2 (#860)
Browse files Browse the repository at this point in the history
* feat: Update to JourneyMap API v2

* fix: Fix missing commonnetworking

* fix: Fix waypoint group usage, add TODO

* fix: Remember waypoint ids to re-access them later

* fix: Don't call getWaypoint with a null id

* fix: Store waystone type per waypoint to properly remove later

* fix: Final fixes for JourneyMap integration
  • Loading branch information
BlayTheNinth authored Jun 24, 2024
1 parent b626da0 commit 37102a9
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 61 deletions.
2 changes: 1 addition & 1 deletion common/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
implementation("net.blay09.mods:balm-common:${balm_version}") {
changing = balm_version.endsWith("SNAPSHOT")
}
compileOnly "info.journeymap:journeymap-api:${journeymap_api_common_version}"
compileOnly "info.journeymap:journeymap-api-common:${journeymap_api_version}"
compileOnly "mcp.mobius.waila:wthit-api:mojmap-$wthit_version"
compileOnly "mezz.jei:jei-$jei_minecraft_version-common-api:$jei_version"
compileOnly "com.github.BlueMap-Minecraft:BlueMapAPI:$bluemap_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package net.blay09.mods.waystones.compat;

import journeymap.client.api.ClientPlugin;
import journeymap.client.api.IClientAPI;
import journeymap.client.api.IClientPlugin;
import journeymap.client.api.display.Waypoint;
import journeymap.client.api.display.WaypointGroup;
import journeymap.client.api.event.ClientEvent;
import journeymap.api.v2.client.IClientAPI;
import journeymap.api.v2.client.IClientPlugin;
import journeymap.api.v2.client.JourneyMapPlugin;
import journeymap.api.v2.client.event.MappingEvent;
import journeymap.api.v2.common.event.ClientEventRegistry;
import journeymap.api.v2.common.waypoint.WaypointFactory;
import journeymap.api.v2.common.waypoint.WaypointGroup;
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.waystones.Waystones;
import net.blay09.mods.waystones.api.*;
Expand All @@ -20,14 +21,17 @@

import java.util.*;

@ClientPlugin
@JourneyMapPlugin(apiVersion = "2.0.0")
public class JourneyMapIntegration implements IClientPlugin {

private static final UUID WAYSTONE_GROUP_ID = UUID.fromString("005bdf11-2dbb-4a27-8aa4-0184e86fa33c");
private static final UUID SHARESTONE_GROUP_ID = UUID.fromString("199e2989-df63-4ab4-bd5d-2fa24e72b4fc");

private IClientAPI api;
private WaypointGroup waystonesGroup;
private WaypointGroup sharestonesGroup;
private boolean journeyMapReady;
private final Map<UUID, String> waystoneToWaypoint = new HashMap<>();
private final Map<String, UUID> waypointToWaystone = new HashMap<>();
private final Map<String, ResourceLocation> waypointTypes = new HashMap<>();

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

private static JourneyMapIntegration instance;
Expand All @@ -44,7 +48,10 @@ public void initialize(IClientAPI iClientAPI) {
api = iClientAPI;

// This fires after all waypoints have been loaded
api.subscribe(Waystones.MOD_ID, EnumSet.of(ClientEvent.Type.MAPPING_STARTED));
ClientEventRegistry.MAPPING_EVENT.subscribe(Waystones.MOD_ID, this::onMappingEvent);

waystonesGroup = WaypointFactory.createWaypointGroup(Waystones.MOD_ID, "waystones");
sharestonesGroup = WaypointFactory.createWaypointGroup(Waystones.MOD_ID, "sharestones");
}

/**
Expand All @@ -60,9 +67,8 @@ public String getModId() {
return Waystones.MOD_ID;
}

@Override
public void onEvent(ClientEvent clientEvent) {
if (clientEvent.type == ClientEvent.Type.MAPPING_STARTED) {
public void onMappingEvent(MappingEvent event) {
if (event.getStage() == MappingEvent.Stage.MAPPING_STARTED) {
journeyMapReady = true;

for (Runnable scheduledJob : scheduledJobsWhenReady) {
Expand Down Expand Up @@ -99,7 +105,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 @@ -112,66 +118,73 @@ private void runWhenJourneyMapIsReady(Runnable runnable) {
}

private void updateAllWaypoints(ResourceLocation waystoneType, List<Waystone> waystones) {
final var idPrefix = waystoneType.getPath() + ":";
final var stillExistingIds = new HashSet<String>();
final var stillExistingIds = new HashSet<UUID>();
for (final var waystone : waystones) {
stillExistingIds.add(getPrefixedWaystoneId(waystone));
stillExistingIds.add(waystone.getWaystoneUid());
updateWaypoint(waystone);
}

final var waypoints = api.getWaypoints(Waystones.MOD_ID);
for (final var waypoint : waypoints) {
if (waypoint.getId().startsWith(idPrefix) && !stillExistingIds.contains(waypoint.getId())) {
api.remove(waypoint);
final var waystoneUid = waypointToWaystone.get(waypoint.getGuid());
final var type = waypointTypes.get(waypoint.getGuid());
if (waystoneType.equals(type) && !stillExistingIds.contains(waystoneUid)) {
api.removeWaypoint(Waystones.MOD_ID, waypoint);
waystoneToWaypoint.remove(waystoneUid);
waypointToWaystone.remove(waypoint.getGuid());
waypointTypes.remove(waypoint.getGuid());
}
}
}

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.remove(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);
waypointTypes.remove(waypoint.getGuid());
}
waystoneToWaypoint.remove(waystoneId);
waypointToWaystone.remove(waypointId);
}
}

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 = waypointId != null ? api.getWaypoint(Waystones.MOD_ID, waypointId) : null;
final var waystoneName = waystone.hasName() ? waystone.getName() : Component.translatable("waystones.map.untitled_waystone");
Waypoint waypoint = new Waypoint(Waystones.MOD_ID, prefixedId, waystoneName.getString(), waystone.getDimension(), waystone.getPos());
waypoint.setName(waystoneName.getString());
waypoint.setGroup(getWaystoneGroup(waystone));
final var waypoint = WaypointFactory.createClientWaypoint(Waystones.MOD_ID,
waystone.getPos(),
waystoneName.getString(),
waystone.getDimension(),
false);
if (oldWaypoint != null) {
waypoint.setEnabled(oldWaypoint.isEnabled());
if (oldWaypoint.hasColor()) {
waypoint.setColor(oldWaypoint.getColor());
}
if (oldWaypoint.hasBackgroundColor()) {
waypoint.setBackgroundColor(oldWaypoint.getBackgroundColor());
}
api.remove(oldWaypoint);
waypoint.setColor(oldWaypoint.getColor());
waypoint.setIconColor(oldWaypoint.getIconColor());
api.removeWaypoint(Waystones.MOD_ID, oldWaypoint);
}
api.addWaypoint(Waystones.MOD_ID, waypoint);
waystoneToWaypoint.put(waystone.getWaystoneUid(), waypoint.getGuid());
waypointToWaystone.put(waypoint.getGuid(), waystone.getWaystoneUid());
waypointTypes.put(waypoint.getGuid(), waystone.getWaystoneType());

final var group = getWaystoneGroup(waystone);
if (group != null) {
group.addWaypoint(waypoint);
}
api.show(waypoint);
} catch (Exception e) {
e.printStackTrace();
}
}

private static WaypointGroup getWaystoneGroup(Waystone waystone) {
private WaypointGroup getWaystoneGroup(Waystone waystone) {
if (WaystoneTypes.isSharestone(waystone.getWaystoneType())) {
return new WaypointGroup(Waystones.MOD_ID, SHARESTONE_GROUP_ID.toString(), "Sharestones");
return sharestonesGroup;
} else {
return new WaypointGroup(Waystones.MOD_ID, WAYSTONE_GROUP_ID.toString(), "Waystones");
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();
}
}
5 changes: 3 additions & 2 deletions fabric/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ dependencies {
if (jei_minecraft_version == minecraft_version) {
modRuntimeOnly "mezz.jei:jei-$jei_minecraft_version-fabric:$jei_version"
}
modImplementation group: 'info.journeymap', name: 'journeymap-api', version: project.journeymap_api_fabric_version, changing: true, transitive: false
//modRuntimeOnly "curse.maven:journeymap-${project.journeymap_project_id}:${project.journeymap_fabric_file_id}"
modImplementation group: 'info.journeymap', name: 'journeymap-api-fabric', version: project.journeymap_api_version, changing: true, transitive: false
modRuntimeOnly "mysticdrew:common-networking-fabric:$journeymap_networking_version"
modRuntimeOnly "curse.maven:journeymap-${project.journeymap_project_id}:${project.journeymap_fabric_file_id}"

modCompileOnly "com.terraformersmc:modmenu:$modmenu_version"
modCompileOnly "curse.maven:jade-324717:$jade_forge_version"
Expand Down
5 changes: 3 additions & 2 deletions forge/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ dependencies {
implementation("net.blay09.mods:balm-forge:${balm_version}") {
changing = balm_version.contains("SNAPSHOT")
}
compileOnly(group: 'info.journeymap', name: 'journeymap-api', version: project.journeymap_api_forge_version, changing: true)
compileOnly(group: 'info.journeymap', name: 'journeymap-api-forge', version: project.journeymap_api_version, changing: true)
compileOnly("mezz.jei:jei-$jei_minecraft_version-common-api:$jei_version")
compileOnly("curse.maven:jade-324717:$jade_forge_version")
compileOnly("mcp.mobius.waila:wthit-api:forge-$wthit_version")
compileOnly("curse.maven:the-one-probe-245211:$theoneprobe_version")
compileOnly "com.github.BlueMap-Minecraft:BlueMapAPI:$bluemap_version"
compileOnly "us.dynmap:DynmapCoreAPI:$dynmap_version"
//runtimeOnly fg.deobf("curse.maven:journeymap-${project.journeymap_project_id}:${project.journeymap_forge_file_id}")
runtimeOnly "mysticdrew:common-networking-forge:$journeymap_networking_version"
runtimeOnly "curse.maven:journeymap-${project.journeymap_project_id}:${project.journeymap_forge_file_id}"
}

configurations {
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ curseforge_fabric_project_id = 500087
modrinth_release_type = release
modrinth_project_id = LOpKHB2A

# Journeymap v5.8.3
# Journeymap
journeymap_project_id=32274
journeymap_forge_file_id=4873840
journeymap_fabric_file_id=4873841
journeymap_api_fabric_version=1.20.2-1.9-fabric-SNAPSHOT
journeymap_api_common_version=1.20.2-1.9-SNAPSHOT
journeymap_api_forge_version=1.20.2-1.9-SNAPSHOT
journeymap_forge_file_id=5452641
journeymap_neoforge_file_id=5452643
journeymap_fabric_file_id=5452642
journeymap_api_version=2.0.0-1.21-SNAPSHOT
journeymap_networking_version=1.0.11-1.21

# Minecraft
minecraft_version = 1.21
Expand Down
4 changes: 3 additions & 1 deletion neoforge/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ dependencies {
implementation("net.blay09.mods:balm-neoforge:${balm_version}") {
changing = balm_version.contains("SNAPSHOT")
}
compileOnly group: 'info.journeymap', name: 'journeymap-api', version: project.journeymap_api_forge_version, changing: true
compileOnly group: 'info.journeymap', name: 'journeymap-api-neoforge', version: project.journeymap_api_version, changing: true
runtimeOnly "mysticdrew:common-networking-neoforge:$journeymap_networking_version"
runtimeOnly "curse.maven:journeymap-${project.journeymap_project_id}:${project.journeymap_neoforge_file_id}"
compileOnly "mezz.jei:jei-$jei_minecraft_version-common-api:$jei_version"
compileOnly "curse.maven:jade-324717:$jade_forge_version"
compileOnly "mcp.mobius.waila:wthit-api:forge-$wthit_version"
Expand Down
1 change: 1 addition & 0 deletions repositories.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ repositories {
url = "https://jm.gserv.me/repository/maven-public/"
content {
includeGroup "info.journeymap"
includeGroup "mysticdrew"
}
}

Expand Down

0 comments on commit 37102a9

Please sign in to comment.