Skip to content

Commit

Permalink
Folia support
Browse files Browse the repository at this point in the history
Co-Authored-By: Camotoy <[email protected]>
  • Loading branch information
Tim203 and Camotoy committed Mar 31, 2023
1 parent f0a20aa commit 3c0e30b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object Versions {
const val cumulusVersion = "1.1.1"
const val eventsVersion = "1.0-SNAPSHOT"
const val configUtilsVersion = "1.0-SNAPSHOT"
const val spigotVersion = "1.13-R0.1-SNAPSHOT"
const val spigotVersion = "1.19.4-R0.1-SNAPSHOT"
const val fastutilVersion = "8.5.3"
const val guiceVersion = "5.1.0"
const val nettyVersion = "4.1.49.Final"
Expand Down
10 changes: 2 additions & 8 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
// mavenLocal()
mavenLocal()

// Geyser, Cumulus etc.
maven("https://repo.opencollab.dev/maven-releases") {
Expand All @@ -21,13 +21,7 @@ dependencyResolutionManagement {
// maven("https://repo.papermc.io/repository/maven-snapshots") {
// mavenContent { snapshotsOnly() }
// }
maven("https://repo.papermc.io/repository/maven-public") {
content {
includeGroupByRegex(
"(io\\.papermc\\..*|com\\.destroystokyo\\..*|com\\.velocitypowered)"
)
}
}
maven("https://repo.papermc.io/repository/maven-public")
// Spigot
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots") {
mavenContent { snapshotsOnly() }
Expand Down
15 changes: 14 additions & 1 deletion spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ var authlibVersion = "1.5.21"
var guavaVersion = "21.0"
var gsonVersion = "2.8.5"

indra {
javaVersions {
// For Folia
target(8)
minimumToolchain(17)
}
}

dependencies {
api(projects.core)

implementation("cloud.commandframework", "cloud-bukkit", Versions.cloudVersion)
// hack to make pre 1.12 work
implementation("com.google.guava", "guava", guavaVersion)

compileOnlyApi("dev.folia", "folia-api", Versions.spigotVersion) {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 17)
}
}
}

relocate("com.google.inject")
Expand All @@ -21,7 +35,6 @@ relocate("com.google.guava")
relocate("it.unimi")

// these dependencies are already present on the platform
provided("com.destroystokyo.paper", "paper-api", Versions.spigotVersion)
provided("com.mojang", "authlib", authlibVersion)
provided("io.netty", "netty-transport", Versions.nettyVersion)
provided("io.netty", "netty-codec", Versions.nettyVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public CommandUtil commandUtil(
SpigotVersionSpecificMethods versionSpecificMethods,
LanguageManager languageManager) {
return new SpigotCommandUtil(
languageManager, plugin.getServer(), api, versionSpecificMethods, plugin);
languageManager, plugin.getServer(), api, versionSpecificMethods);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.floodgate.SpigotPlugin;
import org.geysermc.floodgate.api.event.skin.SkinApplyEvent;
import org.geysermc.floodgate.api.event.skin.SkinApplyEvent.SkinData;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
Expand All @@ -48,7 +47,6 @@
@Singleton
public final class SpigotSkinApplier implements SkinApplier {
@Inject private SpigotVersionSpecificMethods versionSpecificMethods;
@Inject private SpigotPlugin plugin;
@Inject private EventBus eventBus;

@Override
Expand All @@ -62,8 +60,7 @@ private void applySkin0(FloodgatePlayer floodgatePlayer, SkinData skinData, bool
// player is probably not logged in yet
if (player == null) {
if (firstTry) {
Bukkit.getScheduler().runTaskLater(
plugin,
versionSpecificMethods.schedule(
() -> applySkin0(floodgatePlayer, skinData, false),
10 * 20
);
Expand Down Expand Up @@ -94,12 +91,10 @@ private void applySkin0(FloodgatePlayer floodgatePlayer, SkinData skinData, bool

replaceSkin(properties, event.newSkin());

// By running as a task, we don't run into async issues
plugin.getServer().getScheduler().runTask(plugin, () -> {
versionSpecificMethods.maybeSchedule(() -> {
for (Player p : Bukkit.getOnlinePlayers()) {
if (!p.equals(player) && p.canSee(player)) {
versionSpecificMethods.hidePlayer(p, player);
versionSpecificMethods.showPlayer(p, player);
versionSpecificMethods.hideAndShowPlayer(p, player);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class ClassNames {

public static final Field BUNGEE;

public static final boolean IS_FOLIA;

static {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
SPIGOT_MAPPING_PREFIX = "net.minecraft.server." + version;
Expand Down Expand Up @@ -223,6 +225,10 @@ public class ClassNames {
PAPER_VELOCITY_SUPPORT = null;
}
}

IS_FOLIA = ReflectionUtils.getClass(
"io.papermc.paper.threadedregions.scheduler.EntityScheduler"
) != null;
}

private static <T> T checkNotNull(@CheckForNull T toCheck, @CheckForNull String objectName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@

import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.platform.command.CommandUtil;
Expand All @@ -42,19 +40,17 @@
public final class SpigotCommandUtil extends CommandUtil {
private final Server server;
private final SpigotVersionSpecificMethods versionSpecificMethods;
private final JavaPlugin plugin;
private UserAudience console;

public SpigotCommandUtil(
LanguageManager manager,
Server server,
FloodgateApi api,
SpigotVersionSpecificMethods versionSpecificMethods,
JavaPlugin plugin) {
SpigotVersionSpecificMethods versionSpecificMethods
) {
super(manager, api);
this.server = server;
this.versionSpecificMethods = versionSpecificMethods;
this.plugin = plugin;
}

@Override
Expand Down Expand Up @@ -120,7 +116,7 @@ public void sendMessage(Object target, String message) {
public void kickPlayer(Object player, String message) {
// can also be console
if (player instanceof Player) {
Bukkit.getScheduler().runTask(plugin, () -> ((Player) player).kickPlayer(message));
versionSpecificMethods.schedule(() -> ((Player) player).kickPlayer(message), 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@

package org.geysermc.floodgate.util;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.geysermc.floodgate.SpigotPlugin;

public final class SpigotVersionSpecificMethods {
private static final boolean NEW_GET_LOCALE;
private static final Method GET_SPIGOT;
private static final Method OLD_GET_LOCALE;
private static final boolean NEW_VISIBILITY;

static {
NEW_GET_LOCALE = ReflectionUtils.getMethod(Player.class, "getLocale") != null;
GET_SPIGOT = ReflectionUtils.getMethod(Player.class, "spigot");
OLD_GET_LOCALE = ReflectionUtils.getMethod(Player.Spigot.class, "getLocale");

NEW_VISIBILITY = null != ReflectionUtils.getMethod(
Player.class, "hidePlayer",
Plugin.class, Player.class
Expand All @@ -48,27 +53,51 @@ public SpigotVersionSpecificMethods(SpigotPlugin plugin) {
}

public String getLocale(Player player) {
if (NEW_GET_LOCALE) {
if (OLD_GET_LOCALE == null) {
return player.getLocale();
}
return player.spigot().getLocale();
Object spigot = ReflectionUtils.invoke(player, GET_SPIGOT);
return ReflectionUtils.castedInvoke(spigot, OLD_GET_LOCALE);
}

@SuppressWarnings("deprecation")
public void hidePlayer(Player hideFor, Player playerToHide) {
if (NEW_VISIBILITY) {
hideFor.hidePlayer(plugin, playerToHide);
public void hideAndShowPlayer(Player on, Player target) {
// In Folia we don't have to schedule this as there is no concept of a single main thread.
// Instead, we have to schedule the task per player.
if (ClassNames.IS_FOLIA) {
on.getScheduler().execute(plugin, () -> hideAndShowPlayer0(on, target), null, 0);
return;
}
hideFor.hidePlayer(playerToHide);
hideAndShowPlayer0(on, target);
}

public void schedule(Runnable runnable, long delay) {
if (ClassNames.IS_FOLIA) {
plugin.getServer().getAsyncScheduler().runDelayed(
plugin, $ -> runnable.run(), delay * 50, TimeUnit.MILLISECONDS
);
return;
}
plugin.getServer().getScheduler().runTaskLater(plugin, runnable, delay);
}

@SuppressWarnings("deprecation")
public void showPlayer(Player showFor, Player playerToShow) {
private void hideAndShowPlayer0(Player source, Player target) {
if (NEW_VISIBILITY) {
showFor.showPlayer(plugin, playerToShow);
source.hidePlayer(plugin, target);
source.showPlayer(plugin, target);
return;
}
source.hidePlayer(target);
source.showPlayer(target);
}

public void maybeSchedule(Runnable runnable) {
// In Folia we don't have to schedule this as there is no concept of a single main thread.
// Instead, we have to schedule the task per player.
if (ClassNames.IS_FOLIA) {
runnable.run();
return;
}
showFor.showPlayer(playerToShow);
plugin.getServer().getScheduler().runTask(plugin, runnable);
}
}
3 changes: 2 additions & 1 deletion spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version: ${version}
author: ${author}
website: ${url}
main: org.geysermc.floodgate.SpigotPlugin
api-version: 1.13
api-version: 1.13
folia-supported: true

0 comments on commit 3c0e30b

Please sign in to comment.