Simplify Messenger implementation

This commit is contained in:
Kieran Wallbanks 2021-05-17 14:46:25 +01:00
parent 37f0f797fd
commit cdedcd89d6
3 changed files with 9 additions and 67 deletions

View File

@ -29,7 +29,9 @@ public class ChatMessageListener {
final String command = message.replaceFirst(cmdPrefix, "");
// check if we can receive commands
Messenger.receiveCommand(player, () -> COMMAND_MANAGER.execute(player, command));
if (Messenger.canReceiveCommand(player)) {
COMMAND_MANAGER.execute(player, command);
}
// Do not call chat event
return;

View File

@ -1,19 +1,15 @@
package net.minestom.server.message;
import java.util.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import net.minestom.server.utils.Action;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
/**
* Utility class to handle client chat settings.
*/
@ -50,20 +46,11 @@ public class Messenger {
* @param message the message
* @param position the position
* @param uuid the UUID of the sender, if any
* @return a set of players who received the message
*/
public static @NotNull Set<Player> sendMessage(@NotNull Iterable<? extends Player> players, @NotNull Component message,
@NotNull ChatPosition position, @Nullable UUID uuid) {
final Set<Player> sentTo = new HashSet<>();
for (Player player : players) {
if (getChatMessageType(player).accepts(position)) {
sentTo.add(player);
}
}
PacketUtils.sendGroupedPacket(sentTo, new ChatMessagePacket(message, position, uuid));
return sentTo;
public static void sendMessage(@NotNull Collection<Player> players, @NotNull Component message,
@NotNull ChatPosition position, @Nullable UUID uuid) {
PacketUtils.sendGroupedPacket(players, new ChatMessagePacket(message, position, uuid),
player -> getChatMessageType(player).accepts(position));
}
/**
@ -76,21 +63,6 @@ public class Messenger {
return getChatMessageType(player) == ChatMessageType.FULL;
}
/**
* Performs an action if the server can receive messages from a player.
* This method will send the rejection message automatically.
*
* @param player the player
* @param action the action
*/
public static void receiveMessage(@NotNull Player player, @NotNull Action action) {
if (canReceiveMessage(player)) {
action.act();
} else {
sendRejectionMessage(player);
}
}
/**
* Checks if the server should receive commands from a player, given their chat settings.
*
@ -101,21 +73,6 @@ public class Messenger {
return getChatMessageType(player) != ChatMessageType.NONE;
}
/**
* Performs an action if the server can receive commands from a player.
* This method will send the rejection message automatically.
*
* @param player the player
* @param action the action
*/
public static void receiveCommand(@NotNull Player player, @NotNull Action action) {
if (canReceiveCommand(player)) {
action.act();
} else {
sendRejectionMessage(player);
}
}
/**
* Sends a message to the player informing them we are rejecting their message or command.
*

View File

@ -1,17 +0,0 @@
package net.minestom.server.utils;
/**
* A functional interface to perform an action.
*/
@FunctionalInterface
public interface Action {
/**
* An empty action.
*/
Action EMPTY = () -> {};
/**
* Performs the action.
*/
void act();
}