Added null checks.

This commit is contained in:
tastybento 2021-07-24 21:13:46 -07:00
parent b897a71cd2
commit 4b83e9b770
2 changed files with 16 additions and 9 deletions

View File

@ -33,9 +33,12 @@ public class PlaceholdersManager {
* @return string of advancement count * @return string of advancement count
*/ */
public String getCountByLocation(User user) { public String getCountByLocation(User user) {
if (user == null || user.getUniqueId() == null || user.getLocation() == null) return ""; if (user != null && user.getUniqueId() != null && user.getLocation() != null) {
return addon.getIslands().getIslandAt(user.getLocation()) return addon.getIslands().getIslandAt(user.getLocation())
.map(i -> String.valueOf(addon.getAdvManager().getIsland(i).getAdvancements().size())).orElse(""); .map(i -> String.valueOf(addon.getAdvManager().getIsland(i).getAdvancements().size())).orElse("");
} else {
return "";
}
} }

View File

@ -3,6 +3,7 @@ package world.bentobox.boxed.listeners;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.Spliterators; import java.util.Spliterators;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -126,16 +127,16 @@ public class AdvancementListener implements Listener {
int diff = addon.getAdvManager().checkIslandSize(island); int diff = addon.getAdvManager().checkIslandSize(island);
if (diff > 0) { if (diff > 0) {
user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(diff)); user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(diff));
user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F); user.getPlayer().playSound(Objects.requireNonNull(user.getLocation()), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F);
} else if (diff < 0) { } else if (diff < 0) {
user.sendMessage("boxed.size-decreased", TextVariables.NUMBER, String.valueOf(Math.abs(diff))); user.sendMessage("boxed.size-decreased", TextVariables.NUMBER, String.valueOf(Math.abs(diff)));
user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 1F, 2F); user.getPlayer().playSound(Objects.requireNonNull(user.getLocation()), Sound.ENTITY_VILLAGER_DEATH, 1F, 2F);
} }
} }
} }
private void informPlayer(User user, NamespacedKey key, int score) { private void informPlayer(User user, NamespacedKey key, int score) {
user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F); user.getPlayer().playSound(Objects.requireNonNull(user.getLocation()), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F);
user.sendMessage("boxed.completed", TextVariables.NAME, keyToString(user, key)); user.sendMessage("boxed.completed", TextVariables.NAME, keyToString(user, key));
user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(score)); user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(score));
@ -193,7 +194,7 @@ public class AdvancementListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTeamJoinTime(TeamJoinedEvent e) { public void onTeamJoinTime(TeamJoinedEvent e) {
User user = User.getInstance(e.getPlayerUUID()); User user = User.getInstance(e.getPlayerUUID());
if (addon.getSettings().isOnJoinResetAdvancements() && user.isOnline() if (user != null && addon.getSettings().isOnJoinResetAdvancements() && user.isOnline()
&& addon.getOverWorld().equals(Util.getWorld(user.getWorld()))) { && addon.getOverWorld().equals(Util.getWorld(user.getWorld()))) {
// Clear and set advancements // Clear and set advancements
clearAndSetAdv(user, addon.getSettings().isOnJoinResetAdvancements(), addon.getSettings().getOnJoinGrantAdvancements()); clearAndSetAdv(user, addon.getSettings().isOnJoinResetAdvancements(), addon.getSettings().getOnJoinGrantAdvancements());
@ -205,7 +206,7 @@ public class AdvancementListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTeamLeaveTime(TeamLeaveEvent e) { public void onTeamLeaveTime(TeamLeaveEvent e) {
User user = User.getInstance(e.getPlayerUUID()); User user = User.getInstance(e.getPlayerUUID());
if (addon.getSettings().isOnJoinResetAdvancements() && user.isOnline() if (user != null && addon.getSettings().isOnJoinResetAdvancements() && user.isOnline()
&& addon.getOverWorld().equals(Util.getWorld(user.getWorld()))) { && addon.getOverWorld().equals(Util.getWorld(user.getWorld()))) {
// Clear and set advancements // Clear and set advancements
clearAndSetAdv(user, addon.getSettings().isOnLeaveResetAdvancements(), addon.getSettings().getOnLeaveGrantAdvancements()); clearAndSetAdv(user, addon.getSettings().isOnLeaveResetAdvancements(), addon.getSettings().getOnLeaveGrantAdvancements());
@ -215,7 +216,10 @@ public class AdvancementListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onFirstTime(IslandNewIslandEvent e) { public void onFirstTime(IslandNewIslandEvent e) {
clearAndSetAdv(User.getInstance(e.getPlayerUUID()), addon.getSettings().isOnJoinResetAdvancements(), addon.getSettings().getOnJoinGrantAdvancements()); User user = User.getInstance(e.getPlayerUUID());
if (user != null) {
clearAndSetAdv(user, addon.getSettings().isOnJoinResetAdvancements(), addon.getSettings().getOnJoinGrantAdvancements());
}
} }