Skip to content

Commit

Permalink
feat(config): add name config to console
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Nov 15, 2021
1 parent f77794f commit c30afb1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/silthus/chat/SChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void reload() {

if (oldConfig.equals(pluginConfig)) return;

Console.console().setConfig(getPluginConfig().console());
getChannelRegistry().load(getPluginConfig());
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/net/silthus/chat/config/ConsoleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,28 @@

package net.silthus.chat.config;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.*;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.configuration.ConfigurationSection;

import static net.kyori.adventure.text.Component.text;

@Data
@Builder
@NoArgsConstructor
@Accessors(fluent = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ConsoleConfig {

@Builder.Default
private Component name = text("Console");
@Builder.Default
private String defaultChannel = "global";

ConsoleConfig(@NonNull ConfigurationSection config) {
this.name = MiniMessage.miniMessage().deserialize(config.getString("name", "Console"));
this.defaultChannel = config.getString("default_channel", defaultChannel);
}
}
13 changes: 8 additions & 5 deletions src/main/java/net/silthus/chat/identities/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@
package net.silthus.chat.identities;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import net.silthus.chat.ChatSource;
import net.silthus.chat.Constants;
import net.silthus.chat.Message;
import net.silthus.chat.SChat;
import net.silthus.chat.config.ConsoleConfig;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;

import java.util.Optional;

import static net.kyori.adventure.text.Component.text;

@Getter
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public final class Console extends AbstractChatTarget implements ChatSource, Listener {

Expand All @@ -53,12 +52,16 @@ public static Console init(@NonNull ConsoleConfig config) {
return instance;
}

private final ConsoleConfig config;
private ConsoleConfig config;

private Console(ConsoleConfig config) {
super(Constants.Targets.CONSOLE);
setConfig(config);
}

public void setConfig(ConsoleConfig config) {
this.config = config;
setDisplayName(text(Bukkit.getConsoleSender().getName()));
setDisplayName(config.name());
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defaults:
format: '<gold>[</gold><green><channel_name></green><gold>]</gold><reset><yellow><sender_vault_prefix><sender_name><sender_vault_suffix><gray>: <gray><message>'
# You can fine tune the behaviour of the console chat in this section.
console:
# Set the name players see when chatting from the console.
name: Console
# This is the channel the console will send chat messages to.
default_channel: global
# Define your list of channels here.
Expand Down Expand Up @@ -67,6 +69,7 @@ channels:
# The following custom mini message placeholders are available:
# - <channel_name>: The name of the channel (see above).
# - <sender_vault_prefix>: The Vault prefix of the sending player. Requires Vault to work. "Bleeding" color codes in prefix and suffix currently do not work.
# - <sender_world>: The world of the sending player.
# - <sender_name>: The name or nick of the sending player.
# - <sender_vault_suffix>: The Vault suffix of the sending player. Requires Vault to work. "Bleeding" color codes in prefix and suffix currently do not work.
# - <message>: The message that is being sent.
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defaults:
format: '<gold>[</gold><green><channel_name></green><gold>]</gold><reset><yellow><sender_vault_prefix><sender_name><sender_vault_suffix><gray>: <gray><message>'
# You can fine tune the behaviour of the console chat in this section.
console:
# Set the name players see when chatting from the console.
name: Console
# This is the channel the console will send chat messages to.
default_channel: global
# Define your list of channels here.
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/net/silthus/chat/SChatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
package net.silthus.chat;

import co.aikar.commands.BukkitCommandManager;
import net.kyori.adventure.text.Component;
import net.silthus.chat.config.ChannelConfig;
import net.silthus.chat.config.PluginConfig;
import net.silthus.chat.conversations.Channel;
import net.silthus.chat.identities.Console;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Optional;

import static net.kyori.adventure.text.Component.text;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -74,7 +75,7 @@ void onEnable_loadsChannelsFromConfig() {
c -> toText(c.getConfig().format().applyTo(Message.message(ChatSource.player(server.addPlayer()), "test").to(c).build()))
).contains(
"global",
Component.text("Global"),
text("Global"),
"&6[&aGlobal&6]&7[ADMIN]&ePlayer0[!]&7: test"
);
}
Expand All @@ -94,7 +95,6 @@ class Reload {

@BeforeEach
void setUp() {

plugin.setChannelRegistry(spy(plugin.getChannelRegistry()));
}

Expand Down Expand Up @@ -129,6 +129,16 @@ void reloadsChannels_ifConfigChanged() {
plugin.reload();
verify(plugin.getChannelRegistry()).load(any());
}

@Test
void reloadsConsoleConfig() {
loadTestConfig("reload-test.yml");
plugin.reload();

final Console console = Console.console();
assertThat(console.getDisplayName()).isEqualTo(text("MyConsole"));
assertThat(console.getConfig().defaultChannel()).isEqualTo("none");
}
}

}
25 changes: 24 additions & 1 deletion src/test/java/net/silthus/chat/identities/ConsoleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import be.seeseemelk.mockbukkit.command.ConsoleCommandSenderMock;
import net.silthus.chat.*;
import net.silthus.chat.config.ConsoleConfig;
import net.silthus.chat.conversations.Channel;
import org.bukkit.ChatColor;
import org.bukkit.event.server.ServerCommandEvent;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -107,7 +108,11 @@ void onChat_sendsMessageToDefaultTarget() {
.isEqualTo(text("Hi there!"));
assertThat(ChatColor.stripColor(((ConsoleCommandSenderMock) server.getConsoleSender()).nextMessage()))
.isNotNull()
.contains("[Global]CONSOLE: Hi there!");
.contains("[Global]Console: Hi there!");
assertThat(Channel.channel("global").getLastReceivedMessage())
.isNotNull()
.extracting(Message::getSource)
.isEqualTo(Console.console());
}

@Test
Expand All @@ -119,5 +124,23 @@ void onChat_doesNotSendCommandsAsMessage() {
assertThat(((ConsoleCommandSenderMock) server.getConsoleSender()).nextMessage())
.isNull();
}

@Test
void setConfig_changesTheName() {
assertThat(console.getDisplayName()).isEqualTo(text("Console"));

console.setConfig(ConsoleConfig.builder().name(text("Foobar")).build());
assertThat(console.getDisplayName()).isEqualTo(text("Foobar"));
}

@Test
void setConfig_changesDefaultChannel() {
console.setConfig(ConsoleConfig.builder().defaultChannel("none").build());
console.onConsoleChat(new ServerCommandEvent(server.getConsoleSender(), "Hi!"));

assertThat(console.getLastReceivedMessage()).isNull();
assertThat(ChatColor.stripColor(((ConsoleCommandSenderMock) server.getConsoleSender()).nextMessage()))
.isNull();
}
}
}
3 changes: 2 additions & 1 deletion src/test/resources/reload-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defaults:
range: 100
format: '<gold>[</gold><green><channel_name></green><gold>]</gold><reset><yellow><sender_vault_prefix><sender_name><sender_vault_suffix><gray>: <gray><message>'
console:
default_channel: global
default_channel: none
name: MyConsole
private_chats:
global: true
channels:
Expand Down

0 comments on commit c30afb1

Please sign in to comment.