Skip to content

Commit

Permalink
Resolve any potential NullPointerException throws caused by Entity#ge…
Browse files Browse the repository at this point in the history
…tClosestPlayer() (#603)
  • Loading branch information
Litorom authored Jan 4, 2024
2 parents e0628ca + 88f2b25 commit 373ab37
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
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

0 comments on commit 373ab37

Please sign in to comment.