Skip to content

Commit

Permalink
Non null user methods (#1856)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tastybento authored Sep 18, 2021
1 parent cad7dc1 commit 25fe86d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/world/bentobox/bentobox/api/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,16 +176,17 @@ public Set<PermissionAttachmentInfo> 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);
}
Expand Down

0 comments on commit 25fe86d

Please sign in to comment.