Skip to content

Commit

Permalink
Add module for Paper
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 18, 2024
1 parent ed8eac5 commit e578c5d
Show file tree
Hide file tree
Showing 32 changed files with 1,103 additions and 3 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include (
'spark-api',
'spark-common',
'spark-bukkit',
'spark-paper',
'spark-bungeecord',
'spark-velocity',
'spark-velocity4',
Expand Down
2 changes: 2 additions & 0 deletions spark-bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
2 changes: 2 additions & 0 deletions spark-bungeecord/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,19 @@ public boolean checkSupported(SparkPlatform platform) {
if (this.setupException instanceof UnsupportedSystemException) {
platform.getPlugin().log(Level.INFO, "The async-profiler engine is not supported for your os/arch (" +
this.setupException.getMessage() + "), so the built-in Java engine will be used instead.");
} else if (this.setupException instanceof UnsupportedJvmException) {
platform.getPlugin().log(Level.INFO, "The async-profiler engine is not supported for your JVM (" +
this.setupException.getMessage() + "), so the built-in Java engine will be used instead.");
} else if (this.setupException instanceof NativeLoadingException && this.setupException.getCause().getMessage().contains("libstdc++")) {
platform.getPlugin().log(Level.WARNING, "Unable to initialise the async-profiler engine because libstdc++ is not installed.");
platform.getPlugin().log(Level.WARNING, "Please see here for more information: https://spark.lucko.me/docs/misc/Using-async-profiler#install-libstdc");
} else {
platform.getPlugin().log(Level.WARNING, "Unable to initialise the async-profiler engine: " + this.setupException.getMessage());
String error = this.setupException.getMessage();
if (this.setupException.getCause() != null) {
error += " (" + this.setupException.getCause().getMessage() + ")";
}
platform.getPlugin().log(Level.WARNING, "Unable to initialise the async-profiler engine: " + error);
platform.getPlugin().log(Level.WARNING, "Please see here for more information: https://spark.lucko.me/docs/misc/Using-async-profiler");
this.setupException.printStackTrace();
}

}
Expand All @@ -140,6 +146,12 @@ private static AsyncProfiler load(SparkPlatform platform) throws Exception {
// check compatibility
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT).replace(" ", "");
String arch = System.getProperty("os.arch").toLowerCase(Locale.ROOT);
String jvm = System.getProperty("java.vm.name");

// openj9 not supported by async-profiler at the moment
if (jvm.contains("OpenJ9")) {
throw new UnsupportedJvmException(jvm);
}

if (os.equals("linux") && arch.equals("amd64") && isLinuxMusl()) {
arch = "amd64-musl";
Expand All @@ -159,7 +171,7 @@ private static AsyncProfiler load(SparkPlatform platform) throws Exception {
}

// extract the profiler binary from the spark jar file
String resource = "spark/" + libPath + "/libasyncProfiler.so";
String resource = "spark-native/" + libPath + "/libasyncProfiler.so";
URL profilerResource = AsyncProfilerAccess.class.getClassLoader().getResource(resource);
if (profilerResource == null) {
throw new IllegalStateException("Could not find " + resource + " in spark jar file");
Expand Down Expand Up @@ -224,6 +236,12 @@ public UnsupportedSystemException(String os, String arch) {
}
}

private static final class UnsupportedJvmException extends UnsupportedOperationException {
public UnsupportedJvmException(String jvm) {
super(jvm);
}
}

private static final class NativeLoadingException extends RuntimeException {
public NativeLoadingException(Throwable cause) {
super("A runtime error occurred whilst loading the native library", cause);
Expand Down
2 changes: 2 additions & 0 deletions spark-fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'

dependencies {
exclude(dependency('org.ow2.asm::'))
Expand Down
2 changes: 2 additions & 0 deletions spark-forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
2 changes: 2 additions & 0 deletions spark-minestom/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
2 changes: 2 additions & 0 deletions spark-neoforge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
2 changes: 2 additions & 0 deletions spark-nukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ shadowJar {
exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
Expand Down
75 changes: 75 additions & 0 deletions spark-paper/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
plugins {
id 'io.github.goooler.shadow' version '8.1.7'
id 'maven-publish'
}

tasks.withType(JavaCompile) {
// override, compile targeting J21
options.release = 21
}

tasks.jar {
archiveClassifier = 'original'
}

dependencies {
implementation project(':spark-common')
compileOnly 'io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT'
}

repositories {
maven { url "https://repo.papermc.io/repository/maven-public/" }
}

shadowJar {
archiveFileName = "spark-${project.pluginVersion}-paper.jar"
archiveClassifier = ''

dependencies {
exclude(dependency('net.kyori:^(?!adventure-text-feature-pagination).+$'))
exclude(dependency('net.bytebuddy:byte-buddy-agent'))
}

relocate 'net.kyori.adventure.text.feature.pagination', 'me.lucko.spark.paper.lib.adventure.pagination'
relocate 'com.google.protobuf', 'me.lucko.spark.paper.lib.protobuf'
relocate 'org.objectweb.asm', 'me.lucko.spark.paper.lib.asm'
relocate 'one.profiler', 'me.lucko.spark.paper.lib.asyncprofiler'
relocate 'me.lucko.bytesocks.client', 'me.lucko.spark.paper.lib.bytesocks'
relocate 'org.java_websocket', 'me.lucko.spark.paper.lib.bytesocks.ws'

// nest common classes beneath the paper package to avoid conflicts with spark-bukkit
relocate 'me.lucko.spark.common', 'me.lucko.spark.paper.common'
relocate 'me.lucko.spark.proto', 'me.lucko.spark.paper.proto'
relocate 'spark-native', 'spark-paper-native'

exclude 'module-info.class'
exclude 'META-INF/maven/**'
exclude 'META-INF/proguard/**'
exclude '**/*.proto'
exclude '**/*.proto.bin'
exclude '**/*.proto'
exclude '**/*.proto.bin'
}

artifacts {
archives shadowJar
shadow shadowJar
}

publishing {
//repositories {
// maven {
// url = 'https://oss.sonatype.org/content/repositories/snapshots'
// credentials {
// username = sonatypeUsername
// password = sonatypePassword
// }
// }
//}
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
version = "${project.pluginVersion}-SNAPSHOT"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.paper;

import me.lucko.spark.common.sampler.source.ClassSourceLookup;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Field;

public class PaperClassSourceLookup extends ClassSourceLookup.ByClassLoader {
private static final Class<?> PLUGIN_CLASS_LOADER;
private static final Field PLUGIN_FIELD;

private static final Class<?> PAPER_PLUGIN_CLASS_LOADER;
private static final Field PAPER_PLUGIN_FIELD;

static {
try {
PLUGIN_CLASS_LOADER = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
PLUGIN_FIELD = PLUGIN_CLASS_LOADER.getDeclaredField("plugin");
PLUGIN_FIELD.setAccessible(true);

PAPER_PLUGIN_CLASS_LOADER = Class.forName("io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader");
PAPER_PLUGIN_FIELD = PAPER_PLUGIN_CLASS_LOADER.getDeclaredField("loadedJavaPlugin");
PAPER_PLUGIN_FIELD.setAccessible(true);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}

@Override
public String identify(ClassLoader loader) throws ReflectiveOperationException {
if (PLUGIN_CLASS_LOADER.isInstance(loader)) {
JavaPlugin plugin = (JavaPlugin) PLUGIN_FIELD.get(loader);
return plugin.getName();
} else if (PAPER_PLUGIN_CLASS_LOADER.isInstance(loader)) {
JavaPlugin plugin = (JavaPlugin) PAPER_PLUGIN_FIELD.get(loader);
return plugin.getName();
}
return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.paper;

import me.lucko.spark.common.command.sender.AbstractCommandSender;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.UUID;

public class PaperCommandSender extends AbstractCommandSender<CommandSender> {

public PaperCommandSender(CommandSender sender) {
super(sender);
}

@Override
public String getName() {
return this.delegate.getName();
}

@Override
public UUID getUniqueId() {
if (super.delegate instanceof Player player) {
return player.getUniqueId();
}
return null;
}

@Override
public void sendMessage(Component message) {
super.delegate.sendMessage(message);
}

@Override
public boolean hasPermission(String permission) {
return super.delegate.hasPermission(permission);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.paper;

import io.papermc.paper.ServerBuildInfo;
import me.lucko.spark.common.platform.PlatformInfo;

public enum PaperPlatformInfo implements PlatformInfo {
INSTANCE;

@Override
public Type getType() {
return Type.SERVER;
}

@Override
public String getName() {
return "Paper";
}

@Override
public String getBrand() {
return ServerBuildInfo.buildInfo().brandName();
}

@Override
public String getVersion() {
return ServerBuildInfo.buildInfo().asString(ServerBuildInfo.StringRepresentation.VERSION_SIMPLE);
}

@Override
public String getMinecraftVersion() {
return ServerBuildInfo.buildInfo().minecraftVersionId();
}
}
Loading

0 comments on commit e578c5d

Please sign in to comment.