Add paper plugin loading with mode switching

This commit is contained in:
GeorgH93 2023-02-21 19:07:55 +01:00
parent 26d7df2bf4
commit f12d2c6f1c
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
2 changed files with 57 additions and 25 deletions

View File

@ -4,4 +4,8 @@ version: "${version}"
api-version: "1.19"
main: "at.pcgamingfreaks.MinepacksStandalone.Bukkit.Minepacks"
bootstrapper: "at.pcgamingfreaks.Minepacks.Paper.MinepacksBootstrap"
bootstrapper: "at.pcgamingfreaks.Minepacks.Paper.MinepacksBootstrap"
dependencies:
- name: PCGF_PluginLib
required: false

View File

@ -23,8 +23,15 @@
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import java.lang.reflect.Field;
import java.util.logging.Level;
@SuppressWarnings({ "UnstableApiUsage", "unused" })
public class MinepacksBootstrap implements PluginBootstrap
{
private static final String MAIN_CLASS_NORMAL = "at.pcgamingfreaks.Minepacks.Bukkit.Minepacks";
private static final String MAIN_CLASS_STANDALONE = "at.pcgamingfreaks.MinepacksStandalone.Bukkit.Minepacks";
@Override
public void bootstrap(@NotNull PluginProviderContext context)
{
@ -33,36 +40,17 @@ public void bootstrap(@NotNull PluginProviderContext context)
@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context)
{
//TODO find a way to check if PCGF PluginLib exists
//Plugin pcgfPluginLib = Bukkit.getPluginManager().getPlugin("PCGF_PluginLib");
boolean standalone = true;
/*if(pcgfPluginLib != null)
{
if(new Version(pcgfPluginLib.getDescription().getVersion()).olderThan(new Version(MagicValues.MIN_PCGF_PLUGIN_LIB_VERSION)))
{
getLogger().info("PCGF-PluginLib to old! Switching to standalone mode!");
}
else
{
getLogger().info("PCGF-PluginLib installed. Switching to normal mode!");
standalone = false;
}
}
else
{
getLogger().info("PCGF-PluginLib not installed. Switching to standalone mode!");
}*/
try
{
if(standalone)
if(checkPcgfPluginLib(context) && patchPluginMeta(context))
{
Class<?> standaloneClass = Class.forName("at.pcgamingfreaks.MinepacksStandalone.Bukkit.Minepacks");
return (JavaPlugin) standaloneClass.newInstance();
Class<?> normalClass = Class.forName(MAIN_CLASS_NORMAL);
return (JavaPlugin) normalClass.newInstance();
}
else
{
Class<?> normalClass = Class.forName("at.pcgamingfreaks.Minepacks.Bukkit.Minepacks");
return (JavaPlugin) normalClass.newInstance();
Class<?> standaloneClass = Class.forName(MAIN_CLASS_STANDALONE);
return (JavaPlugin) standaloneClass.newInstance();
}
}
catch(Exception e)
@ -70,4 +58,44 @@ public void bootstrap(@NotNull PluginProviderContext context)
throw new RuntimeException("Failed to create Minepacks plugin instance!", e);
}
}
private boolean patchPluginMeta(final @NotNull PluginProviderContext context)
{
try
{
Class<?> pluginMetaClass = context.getConfiguration().getClass();
Field mainField = pluginMetaClass.getDeclaredField("main");
mainField.setAccessible(true);
mainField.set(context.getConfiguration(), MAIN_CLASS_NORMAL);
return true;
}
catch(Exception e)
{
context.getLogger().log(Level.SEVERE, "Failed to patch main class in PluginMeta! Falling back to Standalone mode!", e);
}
return false;
}
private boolean checkPcgfPluginLib(final @NotNull PluginProviderContext context)
{
try
{
Class.forName("at.pcgamingfreaks.PluginLib.Bukkit.PluginLib");
//if (new Version(pcgfPluginLib.getDescription().getVersion()).olderThan(new Version(MagicValues.MIN_PCGF_PLUGIN_LIB_VERSION)))
if (true) // TODO check version
{
context.getLogger().info("PCGF-PluginLib installed. Switching to normal mode!");
}
else
{
context.getLogger().info("PCGF-PluginLib to old! Switching to standalone mode!");
}
return true;
}
catch(ClassNotFoundException ignored)
{
context.getLogger().info("PCGF-PluginLib not installed. Switching to standalone mode!");
}
return false;
}
}