Skip to content

Commit

Permalink
add markdown chat formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
TehBrian committed May 26, 2024
1 parent 3fbb364 commit 62de083
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/city/thefloating/helios/Helios.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import city.thefloating.helios.server.HeliosCommand;
import city.thefloating.helios.server.GameModeCommands;
import city.thefloating.helios.server.JoinQuitListener;
import city.thefloating.helios.server.MarkdownCommand;
import city.thefloating.helios.server.RulesCommand;
import city.thefloating.helios.server.ServerPingListener;
import city.thefloating.helios.server.VoteCommand;
Expand Down Expand Up @@ -205,6 +206,7 @@ private boolean initCommands() {
this.injector.getInstance(GameModeCommands.class).register(this.commandManager);
this.injector.getInstance(HatCommand.class).register(this.commandManager);
this.injector.getInstance(MilkCommand.class).register(this.commandManager);
this.injector.getInstance(MarkdownCommand.class).register(this.commandManager);
this.injector.getInstance(NextbotCommand.class).register(this.commandManager);
this.injector.getInstance(PackCommand.class).register(this.commandManager);
this.injector.getInstance(PianoCommand.class).register(this.commandManager);
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/city/thefloating/helios/server/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import city.thefloating.helios.Permission;
import city.thefloating.helios.config.EmotesConfig;
import city.thefloating.helios.config.LangConfig;
import city.thefloating.helios.soul.Charon;
import com.google.inject.Inject;
import io.papermc.paper.event.player.AsyncChatDecorateEvent;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Sound;
Expand All @@ -32,14 +34,17 @@ public final class ChatListener implements Listener {

private final EmotesConfig emotesConfig;
private final LangConfig langConfig;
private final Charon charon;

@Inject
public ChatListener(
final EmotesConfig emotesConfig,
final LangConfig langConfig
final LangConfig langConfig,
final Charon charon
) {
this.emotesConfig = emotesConfig;
this.langConfig = langConfig;
this.charon = charon;
}

@EventHandler
Expand All @@ -51,6 +56,7 @@ public void onDecorate(final AsyncChatDecorateEvent event) {
}

event.result(this.colorCodes(event.result(), source));
event.result(this.markdown(event.result(), source));
event.result(this.colorPingedPlayers(event.result(), source));
event.result(this.replaceEmotes(event.result()));
event.result(this.greentext(event.result())); // greentext last to overwrite all other colors.
Expand Down Expand Up @@ -84,6 +90,31 @@ private Component greentext(final Component component) {
return component;
}

private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*(.*)\\*\\*");
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*(.*?)\\*");
private static final Pattern UNDERLINED_PATTERN = Pattern.compile("__(.*)__");
private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~(.*)~~");

private Component markdown(final Component component, final Player source) {
if (!this.charon.grab(source).markdown()) {
return component;
}

return component
.replaceText(t -> t
.match(BOLD_PATTERN)
.replacement((m, b) -> Component.text(m.group(1)).decorate(TextDecoration.BOLD)))
.replaceText(t -> t
.match(ITALIC_PATTERN)
.replacement((m, b) -> Component.text(m.group(1)).decorate(TextDecoration.ITALIC)))
.replaceText(t -> t
.match(UNDERLINED_PATTERN)
.replacement((m, b) -> Component.text(m.group(1)).decorate(TextDecoration.UNDERLINED)))
.replaceText(t -> t
.match(STRIKETHROUGH_PATTERN)
.replacement((m, b) -> Component.text(m.group(1)).decorate(TextDecoration.STRIKETHROUGH)));
}

private Component colorPingedPlayers(final Component component, final Player source) {
Component result = component;

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/city/thefloating/helios/server/MarkdownCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package city.thefloating.helios.server;

import city.thefloating.helios.config.LangConfig;
import city.thefloating.helios.soul.Charon;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.paper.PaperCommandManager;
import com.google.inject.Inject;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.spongepowered.configurate.NodePath;

public class MarkdownCommand {

private final LangConfig langConfig;
private final Charon charon;

@Inject
public MarkdownCommand(
final LangConfig langConfig,
final Charon charon
) {
this.langConfig = langConfig;
this.charon = charon;
}

public void register(final PaperCommandManager<CommandSender> commandManager) {
final var main = commandManager.commandBuilder("markdown")
.meta(CommandMeta.DESCRIPTION, "Toggle markdown chat formatting.")
.senderType(Player.class)
.handler(c -> {
final Player sender = (Player) c.getSender();
if (this.charon.grab(sender).toggleMarkdown()) {
sender.sendMessage(this.langConfig.c(NodePath.path("markdown", "enabled")));
} else {
sender.sendMessage(this.langConfig.c(NodePath.path("markdown", "disabled")));
}
});

commandManager.command(main);
}

}
5 changes: 4 additions & 1 deletion src/main/java/city/thefloating/helios/soul/Charon.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public Soul grab(final UUID uuid) {
if (spirit.netherInfractions() != null) {
soul.netherInfractions(spirit.netherInfractions());
}
if (spirit.markdown() != null) {
soul.markdown(spirit.markdown());
}
}
return soul;
});
Expand All @@ -41,7 +44,7 @@ public Soul grab(final Player player) {

public void save() throws ConfigurateException {
for (final var soul : this.souls.values()) {
final var spirit = new Otzar.Data.Spirit(soul.netherInfractions());
final var spirit = new Otzar.Data.Spirit(soul.netherInfractions(), soul.markdown());
this.otzar.spirits().put(soul.getUuid(), spirit);
}
this.otzar.save();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/city/thefloating/helios/soul/Otzar.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected Class<Otzar.Data> getDataClass() {
public record Data(Map<UUID, Spirit> spirits) {

@ConfigSerializable
public record Spirit(Integer netherInfractions) {
public record Spirit(Integer netherInfractions,
Boolean markdown) {

}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/city/thefloating/helios/soul/Soul.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public final class Soul {

private boolean flyBypassEnabled = false;
private int netherInfractions = 0;
private boolean markdown = true;
private boolean elevatorMusicPlaying = false;

private final Piano piano = new Piano(false, Instrument.HARP);
Expand Down Expand Up @@ -43,6 +44,17 @@ public void netherInfractions(final int netherBlindnessCount) {
this.netherInfractions = netherBlindnessCount;
}

public boolean markdown() { return this.markdown; }

public void markdown(final boolean markdown) {
this.markdown = markdown;
}

public boolean toggleMarkdown() {
this.markdown(!this.markdown());
return this.markdown();
}

public boolean elevatorMusicPlaying() {
return this.elevatorMusicPlaying;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/lang.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
banner: "<dark_gray>-=-=-=-=-=-=-=-[<gray> -=[ <#55AAFF>The <#55FFFF>Floating <#55FFAA>City <gray>]=- <dark_gray>]-=-=-=-=-=-=-=-"
chat-format: "<gray><sender><dark_gray>:<reset> <message>"

markdown: {
enabled: "<gray>Markdown formatting has been <green>enabled</green>."
disabled: "<gray>Markdown formatting has been <red>disabled</red>."
}

motd: "<gray>»<white> Welcome back! You were last on <#99FFFF><last></#99FFFF> ago."
motd-new: "<gray>»<white> Welcome to the server, <light_purple><player></light_purple>! Before building, please read the <red>/rules</red>."

Expand Down

0 comments on commit 62de083

Please sign in to comment.