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.
This commit is contained in:
tastybento 2021-09-18 11:22:28 -07:00 committed by GitHub
parent cad7dc1666
commit 25fe86d812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -70,15 +70,16 @@ public class AdminBlueprintCommand extends ConfirmableCommand {
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());

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
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 class User implements MetaDataAble {
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);
}