Skip to content

Commit

Permalink
Fix potential null pointer in voice chat API
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelmax committed Oct 23, 2022
1 parent 0a35317 commit 22c2e07
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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 - <code>null</code> if the group was created programmatically
*/
@Nullable
VoicechatConnection getConnection();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 22c2e07

Please sign in to comment.