stuff, sponge, DI PoC

This commit is contained in:
Draycia committed 2021-05-15 05:26:34 -07:00
1 parent a75fa346b2
commit 2befeb1a08
8 files changed
+123 -17

No files matched your search

@@ -4,14 +4,18 @@ 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 net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.CarbonChatCommon;
import net.draycia.carbon.common.Injector;
import net.draycia.carbon.common.command.Commander;
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.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.jvm.Plugin;
@@ -34,6 +38,11 @@ public final class CarbonChatSponge extends CarbonChatCommon {
) {
this.pluginContainer = pluginContainer;
this.logger = logger;
Injector.provide(CarbonChat.class, this);
Sponge.eventManager().registerListeners(pluginContainer, new SpongeChatListener());
this.initialize();
//metricsFactory.make(BSTATS_PLUGIN_ID);
@@ -1,14 +1,71 @@
package net.draycia.carbon.sponge.listeners;
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.users.UserManager;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.message.PlayerChatEvent;
import java.util.ArrayList;
import static net.draycia.carbon.common.Injector.byInject;
public final class SpongeChatListener {
@Listener
public void onPlayerChat(final @NonNull PlayerChatEvent event) {
private final CarbonChat carbonChat = byInject(CarbonChat.class);
private final UserManager userManager = this.carbonChat.userManager();
@Listener
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player sender) {
var original = event.originalChatRouter().chat(sender, event.originalMessage());
// annoying bullshit, can't get recipients at all, can't format per-player
// cancel event and do EVERYTHING ourselves...
// there was discussion about this issue 6 YEARS AGO
// there have been pending PRs to address this blatant issue for ///6 years///
// https://github.com/SpongePowered/SpongeAPI/issues/612
// https://github.com/SpongePowered/SpongeAPI/pull/2340
event.setCancelled(true);
final var player = this.userManager.carbonPlayer(sender.uniqueId());
if (player == null) {
return;
}
final var recipients = new ArrayList<CarbonPlayer>();
for (final ServerPlayer recipient : Sponge.server().onlinePlayers()) {
final var bukkitRecipient = this.userManager.carbonPlayer(recipient.uniqueId());
if (bukkitRecipient != null) {
recipients.add(bukkitRecipient);
}
}
final var chatEvent = new CarbonChatEvent(player, event.message(), recipients);
final var result = this.carbonChat.eventHandler().emit(chatEvent);
if (result.wasSuccessful()) {
// TODO: send to channels
for (final var recipient : chatEvent.recipients()) {
final var component = chatEvent.renderer()
.render(player, recipient, chatEvent.message());
if (component != null) {
recipient.sendMessage(component);
}
}
}
}
}