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:
authored and GitHub committed 2021-07-01 15:53:17 -07:00
1 parent 715ba92264
commit 8854891e26
56 files changed
+1624 -574

No files matched your search

@@ -4,45 +4,94 @@ import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.velocity.VelocityCommandManager;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import java.net.URISyntaxException;
import java.nio.file.Path;
import net.draycia.carbon.api.CarbonServer;
import java.util.Set;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChannelRegistry;
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.velocity.command.VelocityCommander;
import net.draycia.carbon.velocity.command.VelocityPlayerCommander;
import net.draycia.carbon.velocity.listeners.VelocityChatListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@Plugin(
id = "$[ID]",
name = "$[NAME]",
version = "$[VERSION]",
description = "$[DESCRIPTION]",
url = "$[URL]",
authors = {"Draycia"}
)
@DefaultQualifier(NonNull.class)
public class CarbonChatVelocity extends CarbonChatCommon {
private final Logger logger;
private final CarbonServer server;
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
VelocityChatListener.class
);
private final Path dataDirectory;
private final PluginContainer pluginContainer;
private final ProxyServer proxyServer;
private final PluginContainer pluginContainer;
private final Logger logger;
private final Injector injector;
private final CarbonMessageService messageService;
private final ChannelRegistry channelRegistry;
private final CarbonServerVelocity carbonServer;
@Inject
public CarbonChatVelocity(
final Logger logger,
final CarbonServer server,
@ForCarbon final Path dataDirectory,
final CarbonMessageService messageService,
@DataDirectory final Path dataDirectory,
final ProxyServer proxyServer,
final PluginContainer pluginContainer,
final ProxyServer proxyServer
final Injector injector
) {
super(messageService);
this.logger = logger;
this.server = server;
CarbonChatProvider.register(this);
this.dataDirectory = dataDirectory;
this.pluginContainer = pluginContainer;
this.proxyServer = proxyServer;
this.pluginContainer = pluginContainer;
this.logger = LogManager.getLogger(this.pluginContainer.getDescription().getId());
final CarbonChatVelocityModule carbonVelocityModule;
try {
carbonVelocityModule = new CarbonChatVelocityModule(this.logger, this.dataDirectory);
} catch (final URISyntaxException ex) {
throw new RuntimeException(ex);
}
this.injector = injector.createChildInjector(carbonVelocityModule);
this.messageService = injector.getInstance(CarbonMessageService.class);
this.channelRegistry = injector.getInstance(ChannelRegistry.class);
this.carbonServer = injector.getInstance(CarbonServerVelocity.class);
}
@Subscribe
public void onProxyInitialization(final ProxyInitializeEvent event) {
super.initialize();
for (final Class<?> clazz : LISTENER_CLASSES) {
this.proxyServer.getEventManager().register(this, this.injector.getInstance(clazz));
}
((CarbonChannelRegistry) this.channelRegistry()).loadChannels();
}
@Override
@@ -56,8 +105,18 @@ public class CarbonChatVelocity extends CarbonChatCommon {
}
@Override
public CarbonServer server() {
return this.server;
public CarbonServerVelocity server() {
return this.carbonServer;
}
@Override
public ChannelRegistry channelRegistry() {
return this.channelRegistry;
}
@Override
public CarbonMessageService messageService() {
return this.messageService;
}
@Override
@@ -1,73 +0,0 @@
package net.draycia.carbon.velocity;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Set;
import net.draycia.carbon.velocity.listeners.VelocityChatListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@Plugin(
id = "$[ID]",
name = "$[NAME]",
version = "$[VERSION]",
description = "$[DESCRIPTION]",
url = "$[URL]",
authors = {"Draycia"}
)
@DefaultQualifier(NonNull.class)
public class CarbonChatVelocityEntry {
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
VelocityChatListener.class
);
private final Path dataDirectory;
private final ProxyServer server;
private final PluginContainer pluginContainer;
private final CarbonChatVelocity carbon;
private final Injector injector;
private final Logger logger;
@Inject
public CarbonChatVelocityEntry(
@DataDirectory final Path dataDirectory,
final ProxyServer server,
final PluginContainer pluginContainer,
final Injector injector
) {
this.dataDirectory = dataDirectory;
this.server = server;
this.pluginContainer = pluginContainer;
this.logger = LogManager.getLogger(this.pluginContainer.getDescription().getId());
final CarbonChatVelocityModule carbonVelocityModule;
try {
carbonVelocityModule = new CarbonChatVelocityModule(this.logger, this.dataDirectory);
} catch (final URISyntaxException ex) {
throw new RuntimeException(ex);
}
this.injector = injector.createChildInjector(carbonVelocityModule);
this.carbon = this.injector.getInstance(CarbonChatVelocity.class);
}
@Subscribe
public void onProxyInitialization(final ProxyInitializeEvent event) {
this.carbon.initialize();
for (final Class<?> clazz : LISTENER_CLASSES) {
this.server.getEventManager().register(this, this.injector.getInstance(clazz));
}
}
}
@@ -3,14 +3,10 @@ package net.draycia.carbon.velocity;
import com.google.inject.AbstractModule;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
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.CarbonJar;
import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.velocity.users.MemoryUserManagerVelocity;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -20,7 +16,6 @@ public final class CarbonChatVelocityModule extends AbstractModule {
private final Logger logger;
private final Path dataDirectory;
private final Path pluginJar;
CarbonChatVelocityModule(
final Logger logger,
@@ -28,8 +23,6 @@ public final class CarbonChatVelocityModule extends AbstractModule {
) throws URISyntaxException {
this.logger = logger;
this.dataDirectory = dataDirectory;
this.pluginJar = Paths.get(CarbonChatVelocityEntry.class
.getProtectionDomain().getCodeSource().getLocation().toURI());
}
@Override
@@ -39,9 +32,7 @@ public final class CarbonChatVelocityModule extends AbstractModule {
this.bind(CarbonChat.class).to(CarbonChatVelocity.class);
this.bind(Logger.class).toInstance(this.logger);
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.dataDirectory);
this.bind(Path.class).annotatedWith(CarbonJar.class).toInstance(this.pluginJar);
this.bind(CarbonServer.class).to(CarbonServerVelocity.class);
this.bind(UserManager.class).to(MemoryUserManagerVelocity.class);
}
}
@@ -1,25 +1,45 @@
package net.draycia.carbon.velocity;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.UuidUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
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.velocity.users.CarbonPlayerVelocity;
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 static net.kyori.adventure.text.Component.text;
@DefaultQualifier(NonNull.class)
public final class CarbonServerVelocity implements CarbonServer, ForwardingAudience.Single {
private final ProxyServer server;
private final UserManager userManager;
private final HttpClient client = HttpClient.newHttpClient();
private final Gson gson = new Gson();
@Inject
private CarbonServerVelocity(final ProxyServer server, final UserManager userManager) {
this.server = server;
@@ -41,28 +61,107 @@ public final class CarbonServerVelocity implements CarbonServer, ForwardingAudie
final var players = new ArrayList<CarbonPlayer>();
for (final var player : this.server.getAllPlayers()) {
final @Nullable CarbonPlayer carbonPlayer = this.player(player);
final @Nullable ComponentPlayerResult carbonPlayer = this.player(player).join();
if (carbonPlayer != null) {
players.add(carbonPlayer);
if (carbonPlayer.player() != null) {
players.add(carbonPlayer.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 CarbonPlayerVelocity(this.server, 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 CarbonPlayerVelocity(this.server, 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) {
return CompletableFuture.supplyAsync(() -> {
try {
final HttpRequest request = HttpRequest
.newBuilder(new URI("https://api.mojang.com/users/profiles/minecraft/" + username))
.GET()
.build();
final HttpResponse<String> response =
this.client.send(request, HttpResponse.BodyHandlers.ofString());
final String mojangResponse = response.body();
final JsonArray jsonArray = this.gson.fromJson(mojangResponse, JsonObject.class).getAsJsonArray();
final JsonObject json = (JsonObject) jsonArray.get(1);
return UuidUtils.fromUndashed(json.get("uuid").getAsString());
} catch (final URISyntaxException | IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
});
}
@Override
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
try {
final HttpRequest request = HttpRequest
.newBuilder(new URI("https://sessionserver.mojang.com/session/minecraft/profile/" + UuidUtils.toUndashed(uuid)))
.GET()
.build();
final HttpResponse<String> response =
this.client.send(request, HttpResponse.BodyHandlers.ofString());
final String mojangResponse = response.body();
final JsonArray jsonArray = this.gson.fromJson(mojangResponse, JsonObject.class).getAsJsonArray();
final JsonObject json = (JsonObject) jsonArray.get(1);
return json.get("name").getAsString();
} catch (final URISyntaxException | IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
});
}
}
@@ -29,7 +29,7 @@ public record VelocityPlayerCommander(
@Override
public 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!");
}
}
@@ -3,13 +3,17 @@ package net.draycia.carbon.velocity.listeners;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.Player;
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.velocity.CarbonChatVelocity;
import org.checkerframework.checker.nullness.qual.NonNull;
import static java.util.Objects.requireNonNullElse;
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.text.Component.empty;
@@ -17,28 +21,27 @@ import static net.kyori.adventure.text.Component.text;
public final class VelocityChatListener {
private final CarbonChat carbonChat;
private final BasicChatChannel basicChat;
private final CarbonChatVelocity carbonChat;
private final ChannelRegistry registry;
@Inject
private VelocityChatListener(final CarbonChat carbonChat, final BasicChatChannel basicChat) {
this.carbonChat = carbonChat;
this.basicChat = basicChat;
private VelocityChatListener(final CarbonChat carbonChat, final ChannelRegistry registry) {
this.carbonChat = (CarbonChatVelocity) carbonChat;
this.registry = registry;
}
@Subscribe
public void onPlayerChat(final @NonNull PlayerChatEvent 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;
}
var channel = sender.selectedChannel();
final var sender = playerResult.player();
if (channel == null) {
channel = this.basicChat;
}
final var channel = requireNonNullElse(playerResult.player().selectedChannel(),
this.registry.defaultValue());
final var recipients = channel.recipients(sender);
@@ -55,6 +58,13 @@ public final class VelocityChatListener {
for (final var renderer : chatEvent.renderers()) {
component = renderer.render(sender,
recipient, chatEvent.message(), chatEvent.originalMessage());
if (recipient instanceof Player player) {
final ComponentPlayerResult targetPlayer = this.carbonChat.server().player(player).join();
component = renderer.render(playerResult.player(), targetPlayer.player(), component, chatEvent.message());
} else {
component = renderer.render(playerResult.player(), recipient, component, chatEvent.message());
}
}
if (component != null) {
@@ -2,8 +2,11 @@ package net.draycia.carbon.velocity.users;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
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.identity.Identity;
@@ -16,14 +19,26 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public final class CarbonPlayerVelocity extends CarbonPlayerCommon {
private final ProxyServer server;
public CarbonPlayerVelocity(
final String username,
final UUID uuid,
final ProxyServer server
) {
super(username, uuid, Identity.identity(uuid));
private final CarbonPlayer carbonPlayer;
public CarbonPlayerVelocity(final ProxyServer server, final CarbonPlayer carbonPlayer) {
this.server = server;
this.carbonPlayer = carbonPlayer;
}
@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
@@ -37,6 +52,36 @@ public final class CarbonPlayerVelocity extends CarbonPlayerCommon {
return player;
}
@Override
public String primaryGroup() {
return "default"; // TODO: implement
}
@Override
public List<String> groups() {
return List.of("default"); // TODO: implement
}
@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();
}
@Override
public CarbonPlayer carbonPlayer() {
return this.carbonPlayer;
}
private @Nullable Player player() {
return this.server.getPlayer(this.uuid).orElse(null);
}
@@ -1,60 +0,0 @@
package net.draycia.carbon.velocity.users;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
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;
/**
* This class should not be used.
* No data is persisted or saved.
* This exists merely as a placeholder.
*/
@Singleton
public final class MemoryUserManagerVelocity implements UserManager {
private final ProxyServer server;
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
@Inject
public MemoryUserManagerVelocity(final ProxyServer server) {
this.server = server;
}
@Override
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
if (this.users.containsKey(uuid)) {
return this.users.get(uuid);
}
final Optional<Player> player = this.server.getPlayer(uuid);
if (player.isEmpty()) {
return null;
}
return this.users.computeIfAbsent(player.get().getUniqueId(), key ->
new CarbonPlayerVelocity(player.get().getUsername(), player.get().getUniqueId(), this.server));
}
@Override
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
final Optional<Player> player = this.server.getPlayer(username);
if (player.isEmpty()) {
return null;
}
return this.carbonPlayer(player.get().getUniqueId());
}
}