Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config options regarding join/leave messages. #129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/dev/majek/hexnicks/HexNicks.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dev.majek.hexnicks.api.HexNicksApi;
import dev.majek.hexnicks.command.*;
import dev.majek.hexnicks.config.ConfigValues;
import dev.majek.hexnicks.event.PlayerQuit;
import dev.majek.hexnicks.storage.*;
import dev.majek.hexnicks.event.PaperTabCompleteEvent;
import dev.majek.hexnicks.event.PlayerChat;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void onEnable() {
() -> String.valueOf(config.CHAT_FORMATTER)));

// Register events
this.registerEvents(new PlayerJoin(), new PaperTabCompleteEvent(), new PlayerChat());
this.registerEvents(new PlayerJoin(), new PaperTabCompleteEvent(), new PlayerChat(), new PlayerQuit());

// Check for updates - prompt to update if there is one
if (this.updateChecker.isBehindSpigot()) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class ConfigValues {
public Boolean REQUIRE_ALPHANUMERIC;
public Boolean CHAT_FORMATTER;
public String CHAT_FORMAT;
public Boolean FORMAT_JOIN;
public String JOIN_FORMAT;
public Boolean FORMAT_QUIT;
public String QUIT_FORMAT;
public Boolean LEGACY_COLORS;
public TextColor DEFAULT_NICK_COLOR;
public TextColor DEFAULT_USERNAME_COLOR;
Expand All @@ -75,6 +79,10 @@ public void reload() {
REQUIRE_ALPHANUMERIC = HexNicks.core().getConfig().getBoolean("require-alphanumeric", false);
CHAT_FORMATTER = HexNicks.core().getConfig().getBoolean("chat-formatter", false);
CHAT_FORMAT = HexNicks.core().getConfig().getString("chat-format", "{displayname}: {message}");
FORMAT_JOIN = HexNicks.core().getConfig().getBoolean("format-join", true);
JOIN_FORMAT = HexNicks.core().getConfig().getString("join-format", "<{displayname}> <yellow>connected!");
FORMAT_QUIT = HexNicks.core().getConfig().getBoolean("format-quit", true);
QUIT_FORMAT = HexNicks.core().getConfig().getString("quit-format", "<{displayname}> <yellow>disconnected!");
LEGACY_COLORS = HexNicks.core().getConfig().getBoolean("legacy-colors", false);
DEFAULT_NICK_COLOR = TextColor.fromHexString(HexNicks.core().getConfig().getString("default-nick-color", "#FFFFFF"));
DEFAULT_USERNAME_COLOR = TextColor.fromHexString(HexNicks.core().getConfig().getString("default-username-color", "#FFFFFF"));
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/dev/majek/hexnicks/event/PlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@
package dev.majek.hexnicks.event;

import dev.majek.hexnicks.HexNicks;
import dev.majek.hexnicks.api.HexNicksApi;
import dev.majek.hexnicks.config.Messages;
import dev.majek.hexnicks.message.MiniMessageWrapper;
import dev.majek.hexnicks.util.MiscUtils;
import net.kyori.adventure.text.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
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.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.awt.*;
import java.util.List;

/**
* <p>Handles the player join event.</p>
Expand All @@ -41,6 +55,7 @@ public class PlayerJoin implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) {
HexNicks.storage().updateNicks();
Player player = event.getPlayer();
boolean shouldFormat = HexNicks.config().FORMAT_JOIN;

// Set the joining player's nickname to their stored nickname if they have one
HexNicks.storage().hasNick(player.getUniqueId()).whenCompleteAsync((aBoolean, throwable) -> {
Expand All @@ -51,15 +66,51 @@ public void onPlayerJoin(PlayerJoinEvent event) {
Messages.ANNOUNCE_NICK.announce(player, component);
}
HexNicks.core().setNick(player, component);
if(shouldFormat) { JoinMessage(player, component); }
});
} else {
HexNicks.logging().debug("Player " + player.getName() + " joined and has no nickname.");
if(shouldFormat) { JoinMessage(player, Component.text(player.getName())); }
}
});

// Update prompt
if (event.getPlayer().isOp() && HexNicks.core().hasUpdate() && HexNicks.config().UPDATE_PROMPT) {
Messages.UPDATE.send(event.getPlayer());
}

Component name = HexNicks.core().getNickMap().get(player.getUniqueId());
if (name != null) {
name = Component.text(player.getName());
}

if(shouldFormat) { event.joinMessage(Component.text().content("").build()); }
}

private void JoinMessage (Player source, Component name) {
final MiniMessageWrapper miniMessageWrapper = MiniMessageWrapper.builder()
.advancedTransformations(source.hasPermission("hexnicks.chat.advanced"))
.gradients(source.hasPermission("hexnicks.color.gradient"))
.hexColors(source.hasPermission("hexnicks.color.hex"))
.legacyColors(HexNicks.config().LEGACY_COLORS)
.removeTextDecorations(MiscUtils.blockedDecorations(source))
.removeColors(MiscUtils.blockedColors(source))
.build();

Component text = miniMessageWrapper.mmParse(HexNicks.hooks().applyPlaceHolders(source, HexNicks.config().JOIN_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(name).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(HexNicks.hooks().vaultPrefix(source)).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(HexNicks.hooks().vaultSuffix(source)).build()
);

Bukkit.getServer().getOnlinePlayers().forEach(player -> player.sendMessage(text));
}
}
53 changes: 53 additions & 0 deletions src/main/java/dev/majek/hexnicks/event/PlayerQuit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.majek.hexnicks.event;

import dev.majek.hexnicks.HexNicks;
import dev.majek.hexnicks.config.Messages;
import dev.majek.hexnicks.message.MiniMessageWrapper;
import dev.majek.hexnicks.util.MiscUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
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.PlayerQuitEvent;

public class PlayerQuit implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerQuitEvent event) {
if(!HexNicks.config().FORMAT_QUIT) { return; }

Player player = event.getPlayer();
event.quitMessage(message(player, HexNicks.core().getDisplayName(player)));
}

private Component message(Player source, Component name)
{
final MiniMessageWrapper miniMessageWrapper = MiniMessageWrapper.builder()
.advancedTransformations(source.hasPermission("hexnicks.chat.advanced"))
.gradients(source.hasPermission("hexnicks.color.gradient"))
.hexColors(source.hasPermission("hexnicks.color.hex"))
.legacyColors(HexNicks.config().LEGACY_COLORS)
.removeTextDecorations(MiscUtils.blockedDecorations(source))
.removeColors(MiscUtils.blockedColors(source))
.build();

Component text = miniMessageWrapper.mmParse(HexNicks.hooks().applyPlaceHolders(source, HexNicks.config().QUIT_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(name).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(HexNicks.hooks().vaultPrefix(source)).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(HexNicks.hooks().vaultSuffix(source)).build()
);
return text;
}
}
16 changes: 16 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ chat-formatter: true
# May also include {prefix} and {suffix} placeholders. These items will be pulled from Vault if it is hooked.
chat-format: "<{displayname}> {message}"

# Whether the join message should be formatted.
# If this is false, the default join message will be used.
format-join: true

# The format of join messages. Must include {displayname} placeholder.
# May also include {prefix} and {suffix} placeholders. These items will be pulled from Vault if it is hooked.
join-format: "<{displayname}> <yellow>connected!"

# Whether the quit message should be formatted.
# If this is false, the default quit message will be used.
format-quit: true

# The format of quit messages. Must include {displayname} placeholder.
# May also include {prefix} and {suffix} placeholders. These items will be pulled from Vault if it is hooked.
quit-format: "<{displayname}> <yellow>disconnected!"

# Whether to support legacy nicknames (&a, &l, etc.)
legacy-colors: false

Expand Down