diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java index 536f25b..aa8bcf8 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java @@ -5,6 +5,7 @@ import net.sourcewriters.minecraft.vcompat.provider.VersionControl; import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; +import net.sourcewriters.minecraft.vcompat.version.Versions; public abstract class VersionCompatProvider { @@ -15,11 +16,21 @@ public static VersionCompatProvider get() { if (PROVIDER.isPresent()) { return PROVIDER.get(); } - Object object = ClassLookup.of(IMPLEMENTATION_PATH).init(); - if (object == null || !(object instanceof VersionCompatProvider)) { - throw new IllegalStateException("Can't initialize VersionCompatProvider!"); - } - return PROVIDER.replace((VersionCompatProvider) object).lock().get(); + System.out.println( + "Initializing vCompat on with '" + Versions.getServerAsString() + "' core on '" + Versions.getMinecraftAsString() + "'!"); + Object object = ClassLookup.of(IMPLEMENTATION_PATH).init(); + if (object == null || !(object instanceof VersionCompatProvider)) { + throw new IllegalStateException("Can't initialize VersionCompatProvider!"); + } + VersionCompatProvider provider = PROVIDER.replace((VersionCompatProvider) object).lock().get(); + try { + provider.init(); + } catch (Exception exception) { + throw new IllegalStateException("Failed to initialize vCompat implementation", exception); + } + Runtime.getRuntime().addShutdownHook(new Thread(provider::shutdown)); + System.out.println("Initialization of vCompat complete!"); + return provider; } protected final ClassLookupProvider lookupProvider = new ClassLookupProvider(); @@ -28,6 +39,10 @@ public static VersionCompatProvider get() { * Impl */ + protected abstract void init() throws Exception; + + protected abstract void shutdown(); + public abstract VersionControl getControl(); public final ClassLookupProvider getLookupProvider() { diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java index 237c3bf..a6ac61e 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/provider/lookup/JavaAccessor.java @@ -57,7 +57,7 @@ public Object initialize(Constructor constructor, Object... arguments) { } try { if (arguments.length == 0) { - return lookup().unreflectConstructor(constructor).invokeWithArguments(); + return lookup().unreflectConstructor(constructor).invoke(); } return lookup().unreflectConstructor(constructor).invokeWithArguments(arguments); } catch (Throwable e) { diff --git a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java index 1801d34..49a02f3 100644 --- a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java +++ b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java @@ -2,17 +2,32 @@ import net.sourcewriters.minecraft.vcompat.provider.VersionControl; import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; +import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.utils.java.tools.Container; import net.sourcewriters.minecraft.vcompat.version.Versions; public final class VersionCompat extends VersionCompatProvider { - - private static final String VERSION_PATH = String.format("%s.provider.impl.%s.VersionControl%s", VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString().substring(1)); - private final VersionControl control; + private static final String VERSION_PATH = String.format("%s.provider.impl.%s.VersionControl%s", VersionCompat.class.getPackageName(), + Versions.getServerAsString(), Versions.getServerAsString().substring(1)); - public VersionCompat() { - this.control = initControl(); - Runtime.getRuntime().addShutdownHook(new Thread(control::shutdown)); + private final Container control = Container.of(); + private boolean init = false; + + @Override + protected final void init() throws Exception { + if (init) { + return; + } + init = true; + control.replace(initControl()).lock(); + } + + @Override + protected final void shutdown() { + if(!init) { + return; + } + control.ifPresent(VersionControl::shutdown); } private final VersionControl initControl() { @@ -25,7 +40,7 @@ private final VersionControl initControl() { @Override public final VersionControl getControl() { - return control; + return control.get(); } }