diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/section/PlaceholderConfigSection.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/section/PlaceholderConfigSection.java index 90ca272..981b395 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/section/PlaceholderConfigSection.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/section/PlaceholderConfigSection.java @@ -27,4 +27,11 @@ public interface PlaceholderConfigSection { @ConfDefault.DefaultString("[Cancelled] ") @ConfComments("The text used in placeholder.") String cancelled(); + + @AnnotationBasedSorter.Order(3) + @ConfDefault.DefaultString("x, y, z") + @ConfComments({ + "Format for the placeholder." + }) + String locationFormat(); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/ChatEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/ChatEventBuilder.java deleted file mode 100644 index cd4e920..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/ChatEventBuilder.java +++ /dev/null @@ -1,27 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; - -public final class ChatEventBuilder extends EventBuilder { - public ChatEventBuilder(WebhookLogger webhookLogger) { - super(webhookLogger, webhookLogger.eventsConfig().chat().format()); - } - - @Override - public ChatEventBuilder audience(Audience audience) { - return (ChatEventBuilder) super.audience(audience); - } - - @Override - public ChatEventBuilder cancelled(boolean cancelled) { - return (ChatEventBuilder) super.cancelled(cancelled); - } - - public ChatEventBuilder message(Component message) { - resolvers.add(Placeholder.component("message", message)); - return this; - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/CommandEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/CommandEventBuilder.java deleted file mode 100644 index 1062ee4..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/CommandEventBuilder.java +++ /dev/null @@ -1,29 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; - -public final class CommandEventBuilder extends EventBuilder { - public CommandEventBuilder(WebhookLogger webhookLogger) { - super(webhookLogger, webhookLogger.eventsConfig().command().format()); - } - - @Override - public CommandEventBuilder cancelled(boolean cancelled) { - return (CommandEventBuilder) super.cancelled(cancelled); - } - - @Override - public CommandEventBuilder audience(Audience audience) { - return (CommandEventBuilder) super.audience(audience); - } - - public CommandEventBuilder command(String command) { - if (!command.startsWith("/")) { - command = "/" + command; - } - resolvers.add(Placeholder.unparsed("command", command)); - return this; - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java deleted file mode 100644 index dd27420..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java +++ /dev/null @@ -1,44 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; -import org.jetbrains.annotations.Nullable; - -public final class DeathEventBuilder extends EventBuilder { - private final PlainTextComponentSerializer serializer = PlainTextComponentSerializer.plainText(); - - public DeathEventBuilder(WebhookLogger webhookLogger) { - super(webhookLogger, webhookLogger.eventsConfig().death().format()); - } - - @Override - public DeathEventBuilder cancelled(boolean cancelled) { - return (DeathEventBuilder) super.cancelled(cancelled); - } - - @Override - public DeathEventBuilder audience(Audience audience) { - return (DeathEventBuilder) super.audience(audience); - } - - public DeathEventBuilder location(int x, int y, int z) { - String locationString = String.format("x%s, y%s, z%s", x, y, z); - resolvers.add(Placeholder.unparsed("location", locationString)); - return this; - } - - public DeathEventBuilder message(@Nullable Component message) { - String messageAsString = ""; - - if (message != null) { - messageAsString = serializer.serialize(message); - } - - resolvers.add(Placeholder.unparsed("message", messageAsString)); - - return this; - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/EventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/EventBuilder.java deleted file mode 100644 index 8277689..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/EventBuilder.java +++ /dev/null @@ -1,69 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.config.section.PlaceholderConfigSection; -import fi.fabianadrian.webhooklogger.common.dependency.Dependency; -import io.github.miniplaceholders.api.MiniPlaceholders; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.identity.Identity; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; - -import java.time.Instant; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public abstract class EventBuilder { - protected final WebhookLogger webhookLogger; - protected final PlaceholderConfigSection placeholderConfig; - protected List resolvers = new ArrayList<>(); - protected String format; - - public EventBuilder(WebhookLogger webhookLogger, String format) { - this.webhookLogger = webhookLogger; - this.format = format; - - placeholderConfig = webhookLogger.mainConfig().placeholders(); - - DateTimeFormatter formatter = DateTimeFormatter - .ofPattern(placeholderConfig.timestampFormat()) - .withZone(placeholderConfig.timestampTimezone()); - resolvers.add(Placeholder.unparsed("timestamp", formatter.format(Instant.now()))); - } - - protected EventBuilder audience(Audience audience) { - String name = audience.getOrDefault(Identity.NAME, "unknown"); - Component displayName = audience.getOrDefault(Identity.DISPLAY_NAME, Component.text(name)); - - UUID uuid = audience.getOrDefault(Identity.UUID, null); - String uuidAsString = uuid == null ? "unknown" : uuid.toString(); - - resolvers.add(Placeholder.unparsed("audience_name", name)); - resolvers.add(Placeholder.component("audience_display_name", displayName)); - resolvers.add(Placeholder.unparsed("audience_uuid", uuidAsString)); - - if (webhookLogger.dependencyManager().isPresent(Dependency.MINI_PLACEHOLDERS)) { - resolvers.add(MiniPlaceholders.getAudienceGlobalPlaceholders(audience)); - } - - return this; - } - - protected EventBuilder cancelled(boolean cancelled) { - String cancelledString = cancelled ? placeholderConfig.cancelled() : ""; - resolvers.add(Placeholder.unparsed("cancelled", cancelledString)); - return this; - } - - public Component component() { - TagResolver resolver = TagResolver.builder().resolvers(resolvers).build(); - return MiniMessage.miniMessage().deserialize( - format, - resolver - ); - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/JoinEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/JoinEventBuilder.java deleted file mode 100644 index 26b6a77..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/JoinEventBuilder.java +++ /dev/null @@ -1,51 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; -import org.jetbrains.annotations.Nullable; - -import java.net.InetSocketAddress; - -public final class JoinEventBuilder extends EventBuilder { - private final PlainTextComponentSerializer serializer = PlainTextComponentSerializer.plainText(); - - public JoinEventBuilder(WebhookLogger webhookLogger) { - super(webhookLogger, webhookLogger.eventsConfig().join().format()); - } - - @Override - public JoinEventBuilder cancelled(boolean cancelled) { - return (JoinEventBuilder) super.cancelled(cancelled); - } - - @Override - public JoinEventBuilder audience(Audience audience) { - return (JoinEventBuilder) super.audience(audience); - } - - public JoinEventBuilder location(int x, int y, int z) { - String locationString = String.format("x%s, y%s, z%s", x, y, z); - resolvers.add(Placeholder.unparsed("location", locationString)); - return this; - } - - public JoinEventBuilder message(@Nullable Component message) { - String messageAsString = ""; - - if (message != null) { - messageAsString = serializer.serialize(message); - } - - resolvers.add(Placeholder.unparsed("message", messageAsString)); - - return this; - } - - public JoinEventBuilder address(InetSocketAddress address) { - resolvers.add(Placeholder.unparsed("address", String.valueOf(address))); - return this; - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/PlaceholderFactory.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/PlaceholderFactory.java new file mode 100644 index 0000000..3a79b48 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/PlaceholderFactory.java @@ -0,0 +1,102 @@ +package fi.fabianadrian.webhooklogger.common.event; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.config.section.PlaceholderConfigSection; +import fi.fabianadrian.webhooklogger.common.dependency.Dependency; +import fi.fabianadrian.webhooklogger.common.platform.Player; +import io.github.miniplaceholders.api.MiniPlaceholders; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; + +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public final class PlaceholderFactory { + private final WebhookLogger webhookLogger; + private final PlaceholderConfigSection config; + private final PlainTextComponentSerializer plainTextSerializer = PlainTextComponentSerializer.plainText(); + private final MiniMessage miniMessage = MiniMessage.miniMessage(); + + public PlaceholderFactory(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; + this.config = webhookLogger.mainConfig().placeholders(); + } + + public TagResolver timestamp() { + DateTimeFormatter formatter = DateTimeFormatter + .ofPattern(config.timestampFormat()) + .withZone(config.timestampTimezone()); + return Placeholder.unparsed("timestamp", formatter.format(Instant.now())); + } + + public TagResolver cancelled(boolean cancelled) { + String cancelledString = cancelled ? config.cancelled() : ""; + return Placeholder.unparsed("cancelled", cancelledString); + } + + public TagResolver player(Player player) { + List resolvers = new ArrayList<>(); + + resolvers.add(audience(player)); + + String address = String.valueOf(player.address()); + resolvers.add(Placeholder.unparsed("address", address)); + + Component location = miniMessage.deserialize( + config.locationFormat(), + Placeholder.unparsed("x", String.valueOf(player.location().x())), + Placeholder.unparsed("y", String.valueOf(player.location().y())), + Placeholder.unparsed("z", String.valueOf(player.location().z())) + ); + resolvers.add(Placeholder.component("location", location)); + + return TagResolver.builder().resolvers(resolvers).build(); + } + + public TagResolver audience(Audience audience) { + List resolvers = new ArrayList<>(); + + String name = audience.getOrDefault(Identity.NAME, "unknown"); + resolvers.add(Placeholder.unparsed("name", name)); + + Component displayName = audience.getOrDefault(Identity.DISPLAY_NAME, Component.text(name)); + resolvers.add(Placeholder.component("display_name", displayName)); + + String uuid = audience.get(Identity.UUID).map(UUID::toString).orElse("unknown"); + resolvers.add(Placeholder.unparsed("uuid", uuid)); + + if (webhookLogger.dependencyManager().isPresent(Dependency.MINI_PLACEHOLDERS)) { + resolvers.add(MiniPlaceholders.getAudienceGlobalPlaceholders(audience)); + } + + return TagResolver.builder().resolvers(resolvers).build(); + } + + public TagResolver message(Component message) { + if (message == null) { + return Placeholder.unparsed("placeholder", ""); + } + + if (message instanceof TranslatableComponent) { + message = Component.text(plainTextSerializer.serialize(message)); + } + + return Placeholder.component("message", message); + } + + public TagResolver command(String command) { + if (!command.startsWith("/")) { + command = "/" + command; + } + return Placeholder.unparsed("command", command); + } +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/QuitEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/QuitEventBuilder.java deleted file mode 100644 index 487324f..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/QuitEventBuilder.java +++ /dev/null @@ -1,45 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.event; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; -import org.jetbrains.annotations.Nullable; - - -public final class QuitEventBuilder extends EventBuilder { - private final PlainTextComponentSerializer serializer = PlainTextComponentSerializer.plainText(); - - public QuitEventBuilder(WebhookLogger webhookLogger) { - super(webhookLogger, webhookLogger.eventsConfig().quit().format()); - } - - @Override - public QuitEventBuilder cancelled(boolean cancelled) { - return (QuitEventBuilder) super.cancelled(cancelled); - } - - @Override - public QuitEventBuilder audience(Audience audience) { - return (QuitEventBuilder) super.audience(audience); - } - - public QuitEventBuilder location(int x, int y, int z) { - String locationString = String.format("x%s, y%s, z%s", x, y, z); - resolvers.add(Placeholder.unparsed("location", locationString)); - return this; - } - - public QuitEventBuilder message(@Nullable Component message) { - String messageAsString = ""; - - if (message != null) { - messageAsString = serializer.serialize(message); - } - - resolvers.add(Placeholder.unparsed("message", messageAsString)); - - return this; - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java index 3d64dd5..0a3a102 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java @@ -2,8 +2,11 @@ import dev.vankka.mcdiscordreserializer.discord.DiscordSerializer; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; +import fi.fabianadrian.webhooklogger.common.event.PlaceholderFactory; import fi.fabianadrian.webhooklogger.common.webhook.WebhookClient; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import java.util.ArrayList; import java.util.List; @@ -11,13 +14,15 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public abstract class AbstractListener { +public abstract class AbstractListener { protected final WebhookLogger webhookLogger; + protected final PlaceholderFactory placeholderFactory; private final Map replacements; List clients = new ArrayList<>(); public AbstractListener(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; + this.placeholderFactory = new PlaceholderFactory(webhookLogger); replacements = webhookLogger.mainConfig().textReplacements(); } @@ -25,14 +30,20 @@ public void addWebhook(WebhookClient client) { clients.add(client); } - protected void queue(B builder) { - String discordSerialized = DiscordSerializer.INSTANCE.serialize(builder.component()); + //TODO Fix ugly + protected void queue(String format, TagResolver.Builder builder) { + TagResolver resolver = builder + .resolver(placeholderFactory.timestamp()) + .build(); + + Component component = MiniMessage.miniMessage().deserialize(format, resolver); + + String discordSerialized = DiscordSerializer.INSTANCE.serialize(component); for (Map.Entry entry : replacements.entrySet()) { Matcher matcher = entry.getKey().matcher(discordSerialized); discordSerialized = matcher.replaceAll(entry.getValue()); } - //TODO Fix ugly String finalDiscordSerialized = discordSerialized; clients.forEach(client -> client.queue(finalDiscordSerialized)); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java index 4c12aa1..bef5dde 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.common.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; public abstract class ListenerFactory { @@ -11,5 +10,5 @@ public ListenerFactory(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; } - protected abstract AbstractListener create(EventType eventType); + protected abstract AbstractListener create(EventType eventType); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java index 124f018..cddac07 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.common.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; import fi.fabianadrian.webhooklogger.common.webhook.WebhookClient; @@ -11,7 +10,7 @@ public abstract class ListenerManager { protected final WebhookLogger webhookLogger; - protected Map> registry = new HashMap<>(); + protected Map registry = new HashMap<>(); public ListenerManager(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Location.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Location.java new file mode 100644 index 0000000..eb2f784 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Location.java @@ -0,0 +1,4 @@ +package fi.fabianadrian.webhooklogger.common.platform; + +public record Location(int x, int y, int z) { +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Player.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Player.java new file mode 100644 index 0000000..44defed --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Player.java @@ -0,0 +1,10 @@ +package fi.fabianadrian.webhooklogger.common.platform; + +import net.kyori.adventure.audience.ForwardingAudience; + +import java.net.InetSocketAddress; + +public interface Player extends ForwardingAudience.Single { + InetSocketAddress address(); + Location location(); +} diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java index 80a21cd..ab23211 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.paper.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; @@ -13,7 +12,7 @@ public PaperListenerFactory(WebhookLogger webhookLogger) { } @Override - public AbstractListener create(EventType type) { + public AbstractListener create(EventType type) { switch (type) { case CHAT -> { return new ChatListener(webhookLogger); diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java index 881d782..d9b7ac9 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java @@ -2,13 +2,14 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.ChatEventConfig; -import fi.fabianadrian.webhooklogger.common.event.ChatEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; +import fi.fabianadrian.webhooklogger.paper.platform.PaperPlayer; import io.papermc.paper.event.player.AsyncChatEvent; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -public final class ChatListener extends AbstractListener implements Listener { +public final class ChatListener extends AbstractListener implements Listener { public ChatListener(WebhookLogger webhookLogger) { super(webhookLogger); } @@ -21,10 +22,13 @@ public void onChat(AsyncChatEvent event) { return; } - ChatEventBuilder builder = new ChatEventBuilder(webhookLogger) - .audience(event.getPlayer()) - .cancelled(event.isCancelled()) - .message(event.message()); - queue(builder); + PaperPlayer player = new PaperPlayer(event.getPlayer()); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.player(player), + placeholderFactory.cancelled(event.isCancelled()), + placeholderFactory.message(event.message()) + ); + + queue(config.format(), builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java index f80cd0b..84d8972 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java @@ -2,15 +2,16 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.CommandEventConfig; -import fi.fabianadrian.webhooklogger.common.event.CommandEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; +import fi.fabianadrian.webhooklogger.paper.platform.PaperPlayer; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; -public final class CommandListener extends AbstractListener implements Listener { +public final class CommandListener extends AbstractListener implements Listener { public CommandListener(WebhookLogger webhookLogger) { super(webhookLogger); } @@ -30,11 +31,13 @@ public void onServerCommand(ServerCommandEvent event) { return; } - CommandEventBuilder builder = new CommandEventBuilder(webhookLogger) - .cancelled(event.isCancelled()) - .audience(event.getSender()) - .command(event.getCommand()); - queue(builder); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.audience(event.getSender()), + placeholderFactory.cancelled(event.isCancelled()), + placeholderFactory.command(event.getCommand()) + ); + + queue(config.format(), builder); } @EventHandler @@ -44,10 +47,13 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) { return; } - CommandEventBuilder builder = new CommandEventBuilder(webhookLogger) - .cancelled(event.isCancelled()) - .audience(event.getPlayer()) - .command(event.getMessage()); - queue(builder); + PaperPlayer player = new PaperPlayer(event.getPlayer()); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.player(player), + placeholderFactory.cancelled(event.isCancelled()), + placeholderFactory.command(event.getMessage()) + ); + + queue(config.format(), builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java index 6964df9..5e7c298 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java @@ -2,14 +2,14 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.DeathEventConfig; -import fi.fabianadrian.webhooklogger.common.event.DeathEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; -import org.bukkit.Location; +import fi.fabianadrian.webhooklogger.paper.platform.PaperPlayer; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; -public final class DeathListener extends AbstractListener implements Listener { +public final class DeathListener extends AbstractListener implements Listener { public DeathListener(WebhookLogger webhookLogger) { super(webhookLogger); } @@ -22,13 +22,13 @@ public void onDeath(PlayerDeathEvent event) { return; } - Location loc = event.getEntity().getLocation(); + PaperPlayer player = new PaperPlayer(event.getEntity()); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.player(player), + placeholderFactory.cancelled(event.isCancelled()), + placeholderFactory.message(event.deathMessage()) + ); - DeathEventBuilder builder = new DeathEventBuilder(webhookLogger) - .audience(event.getEntity()) - .cancelled(event.isCancelled()) - .message(event.deathMessage()) - .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); - queue(builder); + queue(config.format(), builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinListener.java index f2a1d55..60694c5 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinListener.java @@ -1,26 +1,29 @@ package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.JoinEventBuilder; +import fi.fabianadrian.webhooklogger.common.config.event.JoinEventConfig; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; -import org.bukkit.Location; +import fi.fabianadrian.webhooklogger.paper.platform.PaperPlayer; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -public final class JoinListener extends AbstractListener implements Listener { +public final class JoinListener extends AbstractListener implements Listener { public JoinListener(WebhookLogger webhookLogger) { super(webhookLogger); } @EventHandler public void onJoin(PlayerJoinEvent event) { - Location loc = event.getPlayer().getLocation(); - JoinEventBuilder builder = new JoinEventBuilder(webhookLogger) - .audience(event.getPlayer()) - .message(event.joinMessage()) - .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()) - .address(event.getPlayer().getAddress()); - queue(builder); + JoinEventConfig config = webhookLogger.eventsConfig().join(); + + PaperPlayer player = new PaperPlayer(event.getPlayer()); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.player(player), + placeholderFactory.message(event.joinMessage()) + ); + + queue(config.format(), builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/QuitListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/QuitListener.java index 5158243..ae8d64b 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/QuitListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/QuitListener.java @@ -1,26 +1,29 @@ package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.QuitEventBuilder; +import fi.fabianadrian.webhooklogger.common.config.event.QuitEventConfig; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; -import org.bukkit.Location; +import fi.fabianadrian.webhooklogger.paper.platform.PaperPlayer; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; -public final class QuitListener extends AbstractListener implements Listener { +public final class QuitListener extends AbstractListener implements Listener { public QuitListener(WebhookLogger webhookLogger) { super(webhookLogger); } @EventHandler public void onQuit(PlayerQuitEvent event) { - Location loc = event.getPlayer().getLocation(); + QuitEventConfig config = webhookLogger.eventsConfig().quit(); - QuitEventBuilder builder = new QuitEventBuilder(webhookLogger) - .audience(event.getPlayer()) - .message(event.quitMessage()) - .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); - queue(builder); + PaperPlayer player = new PaperPlayer(event.getPlayer()); + TagResolver.Builder builder = TagResolver.builder().resolvers( + placeholderFactory.player(player), + placeholderFactory.message(event.quitMessage()) + ); + + queue(config.format(), builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/platform/PaperPlayer.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/platform/PaperPlayer.java new file mode 100644 index 0000000..2c6a3c5 --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/platform/PaperPlayer.java @@ -0,0 +1,36 @@ +package fi.fabianadrian.webhooklogger.paper.platform; + +import fi.fabianadrian.webhooklogger.common.platform.Location; +import fi.fabianadrian.webhooklogger.common.platform.Player; +import net.kyori.adventure.audience.Audience; +import org.jetbrains.annotations.NotNull; + +import java.net.InetSocketAddress; + +public final class PaperPlayer implements Player { + private final org.bukkit.entity.Player player; + + public PaperPlayer(org.bukkit.entity.Player player) { + this.player = player; + } + + @Override + public InetSocketAddress address() { + return player.getAddress(); + } + + @Override + public Location location() { + org.bukkit.Location location = player.getLocation(); + return new Location( + location.getBlockX(), + location.getBlockY(), + location.getBlockX() + ); + } + + @Override + public @NotNull Audience audience() { + return player; + } +} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java index 3230877..8e9ae6a 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java @@ -11,7 +11,7 @@ public SpongeListenerFactory(WebhookLogger webhookLogger) { super(webhookLogger); } - protected AbstractListener create(EventType type) { + protected AbstractListener create(EventType type) { switch (type) { case CHAT -> { return new ChatListener(webhookLogger); diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java index ceb9a77..c2b976e 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java @@ -2,7 +2,6 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.ChatEventConfig; -import fi.fabianadrian.webhooklogger.common.event.ChatEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.event.Listener; diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java index 20e7217..3fb572f 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java @@ -2,7 +2,6 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.CommandEventConfig; -import fi.fabianadrian.webhooklogger.common.event.CommandEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; @@ -12,7 +11,7 @@ import java.util.Optional; -public final class CommandListener extends AbstractListener { +public final class CommandListener extends AbstractListener { public CommandListener(WebhookLogger webhookLogger) { super(webhookLogger); } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java index eeb7e27..aa89231 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java @@ -2,7 +2,6 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.DeathEventConfig; -import fi.fabianadrian.webhooklogger.common.event.DeathEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinListener.java index 88ba7aa..3482668 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinListener.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.JoinEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.network.ServerSideConnectionEvent; diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/QuitListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/QuitListener.java index 37cd9e2..783edab 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/QuitListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/QuitListener.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.QuitEventBuilder; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.network.ServerSideConnectionEvent;