Skip to content

Commit

Permalink
updated annotation to JourneyMapPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticdrew committed Oct 7, 2023
1 parent 7212a21 commit 88d78d9
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 45 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/journeymap/client/api/IClientAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public interface IClientAPI
{
String API_OWNER = "journeymap";
String API_VERSION = "1.9-SNAPSHOT";
String API_VERSION = "2.0-SNAPSHOT";

/**
* Returns the current UIState of the UI specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Interface used by JourneyMap to initialize client plugins and provide the Client API.
* <p>
* Implementation classes must have a no-arg constructor and also have the {@link ClientPlugin} annotation.
* Implementation classes must have a no-arg constructor and also have the {@link JourneyMapPlugin} annotation.
*/
@ParametersAreNonnullByDefault
public interface IClientPlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package journeymap.client.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
Expand All @@ -30,6 +32,15 @@
* and must also implement the {@link IClientPlugin} interface.
*/
@Target(ElementType.TYPE)
public @interface ClientPlugin
@Retention(RetentionPolicy.RUNTIME)
public @interface JourneyMapPlugin
{
/**
* Mod Devs need to supply the version of journeymap-api the mod was built against,
* so that it will not load the plugin if there is a breaking change.
*
* @return - The Api Version
*/
String apiVersion();

}
52 changes: 38 additions & 14 deletions common/src/main/java/journeymap/client/api/util/PluginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package journeymap.client.api.util;

import com.google.common.base.Strings;
import journeymap.client.api.ClientPlugin;
import journeymap.client.api.IClientAPI;
import journeymap.client.api.IClientPlugin;
import journeymap.client.api.JourneyMapPlugin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -36,7 +36,7 @@

/**
* Enum singleton used by JourneyMap to load and initialize plugins. A plugin class must be annotated with
* the {@link ClientPlugin} annotation and also implement the {@link IClientPlugin} interface.
* the {@link JourneyMapPlugin} annotation and also implement the {@link IClientPlugin} interface.
*/
@ParametersAreNonnullByDefault
public enum PluginHelper
Expand Down Expand Up @@ -73,31 +73,49 @@ public Map<String, IClientPlugin> preInitPlugins(List<String> pluginList)
Class<?> pluginClass = Class.forName(className);
if (IClientPlugin.class.isAssignableFrom(pluginClass))
{
Class<? extends IClientPlugin> interfaceImplClass = pluginClass.asSubclass(IClientPlugin.class);
IClientPlugin instance = interfaceImplClass.getDeclaredConstructor().newInstance();
String modId = instance.getModId();
if (Strings.isNullOrEmpty(modId))
if (pluginClass.isAnnotationPresent(JourneyMapPlugin.class))
{
throw new Exception("IClientPlugin.getModId() must return a non-empty, non-null value");
Class<? extends IClientPlugin> interfaceImplClass = pluginClass.asSubclass(IClientPlugin.class);
JourneyMapPlugin annotationClass = pluginClass.getDeclaredAnnotation(JourneyMapPlugin.class);
if (inVersionRange(annotationClass.apiVersion()))
{
IClientPlugin instance = interfaceImplClass.getDeclaredConstructor().newInstance();
String modId = instance.getModId();
if (Strings.isNullOrEmpty(modId))
{
throw new Exception("IClientPlugin.getModId() must return a non-empty, non-null value");
}
if (discovered.containsKey(modId))
{
Class otherPluginClass = discovered.get(modId).getClass();
throw new Exception(String.format("Multiple plugins trying to use the same modId: %s and %s", interfaceImplClass, otherPluginClass));
}
discovered.put(modId, instance);
LOGGER.info(String.format("Found @%s: %s", JourneyMapPlugin.class.getSimpleName(), className));
}
else
{
LOGGER.error("Found @{}: {}, but there is a version incompatibility, skipping plugin version {}, required version {}",
PLUGIN_INTERFACE_NAME, className, annotationClass.apiVersion(), "2.0.0");
}
}
if (discovered.containsKey(modId))
else
{
Class otherPluginClass = discovered.get(modId).getClass();
throw new Exception(String.format("Multiple plugins trying to use the same modId: %s and %s", interfaceImplClass, otherPluginClass));
LOGGER.error(String.format("Found @%s: %s, but it is not annotated with %s",
PLUGIN_INTERFACE_NAME, className, JourneyMapPlugin.class.getSimpleName()));
}
discovered.put(modId, instance);
LOGGER.info(String.format("Found @%s: %s", ClientPlugin.class, className));
}
else
{
LOGGER.error(String.format("Found @%s: %s, but it doesn't implement %s",
ClientPlugin.class, className, PLUGIN_INTERFACE_NAME));
JourneyMapPlugin.class.getSimpleName(), className, PLUGIN_INTERFACE_NAME));
System.out.println(List.of(pluginClass.getInterfaces()));
}
}
catch (Exception e)
{
LOGGER.error(String.format("Found @%s: %s, but failed to instantiate it: %s",
ClientPlugin.class, className, e.getMessage()), e);
JourneyMapPlugin.class.getSimpleName(), className, e.getMessage()), e);
}
}

Expand All @@ -112,6 +130,12 @@ public Map<String, IClientPlugin> preInitPlugins(List<String> pluginList)
return plugins;
}

static boolean inVersionRange(String pluginApiVersion)
{
// TODO: add version parsing logic
return true;
}

