mirror of
https://github.com/Minestom/Minestom.git
synced 2024-06-26 06:24:54 +02:00
Small optimization for entity velocity
This commit is contained in:
parent
3137be35a4
commit
feb4783988
|
@ -182,6 +182,7 @@ public class CollisionUtils {
|
|||
* @param newPosition the future target position
|
||||
* @return the position with the world border collision applied (can be {@code newPosition} if not changed)
|
||||
*/
|
||||
@NotNull
|
||||
public static Position applyWorldBorder(@NotNull Instance instance,
|
||||
@NotNull Position currentPosition, @NotNull Position newPosition) {
|
||||
final WorldBorder worldBorder = instance.getWorldBorder();
|
||||
|
|
|
@ -478,14 +478,17 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||
// Stop here if the position is the same
|
||||
final boolean updatePosition = !newPosition.isSimilar(position);
|
||||
|
||||
// Check chunk
|
||||
if (!ChunkUtils.isLoaded(instance, newPosition.getX(), newPosition.getZ())) {
|
||||
// World border collision
|
||||
final Position finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
||||
final Chunk finalChunk = instance.getChunkAt(finalVelocityPosition);
|
||||
|
||||
// Entity shouldn't be updated when moving in an unloaded chunk
|
||||
if (!ChunkUtils.isLoaded(finalChunk)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// World border and apply the position
|
||||
final Position finalVelocityPosition = CollisionUtils.applyWorldBorder(instance, position, newPosition);
|
||||
if (finalVelocityPosition != null && updatePosition) {
|
||||
// Apply the position
|
||||
if (updatePosition) {
|
||||
refreshPosition(finalVelocityPosition);
|
||||
}
|
||||
|
||||
|
@ -498,7 +501,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||
float drag;
|
||||
if (onGround) {
|
||||
final BlockPosition blockPosition = position.toBlockPosition();
|
||||
final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
|
||||
final CustomBlock customBlock = finalChunk.getCustomBlock(
|
||||
blockPosition.getX(),
|
||||
blockPosition.getY(),
|
||||
blockPosition.getZ());
|
||||
if (customBlock != null) {
|
||||
// Custom drag
|
||||
drag = customBlock.getDrag(instance, blockPosition);
|
||||
|
|
|
@ -266,6 +266,7 @@ public abstract class Chunk implements Viewable, DataContainer {
|
|||
* @param z the block Z
|
||||
* @return the {@link CustomBlock} at the position
|
||||
*/
|
||||
@Nullable
|
||||
public CustomBlock getCustomBlock(int x, int y, int z) {
|
||||
final short customBlockId = getCustomBlockId(x, y, z);
|
||||
return customBlockId != 0 ? BLOCK_MANAGER.getCustomBlock(customBlockId) : null;
|
||||
|
@ -277,6 +278,7 @@ public abstract class Chunk implements Viewable, DataContainer {
|
|||
* @param index the block index
|
||||
* @return the {@link CustomBlock} at the block index
|
||||
*/
|
||||
@Nullable
|
||||
protected CustomBlock getCustomBlock(int index) {
|
||||
final int x = ChunkUtils.blockIndexToChunkPositionX(index);
|
||||
final int y = ChunkUtils.blockIndexToChunkPositionY(index);
|
||||
|
|
Loading…
Reference in New Issue
Block a user