Skip to content

Commit

Permalink
better NPE check
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock committed Oct 9, 2023
1 parent 12f1167 commit 864dfda
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.sacredlabyrinth.phaed.simpleclans.managers.ClanManager;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -67,4 +68,21 @@ public Set<User> targets(final @NotNull User source) {
.collect(Collectors.toSet());
}

@Contract(value = "null -> false", pure = true)
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

AbstractSimpleClansChannel that = (AbstractSimpleClansChannel) o;

return plugin.equals(that.plugin);
}

@Override
public int hashCode() {
return plugin.hashCode();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package at.helpch.chatchat.hooks.simpleclans;

import at.helpch.chatchat.api.holder.FormatsHolder;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public final class SimpleClansAllyChannel extends AbstractSimpleClansChannel {

Expand All @@ -22,11 +23,11 @@ public SimpleClansAllyChannel(@NotNull final String name,

@Override
protected @NotNull Set<ClanPlayer> clanPlayerList(@NotNull final ClanPlayer clanPlayer) {
Set<ClanPlayer> clanPlayerSet = new HashSet<>(Objects.requireNonNull(clanPlayer.getClan()).getAllAllyMembers().stream()
.filter(clanPlayer1 -> clanPlayer.toPlayer() != null)
.toList());
clanPlayerSet.addAll(clanPlayer.getClan().getOnlineMembers());
return clanPlayerSet;
final Clan clan = clanPlayer.getClan();
if (clan == null) return Set.of();
Set<ClanPlayer> clanPlayerSet = new HashSet<>(clan.getAllAllyMembers());
clanPlayerSet.addAll(clan.getOnlineMembers());
return clanPlayerSet.stream().filter(cp -> cp.toPlayer() != null).collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package at.helpch.chatchat.hooks.simpleclans;

import at.helpch.chatchat.api.holder.FormatsHolder;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public final class SimpleClansClanChannel extends AbstractSimpleClansChannel {
Expand All @@ -24,7 +24,9 @@ public SimpleClansClanChannel(@NotNull final String name,
@Contract("_ -> new")
@Override
protected @NotNull Set<ClanPlayer> clanPlayerList(@NotNull final ClanPlayer clanPlayer) {
return new HashSet<>(Objects.requireNonNull(clanPlayer.getClan()).getOnlineMembers());
final Clan clan = clanPlayer.getClan();
if (clan == null) return Set.of();
return new HashSet<>(clan.getOnlineMembers());
}

}
21 changes: 12 additions & 9 deletions plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,10 @@ public boolean chatEnabled() {

@Override
public boolean canSee(@NotNull final User target) {
if (!(target instanceof ChatUser)) {
if (!(target instanceof ChatUser chatUser)) {
return true;
}

final var chatUser = (ChatUser) target;

final var plugin = JavaPlugin.getPlugin(ChatChatPlugin.class);

return plugin.hookManager()
Expand All @@ -169,7 +167,11 @@ public boolean canSee(@NotNull final User target) {

@Override
public @NotNull Player player() {
return Objects.requireNonNull(Bukkit.getPlayer(uuid)); // this will never be null
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
throw new IllegalStateException("Player is not online!");
}
return player; // this will never be null
}

@Override
Expand All @@ -185,10 +187,11 @@ public boolean canSee(@NotNull final User target) {
@Override
public String toString() {
return "ChatUserImpl{" +
"uuid=" + uuid +
", lastMessaged=" + lastMessagedUser().map(ChatUser::uuid) +
", channel=" + channel +
", format=" + format +
'}';
"uuid=" + uuid +
", lastMessaged=" + lastMessagedUser().map(ChatUser::uuid) +
", channel=" + channel +
", format=" + format +
'}';
}

}

0 comments on commit 864dfda

Please sign in to comment.