/**
* Called by JourneyMap during its initialization phase. Can only be called once per runtime.
* <p>
Expand Down
4 changes: 3 additions & 1 deletion docs/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ dependencies {
modCompileOnlyApi group: 'info.journeymap', name: 'journeymap-api', version: project.journeymap_api_version_fabric, changing: true
modRuntimeOnly "curse.maven:journeymap-${project.jm_project_id}:${project.fabric_jm_file_id}"
}
```
Example mods.toml entry for a soft dependency. Set `mandatory=true` for a hard dependency if needed.
Example forge mods.toml entry for a soft dependency. Set `mandatory=true` for a hard dependency if needed.
```
[[dependencies.mymodId]]
modId = "journeymap"
Expand All @@ -69,6 +70,7 @@ versionRange = "[6.0.0,)"
ordering = "NONE"
side = "CLIENT"
```

*Note that the journeymap-api.jar is built with deobfuscated code so that it can be used at compile time and when
stepping through a debugger in your development environment.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import example.mod.ExampleMod;
import journeymap.client.api.IClientAPI;
import journeymap.client.api.IClientPlugin;
import journeymap.client.api.JourneyMapPlugin;
import journeymap.client.api.display.DisplayType;
import journeymap.client.api.event.ClientEvent;
import journeymap.client.api.event.DeathWaypointEvent;
Expand All @@ -47,6 +48,7 @@
* <p>
* The
*/
@JourneyMapPlugin(apiVersion = "2.0")
public class ExampleJourneymapPlugin implements IClientPlugin
{
// API reference
Expand Down Expand Up @@ -204,8 +206,8 @@ void onMappingStarted(ClientEvent event)
// will keep it updated if the player sleeps elsewhere.
// if (jmAPI.playerAccepts(ExampleMod.MODID, DisplayType.Waypoint)) // TODO
// {
BlockPos pos = Minecraft.getInstance().player.getSleepingPos().orElse(new BlockPos(0, 0, 0));
SampleWaypointFactory.createBedWaypoint(jmAPI, pos, event.dimension);
BlockPos pos = Minecraft.getInstance().player.getSleepingPos().orElse(new BlockPos(0, 0, 0));
SampleWaypointFactory.createBedWaypoint(jmAPI, pos, event.dimension);
// }

// Create some random complex polygon overlays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package example.mod.client.plugin;

import example.mod.ExampleMod;
import journeymap.client.api.ClientPlugin;
import journeymap.client.api.IClientAPI;
import journeymap.client.api.IClientPlugin;
import journeymap.client.api.JourneyMapPlugin;
import journeymap.client.api.display.DisplayType;
import journeymap.client.api.display.Displayable;
import journeymap.client.api.event.ClientEvent;
Expand Down Expand Up @@ -52,7 +52,7 @@
* The
*/
@ParametersAreNonnullByDefault
@ClientPlugin
@JourneyMapPlugin(apiVersion = "2.0")
public class ExampleJourneymapPlugin implements IClientPlugin
{
// API reference
Expand Down
29 changes: 6 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,16 @@ A plugin-style (soft dependency) API allowing other mods to create waypoints and
within [JourneyMap for Minecraft](http://journeymap.info) 1.8.9+.

If you have suggestions or improvements to the API structure, feel free to make Pull Requests. Chatting with the TeamJM
developers in Espernet IRC #journeymap is highly suggested. Before you change anything or submit code, however, be sure
developers in [Discord](https://discord.gg/eP8gE69). Before you change anything or submit code, however, be sure
to read the [License Information](docs/license.md).

News
============================================================
* **Q2 2022**: Versions API v1.18.2-1.9-SNAPSHOT for Journeymap 5.9.0 published

* **Q1 2022**: Versions API v1.16.5-1.8, v1.17.1-1.8, v1.18.1-1.8 published

* **22 Nov 2016**: Version 1.11-1.3-SNAPSHOT has been pushed to Maven

* **22 Nov 2016**: Version 1.9.4-1.3 and 1.10.2-1.3 have been pushed to Maven

* **27 May 2016**: Version 1.9-1.2 and 1.9.4-1.2 have been pushed to Maven

* **26 May 2016**: Version 1.8.9-1.2 has been pushed to Maven

* **18 March 2016**: Version 1.9-1.2-SNAPSHOT has been pushed to Maven

* **4 March 2016**: Version 1.8.9-1.1 of the API is in Maven, has basic support implemented in [JourneyMap 5.1.5 for Minecraft 1.8.9](http://minecraft.curseforge.com/projects/journeymap-32274/files/2285371).

* **25 Feb 2016**: Version 1.1 of the API design is complete.

* **26 Jan 2016**: Version 1.0 of the API design is complete, and Techbrew has begun writing the implementation for it in JourneyMap. Watch
this site or follow @JourneyMapMod on Twitter for news about releases that support this API.

* **25 Jan 2016**: JourneyMap API artifacts are now hosted in [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cjourneymap-api)
Versions API v2.0.0
- Updated Waypoints (Breaking Change)
- Removed ClientPlugin annotation in favor of JourneyMapPlugin annotation which takes the API version the addon was built against.
- API project now uses MultiLoader Template.
- Forge version is now a mod that JouneyMap now JarJars in the main mod.


[How to use the JourneyMap API](docs/howto.md)
Expand Down

0 comments on commit 88d78d9

Please sign in to comment.