Skip to content

Commit

Permalink
Finally added paper plugin manager support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Jun 16, 2023
1 parent 472e348 commit c957042
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/.gradle/
.gradle/
/.idea/
/.vscode/
/bin/
/build/
build/
/out/
*.iml
/run
30 changes: 30 additions & 0 deletions PaperSupport/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plugins {
id "java"
id "maven-publish"
id "io.papermc.paperweight.userdev" version "1.5.5"
}

java.toolchain.languageVersion = JavaLanguageVersion.of(17)
compileJava.options.encoding = "UTF-8"

archivesBaseName = "PaperSupport"
group = project.maven_group
version = project.maven_version

repositories {
mavenCentral()
mavenLocal()
}

dependencies {
paperweight.paperDevBundle("1.20-R0.1-SNAPSHOT")
}

java {
withSourcesJar()
withJavadocJar()
}

javadoc {
failOnError = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.lenni0451.spm.storage;

import com.destroystokyo.paper.util.SneakyThrow;
import com.google.common.collect.ImmutableList;
import io.papermc.paper.plugin.entrypoint.Entrypoint;
import io.papermc.paper.plugin.entrypoint.LaunchEntryPointHandler;
import io.papermc.paper.plugin.provider.PluginProvider;
import io.papermc.paper.plugin.provider.type.paper.PaperPluginParent;
import io.papermc.paper.plugin.storage.ServerPluginProviderStorage;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Field;
import java.util.Optional;

public class SingularRuntimePluginProviderStorage extends ServerPluginProviderStorage {

private PluginProvider<JavaPlugin> lastProvider;
private JavaPlugin singleLoaded;

@Override
public void register(PluginProvider<JavaPlugin> provider) {
super.register(provider);
if (this.lastProvider != null) {
SneakyThrow.sneaky(new InvalidPluginException("Plugin registered two JavaPlugins"));
}
if (provider instanceof PaperPluginParent.PaperServerPluginProvider) {
throw new IllegalStateException("Cannot register paper plugins during runtime!");
}
this.lastProvider = provider;
// Register the provider into the server entrypoint, this allows it to show in /plugins correctly.
// Generally it might be better in the future to make a separate storage, as putting it into the entrypoint handlers doesn't make much sense.
LaunchEntryPointHandler.INSTANCE.register(Entrypoint.PLUGIN, provider);
}

@Override
public void enter() {
PluginProvider<JavaPlugin> provider = this.lastProvider;
if (provider == null) {
return;
}
// Manually validate dependencies, LEGACY BEHAVIOR.
// Normally it is logged, but manually adding one plugin will cause it to actually throw exceptions.
PluginDescriptionFile descriptionFile = (PluginDescriptionFile) provider.getMeta();
try {
Field depend = descriptionFile.getClass().getDeclaredField("depend");
depend.setAccessible(true);
depend.set(descriptionFile, ImmutableList.of());
} catch (IllegalAccessException | NoSuchFieldException e) {
SneakyThrow.sneaky(e);
}
// Go through normal plugin loading logic
super.enter();
}

@Override
public void processProvided(PluginProvider<JavaPlugin> provider, JavaPlugin provided) {
super.processProvided(provider, provided);
this.singleLoaded = provided;
}

@Override
public boolean throwOnCycle() {
return false;
}

public Optional<JavaPlugin> getSingleLoaded() {
return Optional.ofNullable(this.singleLoaded);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.lenni0451.spm.utils;

import io.papermc.paper.plugin.entrypoint.Entrypoint;
import io.papermc.paper.plugin.entrypoint.EntrypointHandler;
import io.papermc.paper.plugin.provider.type.PluginFileType;
import io.papermc.paper.plugin.storage.ProviderStorage;
import net.lenni0451.spm.storage.SingularRuntimePluginProviderStorage;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.jar.JarFile;

public class PaperSupport {

public static Plugin loadPlugin(final File targetFile) throws Exception {
Class<?> RuntimePluginEntrypointHandler = Class.forName("io.papermc.paper.plugin.manager.RuntimePluginEntrypointHandler");

Constructor<?> constructor = RuntimePluginEntrypointHandler.getDeclaredConstructor(ProviderStorage.class);
constructor.setAccessible(true);
SingularRuntimePluginProviderStorage storage = new SingularRuntimePluginProviderStorage();
EntrypointHandler handler = (EntrypointHandler) constructor.newInstance(storage);

JarFile file = new JarFile(targetFile);
PluginFileType<?, ?> type = PluginFileType.guessType(file);
type.register(handler, file, targetFile.toPath());

handler.enter(Entrypoint.PLUGIN);
Method getProviderStorage = RuntimePluginEntrypointHandler.getDeclaredMethod("getPluginProviderStorage");
getProviderStorage.setAccessible(true);
SingularRuntimePluginProviderStorage providerStorage = (SingularRuntimePluginProviderStorage) getProviderStorage.invoke(handler);
return providerStorage.getSingleLoaded().get();
}

}
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ dependencies {
compileOnly "spigot:1.8.8-R0.1-SNAPSHOT"
}

jar {
from {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
zipTree(project(":PaperSupport").jar.archiveFile)
}
}

java {
withSourcesJar()
withJavadocJar()
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pluginManagement {
}

rootProject.name = 'SpigotPluginManager'

include(":PaperSupport")
12 changes: 11 additions & 1 deletion src/main/java/net/lenni0451/spm/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ public Plugin loadPlugin(final String name) {
this.updatePlugin(targetFile.get());

try {
targetPlugin = this.getPluginManager().loadPlugin(targetFile.get());
try {
Class.forName("io.papermc.paper.plugin.entrypoint.EntrypointHandler");
Class<?> PaperSupport = Class.forName("net.lenni0451.spm.utils.PaperSupport");
targetPlugin = (Plugin) PaperSupport.getDeclaredMethod("loadPlugin", File.class).invoke(null, targetFile.get());
} catch (ClassNotFoundException e) {
targetPlugin = this.getPluginManager().loadPlugin(targetFile.get());
}
} catch (UnknownDependencyException e) {
// throw new IllegalStateException("Missing Dependency");
throw new IllegalStateException(I18n.t("pm.pluginutils.loadPlugin.missingDependency"));
Expand All @@ -256,6 +262,10 @@ public Plugin loadPlugin(final String name) {
} catch (InvalidDescriptionException e) {
// throw new IllegalStateException("Invalid plugin description");
throw new IllegalStateException(I18n.t("pm.pluginutils.loadPlugin.invalidPluginDescription"));
} catch (Exception e) {
//Only for paper support
// throw new IllegalStateException("Invalid plugin file");
throw new IllegalStateException(I18n.t("pm.pluginutils.loadPlugin.invalidPluginFile"));
}

targetPlugin.onLoad();
Expand Down

0 comments on commit c957042

Please sign in to comment.