diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8cb6717 --- /dev/null +++ b/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + me.youhavetrouble + JustChat + 1.0 + jar + + JustChat + + Just a chat plugin. + + 17 + UTF-8 + + https://youhavetrouble.me + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + purpur + https://repo.purpurmc.org/snapshots + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + + + + + + org.purpurmc.purpur + purpur-api + 1.19.2-R0.1-SNAPSHOT + provided + + + me.clip + placeholderapi + 2.11.2 + provided + + + diff --git a/src/main/java/me/youhavetrouble/justchat/JustChat.java b/src/main/java/me/youhavetrouble/justchat/JustChat.java new file mode 100644 index 0000000..317868c --- /dev/null +++ b/src/main/java/me/youhavetrouble/justchat/JustChat.java @@ -0,0 +1,32 @@ +package me.youhavetrouble.justchat; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +public final class JustChat extends JavaPlugin { + + private static String chatFormat; + + private static final MiniMessage miniMessage = MiniMessage.miniMessage(); + + @Override + public void onEnable() { + reloadPluginConfig(); + getServer().getPluginManager().registerEvents(new JustChatListener(), this); + } + + public void reloadPluginConfig() { + saveDefaultConfig(); + FileConfiguration config = getConfig(); + chatFormat = config.getString("format", "<%player_displayname%> %message%"); + } + + public static String getChatFormat() { + return chatFormat; + } + + public static MiniMessage getMiniMessage() { + return miniMessage; + } +} diff --git a/src/main/java/me/youhavetrouble/justchat/JustChatListener.java b/src/main/java/me/youhavetrouble/justchat/JustChatListener.java new file mode 100644 index 0000000..fd7fa60 --- /dev/null +++ b/src/main/java/me/youhavetrouble/justchat/JustChatListener.java @@ -0,0 +1,88 @@ +package me.youhavetrouble.justchat; + +import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent; +import io.papermc.paper.event.player.AsyncChatDecorateEvent; +import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.AsyncPlayerChatPreviewEvent; +import org.bukkit.permissions.Permission; + + +import java.util.HashMap; + +public class JustChatListener implements Listener { + + private final HashMap formattingPerms = new HashMap<>(); + private final PlainTextComponentSerializer plainTextComponentSerializer = PlainTextComponentSerializer.plainText(); + + public JustChatListener() { + formattingPerms.put(new Permission("justchat.color"), StandardTags.color()); + formattingPerms.put(new Permission("justchat.bold"), StandardTags.decorations(TextDecoration.BOLD)); + formattingPerms.put(new Permission("justchat.italic"), StandardTags.decorations(TextDecoration.ITALIC)); + formattingPerms.put(new Permission("justchat.underlined"), StandardTags.decorations(TextDecoration.UNDERLINED)); + formattingPerms.put(new Permission("justchat.strikethrough"), StandardTags.decorations(TextDecoration.STRIKETHROUGH)); + formattingPerms.put(new Permission("justchat.obfuscated"), StandardTags.decorations(TextDecoration.OBFUSCATED)); + formattingPerms.put(new Permission("justchat.gradient"), StandardTags.gradient()); + formattingPerms.put(new Permission("justchat.font"), StandardTags.font()); + formattingPerms.put(new Permission("justchat.rainbow"), StandardTags.rainbow()); + formattingPerms.put(new Permission("justchat.hover"), StandardTags.hoverEvent()); + formattingPerms.put(new Permission("justchat.click"), StandardTags.clickEvent()); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onjustChatDecorate(AsyncChatDecorateEvent event) { + + if (event.player() == null) return; + + String format = JustChat.getChatFormat(); + + format = PlaceholderAPI.setPlaceholders(event.player(), format); + Component formatComponent = JustChat.getMiniMessage().deserialize(format); + + Component message = parseMessageContent(event.player(), plainTextComponentSerializer.serialize(event.originalMessage())); + + event.result(formatComponent.replaceText(TextReplacementConfig.builder().match("%message%").replacement(message).build())); + } + + @EventHandler + public void onAsyncChatPreviewEvent(AsyncPlayerChatPreviewEvent event) { + event.setFormat("%2$s"); + } + + @EventHandler + public void onAsyncChatEvent(AsyncPlayerChatEvent event) { + event.setFormat("%2$s"); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onjustChatCommandDecorate(AsyncChatCommandDecorateEvent event) { + if (event.player() == null) return; + event.result(parseMessageContent(event.player(), plainTextComponentSerializer.serialize(event.originalMessage()))); + } + + private Component parseMessageContent(Player player, String rawMessage) { + TagResolver.Builder tagResolver = TagResolver.builder(); + + formattingPerms.forEach((perm, resolver) -> { + if (player.hasPermission(perm)) { + tagResolver.resolver(resolver); + } + }); + + MiniMessage messageParser = MiniMessage.builder().tags(tagResolver.build()).build(); + return messageParser.deserialize(rawMessage); + + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..f67ee37 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1 @@ +format: "<%player_displayname%> %message%" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..3f3ef9b --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,60 @@ +name: '${project.name}' +version: '${project.version}' +main: me.youhavetrouble.justchat.JustChat +api-version: 1.19 +depend: [ PlaceholderAPI ] +authors: [ YouHaveTrouble ] +description: '${project.description}' +website: https://youhavetrouble.me +permissions: + justchat.color: + description: "Allows for usage of basic color tags" + default: op + justchat.bold: + description: "Allows for usage of " + default: op + justchat.italic: + description: "Allows for usage of tag" + default: op + justchat.underlined: + description: "Allows for usage of tag" + default: op + justchat.strikethrough: + description: "Allows for usage of tag" + default: op + justchat.obfuscated: + description: "Allows for usage of tag" + default: op + justchat.gradient: + description: "Allows for usage of tag" + default: op + justchat.font: + description: "Allows for usage of tag" + default: op + justchat.rainbow: + description: "Allows for usage of tag" + default: op + justchat.hover: + description: "Allows for usage of tag" + default: op + justchat.click: + description: "Allows for usage of tag" + default: op + justchat.format: + description: "Allows for usage of , , and tags" + children: + justchat.bold: true + justchat.italic: true + justchat.underlined: true + justchat.strikethrough: true + justchat.colors: + description: "Allows for usage of basic color tags, and tags" + children: + justchat.color: true + justchat.gradient: true + justchat.rainbow: true + justchat.actions: + description: "Allows for usage of and tags" + children: + justchat.click: true + justchat.hover: true