mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Refactor user stuff and start on channel stuff (#153)
Co-authored-by: Riley Park <[email protected]> Co-authored-by: broccolai <[email protected]> Co-authored-by: Jason Penilla <[email protected]>
This commit is contained in:
56 files changed
+1624
-574
No files matched your search
@@ -6,7 +6,8 @@ import cloud.commandframework.paper.PaperCommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.nio.file.Path;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.command.BukkitCommander;
|
||||
import net.draycia.carbon.bukkit.command.BukkitPlayerCommander;
|
||||
@@ -25,21 +26,27 @@ public final class CarbonChatBukkit extends CarbonChatCommon {
|
||||
private final UserManager userManager;
|
||||
private final Logger logger;
|
||||
private final CarbonChatBukkitEntry plugin;
|
||||
private final CarbonServer carbonServerBukkit;
|
||||
private final CarbonServerBukkit carbonServerBukkit;
|
||||
private final CarbonMessageService messageService;
|
||||
private final ChannelRegistry channelRegistry;
|
||||
|
||||
@Inject
|
||||
private CarbonChatBukkit(
|
||||
final CarbonChatBukkitEntry plugin,
|
||||
final Logger logger,
|
||||
final CarbonServer carbonServerBukkit,
|
||||
final CarbonServerBukkit carbonServerBukkit,
|
||||
final UserManager userManager,
|
||||
final CarbonMessageService messageService
|
||||
final CarbonMessageService messageService,
|
||||
final ChannelRegistry channelRegistry
|
||||
) {
|
||||
super(messageService);
|
||||
this.userManager = userManager;
|
||||
this.logger = logger;
|
||||
this.plugin = plugin;
|
||||
this.carbonServerBukkit = carbonServerBukkit;
|
||||
this.messageService = messageService;
|
||||
this.channelRegistry = channelRegistry;
|
||||
|
||||
CarbonChatProvider.register(this);
|
||||
}
|
||||
|
||||
public UserManager userManager() {
|
||||
@@ -57,10 +64,20 @@ public final class CarbonChatBukkit extends CarbonChatCommon {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonServer server() {
|
||||
public CarbonServerBukkit server() {
|
||||
return this.carbonServerBukkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelRegistry channelRegistry() {
|
||||
return this.channelRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonMessageService messageService() {
|
||||
return this.messageService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandManager<Commander> createCommandManager() {
|
||||
final PaperCommandManager<Commander> commandManager;
|
||||
|
||||
@@ -7,7 +7,10 @@ import io.papermc.lib.PaperLib;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import net.draycia.carbon.bukkit.listeners.BukkitChatListener;
|
||||
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
@@ -40,6 +43,7 @@ public final class CarbonChatBukkitEntry extends JavaPlugin {
|
||||
this,
|
||||
this.getDataFolder().toPath()
|
||||
));
|
||||
|
||||
this.carbon = this.injector.getInstance(CarbonChatBukkit.class);
|
||||
}
|
||||
|
||||
@@ -55,10 +59,28 @@ public final class CarbonChatBukkitEntry extends JavaPlugin {
|
||||
}
|
||||
|
||||
this.carbon.initialize();
|
||||
|
||||
final long saveDelay = 5 * 60 * 20;
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this,
|
||||
this::savePlayers, saveDelay, saveDelay);
|
||||
|
||||
((CarbonChannelRegistry) this.carbon.channelRegistry()).loadChannels();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.savePlayers();
|
||||
}
|
||||
|
||||
private void savePlayers() {
|
||||
for (final var player : this.carbon.server().players()) {
|
||||
this.carbon.userManager().savePlayer(((CarbonPlayerBukkit) player).carbonPlayer()).thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
this.carbon.server().console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import com.google.inject.AbstractModule;
|
||||
import java.nio.file.Path;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.users.MemoryUserManagerBukkit;
|
||||
import net.draycia.carbon.common.CarbonCommonModule;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -37,7 +35,6 @@ public final class CarbonChatBukkitModule extends AbstractModule {
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.dataDirectory);
|
||||
this.bind(CarbonChatBukkitEntry.class).toInstance(this.plugin);
|
||||
this.bind(CarbonServer.class).to(CarbonServerBukkit.class);
|
||||
this.bind(UserManager.class).to(MemoryUserManagerBukkit.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,16 +4,24 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonServerBukkit implements CarbonServer, ForwardingAudience.Single {
|
||||
@@ -29,7 +37,7 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return Audience.audience(this.console(), Audience.audience(this.players()));
|
||||
return this.chatBukkitEntry.getServer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,28 +50,69 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : this.chatBukkitEntry.getServer().getOnlinePlayers()) {
|
||||
final @Nullable CarbonPlayer carbonPlayer = this.player(player);
|
||||
final @Nullable ComponentPlayerResult result = this.player(player).join();
|
||||
|
||||
if (carbonPlayer != null) {
|
||||
players.add(carbonPlayer);
|
||||
if (result.player() != null) {
|
||||
players.add(result.player());
|
||||
}
|
||||
}
|
||||
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final UUID uuid) {
|
||||
return this.userManager.carbonPlayer(uuid);
|
||||
private CompletableFuture<ComponentPlayerResult> wrapPlayer(final UUID uuid) {
|
||||
return this.userManager.carbonPlayer(uuid).thenCompose(result -> {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
if (result.player() != null) {
|
||||
new ComponentPlayerResult(new CarbonPlayerBukkit(result.player()), Component.empty());
|
||||
}
|
||||
|
||||
final @Nullable String name = this.resolveName(uuid).join();
|
||||
|
||||
if (name != null) {
|
||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(null,
|
||||
null, name, uuid);
|
||||
|
||||
return new ComponentPlayerResult(new CarbonPlayerBukkit(player), Component.empty());
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult(null, text("Name not found for uuid!"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final String username) {
|
||||
return this.userManager.carbonPlayer(username);
|
||||
public CompletableFuture<ComponentPlayerResult> player(final UUID uuid) {
|
||||
return this.wrapPlayer(uuid);
|
||||
}
|
||||
|
||||
private @Nullable CarbonPlayer player(final Player player) {
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult> player(final String username) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable UUID uuid = this.resolveUUID(username).join();
|
||||
|
||||
if (uuid != null) {
|
||||
return this.player(uuid).join();
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult(null, text("No UUID found for name."));
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<ComponentPlayerResult> player(final Player player) {
|
||||
return this.player(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
||||
// TODO: user cache?
|
||||
return CompletableFuture.supplyAsync(() -> Bukkit.getOfflinePlayer(username).getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
||||
// TODO: user cache?
|
||||
return CompletableFuture.supplyAsync(() -> Bukkit.getOfflinePlayer(uuid).getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public record BukkitPlayerCommander(
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.server().player(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
return requireNonNull(this.carbon.server().player(this.player.getUniqueId()).join().player(), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,10 +4,13 @@ import com.google.inject.Inject;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import java.util.ArrayList;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.BasicChatChannel;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -19,24 +22,26 @@ import static net.kyori.adventure.text.Component.empty;
|
||||
|
||||
public final class BukkitChatListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BasicChatChannel basicChat;
|
||||
private final CarbonChatBukkit carbonChat;
|
||||
private final ChannelRegistry registry;
|
||||
|
||||
@Inject
|
||||
public BukkitChatListener(final CarbonChat carbonChat, final BasicChatChannel basicChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.basicChat = basicChat;
|
||||
public BukkitChatListener(final CarbonChat carbonChat, final ChannelRegistry registry) {
|
||||
this.carbonChat = (CarbonChatBukkit) carbonChat;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
final var sender = this.carbonChat.server().player(event.getPlayer().getUniqueId());
|
||||
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
if (sender == null) {
|
||||
if (playerResult.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final var channel = requireNonNullElse(sender.selectedChannel(), this.basicChat);
|
||||
final var sender = playerResult.player();
|
||||
final var channel = requireNonNullElse(playerResult.player().selectedChannel(),
|
||||
this.registry.defaultValue());
|
||||
|
||||
// TODO: option to specify if the channel should invoke ChatChannel#recipients
|
||||
// or ChatChannel#filterRecipients
|
||||
@@ -70,7 +75,12 @@ public final class BukkitChatListener implements Listener {
|
||||
Component component = message;
|
||||
|
||||
for (final var renderer : chatEvent.renderers()) {
|
||||
component = renderer.render(sender, viewer, component, message);
|
||||
if (viewer instanceof Player player) {
|
||||
final ComponentPlayerResult targetPlayer = this.carbonChat.server().player(player).join();
|
||||
component = renderer.render(playerResult.player(), targetPlayer.player(), component, message);
|
||||
} else {
|
||||
component = renderer.render(playerResult.player(), viewer, component, message);
|
||||
}
|
||||
}
|
||||
|
||||
return component;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -14,18 +18,17 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements ForwardingAudience.Single {
|
||||
|
||||
public CarbonPlayerBukkit(
|
||||
final @NonNull String username,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, uuid, Identity.identity(uuid));
|
||||
private final CarbonPlayer carbonPlayer;
|
||||
|
||||
public CarbonPlayerBukkit(final CarbonPlayer carbonPlayer) {
|
||||
this.carbonPlayer = carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
this.carbonPlayer.displayName(displayName);
|
||||
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
@@ -35,6 +38,21 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String username() {
|
||||
return this.carbonPlayer.username();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component displayName() {
|
||||
return this.carbonPlayer.displayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID uuid() {
|
||||
return this.carbonPlayer.uuid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
final @Nullable Player player = this.player();
|
||||
@@ -46,19 +64,9 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
return player;
|
||||
}
|
||||
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
return player.locale();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
public CarbonPlayer carbonPlayer() {
|
||||
return this.carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,4 +115,44 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String primaryGroup() {
|
||||
return "default"; // TODO: implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> groups() {
|
||||
return List.of("default"); // TODO: implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
return player.locale();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.carbonPlayer.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ChatChannel selectedChannel() {
|
||||
return this.carbonPlayer.selectedChannel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectedChannel(final ChatChannel chatChannel) {
|
||||
this.carbonPlayer.selectedChannel(chatChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Identity identity() {
|
||||
return this.carbonPlayer.identity();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* This class should not be used.
|
||||
* No data is persisted or saved.
|
||||
* This exists merely as a placeholder.
|
||||
*/
|
||||
@Singleton
|
||||
public final class MemoryUserManagerBukkit implements UserManager {
|
||||
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
|
||||
if (this.users.containsKey(uuid)) {
|
||||
return this.users.get(uuid);
|
||||
}
|
||||
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.users.computeIfAbsent(player.getUniqueId(), key ->
|
||||
new CarbonPlayerBukkit(player.getName(), player.getUniqueId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Player player = Bukkit.getPlayer(username);
|
||||
|
||||
if (player == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.carbonPlayer(player.getUniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user