I haven't tried this yet, but in the @Mod annotation class, there is now an updateJSON parameter that can contain a "URL to a JSON file that will be checked once per launch to determine if there is an updated version of this mod and notify the end user. The format is defined here: https://gist.github.com/LexManos/7aacb9aa991330523884.
Thanks to Jwosty for suggesting the approach.
It is often nice to notify people when they are running an old version of the mod. It is possible to have the mod compare its version against information posted somewhere on the web. So if you are able to host a web page, you can publish the latest version there for the mod to check.
You will want to do this on the client side, so will need to perform this someplace like your client proxy class (probably in the post-init handler method). Furthermore, since it will go off and check a web site it is considered better practice to have the code run in its own thread so it doesn't block rest of execution.
Create The Version Checking Class (Runnable As Separate Thread)
So first you need to make a VersionChecker class that implements Runnable (this will allow it to run in its own thread):
Of course "MagicBeans" should be replaced with your mod's main class, and this assumes that you use the standard practice of making constants like MODVERSION in your main class. In any case, you just need to check for the version you have against the version from the URL.
Post Latest Version Information On Some Web Site
Next you need to get the information to your URL. An easy way if you're using github is to create a text file in your project that contains the version information on the first line. Then sync your project to the github, go to github website and find your file, then copy the URL (use the raw view) and put that into your VersionChecker class above.
Start The Version Checker Thread In Your Client Proxy Post-Init Handling
Next you need code to actually start the thread. So in your post-init handling method (i.e. the @EventHandler method that takes FMLPostInitializationEvent parameter) in your client proxy you need to add this:
Again, replace "MagicBeans" with the name of your main class.
Tip: The version checking should only be run on client side. It won't technically hurt to run it on server, but is a waste of resources and generally bad practice if you don't need it.
Create the Clickable Warning Message When Player First Plays
In the case where the version isn't latest, you probably want a chat message to appear that is clickable to allow people to go to your site to download the latest version. You want the message to only appear once, so need to remember if you've already shown the message.
Clickable chat messages are available through the ChatStyle.setChatClickEvent() method where the click event can have an action enumerated type of OPEN_URL.
To display a chat message, you need to make sure that you're actually in the game and not just in the loading screen. Furthermore, I tried various events like player joining and found that I think the behavior is best to just use the PlayerTickEvent but remember if you've already warned them.
Putting it all together, in your main class you want to create some public fields for the version checker instance as well as a field to remember if you've already warned the player. So in your main class:
Then you need to handle the PlayerTickEvent by putting something like this into a method that is registered as a handler to the FML event bus (see my tutorial on events for more information on how to do that):
Hopefully you can follow along with the code. Honestly, the chat component stuff is pretty confusing (I'll make tutorial soon). But basically I'm just trying to send a message that is clickable and to do that I am creating a style that is associated with a click event that opens a URL.
Key Point: Change the URL to point to your own page to download the latest version of the mod. Of course also change the references to Magic Beans to match your own mod's name.
Set Your MODVERSION String
The VersionChecker class compares the string value it finds on your website with the MODVERSION string field from your mod. So you need to make sure your mod has something like this in the main class:
Testing It Out
Okay, to test that it is working. You can follow these steps to confirm things are working properly:
- Make sure your version information is properly at the website that will be checked.
- Run Minecraft while checking the console. During loading at some point you should see messages from the VersionChecker class indicating what version that it found and whether it is considered the latest version.
- Create a single player game and look for chat message. If the version is latest (meaning your website string matches your MODVERSION string) then no chat message will display. If it doesn't match, a chat message should display.
- If warning message appears, press "T" to enter chat mode and then use the mouse to click on the text. It should bring up a dialog asking if you really want to follow the link, then should open your browser to the link you put in.
Try testing with and without the versions matching to confirm proper operation.
Conclusion And Further Thoughts
I hope you found this helpful. There are certainly areas where this can be further improved:
- The exception handling in my example is pretty lame (should really have custom handling)
- My approach is assuming thread-safe interaction because the version checking thread should be completed long before the fields are accessed. But it would be better to do more to ensure thread-safe interaction with the fields shared between threads.
As always, please write if you have questions or corrections.