Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve any potential NullPointerException throws caused by Entity#getClosestPlayer() #603

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/client/java/minicraft/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ public boolean isWithin(int tileRadius, Entity other) {
*
* @return the closest player.
*/
@Nullable
protected Player getClosestPlayer() {
return getClosestPlayer(true);
}
Expand All @@ -387,6 +388,7 @@ protected Player getClosestPlayer() {
* @param returnSelf determines if the method can return itself.
* @return The closest player to this entity.
*/
@Nullable
protected Player getClosestPlayer(boolean returnSelf) {
if (this instanceof Player && returnSelf)
return (Player) this;
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/entity/Spark.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void tick() {
y = (int) yy;

Player player = getClosestPlayer();
if (player.isWithin(0, this)) {
if (player != null && player.isWithin(0, this)) {
player.hurt(owner, 1);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/java/minicraft/entity/mob/ObsidianKnight.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public ObsidianKnight(int health) {
@Override
public void tick() {
super.tick();
if (getClosestPlayer().isRemoved()) {
Player player = getClosestPlayer();
if (player == null || player.isRemoved()) {
active = false;
KnightStatue ks = new KnightStatue(health);
level.add(ks, x, y, false);
this.remove();
return;
}

// Achieve phase 2
Expand All @@ -101,7 +103,6 @@ public void tick() {
}

if (attackPhase == AttackPhase.Attacking) {
Player player = getClosestPlayer();
if (attackDelay > 0) {
xmov = ymov = 0;
int dir = (attackDelay - 35) / 4 % 4; // The direction of attack.
Expand Down