Get Minecraft version

So, I’m re-writing a plugin with SpongeAPI and I need to get Minecraft Version.
Of course, it will be 1.8 but… I need it to future versions and a ‘complete’ updater system.

On bukkit API, version could get with plugin.getServer().getVersion().
Something similar to Sponge?

Thanks! :smiley:

MinecraftVersion#getName()

You could achieve this from Game by:

@Inject Game game;

public String getVersion() {
    return this.game.getMinecraftVersion().getName();
}

Or from most events:

@Subscribe public void onInit(InitializationEvent event) {
    String minecraftVersion = event.getGame().getMinecraftVersion().getName();
}

Please also note the JavaDoc which comes along with MinecraftVersion#getName()

    /**
     * Gets the name of this Minecraft version.
     *
     * <p>
     * <strong>Note:</strong> The returned name does not necessarily represent
     * the name of a Minecraft version. Depending on the client and
     * implementation, this may also just return a numeric value.
     * </p>
     *
     * @return The version name
     */

For the note in MinecraftVersion#getName(): in our implementation (Sponge Forge mod and Granite) this applies only when getting the Minecraft version of a client connecting/connected to the server. When using Game.getMinecraftVersion() it will always return the real name of the Minecraft version.

As a small side note, often depending directly on the Minecraft version in plugins is a very bad idea. The API is designed so it doesn’t need to change for every Minecraft versions so plugins should keep (mostly) working across Minecraft versions. Therefore relying on the Minecraft version directly would just limit the usage of your plugin.

1 Like

Very well spoken, @Minecrell.

@Cadiducho If you wouldn’t mind, extrapolating on what you need this for might be beneficial in:

  • Getting you the correct information.
  • Providing more intelligent solutions, assuming there are any.

In addition, it’s especially a bad idea to rely on client Minecraft Versions, as they could (and in the case of some mods, definitely) be unpredictable in what you may receive, limiting not only the usability of your plugin, but the stability.

The idea basically was get SERVER Minecraft version, such as 1.8.3, 1.8.1… and use it. But the server’s one, not the client.
I think it will work… Thanks @FerusGrim @Minecrell (: