Skip to content

Commit

Permalink
Merge pull request #112 from danthedaniel/main
Browse files Browse the repository at this point in the history
Add functionality to announce player nicks on join
  • Loading branch information
Majekdor authored Aug 3, 2023
2 parents 5e056ff + dba4361 commit f3865b2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public class ConfigValues {

public Boolean TAB_NICKS;
public Boolean ANNOUNCE_NICKS_ON_JOIN;
public Integer MAX_LENGTH;
public Integer MIN_LENGTH;
public Boolean REQUIRE_ALPHANUMERIC;
Expand All @@ -68,6 +69,7 @@ public ConfigValues() {
*/
public void reload() {
TAB_NICKS = HexNicks.core().getConfig().getBoolean("tab-nicks", false);
ANNOUNCE_NICKS_ON_JOIN = HexNicks.core().getConfig().getBoolean("announce-nicks-on-join", false);
MAX_LENGTH = HexNicks.core().getConfig().getInt("max-length", 20);
MIN_LENGTH = HexNicks.core().getConfig().getInt("min-length", 3);
REQUIRE_ALPHANUMERIC = HexNicks.core().getConfig().getBoolean("require-alphanumeric", false);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public interface Messages {

Args0 NOT_ALLOWED = () -> MiscUtils.configString("messages.notAllowed", "<red>That nickname is not allowed!");

Args2<Player, Component> ANNOUNCE_NICK = (player, nickname) -> MiscUtils
.configStringPlaceholders("messages.joinAnnouncement", "<yellow>%player% has the nickname</yellow> %nick%", player)
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(player.getName()).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%nick%").replacement(nickname).build());

/**
* A message that has no arguments that need to be replaced.
*/
Expand All @@ -135,6 +140,10 @@ interface Args1<A0> {
default void send(CommandSender sender, A0 arg0) {
MiscUtils.sendMessage(sender, build(arg0));
}

default void announce(A0 arg0) {
MiscUtils.announceMessage(build(arg0));
}
}

/**
Expand All @@ -146,5 +155,9 @@ interface Args2<A0, A1> {
default void send(CommandSender sender, A0 arg0, A1 arg1) {
MiscUtils.sendMessage(sender, build(arg0, arg1));
}

default void announce(A0 arg0, A1 arg1) {
MiscUtils.announceMessage(build(arg0, arg1));
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/dev/majek/hexnicks/event/PlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ public void onPlayerJoin(PlayerJoinEvent event) {
HexNicks.storage().hasNick(player.getUniqueId()).whenCompleteAsync((aBoolean, throwable) -> {
if (aBoolean) {
HexNicks.logging().debug("Player " + player.getName() + " joined and has nickname, setting...");
HexNicks.storage().getNick(player.getUniqueId()).whenCompleteAsync((component, throwable1) ->
HexNicks.core().setNick(player, component)
);
HexNicks.storage().getNick(player.getUniqueId()).whenCompleteAsync((component, throwable1) -> {
if (HexNicks.config().ANNOUNCE_NICKS_ON_JOIN) {
Messages.ANNOUNCE_NICK.announce(player, component);
}
HexNicks.core().setNick(player, component);
});
} else {
HexNicks.logging().debug("Player " + player.getName() + " joined and has no nickname.");
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/dev/majek/hexnicks/util/MiscUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -235,4 +236,17 @@ public static void sendMessage(final @NotNull CommandSender recipient, final @No
recipient.sendMessage(message);
}
}

/**
* Announce a message to the server provided the message isn't empty.
*
* @param message the message to send
*/
public static void announceMessage(final @NotNull Component message) {
if (!PlainTextComponentSerializer.plainText().serialize(message).isEmpty()) {
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendMessage(message);
}
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# Whether nicknames should show in the tab list
tab-nicks: false

# Whether nicknames should be announced when players join
announce-nicks-on-join: false

# The maximum number of characters (excluding color codes) a nickname may be
max-length: 20

Expand Down Expand Up @@ -102,3 +105,4 @@ messages:
latestLog: "<green>View the latest log <click:open_url:'%link%'><aqua><u>here</u></aqua></click>."
working: "<gray>Working..."
notAllowed: "<red>That nickname is not allowed!"
joinAnnouncement: "<yellow>%player% has the nickname</yellow> %nick%"

0 comments on commit f3865b2

Please sign in to comment.