Small optimization for entity velocity

This commit is contained in:
themode 2020-12-26 18:52:44 +01:00
parent 3137be35a4
commit feb4783988
3 changed files with 15 additions and 6 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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);