mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
Change indentation and improve event
This commit is contained in:
1 parent
73c7f3f78f
commit
591a6606cc
31 files changed
+1072
-974
No files matched your search
@@ -23,56 +23,56 @@ import org.spongepowered.plugin.jvm.Plugin;
|
||||
@Plugin("carbonchat")
|
||||
public final class CarbonChatSponge extends CarbonChatCommon {
|
||||
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
private static final int BSTATS_PLUGIN_ID = 11279;
|
||||
|
||||
private final PluginContainer pluginContainer;
|
||||
private final Logger logger;
|
||||
private final PluginContainer pluginContainer;
|
||||
private final Logger logger;
|
||||
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerSponge();
|
||||
private final @NonNull UserManager userManager = new MemoryUserManagerSponge();
|
||||
|
||||
@Inject
|
||||
public CarbonChatSponge(
|
||||
//final Metrics.@NonNull Factory metricsFactory,
|
||||
final @NonNull PluginContainer pluginContainer,
|
||||
final @NonNull Logger logger
|
||||
) {
|
||||
this.pluginContainer = pluginContainer;
|
||||
this.logger = logger;
|
||||
@Inject
|
||||
public CarbonChatSponge(
|
||||
//final Metrics.@NonNull Factory metricsFactory,
|
||||
final @NonNull PluginContainer pluginContainer,
|
||||
final @NonNull Logger logger
|
||||
) {
|
||||
this.pluginContainer = pluginContainer;
|
||||
this.logger = logger;
|
||||
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
Injector.provide(CarbonChat.class, this);
|
||||
|
||||
Sponge.eventManager().registerListeners(pluginContainer, new SpongeChatListener());
|
||||
Sponge.eventManager().registerListeners(pluginContainer, new SpongeChatListener());
|
||||
|
||||
this.initialize();
|
||||
this.initialize();
|
||||
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
}
|
||||
//metricsFactory.make(BSTATS_PLUGIN_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
@Override
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
@Override
|
||||
public @NonNull UserManager userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
final SpongeCommandManager<Commander> commandManager = new SpongeCommandManager<>(
|
||||
this.pluginContainer,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
commander -> ((SpongeCommander) commander).commandCause(),
|
||||
commandCause -> {
|
||||
if (commandCause.subject() instanceof ServerPlayer player) {
|
||||
return new SpongePlayerCommander(this, player, commandCause);
|
||||
}
|
||||
return SpongeCommander.from(commandCause);
|
||||
}
|
||||
);
|
||||
commandManager.parserMapper().cloudNumberSuggestions(true);
|
||||
return commandManager;
|
||||
}
|
||||
@Override
|
||||
protected @NonNull CommandManager<Commander> createCommandManager() {
|
||||
final SpongeCommandManager<Commander> commandManager = new SpongeCommandManager<>(
|
||||
this.pluginContainer,
|
||||
AsynchronousCommandExecutionCoordinator.<Commander>newBuilder().build(),
|
||||
commander -> ((SpongeCommander) commander).commandCause(),
|
||||
commandCause -> {
|
||||
if (commandCause.subject() instanceof ServerPlayer player) {
|
||||
return new SpongePlayerCommander(this, player, commandCause);
|
||||
}
|
||||
return SpongeCommander.from(commandCause);
|
||||
}
|
||||
);
|
||||
commandManager.parserMapper().cloudNumberSuggestions(true);
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,30 +8,30 @@ import org.spongepowered.api.command.CommandCause;
|
||||
|
||||
public interface SpongeCommander extends Commander, ForwardingAudience.Single {
|
||||
|
||||
@NonNull CommandCause commandCause();
|
||||
@NonNull CommandCause commandCause();
|
||||
|
||||
static @NonNull SpongeCommander from(final @NonNull CommandCause commandCause) {
|
||||
return new SpongeCommanderImpl(commandCause);
|
||||
}
|
||||
|
||||
final class SpongeCommanderImpl implements SpongeCommander {
|
||||
|
||||
private final CommandCause commandCause;
|
||||
|
||||
private SpongeCommanderImpl(final @NonNull CommandCause commandCause) {
|
||||
this.commandCause = commandCause;
|
||||
static @NonNull SpongeCommander from(final @NonNull CommandCause commandCause) {
|
||||
return new SpongeCommanderImpl(commandCause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandCause commandCause() {
|
||||
return this.commandCause;
|
||||
}
|
||||
final class SpongeCommanderImpl implements SpongeCommander {
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandCause.audience();
|
||||
}
|
||||
private final CommandCause commandCause;
|
||||
|
||||
}
|
||||
private SpongeCommanderImpl(final @NonNull CommandCause commandCause) {
|
||||
this.commandCause = commandCause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull CommandCause commandCause() {
|
||||
return this.commandCause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandCause.audience();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,19 +11,19 @@ import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public record SpongePlayerCommander(
|
||||
@NonNull CarbonChat carbon,
|
||||
@NonNull ServerPlayer player,
|
||||
@NonNull CommandCause commandCause
|
||||
@NonNull CarbonChat carbon,
|
||||
@NonNull ServerPlayer player,
|
||||
@NonNull CommandCause commandCause
|
||||
) implements PlayerCommander, SpongeCommander {
|
||||
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.uniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
@Override
|
||||
public @NonNull CarbonPlayer carbonPlayer() {
|
||||
return requireNonNull(this.carbon.userManager().carbonPlayer(this.player.uniqueId()), "No CarbonPlayer for logged in Player!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandCause.audience();
|
||||
}
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.commandCause.audience();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,9 @@ 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 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.entity.living.player.Player;
|
||||
@@ -21,53 +21,58 @@ 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();
|
||||
private final CarbonChatSponge carbonChat = byInject(CarbonChat.class); // lol this is dumb
|
||||
private final UserManager userManager = this.carbonChat.userManager();
|
||||
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player sender) {
|
||||
// https://github.com/SpongePowered/SpongeAPI/pull/2340
|
||||
// this event currently doesn't have a concept of recipients, it seems?
|
||||
// or they're in the stack?
|
||||
// idk, let's just do our own thing in the meantime
|
||||
event.setCancelled(true);
|
||||
@Listener
|
||||
public void onPlayerChat(final @NonNull PlayerChatEvent event, final @First Player messageSender) {
|
||||
// https://github.com/SpongePowered/SpongeAPI/pull/2340
|
||||
// this event currently doesn't have a concept of recipients, it seems?
|
||||
// or they're in the stack?
|
||||
// idk, let's just do our own thing in the meantime
|
||||
event.setCancelled(true);
|
||||
|
||||
final var player = this.userManager.carbonPlayer(sender.uniqueId());
|
||||
final var sender = this.userManager.carbonPlayer(messageSender.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,
|
||||
Map.of(key("carbon", "default"), this.carbonChat.vanillaChatRenderer));
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (result.wasSuccessful()) {
|
||||
// TODO: send to channels
|
||||
|
||||
for (final var recipient : chatEvent.recipients()) {
|
||||
var component = chatEvent.message();
|
||||
|
||||
for (final var entry : chatEvent.renderers().entrySet()) {
|
||||
component = entry.getValue().render(player,
|
||||
recipient, chatEvent.message(), chatEvent.originalMessage());
|
||||
if (sender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (component != null) {
|
||||
recipient.sendMessage(component);
|
||||
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());
|
||||
|
||||
if (recipient != null && channel.mayReceiveMessages(recipient)) {
|
||||
recipients.add(recipient);
|
||||
}
|
||||
}
|
||||
|
||||
// console too!
|
||||
// i'm not 100% sure this is "console" but I literally cannot find anything that explicitly says console
|
||||
recipients.add(Sponge.systemSubject());
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, event.message(), recipients,
|
||||
Map.of(key("carbon", "default"), channel.renderer()));
|
||||
final var result = this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
if (result.wasSuccessful()) {
|
||||
// TODO: send to channels
|
||||
|
||||
for (final var recipient : chatEvent.recipients()) {
|
||||
var component = chatEvent.message();
|
||||
|
||||
for (final var entry : chatEvent.renderers().entrySet()) {
|
||||
component = entry.getValue().render(sender,
|
||||
recipient, chatEvent.message(), chatEvent.originalMessage());
|
||||
}
|
||||
|
||||
if (component != null) {
|
||||
recipient.sendMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,67 +17,67 @@ import java.util.UUID;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
|
||||
this.player().ifPresent(player -> {
|
||||
if (displayName != null) {
|
||||
player.offer(Keys.CUSTOM_NAME, displayName);
|
||||
} else {
|
||||
player.remove(Keys.CUSTOM_NAME);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player()
|
||||
.map(player -> (Audience) player)
|
||||
.orElseGet(Audience::empty);
|
||||
}
|
||||
|
||||
private @NonNull Optional<ServerPlayer> player() {
|
||||
return Sponge.server().player(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final ServerPlayer player = this.player().orElse(null);
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
public CarbonPlayerSponge(
|
||||
final @NonNull String username,
|
||||
final @NonNull Component displayName,
|
||||
final @NonNull UUID uuid
|
||||
) {
|
||||
super(username, displayName, uuid, Identity.identity(uuid));
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
super.displayName(displayName);
|
||||
|
||||
final ItemStack mainHand = player.itemInHand(HandTypes.MAIN_HAND);
|
||||
|
||||
if (!mainHand.isEmpty()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.itemInHand(HandTypes.OFF_HAND);
|
||||
|
||||
if (!offHand.isEmpty()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
this.player().ifPresent(player -> {
|
||||
if (displayName != null) {
|
||||
player.offer(Keys.CUSTOM_NAME, displayName);
|
||||
} else {
|
||||
player.remove(Keys.CUSTOM_NAME);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (itemStack == null || itemStack.isEmpty()) {
|
||||
return Component.empty();
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
return this.player()
|
||||
.map(player -> (Audience) player)
|
||||
.orElseGet(Audience::empty);
|
||||
}
|
||||
|
||||
return itemStack.get(Keys.DISPLAY_NAME)
|
||||
.orElse(itemStack.type().asComponent());
|
||||
}
|
||||
private @NonNull Optional<ServerPlayer> player() {
|
||||
return Sponge.server().player(this.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createItemHoverComponent() {
|
||||
final ServerPlayer player = this.player().orElse(null);
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
final ItemStack itemStack;
|
||||
|
||||
final ItemStack mainHand = player.itemInHand(HandTypes.MAIN_HAND);
|
||||
|
||||
if (!mainHand.isEmpty()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.itemInHand(HandTypes.OFF_HAND);
|
||||
|
||||
if (!offHand.isEmpty()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemStack == null || itemStack.isEmpty()) {
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
return itemStack.get(Keys.DISPLAY_NAME)
|
||||
.orElse(itemStack.type().asComponent());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,42 +21,42 @@ import static net.kyori.adventure.text.Component.text;
|
||||
*/
|
||||
public final class MemoryUserManagerSponge implements UserManager {
|
||||
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
private final @NonNull Map<UUID, @NonNull CarbonPlayer> users = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
|
||||
if (this.users.containsKey(uuid)) {
|
||||
return this.users.get(uuid);
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull UUID uuid) {
|
||||
if (this.users.containsKey(uuid)) {
|
||||
return this.users.get(uuid);
|
||||
}
|
||||
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(uuid);
|
||||
|
||||
if (player.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final CarbonPlayer carbonPlayer = new CarbonPlayerSponge(player.get().name(),
|
||||
text(player.get().name()), player.get().uniqueId());
|
||||
|
||||
this.users.put(uuid, carbonPlayer);
|
||||
|
||||
return carbonPlayer;
|
||||
}
|
||||
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(uuid);
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(username);
|
||||
|
||||
if (player.isEmpty()) {
|
||||
return null;
|
||||
if (player.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.users.containsKey(player.get().uniqueId())) {
|
||||
return this.users.get(player.get().uniqueId());
|
||||
}
|
||||
|
||||
return new CarbonPlayerSponge(player.get().name(), text(player.get().name()),
|
||||
player.get().uniqueId());
|
||||
}
|
||||
|
||||
final CarbonPlayer carbonPlayer = new CarbonPlayerSponge(player.get().name(),
|
||||
text(player.get().name()), player.get().uniqueId());
|
||||
|
||||
this.users.put(uuid, carbonPlayer);
|
||||
|
||||
return carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CarbonPlayer carbonPlayer(final @NonNull String username) {
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(username);
|
||||
|
||||
if (player.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.users.containsKey(player.get().uniqueId())) {
|
||||
return this.users.get(player.get().uniqueId());
|
||||
}
|
||||
|
||||
return new CarbonPlayerSponge(player.get().name(), text(player.get().name()),
|
||||
player.get().uniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user