Refactor Nms class to be more readable and make it final

This commit is contained in:
Christian Koop 2023-06-10 22:14:45 +02:00
parent 1e0de3d844
commit f66964b2ee
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -2,21 +2,26 @@ package com.songoda.core.nms;
import com.songoda.core.compatibility.ServerVersion;
public class Nms {
protected static NmsImplementations impl;
public final class Nms {
private static NmsImplementations cachedImplementation;
/**
* @return The implementations for the current server version
*/
public static NmsImplementations getImplementations() throws UnsupportedServerVersionException {
if (impl == null) {
if (cachedImplementation == null) {
try {
impl = (NmsImplementations) Class.forName("com.songoda.core.nms." + ServerVersion.getServerVersionString() + ".NmsImplementationsImpl").getConstructors()[0].newInstance();
Class<?> implementationClazz = Class.forName(getImplementationClassName());
cachedImplementation = (NmsImplementations) implementationClazz.getConstructors()[0].newInstance();
} catch (ReflectiveOperationException ex) {
throw new UnsupportedServerVersionException(ex);
}
}
return impl;
return cachedImplementation;
}
private static String getImplementationClassName() {
return String.format("com.songoda.core.nms.%s.NmsImplementationsImpl", ServerVersion.getServerVersionString());
}
}