mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
Implement velocity module (#151)
Co-authored-by: jmp <[email protected]>
This commit is contained in:
1 parent
eb7ec4353f
commit
e402ef2e70
26 files changed
+702
-28
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package net.draycia.carbon.velocity;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
|
||||
import cloud.commandframework.velocity.VelocityCommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.nio.file.Path;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.common.CarbonChatCommon;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
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 org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonChatVelocity extends CarbonChatCommon {
|
||||
|
||||
private final Logger logger;
|
||||
private final CarbonServer server;
|
||||
private final Path dataDirectory;
|
||||
private final PluginContainer pluginContainer;
|
||||
private final ProxyServer proxyServer;
|
||||
|
||||
@Inject
|
||||
public CarbonChatVelocity(
|
||||
final Logger logger,
|
||||
final CarbonServer server,
|
||||
@ForCarbon final Path dataDirectory,
|
||||
final CarbonMessageService messageService,
|
||||
final PluginContainer pluginContainer,
|
||||
final ProxyServer proxyServer
|
||||
) {
|
||||
super(messageService);
|
||||
this.logger = logger;
|
||||
this.server = server;
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.pluginContainer = pluginContainer;
|
||||
this.proxyServer = proxyServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path dataDirectory() {
|
||||
return this.dataDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonServer server() {
|
||||
return this.server;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
final VelocityCommandManager<Commander> commandManager = new VelocityCommandManager<>(
|
||||
this.pluginContainer,
|
||||
this.proxyServer,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
commandSender -> {
|
||||
if (commandSender instanceof Player player) {
|
||||
return new VelocityPlayerCommander(this, player);
|
||||
}
|
||||
return VelocityCommander.from(commandSender);
|
||||
},
|
||||
commander -> ((VelocityCommander) commander).commandSource()
|
||||
);
|
||||
final var brigadierManager = commandManager.brigadierManager();
|
||||
brigadierManager.setNativeNumberSuggestions(false);
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatVelocityModule extends AbstractModule {
|
||||
|
||||
private final Logger logger;
|
||||
private final Path dataDirectory;
|
||||
private final Path pluginJar;
|
||||
|
||||
CarbonChatVelocityModule(
|
||||
final Logger logger,
|
||||
final Path dataDirectory
|
||||
) throws URISyntaxException {
|
||||
this.logger = logger;
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.pluginJar = Paths.get(CarbonChatVelocityEntry.class
|
||||
.getProtectionDomain().getCodeSource().getLocation().toURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
this.install(new CarbonCommonModule());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package net.draycia.carbon.velocity;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonServerVelocity implements CarbonServer, ForwardingAudience.Single {
|
||||
|
||||
private final ProxyServer server;
|
||||
private final UserManager userManager;
|
||||
|
||||
@Inject
|
||||
private CarbonServerVelocity(final ProxyServer server, final UserManager userManager) {
|
||||
this.server = server;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return this.console();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return this.server.getConsoleCommandSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : this.server.getAllPlayers()) {
|
||||
final @Nullable CarbonPlayer carbonPlayer = this.player(player);
|
||||
|
||||
if (carbonPlayer != null) {
|
||||
players.add(carbonPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final UUID uuid) {
|
||||
return this.userManager.carbonPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final String username) {
|
||||
return this.userManager.carbonPlayer(username);
|
||||
}
|
||||
|
||||
private @Nullable CarbonPlayer player(final Player player) {
|
||||
return this.player(player.getUniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.draycia.carbon.velocity.command;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface VelocityCommander extends Commander, ForwardingAudience.Single {
|
||||
|
||||
CommandSource commandSource();
|
||||
|
||||
static VelocityCommander from(final CommandSource source) {
|
||||
return new VelocityCommanderImpl(source);
|
||||
}
|
||||
|
||||
final class VelocityCommanderImpl implements VelocityCommander {
|
||||
|
||||
private final CommandSource commandSource;
|
||||
|
||||
private VelocityCommanderImpl(final CommandSource commandSource) {
|
||||
this.commandSource = commandSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandSource commandSource() {
|
||||
return this.commandSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return this.commandSource;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package net.draycia.carbon.velocity.command;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public record VelocityPlayerCommander(
|
||||
CarbonChat carbon,
|
||||
Player player
|
||||
) implements PlayerCommander, VelocityCommander {
|
||||
|
||||
@Override
|
||||
public CommandSource commandSource() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.server().player(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
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 java.util.ArrayList;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.BasicChatChannel;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
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;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public final class VelocityChatListener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BasicChatChannel basicChat;
|
||||
|
||||
@Inject
|
||||
private VelocityChatListener(final CarbonChat carbonChat, final BasicChatChannel basicChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.basicChat = basicChat;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event) {
|
||||
final var sender = this.carbonChat.server().player(event.getPlayer().getUniqueId());
|
||||
|
||||
if (sender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final var recipients = new ArrayList<Audience>();
|
||||
var channel = sender.selectedChannel();
|
||||
|
||||
if (channel == null) {
|
||||
channel = this.basicChat;
|
||||
}
|
||||
|
||||
for (final CarbonPlayer player : this.carbonChat.server().players()) {
|
||||
if (channel.hearingPermitted(player).permitted()) {
|
||||
recipients.add(player);
|
||||
}
|
||||
}
|
||||
|
||||
// console too!
|
||||
recipients.add(this.carbonChat.server());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel));
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, text(event.getMessage()), recipients, renderers);
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (result.wasSuccessful()) {
|
||||
for (final var recipient : chatEvent.recipients()) {
|
||||
var component = chatEvent.message();
|
||||
|
||||
for (final var renderer : chatEvent.renderers()) {
|
||||
component = renderer.render(sender,
|
||||
recipient, chatEvent.message(), chatEvent.originalMessage());
|
||||
}
|
||||
|
||||
if (component != null) {
|
||||
recipient.sendMessage(component);
|
||||
}
|
||||
}
|
||||
} else if (chatEvent.result().cancelled()) {
|
||||
|
||||
final var message = chatEvent.result().reason();
|
||||
|
||||
if (!message.equals(empty())) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
event.setResult(PlayerChatEvent.ChatResult.denied());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package net.draycia.carbon.velocity.users;
|
||||
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
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;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
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));
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
private @Nullable Player player() {
|
||||
return this.server.getPlayer(this.uuid).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
return player.getPlayerSettings().getLocale();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component createItemHoverComponent() {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final String permission) {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
return player.hasPermission(permission);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user