mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +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
@@ -31,7 +31,7 @@ sponge {
|
||||
plugin(rootProject.name.toLowerCase(Locale.ROOT)) {
|
||||
loader(PluginLoaders.JAVA_PLAIN)
|
||||
displayName(rootProject.name)
|
||||
mainClass("net.draycia.carbon.sponge.CarbonChatSpongeEntry")
|
||||
mainClass("net.draycia.carbon.sponge.CarbonChatSponge")
|
||||
description(project.description)
|
||||
links {
|
||||
homepage(GITHUB_REPO_URL)
|
||||
|
||||
@@ -4,48 +4,110 @@ import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
|
||||
import cloud.commandframework.sponge.SpongeCommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.Injector;
|
||||
import java.nio.file.Path;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.CarbonChatCommon;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.sponge.command.SpongeCommander;
|
||||
import net.draycia.carbon.sponge.command.SpongePlayerCommander;
|
||||
import net.draycia.carbon.sponge.listeners.SpongeChatListener;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Server;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
|
||||
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
import org.spongepowered.plugin.jvm.Plugin;
|
||||
|
||||
// TODO: this class should die
|
||||
@Plugin("carbonchat")
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@Singleton
|
||||
public final class CarbonChatSponge extends CarbonChatCommon {
|
||||
|
||||
private final UserManager userManager;
|
||||
private final Logger logger;
|
||||
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
|
||||
SpongeChatListener.class
|
||||
);
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
private final CarbonServerSponge carbonServerSponge;
|
||||
private final ChannelRegistry channelRegistry;
|
||||
private final Game game;
|
||||
private final Injector injector;
|
||||
private final Logger logger;
|
||||
private final Path dataDirectory;
|
||||
private final PluginContainer pluginContainer;
|
||||
private final UserManager userManager;
|
||||
|
||||
@Inject
|
||||
private CarbonChatSponge(
|
||||
public CarbonChatSponge(
|
||||
//final Metrics.Factory metricsFactory,
|
||||
final Game game,
|
||||
final PluginContainer pluginContainer,
|
||||
final Injector injector,
|
||||
final Logger logger,
|
||||
final @ForCarbon Path dataDirectory,
|
||||
final CarbonServerSponge carbonServerSponge,
|
||||
final UserManager userManager,
|
||||
final CarbonMessageService messageService
|
||||
@ConfigDir(sharedRoot = false) final Path dataDirectory
|
||||
) {
|
||||
super(messageService);
|
||||
this.userManager = userManager;
|
||||
this.logger = logger;
|
||||
this.carbonServerSponge = carbonServerSponge;
|
||||
this.dataDirectory = dataDirectory;
|
||||
CarbonChatProvider.register(this);
|
||||
|
||||
this.game = game;
|
||||
this.pluginContainer = pluginContainer;
|
||||
|
||||
this.injector = injector.createChildInjector(new CarbonChatSpongeModule(
|
||||
this, dataDirectory));
|
||||
|
||||
this.logger = logger;
|
||||
this.messageService = this.injector.getInstance(CarbonMessageService.class);
|
||||
this.channelRegistry = this.injector.getInstance(ChannelRegistry.class);
|
||||
this.carbonServerSponge = this.injector.getInstance(CarbonServerSponge.class);
|
||||
this.userManager = this.injector.getInstance(UserManager.class);
|
||||
this.dataDirectory = dataDirectory;
|
||||
|
||||
for (final Class<?> clazz : LISTENER_CLASSES) {
|
||||
this.game.eventManager().registerListeners(this.pluginContainer, this.injector.getInstance(clazz));
|
||||
}
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
Sponge.asyncScheduler().submit(Task.builder()
|
||||
.interval(5, TimeUnit.MINUTES)
|
||||
.plugin(this.pluginContainer)
|
||||
.execute(this::savePlayers)
|
||||
.build());
|
||||
|
||||
((CarbonChannelRegistry) this.channelRegistry()).loadChannels();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onDisable(final StoppingEngineEvent<Server> event) {
|
||||
this.savePlayers();
|
||||
}
|
||||
|
||||
private void savePlayers() {
|
||||
for (final var player : this.server().players()) {
|
||||
this.userManager().savePlayer(player.carbonPlayer()).thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
this.server().console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public UserManager userManager() {
|
||||
@@ -63,10 +125,20 @@ public final class CarbonChatSponge extends CarbonChatCommon {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonServer server() {
|
||||
public CarbonServerSponge server() {
|
||||
return this.carbonServerSponge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelRegistry channelRegistry() {
|
||||
return this.channelRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonMessageService messageService() {
|
||||
return this.messageService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandManager<Commander> createCommandManager() {
|
||||
final SpongeCommandManager<Commander> commandManager = new SpongeCommandManager<>(
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import java.util.Set;
|
||||
import net.draycia.carbon.sponge.listeners.SpongeChatListener;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Server;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
import org.spongepowered.plugin.jvm.Plugin;
|
||||
|
||||
@Plugin("carbonchat")
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatSpongeEntry {
|
||||
|
||||
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
|
||||
SpongeChatListener.class
|
||||
);
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private final @MonotonicNonNull CarbonChatSponge carbon;
|
||||
private final Game game;
|
||||
private final Injector injector;
|
||||
|
||||
private final PluginContainer pluginContainer;
|
||||
|
||||
@Inject
|
||||
public CarbonChatSpongeEntry(
|
||||
//final Metrics.Factory metricsFactory,
|
||||
final Game game,
|
||||
final PluginContainer pluginContainer,
|
||||
final Injector injector
|
||||
) {
|
||||
this.game = game;
|
||||
this.pluginContainer = pluginContainer;
|
||||
|
||||
this.injector = injector.createChildInjector(injector.getInstance(CarbonChatSpongeModule.class));
|
||||
this.carbon = this.injector.getInstance(CarbonChatSponge.class);
|
||||
|
||||
for (final Class<?> clazz : LISTENER_CLASSES) {
|
||||
this.game.eventManager().registerListeners(this.pluginContainer, this.injector.getInstance(clazz));
|
||||
}
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +1,25 @@
|
||||
package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Inject;
|
||||
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.common.CarbonCommonModule;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.sponge.users.MemoryUserManagerSponge;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatSpongeModule extends AbstractModule {
|
||||
|
||||
private final PluginContainer pluginContainer;
|
||||
private final CarbonChatSponge carbonChatSponge;
|
||||
private final Path configDir;
|
||||
|
||||
@Inject
|
||||
private CarbonChatSpongeModule(
|
||||
final PluginContainer pluginContainer,
|
||||
@ConfigDir(sharedRoot = false) final Path configDir
|
||||
public CarbonChatSpongeModule(
|
||||
final CarbonChatSponge carbonChatSponge,
|
||||
final Path configDir
|
||||
) {
|
||||
this.pluginContainer = pluginContainer;
|
||||
this.carbonChatSponge = carbonChatSponge;
|
||||
this.configDir = configDir;
|
||||
}
|
||||
|
||||
@@ -34,10 +28,8 @@ public final class CarbonChatSpongeModule extends AbstractModule {
|
||||
this.install(new CarbonCommonModule());
|
||||
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.configDir);
|
||||
this.bind(CarbonChat.class).to(CarbonChatSponge.class);
|
||||
this.bind(CarbonChatSponge.class);
|
||||
this.bind(CarbonChat.class).toInstance(this.carbonChatSponge);
|
||||
this.bind(CarbonServer.class).to(CarbonServerSponge.class);
|
||||
this.bind(UserManager.class).to(MemoryUserManagerSponge.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,17 +4,23 @@ 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.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.sponge.users.CarbonPlayerSponge;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@@ -25,47 +31,91 @@ public final class CarbonServerSponge implements CarbonServer, ForwardingAudienc
|
||||
|
||||
@Inject
|
||||
private CarbonServerSponge(final UserManager userManager, final Game game) {
|
||||
this.userManager = userManager;
|
||||
this.game = game;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return Audience.audience(this.console(), Audience.audience(this.players()));
|
||||
return this.game.server();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return Sponge.systemSubject();
|
||||
return this.game.systemSubject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
public Iterable<CarbonPlayerSponge> players() {
|
||||
final var players = new ArrayList<CarbonPlayerSponge>();
|
||||
|
||||
for (final var player : this.game.server().onlinePlayers()) {
|
||||
final @Nullable CarbonPlayer carbonPlayer = this.player(player);
|
||||
for (final var player : Sponge.server().onlinePlayers()) {
|
||||
final ComponentPlayerResult result = this.player(player).join();
|
||||
|
||||
if (carbonPlayer != null) {
|
||||
players.add(carbonPlayer);
|
||||
if (result.player() != null) {
|
||||
players.add((CarbonPlayerSponge) 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 CarbonPlayerSponge(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 CarbonPlayerSponge(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 ServerPlayer player) {
|
||||
return this.player(player.uniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
||||
// TODO: user cache?
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
Sponge.server().gameProfileManager().basicProfile(username).join().uuid()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
Sponge.server().gameProfileManager().basicProfile(uuid).join().name().get()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public record SpongePlayerCommander(
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.server().player(this.player.uniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
return requireNonNull(this.carbon.server().player(this.player.uniqueId()).join().player(), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,14 +3,17 @@ package net.draycia.carbon.sponge.listeners;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
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.sponge.CarbonChatSponge;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.filter.cause.First;
|
||||
import org.spongepowered.api.event.message.PlayerChatEvent;
|
||||
@@ -23,43 +26,45 @@ import static net.kyori.adventure.text.Component.empty;
|
||||
public final class SpongeChatListener {
|
||||
|
||||
private final CarbonChatSponge carbonChat;
|
||||
private final BasicChatChannel basicChat;
|
||||
private final ChannelRegistry registry;
|
||||
|
||||
@Inject
|
||||
private SpongeChatListener(
|
||||
final CarbonChatSponge carbonChat,
|
||||
final BasicChatChannel basicChat
|
||||
final CarbonChat carbonChat,
|
||||
final ChannelRegistry registry
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.basicChat = basicChat;
|
||||
this.carbonChat = (CarbonChatSponge) carbonChat;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player source) {
|
||||
final var sender = this.carbonChat.server().player(source.uniqueId());
|
||||
final var playerResult = this.carbonChat.server().player(source.uniqueId()).join();
|
||||
|
||||
if (sender == null) {
|
||||
if (playerResult.player() == null) {
|
||||
this.carbonChat.server().console().sendMessage(playerResult.reason());
|
||||
return;
|
||||
}
|
||||
|
||||
final var channel = requireNonNullElse(sender.selectedChannel(), this.basicChat);
|
||||
final var channel = requireNonNullElse(playerResult.player().selectedChannel(),
|
||||
this.registry.defaultValue());
|
||||
|
||||
// TODO: option to specify if the channel should invoke ChatChannel#recipients
|
||||
// or ChatChannel#filterRecipients
|
||||
// for now we will just always invoke ChatChannel#recipients
|
||||
final var recipients = channel.recipients(sender);
|
||||
final var recipients = channel.recipients(playerResult.player());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel));
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients, renderers);
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
final var chatEvent = new CarbonChatEvent(playerResult.player(), event.message(), recipients, renderers);
|
||||
final var eventResult = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (!result.wasSuccessful()) {
|
||||
if (!eventResult.wasSuccessful()) {
|
||||
final var message = chatEvent.result().reason();
|
||||
|
||||
if (!message.equals(empty())) {
|
||||
sender.sendMessage(message);
|
||||
playerResult.player().sendMessage(message);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -67,7 +72,8 @@ public final class SpongeChatListener {
|
||||
|
||||
try {
|
||||
event.setAudience(Audience.audience(chatEvent.recipients()));
|
||||
} catch (final UnsupportedOperationException ignored) {
|
||||
} catch (final UnsupportedOperationException exception) {
|
||||
exception.printStackTrace();
|
||||
// Do we log something here? Would get spammy fast.
|
||||
}
|
||||
|
||||
@@ -75,7 +81,12 @@ public final class SpongeChatListener {
|
||||
Component component = message;
|
||||
|
||||
for (final var renderer : chatEvent.renderers()) {
|
||||
component = renderer.render(sender, target, component, message);
|
||||
if (target instanceof ServerPlayer serverPlayer) {
|
||||
final ComponentPlayerResult targetPlayer = this.carbonChat.server().player(serverPlayer).join();
|
||||
component = renderer.render(playerResult.player(), targetPlayer.player(), component, message);
|
||||
} else {
|
||||
component = renderer.render(playerResult.player(), target, component, message);
|
||||
}
|
||||
}
|
||||
|
||||
if (component == Component.empty()) {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package net.draycia.carbon.sponge.users;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
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.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -21,18 +24,38 @@ import static net.kyori.adventure.text.Component.translatable;
|
||||
import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
public final class CarbonPlayerSponge implements CarbonPlayer, ForwardingAudience.Single {
|
||||
|
||||
public CarbonPlayerSponge(
|
||||
final @NonNull String username,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, uuid, Identity.identity(uuid));
|
||||
private final CarbonPlayer carbonPlayer;
|
||||
|
||||
public CarbonPlayerSponge(final CarbonPlayer carbonPlayer) {
|
||||
this.carbonPlayer = carbonPlayer;
|
||||
}
|
||||
|
||||
public CarbonPlayer carbonPlayer() {
|
||||
return this.carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player()
|
||||
.map(player -> (Audience) player)
|
||||
.orElseGet(Audience::empty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String username() {
|
||||
return this.carbonPlayer.username();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component displayName() {
|
||||
return this.carbonPlayer.displayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
this.carbonPlayer.displayName(displayName);
|
||||
|
||||
this.player().ifPresent(player -> {
|
||||
if (displayName != null) {
|
||||
@@ -44,21 +67,12 @@ public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player()
|
||||
.map(player -> (Audience) player)
|
||||
.orElseGet(Audience::empty);
|
||||
public UUID uuid() {
|
||||
return this.carbonPlayer.uuid();
|
||||
}
|
||||
|
||||
private @NonNull Optional<ServerPlayer> player() {
|
||||
return Sponge.server().player(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
return this.player()
|
||||
.map(LocaleSource::locale)
|
||||
.orElse(null);
|
||||
return Sponge.server().player(this.carbonPlayer.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -99,11 +113,40 @@ public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
final var player = this.player();
|
||||
|
||||
// Ignore inspection. Don't make code harder to read, IntelliJ.
|
||||
if (player.isPresent()) {
|
||||
return player.get().hasPermission(permission);
|
||||
}
|
||||
return player.map(serverPlayer -> serverPlayer.hasPermission(permission))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
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() {
|
||||
return this.player()
|
||||
.map(LocaleSource::locale)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@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,54 +0,0 @@
|
||||
package net.draycia.carbon.sponge.users;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
|
||||
/**
|
||||
* This class should not be used.
|
||||
* No data is persisted or saved.
|
||||
* This exists merely as a placeholder.
|
||||
*/
|
||||
@Singleton
|
||||
public final class MemoryUserManagerSponge 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 Optional<ServerPlayer> serverPlayer = Sponge.server().player(uuid);
|
||||
|
||||
if (serverPlayer.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final ServerPlayer player = serverPlayer.get();
|
||||
|
||||
return this.users.computeIfAbsent(player.uniqueId(), key ->
|
||||
new CarbonPlayerSponge(player.name(), player.uniqueId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(username);
|
||||
|
||||
if (player.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.carbonPlayer(player.get().uniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user