diff --git a/common/src/main/java/net/draycia/carbon/common/config/PingSettings.java b/common/src/main/java/net/draycia/carbon/common/config/PingSettings.java new file mode 100644 index 00000000..615e531f --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/config/PingSettings.java @@ -0,0 +1,75 @@ +/* + * CarbonChat + * + * Copyright (c) 2021 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.config; + +import net.kyori.adventure.key.Key; +import net.kyori.adventure.sound.Sound; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Comment; + +@ConfigSerializable +public class PingSettings { + + @Comment("The color your name will be when another player mentions you.") + private TextColor highlightTextColor = NamedTextColor.YELLOW; + + private String prefix = "@"; + + private boolean playSound = false; + private Key name = Key.key("block.anvil.use"); + private Sound.Source source = Sound.Source.MASTER; + private float volume = 1.0f; // 0.0 -> infinity + private float pitch = 1.0f; // 0.0 -> 2.0 + + public TextColor highlightTextColor() { + return this.highlightTextColor; + } + + public boolean playSound() { + return this.playSound; + } + + public Key name() { + return this.name; + } + + public String prefix() { + return this.prefix; + } + + public Sound.Source source() { + return this.source; + } + + public float volume() { + return this.volume; + } + + public float pitch() { + return this.pitch; + } + + public Sound sound() { + return Sound.sound(this.name, this.source, this.volume, this.pitch); + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java index 2c5f7eb0..dec082a9 100644 --- a/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java +++ b/common/src/main/java/net/draycia/carbon/common/config/PrimaryConfig.java @@ -58,6 +58,9 @@ public class PrimaryConfig { """) private Map customPlaceholders = Map.of(); + @Comment("Various settings related to pinging players in channels.") + private PingSettings pingSettings = new PingSettings(); + public Locale defaultLocale() { return this.defaultLocale; } @@ -82,6 +85,10 @@ public class PrimaryConfig { return this.customPlaceholders; } + public PingSettings pings() { + return this.pingSettings; + } + public enum StorageType { JSON, MYSQL, diff --git a/common/src/main/java/net/draycia/carbon/common/listeners/PingHandler.java b/common/src/main/java/net/draycia/carbon/common/listeners/PingHandler.java new file mode 100644 index 00000000..0b147160 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/listeners/PingHandler.java @@ -0,0 +1,71 @@ +/* + * CarbonChat + * + * Copyright (c) 2021 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.listeners; + +import com.google.inject.Inject; +import java.util.regex.Pattern; +import net.draycia.carbon.api.CarbonChat; +import net.draycia.carbon.api.events.CarbonChatEvent; +import net.draycia.carbon.api.users.CarbonPlayer; +import net.draycia.carbon.api.util.KeyedRenderer; +import net.draycia.carbon.api.util.RenderedMessage; +import net.draycia.carbon.common.config.PrimaryConfig; +import net.kyori.adventure.audience.MessageType; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.TextReplacementConfig; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer; +import static net.kyori.adventure.key.Key.key; + +@DefaultQualifier(NonNull.class) +public class PingHandler { + + private final Key muteKey = key("carbon", "pings"); + private final KeyedRenderer renderer; + + @Inject + public PingHandler(final CarbonChat carbonChat, final PrimaryConfig primaryConfig) { + this.renderer = keyedRenderer(this.muteKey, (sender, recipient, message, originalMessage) -> { + if (!(recipient instanceof CarbonPlayer recipientPlayer)) { + return new RenderedMessage(message, MessageType.CHAT); + } + + final String prefix = primaryConfig.pings().prefix(); + + return new RenderedMessage(message.replaceText(TextReplacementConfig.builder() + .match(Pattern.compile(Pattern.quote(prefix + recipientPlayer.username()), Pattern.CASE_INSENSITIVE)) + .replacement(matchedText -> { + if (primaryConfig.pings().playSound()) { + recipient.playSound(primaryConfig.pings().sound()); + } + + return matchedText.color(primaryConfig.pings().highlightTextColor()); + }) + .build()), MessageType.CHAT); + }); + + carbonChat.eventHandler().subscribe(CarbonChatEvent.class, 1, false, event -> { + event.renderers().add(0, this.renderer); + }); + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/util/ListenerUtils.java b/common/src/main/java/net/draycia/carbon/common/util/ListenerUtils.java index 2b7f149f..9650617d 100644 --- a/common/src/main/java/net/draycia/carbon/common/util/ListenerUtils.java +++ b/common/src/main/java/net/draycia/carbon/common/util/ListenerUtils.java @@ -25,6 +25,7 @@ import net.draycia.carbon.common.listeners.DeafenHandler; import net.draycia.carbon.common.listeners.IgnoreHandler; import net.draycia.carbon.common.listeners.ItemLinkHandler; import net.draycia.carbon.common.listeners.MuteHandler; +import net.draycia.carbon.common.listeners.PingHandler; public final class ListenerUtils { @@ -33,7 +34,7 @@ public final class ListenerUtils { } public static final List> LISTENER_CLASSES = List.of(DeafenHandler.class, IgnoreHandler.class, - ItemLinkHandler.class, MuteHandler.class); + ItemLinkHandler.class, MuteHandler.class, PingHandler.class); public static void registerCommonListeners(final Injector injector) { for (final var listenerClass : LISTENER_CLASSES) {