watergov / Amethyst

GNU General Public License v3.0
1 stars 0 forks source link

Java 11 detection #11

Closed lucyydotp closed 3 years ago

lucyydotp commented 3 years ago

WaterCore depends on Java 11, it should gracefully disable itself if it's not running on it.

Skippysunday12 commented 3 years ago

Would System.getProperty("java.version").startsWith("11") work?

Edit: Full code: @Override public void onEnable(){ if(!System.getProperty("java.version").startsWith("11")) this.getServer().getPluginManager().disablePlugin(this); }

PulseBeat02 commented 3 years ago

Well, let's consider other scenarios. If we are running on Java 15 or something above Java 11, should WaterCore also run? Because if that is the case System.getProperty("java.version").startsWith("11") will not work.

Skippysunday12 commented 3 years ago

Well, in that case, it would be better to do: @Override public void onEnable(){ try{ int ver = Integer.parseInt(System.getProperty("java.version").substring(0, 2)); if(!(ver >= 11)) this.getServer().getPluginManager().disablePlugin(this); } catch(NumberFormatException e) { this.getServer().getPluginManager().disablePlugin(this); } }

PulseBeat02 commented 3 years ago

Maybe use this instead to avoid the try/catch statement.

@Override
public void onEnable() {
  if (getVersion() < 11) {
    getServer().getPluginManager().disablePlugin(this);
  }
}

private static int getVersion() {
    final String ver = System.getProperty("java.version");
    if (ver.startsWith("1.")) {
        ver = ver.substring(2, 3);
    } else {
        int dot = ver.indexOf(".");
        if (dot != -1) { 
          ver = ver.substring(0, dot); 
        }
    } 
    return Integer.parseInt(version);
}

"Throwing/Handling should normally be used for exceptional events, not normal flow control"

Skippysunday12 commented 3 years ago

That works too.

lucyydotp commented 3 years ago

anyone up for PRing this in?

lucyydotp commented 3 years ago

Krypton depends on java 11 anyway, we don't need to check it there

lucyydotp commented 3 years ago

Maybe use this instead to avoid the try/catch statement.

@Override
public void onEnable() {
  if (getVersion() < 11) {
    getServer().getPluginManager().disablePlugin(this);
  }
}

private static int getVersion() {
    final String ver = System.getProperty("java.version");
    if (ver.startsWith("1.")) {
        ver = ver.substring(2, 3);
    } else {
        int dot = ver.indexOf(".");
        if (dot != -1) { 
          ver = ver.substring(0, dot); 
        }
    } 
    return Integer.parseInt(version);
}

"Throwing/Handling should normally be used for exceptional events, not normal flow control"

Could you not just read up to the first dot? Seeing as we're only checking for 11+, if it starts with 1 it doesn't matter as it's too old.


final String ver = System.getProperty("java.version");
final int dotIdx = ver.indexOf('.');
return Integer.parseInt(dotIdx == -1 ? ver : ver.substring(0, dotIdx));
BomBardyGamer commented 3 years ago

We can't directly use that plugin manager disable system, since we need to have it work multiplatform, so maybe this could call the plugin abstraction player's disable and that's how it disables itself?

lucyydotp commented 3 years ago

Krypton depends on java 11 anyway, we don't need to check it there

can you read

BomBardyGamer commented 3 years ago

Other platforms too. What about BungeeCord for example?

lucyydotp commented 3 years ago

This issue is labeled "platform: bukkit", other platforms are currently not a concern.

PulseBeat02 commented 3 years ago

ooooooooof. And also yeah that works too. I just used that way cause I usually log java versions lmao.

Skippysunday12 commented 3 years ago

I can pr this later. Ill just add it to the main class.