| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.minecraft.world.level.ChunkPos;
|
| 6 |
|
| 7 | public interface ChunkTrackingView {
|
| 8 | ChunkTrackingView EMPTY = new ChunkTrackingView() {
|
| 9 | @Override
|
| 10 | public boolean contains(final int chunkX, final int chunkZ, final boolean includeNeighbors) {
|
| 11 | return false;
|
| 12 | }
|
| 13 |
|
| 14 | @Override
|
| 15 | public void forEach(final Consumer<ChunkPos> consumer) {
|
| 16 | }
|
| 17 | };
|
| 18 |
|
| 19 | static ChunkTrackingView of(final ChunkPos center, final int radius) {
|
| 20 | return new ChunkTrackingView.Positioned(center, radius);
|
| 21 | }
|
| 22 |
|
| 23 | static void difference(final ChunkTrackingView from, final ChunkTrackingView to, final Consumer<ChunkPos> onEnter, final Consumer<ChunkPos> onLeave) {
|
| 24 | if (!from.equals(to)) {
|
| 25 | if (from instanceof ChunkTrackingView.Positioned last && to instanceof ChunkTrackingView.Positioned next && last.squareIntersects(next)) {
|
| 26 | int minX = Math.min(last.minX(), next.minX());
|
| 27 | int minZ = Math.min(last.minZ(), next.minZ());
|
| 28 | int maxX = Math.max(last.maxX(), next.maxX());
|
| 29 | int maxZ = Math.max(last.maxZ(), next.maxZ());
|
| 30 |
|
| 31 | for (int x = minX; x <= maxX; x++) {
|
| 32 | for (int z = minZ; z <= maxZ; z++) {
|
| 33 | boolean saw = last.contains(x, z);
|
| 34 | boolean sees = next.contains(x, z);
|
| 35 | if (saw != sees) {
|
| 36 | if (sees) {
|
| 37 | onEnter.accept(new ChunkPos(x, z));
|
| 38 | } else {
|
| 39 | onLeave.accept(new ChunkPos(x, z));
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 | } else {
|
| 45 | from.forEach(onLeave);
|
| 46 | to.forEach(onEnter);
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | default boolean contains(final ChunkPos pos) {
|
| 52 | return this.contains(pos.x(), pos.z());
|
| 53 | }
|
| 54 |
|
| 55 | default boolean contains(final int x, final int z) {
|
| 56 | return this.contains(x, z, true);
|
| 57 | }
|
| 58 |
|
| 59 | boolean contains(int chunkX, int chunkZ, boolean includeNeighbors);
|
| 60 |
|
| 61 | void forEach(Consumer<ChunkPos> consumer);
|
| 62 |
|
| 63 | default boolean isInViewDistance(final int chunkX, final int chunkZ) {
|
| 64 | return this.contains(chunkX, chunkZ, false);
|
| 65 | }
|
| 66 |
|
| 67 | static boolean isInViewDistance(final int centerX, final int centerZ, final int viewDistance, final int chunkX, final int chunkZ) {
|
| 68 | return isWithinDistance(centerX, centerZ, viewDistance, chunkX, chunkZ, false);
|
| 69 | }
|
| 70 |
|
| 71 | static boolean isWithinDistance(
|
| 72 | final int centerX, final int centerZ, final int viewDistance, final int chunkX, final int chunkZ, final boolean includeNeighbors
|
| 73 | ) {
|
| 74 | int bufferRange = includeNeighbors ? 2 : 1;
|
| 75 | long deltaX = Math.max(0, Math.abs(chunkX - centerX) - bufferRange);
|
| 76 | long deltaZ = Math.max(0, Math.abs(chunkZ - centerZ) - bufferRange);
|
| 77 | long distanceSquared = deltaX * deltaX + deltaZ * deltaZ;
|
| 78 | int radiusSquared = viewDistance * viewDistance;
|
| 79 | return distanceSquared < radiusSquared;
|
| 80 | }
|
| 81 |
|
| 82 | record Positioned(ChunkPos center, int viewDistance) implements ChunkTrackingView {
|
| 83 | private int minX() {
|
| 84 | return this.center.x() - this.viewDistance - 1;
|
| 85 | }
|
| 86 |
|
| 87 | private int minZ() {
|
| 88 | return this.center.z() - this.viewDistance - 1;
|
| 89 | }
|
| 90 |
|
| 91 | private int maxX() {
|
| 92 | return this.center.x() + this.viewDistance + 1;
|
| 93 | }
|
| 94 |
|
| 95 | private int maxZ() {
|
| 96 | return this.center.z() + this.viewDistance + 1;
|
| 97 | }
|
| 98 |
|
| 99 | @VisibleForTesting
|
| 100 | boolean squareIntersects(final ChunkTrackingView.Positioned other) {
|
| 101 | return this.minX() <= other.maxX() && this.maxX() >= other.minX() && this.minZ() <= other.maxZ() && this.maxZ() >= other.minZ();
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public boolean contains(final int chunkX, final int chunkZ, final boolean includeNeighbors) {
|
| 106 | return ChunkTrackingView.isWithinDistance(this.center.x(), this.center.z(), this.viewDistance, chunkX, chunkZ, includeNeighbors);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void forEach(final Consumer<ChunkPos> consumer) {
|
| 111 | for (int x = this.minX(); x <= this.maxX(); x++) {
|
| 112 | for (int z = this.minZ(); z <= this.maxZ(); z++) {
|
| 113 | if (this.contains(x, z)) {
|
| 114 | consumer.accept(new ChunkPos(x, z));
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|