Skip to content

Commit

Permalink
Add folia platform
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Oct 20, 2024
1 parent dc9c115 commit 34fe9d5
Show file tree
Hide file tree
Showing 24 changed files with 1,089 additions and 22 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {

rootProject.name = 'spark'
include (
'spark-folia',
'spark-geyser',
'spark-minestom',
'spark-nukkit',
Expand Down
70 changes: 70 additions & 0 deletions spark-folia/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
plugins {
id 'com.gradleup.shadow' version '8.3.0'
id 'io.papermc.paperweight.userdev' version '1.7.2'
}

tasks.withType(JavaCompile) {
options.release = 21
}

java {
disableAutoTargetJvm()
}

dependencies {
implementation "me.lucko:spark-common:${project.baseVersion}-SNAPSHOT"

implementation 'net.kyori:adventure-platform-bukkit:4.3.3'
paperweight.foliaDevBundle("1.20.6-R0.1-SNAPSHOT")

// placeholders
compileOnly 'me.clip:placeholderapi:2.10.3'
compileOnly('be.maximvdw:MVdWPlaceholderAPI:3.0.1-SNAPSHOT') {
exclude(module: 'MVdWUpdater')
}
}

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

processResources {
from(sourceSets.main.resources.srcDirs) {
expand (
'pluginVersion': project.pluginVersion,
'pluginDescription': project.pluginDescription
)
include 'plugin.yml'
}
}

tasks {
assemble {
dependsOn reobfJar
}
}

shadowJar {
archiveFileName = "spark-${project.pluginVersion}-bukkit.jar"

relocate 'net.kyori.adventure', 'me.lucko.spark.lib.adventure'
relocate 'net.kyori.examination', 'me.lucko.spark.lib.adventure.examination'
relocate 'net.kyori.option', 'me.lucko.spark.lib.adventure.option'
relocate 'net.bytebuddy', 'me.lucko.spark.lib.bytebuddy'
relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf'
relocate 'org.objectweb.asm', 'me.lucko.spark.lib.asm'
relocate 'one.profiler', 'me.lucko.spark.lib.asyncprofiler'
relocate 'me.lucko.bytesocks.client', 'me.lucko.spark.lib.bytesocks'
relocate 'org.java_websocket', 'me.lucko.spark.lib.bytesocks.ws'

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

artifacts {
archives shadowJar
shadow shadowJar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.folia;

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

import java.lang.reflect.Field;

public class FoliaClassSourceLookup 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,62 @@
/*
* 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.folia;

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

import java.util.UUID;

public class FoliaCommandSender extends AbstractCommandSender<CommandSender> {
private final Audience audience;

public FoliaCommandSender(CommandSender sender, BukkitAudiences audienceFactory) {
super(sender);
this.audience = audienceFactory.sender(sender);
}

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

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

@Override
public void sendMessage(Component message) {
this.audience.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,82 @@
/*
* 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.folia;

import me.lucko.spark.common.platform.PlatformInfo;
import org.bukkit.Server;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class FoliaPlatformInfo implements PlatformInfo {
private final Server server;

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

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

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

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

@Override
public String getVersion() {
return this.server.getVersion();
}

@Override
public String getMinecraftVersion() {
try {
return this.server.getMinecraftVersion();
} catch (NoSuchMethodError e) {
// ignore
}

Class<? extends Server> serverClass = this.server.getClass();
try {
Field minecraftServerField = serverClass.getDeclaredField("console");
minecraftServerField.setAccessible(true);

Object minecraftServer = minecraftServerField.get(this.server);
Class<?> minecraftServerClass = minecraftServer.getClass();

Method getVersionMethod = minecraftServerClass.getDeclaredMethod("getVersion");
getVersionMethod.setAccessible(true);

return (String) getVersionMethod.invoke(minecraftServer);
} catch (Exception e) {
// ignore
}

return serverClass.getPackage().getName().split("\\.")[3];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.folia;

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 FoliaPlayerPingProvider implements PlayerPingProvider {

private final Server server;

public FoliaPlayerPingProvider(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 34fe9d5

Please sign in to comment.