mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +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
@@ -27,8 +27,8 @@
|
||||
"http://checkstyle.org/dtds/suppressions_1_2.dtd">
|
||||
<suppressions>
|
||||
<!-- add any necessary suppressions here -->
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common)[\\/].*" checks="FilteringWriteTag" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common)[\\/].*" checks="MissingJavadocMethod" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common)[\\/].*" checks="MissingJavadocPackage" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common)[\\/].*" checks="MissingJavadocType" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common|velocity)[\\/].*" checks="FilteringWriteTag" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common|velocity)[\\/].*" checks="MissingJavadocMethod" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common|velocity)[\\/].*" checks="MissingJavadocPackage" />
|
||||
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/](bukkit|sponge|common|velocity)[\\/].*" checks="MissingJavadocType" />
|
||||
</suppressions>
|
||||
@@ -1,6 +1,7 @@
|
||||
dependencies {
|
||||
// Doesn't add any dependencies, only version constraints
|
||||
api(platform(libs.adventureBom))
|
||||
api(platform(libs.log4jBom))
|
||||
|
||||
// These are provided by Paper and Sponge
|
||||
compileOnlyApi(libs.adventureApi)
|
||||
|
||||
@@ -5,8 +5,10 @@ import net.kyori.event.EventSubscriber;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
record EventSubscriberImpl<T extends CarbonEvent>(Consumer<T> consumer, int postOrder, boolean acceptsCancelled) implements EventSubscriber<T> {
|
||||
|
||||
@Override
|
||||
public void on(final @NonNull T event) {
|
||||
this.consumer.accept(event);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -51,10 +51,10 @@ bukkit {
|
||||
loadBefore = listOf("Essentials")
|
||||
website = GITHUB_REPO_URL
|
||||
permissions {
|
||||
create("carbonchat.channels.global.see") {
|
||||
register("carbonchat.channels.global.see") {
|
||||
default = TRUE
|
||||
}
|
||||
create("carbonchat.channels.global.use") {
|
||||
register("carbonchat.channels.global.use") {
|
||||
default = TRUE
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -15,7 +16,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonServerBukkit implements CarbonServer {
|
||||
public final class CarbonServerBukkit implements CarbonServer, ForwardingAudience.Single {
|
||||
|
||||
private final CarbonChatBukkitEntry chatBukkitEntry;
|
||||
private final UserManager userManager;
|
||||
@@ -26,6 +27,11 @@ public final class CarbonServerBukkit implements CarbonServer {
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return this.console();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return this.chatBukkitEntry.getServer().getConsoleSender();
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.BasicChatChannel;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -53,7 +52,7 @@ public final class BukkitChatListener implements Listener {
|
||||
}
|
||||
|
||||
// console too!
|
||||
recipients.add(Bukkit.getConsoleSender());
|
||||
recipients.add(this.carbonChat.server());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel));
|
||||
|
||||
@@ -18,10 +18,9 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
|
||||
public CarbonPlayerBukkit(
|
||||
final @NonNull String username,
|
||||
final @NonNull Component displayName,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, displayName, uuid, Identity.identity(uuid));
|
||||
super(username, uuid, Identity.identity(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -34,7 +34,7 @@ public final class MemoryUserManagerBukkit implements UserManager {
|
||||
}
|
||||
|
||||
return this.users.computeIfAbsent(player.getUniqueId(), key ->
|
||||
new CarbonPlayerBukkit(player.getName(), player.displayName(), player.getUniqueId()));
|
||||
new CarbonPlayerBukkit(player.getName(), player.getUniqueId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,19 +17,17 @@ import static net.kyori.adventure.text.Component.text;
|
||||
public abstract class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single {
|
||||
|
||||
protected final String username;
|
||||
protected Component displayName;
|
||||
protected @Nullable Component displayName;
|
||||
protected final UUID uuid;
|
||||
protected final Identity identity;
|
||||
protected @Nullable ChatChannel selectedChannel;
|
||||
|
||||
protected CarbonPlayerCommon(
|
||||
final String username,
|
||||
final Component displayName,
|
||||
final UUID uuid,
|
||||
final Identity identity
|
||||
) {
|
||||
this.username = username;
|
||||
this.displayName = displayName;
|
||||
this.uuid = uuid;
|
||||
this.identity = identity;
|
||||
this.selectedChannel = null;
|
||||
@@ -42,7 +40,7 @@ public abstract class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudi
|
||||
|
||||
@Override
|
||||
public Component displayName() {
|
||||
return this.displayName;
|
||||
return requireNonNullElseGet(this.displayName, () -> text(this.username));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ metadata:
|
||||
- plugins
|
||||
|
||||
plugins:
|
||||
net.kyori.blossom: 1.2.0
|
||||
net.kyori.indra: &indra 2.0.5
|
||||
net.kyori.indra.checkstyle: *indra
|
||||
net.kyori.indra.publishing: *indra
|
||||
@@ -31,6 +32,7 @@ versions:
|
||||
log4j: 2.11.2
|
||||
moonshine: 0.2.4-SNAPSHOT
|
||||
guice: 5.0.1
|
||||
velocityApi: 3.0.0-SNAPSHOT
|
||||
|
||||
dependencies:
|
||||
adventureBom:
|
||||
@@ -58,10 +60,13 @@ dependencies:
|
||||
name: adventure-text-minimessage
|
||||
version: { ref: minimessage }
|
||||
|
||||
log4jBom:
|
||||
group: org.apache.logging.log4j
|
||||
name: log4j-bom
|
||||
version: { ref: log4j }
|
||||
log4jApi:
|
||||
group: org.apache.logging.log4j
|
||||
name: log4j-api
|
||||
version: { ref: log4j }
|
||||
|
||||
moonshine:
|
||||
group: com.proximyst.moonshine
|
||||
@@ -93,6 +98,10 @@ dependencies:
|
||||
group: cloud.commandframework
|
||||
name: cloud-sponge
|
||||
version: { ref: cloud }
|
||||
cloudVelocity:
|
||||
group: cloud.commandframework
|
||||
name: cloud-velocity
|
||||
version: { ref: cloud }
|
||||
|
||||
configurateCore:
|
||||
group: org.spongepowered
|
||||
@@ -148,4 +157,9 @@ dependencies:
|
||||
name: paperlib
|
||||
version: { ref: paperLib }
|
||||
|
||||
velocityApi:
|
||||
group: com.velocitypowered
|
||||
name: velocity-api
|
||||
version: { ref: velocityApi }
|
||||
|
||||
bundles:
|
||||
@@ -9,6 +9,16 @@ dependencyResolutionManagement {
|
||||
maven("https://papermc.io/repo/repository/maven-public/")
|
||||
// Sponge API
|
||||
maven("https://repo.spongepowered.org/repository/maven-public/")
|
||||
// Velocity API
|
||||
maven("https://nexus.velocitypowered.com/repository/maven-public/")
|
||||
// Velocity Proxy for run config
|
||||
//ivy("https://versions.velocitypowered.com/download/") {
|
||||
ivy("https://ci.velocitypowered.com/job/velocity-3.0.0/lastSuccessfulBuild/artifact/proxy/build/libs/") {
|
||||
//patternLayout { artifact("[revision].[ext]") }
|
||||
patternLayout { artifact("velocity-proxy-3.0.0-SNAPSHOT-all.jar") }
|
||||
metadataSources { artifact() }
|
||||
content { includeModule("com.velocitypowered", "velocity-proxy") }
|
||||
}
|
||||
// Moonshine
|
||||
maven("https://nexus.proximyst.com/repository/maven-public/") {
|
||||
content { includeGroup("com.proximyst.moonshine") }
|
||||
@@ -60,6 +70,9 @@ setupSubproject("carbonchat-bukkit") {
|
||||
setupSubproject("carbonchat-sponge") {
|
||||
projectDir = file("sponge")
|
||||
}
|
||||
setupSubproject("carbonchat-velocity") {
|
||||
projectDir = file("velocity")
|
||||
}
|
||||
|
||||
inline fun setupSubproject(name: String, block: ProjectDescriptor.() -> Unit) {
|
||||
include(name)
|
||||
|
||||
@@ -2,6 +2,7 @@ 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;
|
||||
@@ -17,9 +18,12 @@ import org.spongepowered.plugin.jvm.Plugin;
|
||||
@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 @MonotonicNonNull CarbonChatSponge carbon;
|
||||
private final @MonotonicNonNull CarbonChatSponge carbon;
|
||||
private final Game game;
|
||||
private final Injector injector;
|
||||
|
||||
@@ -38,9 +42,10 @@ public final class CarbonChatSpongeEntry {
|
||||
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.game.eventManager().registerListeners(this.pluginContainer,
|
||||
this.injector.getInstance(SpongeChatListener.class));
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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;
|
||||
@@ -17,7 +18,7 @@ import org.spongepowered.api.entity.living.player.Player;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonServerSponge implements CarbonServer {
|
||||
public final class CarbonServerSponge implements CarbonServer, ForwardingAudience.Single {
|
||||
|
||||
private final Game game;
|
||||
private final UserManager userManager;
|
||||
@@ -28,6 +29,11 @@ public final class CarbonServerSponge implements CarbonServer {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience audience() {
|
||||
return this.console();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return Sponge.systemSubject();
|
||||
|
||||
@@ -26,7 +26,11 @@ public final class SpongeChatListener {
|
||||
private final BasicChatChannel basicChat;
|
||||
|
||||
@Inject
|
||||
private SpongeChatListener(final Game game, final CarbonChatSponge carbonChat, final BasicChatChannel basicChat) {
|
||||
private SpongeChatListener(
|
||||
final Game game,
|
||||
final CarbonChatSponge carbonChat,
|
||||
final BasicChatChannel basicChat
|
||||
) {
|
||||
this.game = game;
|
||||
this.carbonChat = carbonChat;
|
||||
this.basicChat = basicChat;
|
||||
@@ -62,7 +66,7 @@ public final class SpongeChatListener {
|
||||
}
|
||||
|
||||
// console too!
|
||||
recipients.add(this.game.systemSubject());
|
||||
recipients.add(this.carbonChat.server());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel));
|
||||
|
||||
@@ -25,10 +25,9 @@ public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
|
||||
public CarbonPlayerSponge(
|
||||
final @NonNull String username,
|
||||
final @NonNull Component displayName,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, displayName, uuid, Identity.identity(uuid));
|
||||
super(username, uuid, Identity.identity(uuid));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,8 +12,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
/**
|
||||
* This class should not be used.
|
||||
* No data is persisted or saved.
|
||||
@@ -39,7 +37,7 @@ public final class MemoryUserManagerSponge implements UserManager {
|
||||
final ServerPlayer player = serverPlayer.get();
|
||||
|
||||
return this.users.computeIfAbsent(player.uniqueId(), key ->
|
||||
new CarbonPlayerSponge(player.name(), text(player.name()), player.uniqueId()));
|
||||
new CarbonPlayerSponge(player.name(), player.uniqueId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import java.util.Locale
|
||||
|
||||
plugins {
|
||||
id("carbon.shadow-platform")
|
||||
id("net.kyori.blossom")
|
||||
}
|
||||
|
||||
val velocityRun: Configuration by configurations.creating {
|
||||
attributes {
|
||||
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class, Usage.JAVA_RUNTIME))
|
||||
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category::class, Category.LIBRARY))
|
||||
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements::class, LibraryElements.JAR))
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(projects.carbonchatCommon)
|
||||
|
||||
compileOnly(libs.velocityApi)
|
||||
annotationProcessor(libs.velocityApi)
|
||||
|
||||
implementation(libs.cloudVelocity)
|
||||
|
||||
velocityRun("com.velocitypowered", "velocity-proxy", libs.versions.velocityApi.get())
|
||||
}
|
||||
|
||||
tasks {
|
||||
shadowJar {
|
||||
dependencies {
|
||||
relocateDependency("io.leangen.geantyref")
|
||||
|
||||
// included in velocity
|
||||
exclude(dependency("com.google.inject:guice"))
|
||||
exclude(dependency("aopalliance:aopalliance"))
|
||||
exclude(dependency("javax.inject:javax.inject"))
|
||||
}
|
||||
}
|
||||
register<JavaExec>("runProxy") {
|
||||
standardInput = System.`in`
|
||||
classpath(velocityRun.asFileTree)
|
||||
workingDir = layout.projectDirectory.dir("run").asFile
|
||||
|
||||
dependsOn(shadowJar)
|
||||
|
||||
doFirst {
|
||||
if (!workingDir.exists()) {
|
||||
workingDir.mkdirs()
|
||||
}
|
||||
val plugins = workingDir.resolve("plugins")
|
||||
if (!plugins.exists()) {
|
||||
plugins.mkdirs()
|
||||
}
|
||||
|
||||
shadowJar.get().archiveFile.get().asFile.copyTo(plugins.resolve("carbon.jar"), overwrite = true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
blossom {
|
||||
mapOf(
|
||||
"ID" to rootProject.name.toLowerCase(Locale.ROOT),
|
||||
"NAME" to rootProject.name,
|
||||
"VERSION" to version,
|
||||
"DESCRIPTION" to description,
|
||||
"URL" to GITHUB_REPO_URL
|
||||
).forEach { (k, v) ->
|
||||
replaceToken("$[$k]", v)
|
||||
}
|
||||
}
|
||||
@@ -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