Folia support & display name state handling improvements (#240)

This commit is contained in:
Jason authored and GitHub committed 2023-04-25 12:48:40 -07:00
1 parent 0efeeaa5c8
commit 052c3731f5
12 files changed
+232 -17

No files matched your search

@@ -65,6 +65,7 @@ class PaperPluginDescription(project: Project) : PluginDescription {
@Optional
@JsonProperty("default-permission")
var defaultPermission: BukkitPluginDescription.Permission.Default? = null
@Input @Optional @JsonProperty("folia-supported") var foliaSupported: Boolean? = null
@Nested
val dependencies: MutableList<Dependency> = mutableListOf()
@@ -0,0 +1,45 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* 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 <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface PlatformScheduler {
void scheduleForPlayer(CarbonPlayer carbonPlayer, Runnable runnable);
@Singleton
final class RunImmediately implements PlatformScheduler {
@Inject
private RunImmediately() {
}
@Override
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
runnable.run();
}
}
}
@@ -33,6 +33,7 @@ import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.PlatformScheduler;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
@@ -51,6 +52,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
private transient @MonotonicNonNull @Inject ProfileResolver profileResolver;
private transient @MonotonicNonNull @Inject PlatformScheduler scheduler;
private volatile transient long transientLoadedSince = -1;
protected final PersistentUserProperty<Boolean> muted;
@@ -141,6 +143,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
);
}
public void schedule(final Runnable task) {
this.scheduler.scheduleForPlayer(this, task);
}
public void registerPropertyUpdateListener(final Runnable task) {
this.properties().forEach(prop -> prop.registerUpdateListener(task));
}
@@ -34,6 +34,7 @@ import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
@@ -122,6 +123,7 @@ public final class CarbonChatFabricModule extends AbstractModule {
this.bind(Path.class).annotatedWith(DataDirectory.class).toInstance(FabricLoader.getInstance().getConfigDir().resolve(this.modContainer.getMetadata().getId()));
this.bind(CarbonServer.class).to(CarbonServerFabric.class);
this.bind(ProfileResolver.class).to(FabricProfileResolver.class);
this.bind(PlatformScheduler.class).to(FabricScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(FabricUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(FabricUserManager.class);
}
@@ -0,0 +1,45 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* 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 <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.PlatformScheduler;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
@Singleton
public final class FabricScheduler implements PlatformScheduler {
private final MinecraftServerHolder serverHolder;
@Inject
private FabricScheduler(final MinecraftServerHolder serverHolder) {
this.serverHolder = serverHolder;
}
@Override
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
this.serverHolder.requireServer().execute(runnable);
}
}
+7 -2
View File
@@ -6,8 +6,8 @@ metadata:
plugins:
net.kyori.blossom: 1.3.0
org.spongepowered.gradle.plugin: 2.1.1
xyz.jpenilla.run-paper: 2.0.1
xyz.jpenilla.run-velocity: 2.0.1
xyz.jpenilla.run-paper: &run-task 2.1.0
xyz.jpenilla.run-velocity: *run-task
io.papermc.hangar-publish-plugin: 0.0.5
versions:
@@ -26,6 +26,7 @@ versions:
bstats: 3.0.0
paperApi: 1.19.4-R0.1-SNAPSHOT
paperTrail: 0.0.1-SNAPSHOT
foliaApi: 1.19.4-R0.1-SNAPSHOT
kyoriEventApi: 5.0.0-SNAPSHOT
kyoriRegistry: 1.0.0-SNAPSHOT
kyoriMoonshine: 2.0.4
@@ -281,6 +282,10 @@ dependencies:
group: io.papermc
name: paper-trail
version: { ref: paperTrail }
foliaApi:
group: dev.folia
name: folia-api
version: { ref: foliaApi }
velocityApi:
group: com.velocitypowered
+8 -1
View File
@@ -10,7 +10,7 @@ dependencies {
implementation(projects.carbonchatCommon)
// Server
compileOnly(libs.paperApi)
compileOnly(libs.foliaApi)
implementation(libs.paperTrail)
// Commands
@@ -40,6 +40,8 @@ tasks {
}
}
runPaper.folia.registerTask()
paper {
name = rootProject.name
version = project.version as String
@@ -57,6 +59,7 @@ paper {
dependencies += PaperPluginDescription.Dependency("DiscordSRV", false)
dependencies += PaperPluginDescription.Dependency("MiniPlaceholders", false)
website = GITHUB_REPO_URL
foliaSupported = true
}
bukkit {
@@ -76,3 +79,7 @@ carbonPermission.permissions.get().forEach {
}
}
}
modrinth {
loaders.addAll("paper", "folia")
}
@@ -35,6 +35,7 @@ import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
@@ -128,6 +129,7 @@ public final class CarbonChatPaperModule extends AbstractModule {
this.bind(CarbonServer.class).to(CarbonServerPaper.class);
this.bind(PlayerSuggestions.class).toInstance(new PlayerArgument.PlayerParser<Commander>()::suggestions);
this.bind(ProfileResolver.class).to(PaperProfileResolver.class);
this.bind(PlatformScheduler.class).to(PaperScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(PaperUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(PaperUserManager.class);
}
@@ -69,4 +69,13 @@ public final class CarbonServerPaper implements CarbonServer, ForwardingAudience
return this.userManager;
}
public static boolean isFolia() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
return true;
} catch (final ClassNotFoundException ignored) {
return false;
}
}
}
@@ -0,0 +1,96 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* 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 <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.paper;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.PlatformScheduler;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@Singleton
@DefaultQualifier(NonNull.class)
public final class PaperScheduler implements PlatformScheduler {
private static final boolean FOLIA;
static {
boolean folia;
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
folia = true;
} catch (final ClassNotFoundException exception) {
folia = false;
}
FOLIA = folia;
}
private final JavaPlugin plugin;
private final Server server;
private final @Nullable Folia folia;
@Inject
private PaperScheduler(final JavaPlugin plugin, final Server server) {
this.plugin = plugin;
this.server = server;
this.folia = FOLIA ? new Folia() : null;
}
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
if (this.folia != null) {
this.folia.scheduleForPlayer(carbonPlayer, runnable);
return;
}
if (this.server.isPrimaryThread()) {
runnable.run();
} else {
this.server.getScheduler().runTask(this.plugin, runnable);
}
}
// inner class to avoid Guice trying to load ScheduledTask when scanning for methods to inject,
// and finding the synthetic method generated for our ScheduledTask consumer lambda
private final class Folia implements PlatformScheduler {
@Override
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
final @Nullable Player player = PaperScheduler.this.server.getPlayer(carbonPlayer.uuid());
if (player == null) {
runnable.run();
return;
}
if (PaperScheduler.this.server.isOwnedByCurrentRegion(player)) {
runnable.run();
} else {
player.getScheduler().run(PaperScheduler.this.plugin, $ -> runnable.run(), null);
}
}
}
}
@@ -22,8 +22,8 @@ package net.draycia.carbon.paper.users;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
@@ -52,14 +52,8 @@ public final class CarbonPlayerPaper extends WrappedCarbonPlayer implements Forw
public CarbonPlayerPaper(final CarbonPlayerCommon carbonPlayerCommon) {
this.carbonPlayerCommon = carbonPlayerCommon;
this.player().ifPresent(player -> Optional.ofNullable(carbonPlayerCommon.displayName()).ifPresent(displayName -> {
if (!Objects.equals(player.displayName(), displayName)) {
player.displayName(displayName);
}
if (!Objects.equals(player.playerListName(), displayName)) {
player.playerListName(displayName);
}
}));
this.player().ifPresent(this.applyDisplayNameToBukkit(carbonPlayerCommon.displayName()));
}
private Optional<Player> player() {
@@ -112,12 +106,13 @@ public final class CarbonPlayerPaper extends WrappedCarbonPlayer implements Forw
public void displayName(final @Nullable Component displayName) {
this.carbonPlayerCommon.displayName(displayName);
this.player().ifPresent(player -> {
// Update player's name in chat
player.displayName(displayName);
this.player().ifPresent(this.applyDisplayNameToBukkit(displayName));
}
// Update player's name in the tab player list
player.playerListName(displayName);
private Consumer<Player> applyDisplayNameToBukkit(final @Nullable Component displayName) {
return bukkit -> this.carbonPlayerCommon.schedule(() -> {
bukkit.displayName(displayName);
bukkit.playerListName(displayName);
});
}
@@ -38,6 +38,7 @@ import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.users.ProfileResolver;
@@ -116,6 +117,7 @@ public final class CarbonChatVelocityModule extends AbstractModule {
this.bind(ProfileResolver.class).to(VelocityProfileResolver.class);
this.bind(Path.class).annotatedWith(DataDirectory.class).toInstance(this.dataDirectory);
this.bind(Logger.class).toInstance(this.logger);
this.bind(PlatformScheduler.class).to(PlatformScheduler.RunImmediately.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(VelocityUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(VelocityUserManager.class);
}