Avoid NPEs when obtaining chat message type from player

This commit is contained in:
Kieran Wallbanks 2021-05-06 16:12:46 +01:00
parent a9d2f4e8ca
commit 2404f19fe2
3 changed files with 20 additions and 7 deletions

View File

@ -2641,7 +2641,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* Gets the player chat mode.
*
* @return the player chat mode
* @deprecated Use {@link #getChatMessageType()}
*/
@Deprecated
public ChatMode getChatMode() {
return ChatMode.values()[chatMessageType.ordinal()];
}
@ -2651,7 +2653,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*
* @return the messages
*/
public @NotNull ChatMessageType getChatMessageType() {
public @Nullable ChatMessageType getChatMessageType() {
return chatMessageType;
}

View File

@ -10,6 +10,7 @@ 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;
@ -34,7 +35,7 @@ public class Messenger {
* @return if the message was sent
*/
public static boolean sendMessage(@NotNull Player player, @NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) {
if (player.getSettings().getChatMessageType().accepts(position)) {
if (getChatMessageType(player).accepts(position)) {
player.getPlayerConnection().sendPacket(new ChatMessagePacket(message, position, uuid));
return true;
}
@ -56,7 +57,7 @@ public class Messenger {
final Set<Player> sentTo = new HashSet<>();
for (Player player : players) {
if (player.getSettings().getChatMessageType().accepts(position)) {
if (getChatMessageType(player).accepts(position)) {
sentTo.add(player);
}
}
@ -72,7 +73,7 @@ public class Messenger {
* @return if the server should receive messages from them
*/
public static boolean canReceiveMessage(@NotNull Player player) {
return player.getSettings().getChatMessageType() == ChatMessageType.FULL;
return getChatMessageType(player) == ChatMessageType.FULL;
}
/**
@ -97,7 +98,7 @@ public class Messenger {
* @return if the server should receive commands from them
*/
public static boolean canReceiveCommand(@NotNull Player player) {
return player.getSettings().getChatMessageType() != ChatMessageType.NONE;
return getChatMessageType(player) != ChatMessageType.NONE;
}
/**
@ -121,6 +122,16 @@ public class Messenger {
* @param player the player
*/
public static void sendRejectionMessage(@NotNull Player player) {
player.getPlayerConnection().sendPacket(CANNOT_SEND_PACKET);
player.getPlayerConnection().sendPacket(CANNOT_SEND_PACKET, false);
}
/**
* Gets the chat message type for a player, returning {@link ChatMessageType#FULL} if not set.
*
* @param player the player
* @return the chat message type
*/
private static @NotNull ChatMessageType getChatMessageType(@NotNull Player player) {
return Objects.requireNonNullElse(player.getSettings().getChatMessageType(), ChatMessageType.FULL);
}
}

View File

@ -95,7 +95,7 @@ public abstract class PlayerConnection {
}
/**
* Serializes the packet and send it to the client, skipping the translation phase.
* Serializes the packet and send it to the client, optionally skipping the translation phase.
* <p>
* Also responsible for executing {@link ConnectionManager#onPacketSend(ServerPacketConsumer)} consumers.
*