mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-09 08:51:38 +01:00
Fixed synchronization issue with pathfinder
This commit is contained in:
parent
08b4b8576e
commit
dcf6bc2082
@ -26,6 +26,7 @@ import net.minestom.server.utils.validate.Check;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public abstract class EntityCreature extends LivingEntity {
|
public abstract class EntityCreature extends LivingEntity {
|
||||||
@ -50,6 +51,9 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
private ItemStack leggings;
|
private ItemStack leggings;
|
||||||
private ItemStack boots;
|
private ItemStack boots;
|
||||||
|
|
||||||
|
// Lock used for the pathfinder
|
||||||
|
private ReentrantLock pathLock = new ReentrantLock();
|
||||||
|
|
||||||
|
|
||||||
public EntityCreature(EntityType entityType, Position spawnPosition) {
|
public EntityCreature(EntityType entityType, Position spawnPosition) {
|
||||||
super(entityType, spawnPosition);
|
super(entityType, spawnPosition);
|
||||||
@ -115,6 +119,8 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
|
|
||||||
|
|
||||||
// Path finding
|
// Path finding
|
||||||
|
{
|
||||||
|
pathLock.lock();
|
||||||
path = pathFinder.updatePathFor(pathingEntity);
|
path = pathFinder.updatePathFor(pathingEntity);
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
|
final float speed = getAttributeValue(Attribute.MOVEMENT_SPEED);
|
||||||
@ -126,6 +132,8 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
this.pathFinder.reset();
|
this.pathFinder.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pathLock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
super.update(time);
|
super.update(time);
|
||||||
}
|
}
|
||||||
@ -323,7 +331,13 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
* @param position the position to find the path to, null to reset the pathfinder
|
* @param position the position to find the path to, null to reset the pathfinder
|
||||||
* @return true if a path has been found
|
* @return true if a path has been found
|
||||||
*/
|
*/
|
||||||
public synchronized boolean setPathTo(Position position) {
|
public boolean setPathTo(Position position) {
|
||||||
|
if (position != null && getPathPosition() != null && position.isSimilar(getPathPosition())) {
|
||||||
|
// Tried to set path to the same target position
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pathLock.lock();
|
||||||
this.pathFinder.reset();
|
this.pathFinder.reset();
|
||||||
if (position == null) {
|
if (position == null) {
|
||||||
return false;
|
return false;
|
||||||
@ -348,6 +362,7 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
} catch (NullPointerException | IndexOutOfBoundsException e) {
|
||||||
this.path = null;
|
this.path = null;
|
||||||
}
|
}
|
||||||
|
pathLock.unlock();
|
||||||
|
|
||||||
final boolean success = path != null;
|
final boolean success = path != null;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user