1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.owlmaddie.goals;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
/**
* The {@code PlayerBaseGoal} class sets a targetEntity, and will automatically update the targetEntity
* when a player die's and respawns, or logs back in, etc... Other types of targetEntity classes will
* be set to null after they die.
*/
public abstract class PlayerBaseGoal extends Goal {
protected LivingEntity targetEntity;
private final int updateInterval = 20;
private int tickCounter = 0;
public PlayerBaseGoal(LivingEntity targetEntity) {
this.targetEntity = targetEntity;
}
@Override
public boolean canStart() {
if (++tickCounter >= updateInterval) {
tickCounter = 0;
updateTargetEntity();
}
return targetEntity != null && targetEntity.isAlive();
}
private void updateTargetEntity() {
if (targetEntity != null && !targetEntity.isAlive()) {
if (targetEntity instanceof ServerPlayerEntity) {
ServerWorld world = (ServerWorld) targetEntity.getWorld();
ServerPlayerEntity lookupPlayer = (ServerPlayerEntity)world.getPlayerByUuid(targetEntity.getUuid());
if (lookupPlayer != null && lookupPlayer.isAlive()) {
// Update player to alive player with same UUID
targetEntity = lookupPlayer;
}
} else {
targetEntity = null;
}
}
}
}