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

@@ -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);
});
}