Skip to content

Commit 308f6ee

Browse files
committed
e
1 parent cc36f9f commit 308f6ee

5 files changed

Lines changed: 60 additions & 34 deletions

File tree

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/commands/messaging/channels/ChannelCommand.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ private void execute(Player player, CommandArguments args) {
3535
CustomChat.getPlayerChat().remove(player);
3636
Component msg = Config.replaceFormat(
3737
this.getMsg("chatChannelChange"),
38-
new Replacer("chat-name", "<yellow>Global")
38+
new Replacer("chat-name", channel.getName())
3939
);
4040
player.sendMessage(msg);
4141
return;
4242
}
43+
//TODO: make this message configurable
44+
if (CustomChat.getPlayerIgnoredChannels(player).contains(channel)) {
45+
player.sendMessage(Config.replaceFormat(
46+
"<red>Cannot switch channel to <channel> because you've toggled it off!",
47+
new Replacer("channel", channel.getName())
48+
));
49+
return;
50+
}
4351
CustomChat.getPlayerChat().put(player, channel);
4452
Component msg = Config.replaceFormat(Config.getCmdMsg("channel", "chatChannelChange"),
4553
new Replacer("chat-name", channel.getName()));
@@ -50,19 +58,22 @@ public static class ChannelArgument extends CustomArgument<CustomChat, String> {
5058
public ChannelArgument(String nodeName) {
5159
super(new StringArgument(nodeName), info -> {
5260
CustomChat channel = CustomChat.getChatMap().get(info.input());
53-
if (channel == null || !info.sender().hasPermission(channel.getPermission()))
54-
throw CustomArgumentException.fromMessageBuilder(new MessageBuilder("Unknown channel: ").appendArgInput());
61+
if (channel == null || !channel.checkPerm(info.sender()))
62+
throw CustomArgumentException.fromAdventureComponent(
63+
Config.replaceFormat(
64+
Config.getCmdMsg("channel", "chatNotFound"),
65+
new Replacer("chat-name", info.input())
66+
)
67+
);
5568
else return channel;
5669
});
5770
this.replaceSuggestions(
58-
ArgumentSuggestions.strings(info -> {
59-
List<String> chats = new ArrayList<>(CustomChat.getCustomChats().stream()
71+
ArgumentSuggestions.strings(info ->
72+
CustomChat.getCustomChats().stream()
6073
.filter(chat ->
61-
info.sender().hasPermission(chat.getPermission())
62-
).map(CustomChat::getKey).toList());
63-
chats.add("global");
64-
return chats.toArray(String[]::new);
65-
}
74+
chat.checkPerm(info.sender())
75+
).map(CustomChat::getKey).toArray(String[]::new)
76+
6677
));
6778
}
6879
@Override

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/commands/messaging/channels/ToggleChannelCommand.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
import java.util.Set;
1414

1515
public class ToggleChannelCommand extends PotatoCommand {
16+
1617
public ToggleChannelCommand() {
1718
super("togglechannel", NAMESPACE+".togglechannel", "togglechat");
1819
}
1920

20-
// Set<String> chats = new HashSet<>();
21-
// for (CustomChat chat : CustomChat.getCustomChats()) {
22-
// if (info.sender().hasPermission(chat.getPermission())) chats.add(chat.getKey());
23-
// }
24-
2521
private final Argument<CustomChat> channelArgument = new ChannelArgument("channel");
2622

2723
@Override
@@ -36,6 +32,22 @@ private void execute(Player player, CommandArguments args) {
3632
Set<CustomChat> ignoredChannels = CustomChat.getPlayerIgnoredChannels(player);
3733
CustomChat channel = args.getByArgument(channelArgument);
3834
Objects.requireNonNull(channel);
35+
36+
//TODO: make these messages configurable
37+
if (channel.getKey().equals("global")) {
38+
player.sendMessage(Config.replaceFormat(
39+
"<red>Cannot toggle <global> channel",
40+
new Replacer("global", channel.getName())
41+
));
42+
return;
43+
}
44+
if (channel.equals(CustomChat.getPlayerChat().get(player))) {
45+
player.sendMessage(Config.replaceFormat(
46+
"<gray>Toggled current channel, moved to <global>",
47+
new Replacer("global", CustomChat.getChatMap().get("global").getName())
48+
));
49+
CustomChat.getPlayerChat().remove(player);
50+
}
3951
boolean ignored = ignoredChannels.add(channel);
4052
if (!ignored) ignoredChannels.remove(channel);
4153
player.sendMessage(Config.replaceFormat(

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/objects/CustomChat.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public class CustomChat {
3131
private static final Set<String> chatCommandList = new HashSet<>();
3232
private final String key;
3333
private final String name;
34-
private final String permission;
34+
private final @Nullable String permission;
3535
private final @Nullable String command;
3636
private final @Nullable Integer cooldown;
3737
@Getter
3838
private final HashMap<CommandSender, Long> lastChatTimes = new HashMap<>();
3939

40-
public CustomChat(String key, String name, String permission, @Nullable String command, @Nullable Integer cooldown) {
40+
public CustomChat(String key, String name, @Nullable String permission, @Nullable String command, @Nullable Integer cooldown) {
4141
this.key = key;
4242
this.name = name;
4343
this.permission = permission;
@@ -62,13 +62,14 @@ public CustomChat(String key, String name, String permission, @Nullable String c
6262

6363
chatCommandList.add(command);
6464
var messageArg = new GreedyStringArgument("message");
65-
new CommandAPICommand(command)
66-
.withPermission(permission)
65+
var cmd = new CommandAPICommand(command)
6766
.withArguments(messageArg)
6867
.executes((sender, args) -> {
6968
String message = args.getByArgument(messageArg);
7069
this.sendMessage(sender,message);
71-
}).register("potatoessentialscustomchat");
70+
});
71+
if (permission!=null) cmd.withPermission(permission);
72+
cmd.register("potatoessentialschannel");
7273
}
7374
public void sendMessage(CommandSender sender, String message) {
7475
if (this.getCooldown()!=null) {
@@ -103,11 +104,14 @@ public void sendMessage(CommandSender sender, String message) {
103104
var recipients = Bukkit.getOnlinePlayers().stream()
104105
.filter(p -> {
105106
boolean ignoredByPlayer = getPlayerIgnoredChannels(p).contains(this);
106-
return p.hasPermission(this.getPermission()) && !ignoredByPlayer;
107+
return checkPerm(p) && !ignoredByPlayer;
107108
}).toList();
108109
recipients.forEach(p -> p.sendMessage(msg));
109110
Bukkit.getConsoleSender().sendMessage(msg);
110111
}
112+
public boolean checkPerm(CommandSender sender) {
113+
return this.getPermission() == null || sender.hasPermission(this.getPermission());
114+
}
111115
public static Set<CustomChat> getPlayerIgnoredChannels(Player p) {
112116
ignoredChannels.computeIfAbsent(p, p1 -> new HashSet<>());
113117
return ignoredChannels.get(p);

PotatoEssentials/src/main/java/com/mcdragonmasters/potatoessentials/utils/Config.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public static String reload() {
7878
CustomChat.clearAll();
7979
if (customChatsEnabled() && chatsSection != null) {
8080
for (String key : chatsSection.getKeys(false)) {
81-
if ("global".equals(key) || "all".equals(key)) {
82-
return "keys 'global' and 'all' are reserved for global chat.";
81+
if ("global".equals(key)) {
82+
return "key 'global' is reserved for global chat.";
8383
}
8484
String chatConfPrefix = "chats.customChats."+key+".";
8585
LOGGER.debug("Registering custom chat '%s'".formatted(key));
@@ -89,17 +89,18 @@ public static String reload() {
8989
getString(chatConfPrefix+"command"),
9090
getInt(chatConfPrefix+".cooldown"));
9191
}
92+
new CustomChat("global", "<yellow>Global</yellow>", null, null, null);
9293
}
9394

9495
motdEnabled = getBoolean("serverList.motdEnabled");
9596
hoverInfoEnabled = getBoolean("serverList.hoverInfoEnabled");
9697
List<String> motd = config.getStringList("serverList.motd");
9798
ServerListPingListener.motd = Utils.miniMessage(motd.getFirst()+"<reset><newline>"+motd.getLast());
9899
ServerListPingListener.hoverInfo.clear();
99-
config.getStringList("serverList.hoverInfo").forEach(s ->
100-
ServerListPingListener.hoverInfo.add(
101-
LegacyComponentSerializer.legacySection()
102-
.serialize(Utils.miniMessage(s))));
100+
ServerListPingListener.hoverInfo = config.getStringList("serverList.hoverInfo").stream()
101+
.map(s ->
102+
LegacyComponentSerializer.legacySection().serialize(Utils.miniMessage(s))
103+
).toList();
103104

104105
for (ServerLinks.ServerLink serverLink : serverLinks) {
105106
Bukkit.getServerLinks().removeLink(serverLink);
@@ -154,9 +155,6 @@ public static String messageSocialSpy() {
154155
public static String broadcastFormat() {
155156
return getString("commands.broadcast.message");
156157
}
157-
public static boolean commandEnabled(String s, FileConfiguration config) {
158-
return config.getBoolean("commands."+s+".enabled");
159-
}
160158
public static boolean emojisEnabled() {
161159
return getBoolean("chat.emojis-enabled");
162160
}

PotatoEssentials/src/main/resources/config.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ chats:
4949
format: "<chat-name> <dark_gray>»<white> <prefix><name><white>: <message>"
5050
customChats:
5151
staff:
52-
cooldown: 0
53-
name: "<gold>Staff Chat"
52+
name: "<gold>Staff Chat</gold>"
5453
permission: "potatoessentials.chats.examplestaff"
55-
# optional field
54+
# - optional fields
5655
command: "sc" # (needs server restart when first added)
56+
cooldown: 0
57+
# -
5758
#vip:
5859
# name: "<green>VIP Chat"
5960
# permission: "potatoessentials.chats.examplevip"
@@ -174,7 +175,7 @@ commands:
174175
channel:
175176
enabled: true
176177
chatChannelChange: "<yellow>Chat channel</yellow><gray> set to </gray><chat-name>"
177-
chatNotFound: "<red>Chat <chat-name> does not exist!"
178+
chatNotFound: "<red>Chat <chat-name> not found"
178179
togglechannel:
179180
enabled: true
180181
toggledOff: "<gray>Messages for channel '<yellow><channel></yellow>' have been <red>Disabled"

0 commit comments

Comments
 (0)