mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Implement Guice and Moonshine (#139)
* Initial pass - 1/? * oops * War against legacy, part 1 * Stuff & Bukkit * Sponge * Initial pass - 1/? * more guice * Separate guice module out * Fix broken rebase * Hello from Carbon! * stuff * Move init to correct location * Bukkit has moonshine! * Minor adjustments * Sponge guice part 1 * No turning back from this party * fix relocations and exclusions * Some cleanup with the Bukkit side of things, too * Add comment explaining why we pass this argument Co-authored-by: Riley Park <[email protected]> Co-authored-by: jmp <[email protected]>
This commit is contained in:
38 files changed
+908
-129
No files matched your search
@@ -24,13 +24,6 @@ dependencies {
|
||||
isTransitive = false
|
||||
}
|
||||
|
||||
api(libs.configurateCore)
|
||||
api(libs.adventureSerializerConfigurate4) {
|
||||
isTransitive = false
|
||||
}
|
||||
api(libs.configurateHocon)
|
||||
api(libs.typesafeConfig)
|
||||
|
||||
api(libs.moonshine)
|
||||
|
||||
// we shade and relocate a newer version than minecraft provides
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package net.draycia.carbon.api;
|
||||
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* The main plugin interface.<br>
|
||||
* Instances may be obtained through {@link CarbonChatProvider#carbonChat()}.
|
||||
@@ -25,6 +25,15 @@ public interface CarbonChat {
|
||||
*/
|
||||
Logger logger();
|
||||
|
||||
/**
|
||||
* The plugin's data storage directory.<br>
|
||||
* This is where configs and misc files will be stored.
|
||||
*
|
||||
* @return the plugin's data directory
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Path dataDirectory();
|
||||
|
||||
/**
|
||||
* The event handler, used for listening to and emitting events.
|
||||
*
|
||||
@@ -34,11 +43,11 @@ public interface CarbonChat {
|
||||
CarbonEventHandler eventHandler();
|
||||
|
||||
/**
|
||||
* The user manager, used for loading and obtaining {@link CarbonPlayer CarbonPlayers}.
|
||||
* The server that carbon is running on.
|
||||
*
|
||||
* @return the user manager
|
||||
* @return the server
|
||||
* @since 2.0.0
|
||||
*/
|
||||
UserManager userManager();
|
||||
CarbonServer server();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.draycia.carbon.api;
|
||||
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* The server that carbon is running on.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface CarbonServer extends Audience {
|
||||
|
||||
/**
|
||||
* The server's console.
|
||||
*
|
||||
* @return the server's console
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Audience console();
|
||||
|
||||
/**
|
||||
* The players that are online on the server.
|
||||
*
|
||||
* @return the online players
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Iterable<? extends CarbonPlayer> players();
|
||||
|
||||
/**
|
||||
* Obtains a {@link CarbonPlayer} instance for the specified uuid.
|
||||
*
|
||||
* @param uuid the player's uuid
|
||||
* @return the player
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Nullable CarbonPlayer player(final UUID uuid);
|
||||
|
||||
/**
|
||||
* Obtains a {@link CarbonPlayer} instance for the specified username.
|
||||
*
|
||||
* @param username the player's username
|
||||
* @return the player
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Nullable CarbonPlayer player(final String username);
|
||||
|
||||
}
|
||||
@@ -75,4 +75,13 @@ public interface CarbonPlayer extends Audience, Identified {
|
||||
*/
|
||||
void selectedChannel(final ChatChannel chatChannel);
|
||||
|
||||
/**
|
||||
* Checks if the player has the specified permission.
|
||||
*
|
||||
* @param permission the permission to check
|
||||
* @return if the player has the permission
|
||||
* @since 2.0.0
|
||||
*/
|
||||
boolean hasPermission(final String permission);
|
||||
|
||||
}
|
||||
@@ -28,9 +28,15 @@ tasks {
|
||||
shadowJar {
|
||||
relocateDependency("io.papermc.lib")
|
||||
relocateDependency("io.leangen.geantyref")
|
||||
|
||||
// Guice
|
||||
relocateDependency("com.google.inject")
|
||||
relocateDependency("org.aopalliance")
|
||||
relocateDependency("javax.inject")
|
||||
}
|
||||
runServer {
|
||||
minecraftVersion("1.16.5")
|
||||
jvmArgs("-DLog4jContextSelector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector") // https://github.com/PaperMC/Paper/issues/4155
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,43 +3,67 @@ package net.draycia.carbon.bukkit;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
|
||||
import cloud.commandframework.paper.PaperCommandManager;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.command.BukkitCommander;
|
||||
import net.draycia.carbon.bukkit.command.BukkitPlayerCommander;
|
||||
import net.draycia.carbon.bukkit.users.MemoryUserManagerBukkit;
|
||||
import net.draycia.carbon.common.CarbonChatCommon;
|
||||
import net.draycia.carbon.common.Injector;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@Singleton
|
||||
public final class CarbonChatBukkit extends CarbonChatCommon {
|
||||
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerBukkit();
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
private final UserManager userManager;
|
||||
private final Logger logger;
|
||||
private final CarbonChatBukkitEntry plugin;
|
||||
private final CarbonServer carbonServerBukkit;
|
||||
|
||||
CarbonChatBukkit(final @NonNull CarbonChatBukkitEntry plugin) {
|
||||
@Inject
|
||||
private CarbonChatBukkit(
|
||||
final CarbonChatBukkitEntry plugin,
|
||||
final Logger logger,
|
||||
final CarbonServer carbonServerBukkit,
|
||||
final UserManager userManager,
|
||||
final CarbonMessageService messageService
|
||||
) {
|
||||
super(messageService);
|
||||
this.userManager = userManager;
|
||||
this.logger = logger;
|
||||
this.plugin = plugin;
|
||||
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
this.carbonServerBukkit = carbonServerBukkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
public UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
public Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path dataDirectory() {
|
||||
return this.plugin.getDataFolder().toPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonServer server() {
|
||||
return this.carbonServerBukkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandManager<Commander> createCommandManager() {
|
||||
final PaperCommandManager<Commander> commandManager;
|
||||
try {
|
||||
commandManager = new PaperCommandManager<>(
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
package net.draycia.carbon.bukkit;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Singleton;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.draycia.carbon.bukkit.listeners.BukkitChatListener;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Singleton
|
||||
public final class CarbonChatBukkitEntry extends JavaPlugin {
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
|
||||
private @MonotonicNonNull CarbonChatBukkit carbon;
|
||||
private Injector injector;
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
this.injector = Guice.createInjector(new CarbonChatBukkitModule(this,
|
||||
this.getDataFolder().toPath()));
|
||||
this.carbon = this.injector.getInstance(CarbonChatBukkit.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -24,19 +35,22 @@ public final class CarbonChatBukkitEntry extends JavaPlugin {
|
||||
this.getLogger().log(Level.SEVERE, "* Upgrade your server to Paper in order to use CarbonChat.");
|
||||
this.getLogger().log(Level.SEVERE, "*");
|
||||
PaperLib.suggestPaper(this, Level.SEVERE);
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
this.getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
this.carbon = new CarbonChatBukkit(this);
|
||||
|
||||
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new BukkitChatListener(), this);
|
||||
this.getServer().getPluginManager().registerEvents(
|
||||
this.injector.getInstance(BukkitChatListener.class), this);
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
public Injector injector() {
|
||||
return this.injector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.draycia.carbon.bukkit;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.users.MemoryUserManagerBukkit;
|
||||
import net.draycia.carbon.common.CarbonCommonModule;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatBukkitModule extends AbstractModule {
|
||||
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
private final CarbonChatBukkitEntry plugin;
|
||||
private final Path dataDirectory;
|
||||
|
||||
CarbonChatBukkitModule(
|
||||
final CarbonChatBukkitEntry plugin,
|
||||
final Path dataDirectory
|
||||
) {
|
||||
this.plugin = plugin;
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
this.install(new CarbonCommonModule());
|
||||
|
||||
this.bind(CarbonChat.class).to(CarbonChatBukkit.class);
|
||||
this.bind(Logger.class).toInstance(this.logger);
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.dataDirectory);
|
||||
this.bind(CarbonChatBukkitEntry.class).toInstance(this.plugin);
|
||||
this.bind(CarbonServer.class).to(CarbonServerBukkit.class);
|
||||
this.bind(UserManager.class).to(MemoryUserManagerBukkit.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.draycia.carbon.bukkit;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
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 org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonServerBukkit implements CarbonServer {
|
||||
|
||||
@Inject
|
||||
private CarbonChatBukkitEntry chatBukkitEntry;
|
||||
|
||||
@Inject
|
||||
private UserManager userManager;
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return this.chatBukkitEntry.getServer().getConsoleSender();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : this.chatBukkitEntry.getServer().getOnlinePlayers()) {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public interface BukkitCommander extends Commander, ForwardingAudience.Single {
|
||||
this.commandSender = commandSender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandSender commandSender() {
|
||||
return this.commandSender;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public record BukkitPlayerCommander(
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
return requireNonNull(this.carbon.server().player(this.player.getUniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package net.draycia.carbon.bukkit.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -16,17 +15,16 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.Injector.byInject;
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
|
||||
public final class BukkitChatListener implements Listener {
|
||||
|
||||
private final CarbonChatBukkit carbonChat = byInject(CarbonChat.class); // lol this is dumb
|
||||
private final UserManager userManager = this.carbonChat.userManager();
|
||||
@Inject
|
||||
private CarbonChat carbonChat;
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
final var sender = this.userManager.carbonPlayer(event.getPlayer().getUniqueId());
|
||||
final var sender = this.carbonChat.server().player(event.getPlayer().getUniqueId());
|
||||
|
||||
if (sender == null) {
|
||||
return;
|
||||
@@ -36,7 +34,7 @@ public final class BukkitChatListener implements Listener {
|
||||
final var channel = sender.selectedChannel();
|
||||
|
||||
for (final Player bukkitRecipient : event.recipients()) {
|
||||
final var recipient = this.userManager.carbonPlayer(bukkitRecipient.getUniqueId());
|
||||
final var recipient = this.carbonChat.server().player(bukkitRecipient.getUniqueId());
|
||||
|
||||
if (recipient != null && channel.hearingPermitted(recipient).permitted()) {
|
||||
recipients.add(recipient);
|
||||
|
||||
@@ -9,9 +9,11 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
|
||||
public CarbonPlayerBukkit(
|
||||
@@ -26,7 +28,7 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
|
||||
final Player player = this.player();
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
player.displayName(displayName);
|
||||
@@ -36,7 +38,7 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
final Player player = this.player();
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
@@ -51,22 +53,22 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final Player player = this.player(); // This is temporary (it's not)
|
||||
final @Nullable Player player = this.player(); // This is temporary (it's not)
|
||||
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
final @Nullable ItemStack itemStack;
|
||||
|
||||
final ItemStack mainHand = player.getInventory().getItemInMainHand();
|
||||
final @Nullable ItemStack mainHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (!mainHand.getType().isAir()) {
|
||||
if (mainHand != null && !mainHand.getType().isAir()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.getInventory().getItemInMainHand();
|
||||
final @Nullable ItemStack offHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (!offHand.getType().isAir()) {
|
||||
if (offHand != null && !offHand.getType().isAir()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
@@ -84,4 +86,15 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon {
|
||||
return itemStack.displayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final String permission) {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player != null) {
|
||||
return player.hasPermission(permission);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -16,6 +17,7 @@ import java.util.UUID;
|
||||
* No data is persisted or saved.
|
||||
* This exists merely as a placeholder.
|
||||
*/
|
||||
@Singleton
|
||||
public final class MemoryUserManagerBukkit implements UserManager {
|
||||
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
|
||||
+11
-1
@@ -1,3 +1,13 @@
|
||||
dependencies {
|
||||
api(projects.carbonchatApi)
|
||||
api(projects.carbonchatApi)
|
||||
|
||||
// Configs
|
||||
api(libs.configurateHocon)
|
||||
api(libs.adventureSerializerConfigurate4) {
|
||||
isTransitive = false
|
||||
}
|
||||
api(libs.typesafeConfig)
|
||||
|
||||
// Other
|
||||
api(libs.guice)
|
||||
}
|
||||
@@ -5,17 +5,22 @@ import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY;
|
||||
import static net.kyori.adventure.text.format.TextDecoration.BOLD;
|
||||
|
||||
public abstract class CarbonChatCommon implements CarbonChat {
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
|
||||
protected CarbonChatCommon(final CarbonMessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
private final CarbonEventHandler eventHandler = new CarbonEventHandler();
|
||||
|
||||
public final void initialize() {
|
||||
@@ -23,10 +28,9 @@ public abstract class CarbonChatCommon implements CarbonChat {
|
||||
commandManager.command(commandManager.commandBuilder("carbon")
|
||||
.handler(ctx -> {
|
||||
final Commander sender = ctx.getSender();
|
||||
sender.sendMessage(text()
|
||||
.content("Hello from ")
|
||||
.append(text("Carbon", DARK_GRAY, BOLD))
|
||||
.append(text('!')));
|
||||
|
||||
this.messageService.exampleCommandFeedback(sender, this.messageService.pluginName());
|
||||
|
||||
if (sender instanceof PlayerCommander) {
|
||||
final Component itemComponent = ((PlayerCommander) sender).carbonPlayer().createItemHoverComponent();
|
||||
if (itemComponent != empty()) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.Moonshine;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageParser;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSender;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSource;
|
||||
import net.draycia.carbon.common.messages.ComponentPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.ServerReceiverResolver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public final class CarbonCommonModule extends AbstractModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public CarbonMessageService messageService(
|
||||
final @NonNull ServerReceiverResolver serverReceiverResolver,
|
||||
final @NonNull ComponentPlaceholderResolver<Audience> componentPlaceholderResolver,
|
||||
final @NonNull CarbonMessageSource carbonMessageSource,
|
||||
final @NonNull CarbonMessageParser carbonMessageParser,
|
||||
final @NonNull CarbonMessageSender carbonMessageSender
|
||||
) {
|
||||
return Moonshine.<Audience>builder()
|
||||
.receiver(serverReceiverResolver)
|
||||
.placeholder(Component.class, componentPlaceholderResolver)
|
||||
.source(carbonMessageSource)
|
||||
.parser(carbonMessageParser)
|
||||
.sender(carbonMessageSender)
|
||||
.create(CarbonMessageService.class, this.getClass().getClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import com.google.inject.BindingAnnotation;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@BindingAnnotation
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface ForCarbon {
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
// TODO: remove
|
||||
public final class Injector {
|
||||
|
||||
private Injector() {
|
||||
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, Object> providers = new HashMap<>();
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <T, S extends T> S byInject(final @NonNull Class<T> clazz) {
|
||||
return (S) providers.get(clazz);
|
||||
}
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <T> void provide(final @NonNull Class<T> clazz, final @NonNull T provider) {
|
||||
providers.put(clazz, provider);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ConfigLoader {
|
||||
|
||||
public CommentedConfigurationNode loadConfig(final File file) throws ConfigurateException {
|
||||
final var builder = HoconConfigurationLoader.builder().prettyPrinting(true);
|
||||
|
||||
final var loader = this.loadConfigFile(builder, file);
|
||||
final CommentedConfigurationNode node = loader.load();
|
||||
|
||||
if (!file.exists()) {
|
||||
loader.save(node);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private HoconConfigurationLoader loadConfigFile(final HoconConfigurationLoader.Builder builder, final File config) {
|
||||
return builder
|
||||
.defaultOptions(opts -> {
|
||||
final ConfigurateComponentSerializer serializer = ConfigurateComponentSerializer.configurate();
|
||||
|
||||
return opts.shouldCopyDefaults(true).serializers(serializerBuilder ->
|
||||
serializerBuilder.registerAll(serializer.serializers()));
|
||||
})
|
||||
.file(config)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageParser;
|
||||
import com.proximyst.moonshine.message.ParsingContext;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
|
||||
@Singleton
|
||||
public class CarbonMessageParser implements IMessageParser<String, Component, Audience> {
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
|
||||
private final boolean checkForLegacy = !Boolean.getBoolean("disableLegacyCheck");
|
||||
private final String legacyChar = String.valueOf(LegacyComponentSerializer.SECTION_CHAR);
|
||||
|
||||
@Inject
|
||||
public CarbonMessageParser(final CarbonMessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
public Component parse(String message, final ParsingContext<Audience> parsingContext) {
|
||||
if (this.checkForLegacy) {
|
||||
if (message.contains(this.legacyChar)) {
|
||||
message = message.replace(this.legacyChar, "");
|
||||
this.messageService.unsupportedLegacyChar(parsingContext.receiver(), message);
|
||||
}
|
||||
}
|
||||
|
||||
return MiniMessage.get().parse(message, parsingContext.placeholders());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.message.IMessageSender;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class CarbonMessageSender implements IMessageSender<Audience, Component> {
|
||||
|
||||
@Override
|
||||
public void sendMessage(final Audience receiver, final Component message) {
|
||||
receiver.sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.annotation.Message;
|
||||
import com.proximyst.moonshine.annotation.Placeholder;
|
||||
import com.proximyst.moonshine.annotation.Receiver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public interface CarbonMessageService {
|
||||
|
||||
@Message("example.command.hello")
|
||||
void exampleCommandFeedback(
|
||||
@Receiver final Audience audience,
|
||||
@Placeholder final Component plugin
|
||||
);
|
||||
|
||||
@Message("unsupported.legacy")
|
||||
void unsupportedLegacyChar(
|
||||
@Receiver final Audience audience,
|
||||
@Placeholder final String message
|
||||
);
|
||||
|
||||
@Message("placeholders.plugin")
|
||||
Component pluginName();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageSource;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonMessageSource implements IMessageSource<String, Audience> {
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
@Inject
|
||||
CarbonMessageSource(final @ForCarbon Path dataDirectory) throws IOException {
|
||||
final var directoryFile = dataDirectory.toFile();
|
||||
|
||||
if (!directoryFile.exists()) {
|
||||
directoryFile.mkdirs();
|
||||
}
|
||||
|
||||
this.properties = new Properties();
|
||||
|
||||
// TODO: read file name from config, allow users to specify which file.
|
||||
// TODO: have a fallback that uses messages-en.properties in the jar
|
||||
// so that any missing entries in the user's file will be resolvable.
|
||||
final var fileName = "messages-en.properties";
|
||||
final var file = dataDirectory.resolve(fileName).toFile();
|
||||
|
||||
if (file.isFile()) {
|
||||
try (final Reader reader = new FileReader(file, StandardCharsets.UTF_8)) {
|
||||
this.properties.load(reader);
|
||||
}
|
||||
}
|
||||
|
||||
boolean write = !file.isFile();
|
||||
|
||||
try (final InputStream stream = CarbonChat.class.getResourceAsStream("/" + fileName)) {
|
||||
final Properties packaged = new Properties();
|
||||
packaged.load(stream);
|
||||
|
||||
for (final Map.Entry<Object, Object> entry : packaged.entrySet()) {
|
||||
write |= this.properties.putIfAbsent(entry.getKey(), entry.getValue()) == null;
|
||||
}
|
||||
}
|
||||
|
||||
if (write) {
|
||||
try (final Writer writer = new FileWriter(file, StandardCharsets.UTF_8)) {
|
||||
this.properties.store(writer, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String message(final String key, final Audience receiver) {
|
||||
final String value = this.properties.getProperty(key);
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("No message mapping for key " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.proximyst.moonshine.component.placeholder.IPlaceholderResolver;
|
||||
import com.proximyst.moonshine.component.placeholder.PlaceholderContext;
|
||||
import com.proximyst.moonshine.component.placeholder.ResolveResult;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ComponentPlaceholderResolver<R> implements IPlaceholderResolver<R, Component> {
|
||||
|
||||
public ResolveResult resolve(
|
||||
final String placeholderName,
|
||||
final Component value,
|
||||
final PlaceholderContext<R> ctx,
|
||||
final Multimap<String, @Nullable Object> flags
|
||||
) {
|
||||
return ResolveResult.ok(placeholderName, MiniMessage.get().serialize(value));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ServerReceiver {
|
||||
String permission() default "";
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published
|
||||
// by the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Modified to better fit in Carbon
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiver;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiverResolver;
|
||||
import com.proximyst.moonshine.component.receiver.ReceiverContext;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@Singleton
|
||||
public final class ServerReceiverResolver implements IReceiverResolver<Audience> {
|
||||
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
|
||||
@Inject
|
||||
public ServerReceiverResolver(final @NonNull CarbonServer carbonServer) {
|
||||
this.carbonServer = carbonServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<IReceiver<Audience>> resolve(final Method method) {
|
||||
final ServerReceiver annotation = method.getAnnotation(ServerReceiver.class);
|
||||
if (annotation == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(new Resolver(this.carbonServer, annotation.permission()));
|
||||
}
|
||||
|
||||
private static final class Resolver implements IReceiver<Audience> {
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
private final @Nullable String permission;
|
||||
|
||||
private Resolver(final @NonNull CarbonServer carbonServer,
|
||||
final @Nullable String permission) {
|
||||
this.carbonServer = carbonServer;
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience find(final ReceiverContext ctx) {
|
||||
if (this.permission == null) {
|
||||
return this.carbonServer;
|
||||
}
|
||||
|
||||
return Audience.audience(Stream.concat(Stream.of(this.carbonServer.console()),
|
||||
Streams.stream(this.carbonServer.players())
|
||||
.filter(player -> player.hasPermission(this.permission)))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
example.command.hello=<green>Hello from </green><plugin><green>!</green>
|
||||
unsupported.legacy=<red>Legacy color codes have been detected in a message. This is not supported behavior. Message: '<message>'
|
||||
placeholders.plugin=<#1DA3A6><bold>Carbon</bold></#1DA3A6>
|
||||
@@ -30,6 +30,7 @@ versions:
|
||||
kyoriRegistry: 1.0.0-SNAPSHOT
|
||||
log4j: 2.11.2
|
||||
moonshine: 0.2.4-SNAPSHOT
|
||||
guice: 5.0.1
|
||||
|
||||
dependencies:
|
||||
adventureBom:
|
||||
@@ -133,6 +134,11 @@ dependencies:
|
||||
name: bstats-sponge
|
||||
version: { ref: bstats }
|
||||
|
||||
guice:
|
||||
group: com.google.inject
|
||||
name: guice
|
||||
version: { ref: guice }
|
||||
|
||||
paperApi:
|
||||
group: com.destroystokyo.paper
|
||||
name: paper-api
|
||||
|
||||
@@ -21,6 +21,9 @@ tasks {
|
||||
dependencies {
|
||||
// included in sponge
|
||||
exclude(dependency("io.leangen.geantyref:geantyref"))
|
||||
exclude(dependency("com.google.inject:guice"))
|
||||
exclude(dependency("aopalliance:aopalliance"))
|
||||
exclude(dependency("javax.inject:javax.inject"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +33,7 @@ sponge {
|
||||
plugin(rootProject.name.toLowerCase(Locale.ROOT)) {
|
||||
loader(PluginLoaders.JAVA_PLAIN)
|
||||
displayName(rootProject.name)
|
||||
mainClass("net.draycia.carbon.sponge.CarbonChatSponge")
|
||||
mainClass("net.draycia.carbon.sponge.CarbonChatSpongeEntry")
|
||||
description(project.description)
|
||||
links {
|
||||
homepage(GITHUB_REPO_URL)
|
||||
|
||||
@@ -4,62 +4,72 @@ import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
|
||||
import cloud.commandframework.sponge.SpongeCommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import com.google.inject.Singleton;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.CarbonChatCommon;
|
||||
import net.draycia.carbon.common.Injector;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
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 net.draycia.carbon.sponge.users.MemoryUserManagerSponge;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
import org.spongepowered.plugin.jvm.Plugin;
|
||||
|
||||
@Plugin("carbonchat")
|
||||
import java.nio.file.Path;
|
||||
|
||||
// TODO: this class should die
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@Singleton
|
||||
public final class CarbonChatSponge extends CarbonChatCommon {
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private final PluginContainer pluginContainer;
|
||||
private final UserManager userManager;
|
||||
private final Logger logger;
|
||||
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerSponge();
|
||||
private final CarbonServerSponge carbonServerSponge;
|
||||
private final Path dataDirectory;
|
||||
private final PluginContainer pluginContainer;
|
||||
|
||||
@Inject
|
||||
public CarbonChatSponge(
|
||||
//final Metrics.@NonNull Factory metricsFactory,
|
||||
final @NonNull PluginContainer pluginContainer,
|
||||
final @NonNull Logger logger
|
||||
private CarbonChatSponge(
|
||||
final PluginContainer pluginContainer,
|
||||
final Logger logger,
|
||||
final @ForCarbon Path dataDirectory,
|
||||
final CarbonServerSponge carbonServerSponge,
|
||||
final UserManager userManager,
|
||||
final CarbonMessageService messageService
|
||||
) {
|
||||
this.pluginContainer = pluginContainer;
|
||||
super(messageService);
|
||||
this.userManager = userManager;
|
||||
this.logger = logger;
|
||||
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
|
||||
Sponge.eventManager().registerListeners(pluginContainer, new SpongeChatListener());
|
||||
|
||||
this.initialize();
|
||||
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
this.carbonServerSponge = carbonServerSponge;
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.pluginContainer = pluginContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
public UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
public Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path dataDirectory() {
|
||||
return this.dataDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonServer server() {
|
||||
return this.carbonServerSponge;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandManager<Commander> createCommandManager() {
|
||||
final SpongeCommandManager<Commander> commandManager = new SpongeCommandManager<>(
|
||||
this.pluginContainer,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
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 int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private @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(new CarbonChatSpongeModule());
|
||||
this.carbon = this.injector.getInstance(CarbonChatSponge.class);
|
||||
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onInitialize(final StartingEngineEvent<Server> event) {
|
||||
this.game.eventManager().registerListeners(this.pluginContainer,
|
||||
this.injector.getInstance(SpongeChatListener.class));
|
||||
|
||||
this.carbon.initialize();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
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.spongepowered.api.config.ConfigDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class CarbonChatSpongeModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
this.install(new CarbonCommonModule());
|
||||
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toProvider(new Provider<>() {
|
||||
@Inject @ConfigDir(sharedRoot = false) Path path;
|
||||
|
||||
@Override
|
||||
public Path get() {
|
||||
return this.path;
|
||||
}
|
||||
});
|
||||
this.bind(CarbonChat.class).to(CarbonChatSponge.class);
|
||||
this.bind(CarbonChatSponge.class);
|
||||
this.bind(CarbonServer.class).to(CarbonServerSponge.class);
|
||||
this.bind(UserManager.class).to(MemoryUserManagerSponge.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonServerSponge implements CarbonServer {
|
||||
|
||||
@Inject
|
||||
private CarbonChatSponge carbonChatSponge;
|
||||
|
||||
@Override
|
||||
public Audience console() {
|
||||
return Sponge.systemSubject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : Sponge.server().onlinePlayers()) {
|
||||
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.carbonChatSponge.server().player(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer player(final String username) {
|
||||
return this.carbonChatSponge.server().player(username);
|
||||
}
|
||||
|
||||
private @Nullable CarbonPlayer player(final Player player) {
|
||||
return this.player(player.uniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public record SpongePlayerCommander(
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.uniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
return requireNonNull(this.carbon.server().player(this.player.uniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package net.draycia.carbon.sponge.listeners;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.sponge.CarbonChatSponge;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
@@ -17,13 +16,13 @@ import org.spongepowered.api.event.message.PlayerChatEvent;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.Injector.byInject;
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
|
||||
public final class SpongeChatListener {
|
||||
|
||||
private final CarbonChatSponge carbonChat = byInject(CarbonChat.class); // lol this is dumb
|
||||
private final UserManager userManager = this.carbonChat.userManager();
|
||||
@Inject private Game game;
|
||||
@Inject
|
||||
private CarbonChatSponge carbonChat;
|
||||
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player messageSender) {
|
||||
@@ -33,7 +32,7 @@ public final class SpongeChatListener {
|
||||
// idk, let's just do our own thing in the meantime
|
||||
event.setCancelled(true);
|
||||
|
||||
final var sender = this.userManager.carbonPlayer(messageSender.uniqueId());
|
||||
final var sender = this.carbonChat.server().player(messageSender.uniqueId());
|
||||
|
||||
if (sender == null) {
|
||||
return;
|
||||
@@ -42,8 +41,8 @@ public final class SpongeChatListener {
|
||||
final var recipients = new ArrayList<Audience>();
|
||||
final var channel = sender.selectedChannel();
|
||||
|
||||
for (final ServerPlayer spongeRecipient : Sponge.server().onlinePlayers()) {
|
||||
final var recipient = this.userManager.carbonPlayer(spongeRecipient.uniqueId());
|
||||
for (final ServerPlayer spongeRecipient : this.game.server().onlinePlayers()) {
|
||||
final var recipient = this.carbonChat.server().player(spongeRecipient.uniqueId());
|
||||
|
||||
if (recipient != null && channel.hearingPermitted(recipient).permitted()) {
|
||||
recipients.add(recipient);
|
||||
@@ -52,7 +51,7 @@ public final class SpongeChatListener {
|
||||
|
||||
// console too!
|
||||
// i'm not 100% sure this is "console" but I literally cannot find anything that explicitly says console
|
||||
recipients.add(Sponge.systemSubject());
|
||||
recipients.add(this.game.systemSubject());
|
||||
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(key("carbon", "default"), channel.renderer()));
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.data.Keys;
|
||||
import org.spongepowered.api.data.type.HandTypes;
|
||||
@@ -15,6 +16,7 @@ import org.spongepowered.api.item.inventory.ItemStack;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
|
||||
public CarbonPlayerSponge(
|
||||
@@ -80,4 +82,16 @@ public final class CarbonPlayerSponge extends CarbonPlayerCommon {
|
||||
.orElse(itemStack.type().asComponent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(final String permission) {
|
||||
final var player = this.player();
|
||||
|
||||
// Ignore inspection. Don't make code harder to read, IntelliJ.
|
||||
if (player.isPresent()) {
|
||||
return player.get().hasPermission(permission);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.draycia.carbon.sponge.users;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -19,6 +20,7 @@ import static net.kyori.adventure.text.Component.text;
|
||||
* 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<>();
|
||||
|
||||
Reference in new issue
Block a user