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 a996df7
Show file tree
Hide file tree
Showing 20 changed files with 1,057 additions and 1 deletion.
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,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
73 changes: 73 additions & 0 deletions spark-paper/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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'
}

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 "Bukkit"; // TODO: change to Paper and update viewer code accordingly
}

@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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 com.google.common.collect.ImmutableMap;
import me.lucko.spark.common.monitor.ping.PlayerPingProvider;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.util.Map;

public class PaperPlayerPingProvider implements PlayerPingProvider {
private final Server server;

public PaperPlayerPingProvider(Server server) {
this.server = server;
}

@Override
public Map<String, Integer> poll() {
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
for (Player player : this.server.getOnlinePlayers()) {
builder.put(player.getName(), player.getPing());
}
return builder.build();
}
}
Loading

0 comments on commit a996df7

Please sign in to comment.