From 22c2e0793c8628f18e29a9c9b2478b5e518e9e97 Mon Sep 17 00:00:00 2001 From: Max Henkel Date: Sun, 23 Oct 2022 16:08:57 +0200 Subject: [PATCH] Fix potential null pointer in voice chat API --- .../de/maxhenkel/voicechat/api/events/CreateGroupEvent.java | 5 ++++- .../java/de/maxhenkel/voicechat/plugins/PluginManager.java | 5 ++++- .../voicechat/plugins/impl/VoicechatConnectionImpl.java | 5 ++++- .../voicechat/plugins/impl/events/CreateGroupEventImpl.java | 4 +++- .../java/de/maxhenkel/voicechat/plugins/PluginManager.java | 5 ++++- .../voicechat/plugins/impl/VoicechatConnectionImpl.java | 5 ++++- .../voicechat/plugins/impl/events/CreateGroupEventImpl.java | 4 +++- 7 files changed, 26 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/de/maxhenkel/voicechat/api/events/CreateGroupEvent.java b/api/src/main/java/de/maxhenkel/voicechat/api/events/CreateGroupEvent.java index 3b1659b76..5ec52baae 100644 --- a/api/src/main/java/de/maxhenkel/voicechat/api/events/CreateGroupEvent.java +++ b/api/src/main/java/de/maxhenkel/voicechat/api/events/CreateGroupEvent.java @@ -3,6 +3,8 @@ import de.maxhenkel.voicechat.api.Group; import de.maxhenkel.voicechat.api.VoicechatConnection; +import javax.annotation.Nullable; + public interface CreateGroupEvent extends GroupEvent { /** @@ -11,8 +13,9 @@ public interface CreateGroupEvent extends GroupEvent { Group getGroup(); /** - * @return the connection of the player that created the group + * @return the connection of the player that created the group - null if the group was created programmatically */ + @Nullable VoicechatConnection getConnection(); } diff --git a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java index 0de5bbae1..6e77f786d 100644 --- a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java +++ b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java @@ -124,8 +124,11 @@ public boolean onJoinGroup(Player player, @Nullable Group group) { return dispatchEvent(JoinGroupEvent.class, new JoinGroupEventImpl(new GroupImpl(group), VoicechatConnectionImpl.fromPlayer(player))); } - public boolean onCreateGroup(Player player, Group group) { + public boolean onCreateGroup(@Nullable Player player, @Nullable Group group) { if (group == null) { + if (player == null) { + return false; + } return onLeaveGroup(player); } return dispatchEvent(CreateGroupEvent.class, new CreateGroupEventImpl(new GroupImpl(group), VoicechatConnectionImpl.fromPlayer(player))); diff --git a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java index e75394e6b..9033ef9ed 100644 --- a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java +++ b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java @@ -26,7 +26,10 @@ public VoicechatConnectionImpl(Player player, PlayerState state) { } @Nullable - public static VoicechatConnectionImpl fromPlayer(Player player) { + public static VoicechatConnectionImpl fromPlayer(@Nullable Player player) { + if (player == null) { + return null; + } Server server = Voicechat.SERVER.getServer(); if (server == null) { return null; diff --git a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java index c488e04ae..a9db26711 100644 --- a/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java +++ b/bukkit/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java @@ -4,9 +4,11 @@ import de.maxhenkel.voicechat.api.VoicechatConnection; import de.maxhenkel.voicechat.api.events.CreateGroupEvent; +import javax.annotation.Nullable; + public class CreateGroupEventImpl extends GroupEventImpl implements CreateGroupEvent { - public CreateGroupEventImpl(Group group, VoicechatConnection connection) { + public CreateGroupEventImpl(Group group, @Nullable VoicechatConnection connection) { super(group, connection); } } diff --git a/common/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java b/common/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java index e82f1fae1..0343b822f 100644 --- a/common/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java +++ b/common/src/main/java/de/maxhenkel/voicechat/plugins/PluginManager.java @@ -131,8 +131,11 @@ public boolean onJoinGroup(ServerPlayer player, @Nullable Group group) { return dispatchEvent(JoinGroupEvent.class, new JoinGroupEventImpl(new GroupImpl(group), VoicechatConnectionImpl.fromPlayer(player))); } - public boolean onCreateGroup(ServerPlayer player, Group group) { + public boolean onCreateGroup(@Nullable ServerPlayer player, @Nullable Group group) { if (group == null) { + if (player == null) { + return false; + } return onLeaveGroup(player); } return dispatchEvent(CreateGroupEvent.class, new CreateGroupEventImpl(new GroupImpl(group), VoicechatConnectionImpl.fromPlayer(player))); diff --git a/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java b/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java index 00c65eb71..7fac7fa1e 100644 --- a/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java +++ b/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/VoicechatConnectionImpl.java @@ -25,7 +25,10 @@ public VoicechatConnectionImpl(net.minecraft.server.level.ServerPlayer player, P } @Nullable - public static VoicechatConnectionImpl fromPlayer(net.minecraft.server.level.ServerPlayer player) { + public static VoicechatConnectionImpl fromPlayer(@Nullable net.minecraft.server.level.ServerPlayer player) { + if (player == null) { + return null; + } Server server = Voicechat.SERVER.getServer(); if (server == null) { return null; diff --git a/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java b/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java index c488e04ae..a9db26711 100644 --- a/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java +++ b/common/src/main/java/de/maxhenkel/voicechat/plugins/impl/events/CreateGroupEventImpl.java @@ -4,9 +4,11 @@ import de.maxhenkel.voicechat.api.VoicechatConnection; import de.maxhenkel.voicechat.api.events.CreateGroupEvent; +import javax.annotation.Nullable; + public class CreateGroupEventImpl extends GroupEventImpl implements CreateGroupEvent { - public CreateGroupEventImpl(Group group, VoicechatConnection connection) { + public CreateGroupEventImpl(Group group, @Nullable VoicechatConnection connection) { super(group, connection); } }