From 25fe86d81243d7356d1b607d9910fc4bd51b744d Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 18 Sep 2021 11:22:28 -0700 Subject: [PATCH] Non null user methods (#1856) * This makes some User methods non-null. Instead of returning null, some methods will throw an error if they are called on non-Players. This means code does not have to do null checks. * Perform null check in method. --- .../admin/blueprints/AdminBlueprintCommand.java | 7 ++++--- .../java/world/bentobox/bentobox/api/user/User.java | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java index 2d7514c90..63312b29d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java @@ -70,15 +70,16 @@ protected void showClipboard(User user) { if (clipboards.containsKey(user.getUniqueId())) { BlueprintClipboard clipboard = clipboards.get(user.getUniqueId()); - if (clipboard.getPos1() != null && clipboard.getPos2() != null) { - paintAxis(user, clipboard); - } + paintAxis(user, clipboard); } }, 20, 20)); } private void paintAxis(User user, BlueprintClipboard clipboard) { + if (clipboard.getPos1() == null || clipboard.getPos2() == null) { + return; + } int minX = Math.min(clipboard.getPos1().getBlockX(), clipboard.getPos2().getBlockX()); int minY = Math.min(clipboard.getPos1().getBlockY(), clipboard.getPos2().getBlockY()); int minZ = Math.min(clipboard.getPos1().getBlockZ(), clipboard.getPos2().getBlockZ()); diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 9f7a191e1..411318084 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -175,16 +176,17 @@ public Set getEffectivePermissions() { return sender.getEffectivePermissions(); } - @Nullable + @NonNull public PlayerInventory getInventory() { - return player != null ? player.getInventory() : null; + return Objects.requireNonNull(player, "getInventory can only be called for online players!").getInventory(); } - @Nullable + @NonNull public Location getLocation() { - return player != null ? player.getLocation() : null; + return Objects.requireNonNull(player, "getLocation can only be called for online players!").getLocation(); } + @NonNull public String getName() { return player != null ? player.getName() : plugin.getPlayers().getName(playerUUID); }