RotatingSectionStorage .java

net.minecraft.client.RotatingSectionStorage (class): members: sectionGridSizeXZ, centerSectionPos, repositionCenter, sectionGridSizeY, ValueCreator, Node.

net.minecraft.clientclass181 linesclientextends RotatingSectionStorage.Value>5.3 KB

Implements

API Surface (12)

boolean repositionCenter(final SectionPos newCenterSectionPos)
int radius()
int minY()
int maxY()
int height()
SectionPos centerSectionPos()
Iterator<T> iterator()
boolean hasNext()
T next()
void forEach(final Consumer<? super T> action)
Spliterator<T> spliterator()
int size()

Fields & Constants (7)

nodes
radius
minY
maxY
sectionGridSizeY
sectionGridSizeXZ
centerSectionPos

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

RotatingSectionStorage.java181 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client;
2
3
import java.util.Iterator;
4
import java.util.NoSuchElementException;
5
import java.util.Spliterator;
6
import java.util.Spliterators;
7
import java.util.function.Consumer;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.SectionPos;
12
import org.jspecify.annotations.Nullable;
13
14
@Environment(EnvType.CLIENT)
15
public class RotatingSectionStorage<T extends RotatingSectionStorage.Value> implements Iterable<T> {
16
	private final RotatingSectionStorage.Node<T>[] nodes;
17
	private final int radius;
18
	private final int minY;
19
	private final int maxY;
20
	private final int sectionGridSizeY;
21
	private final int sectionGridSizeXZ;
22
	private SectionPos centerSectionPos = SectionPos.of(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
23
24
	public RotatingSectionStorage(final int radius, final int minY, final int maxY, final RotatingSectionStorage.ValueCreator<T> valueCreator) {
25
		this.radius = radius;
26
		this.minY = minY;
27
		this.maxY = maxY;
28
		this.sectionGridSizeY = maxY - minY + 1;
29
		this.sectionGridSizeXZ = radius * 2 + 1;
30
		int totalSections = this.sectionGridSizeXZ * this.sectionGridSizeXZ * this.sectionGridSizeY;
31
		this.nodes = new RotatingSectionStorage.Node[totalSections];
32
33
		for (int x = 0; x < this.sectionGridSizeXZ; x++) {
34
			for (int y = 0; y < this.sectionGridSizeY; y++) {
35
				for (int z = 0; z < this.sectionGridSizeXZ; z++) {
36
					int index = this.getSectionIndex(x, y, z);
37
					long sectionNode = SectionPos.asLong(x, y + minY, z);
38
					this.nodes[index] = new RotatingSectionStorage.Node<>(valueCreator.createValue(index, sectionNode));
39
				}
40
			}
41
		}
42
	}
43
44
	public boolean repositionCenter(final SectionPos newCenterSectionPos) {
45
		if (newCenterSectionPos.equals(this.centerSectionPos)) {
46
			return false;
47
		}
48
49
		int lowestX = newCenterSectionPos.x() - this.radius;
50
		int lowestZ = newCenterSectionPos.z() - this.radius;
51
52
		for (int gridX = 0; gridX < this.sectionGridSizeXZ; gridX++) {
53
			int newSectionX = lowestX + Math.floorMod(gridX - lowestX, this.sectionGridSizeXZ);
54
55
			for (int gridZ = 0; gridZ < this.sectionGridSizeXZ; gridZ++) {
56
				int newSectionZ = lowestZ + Math.floorMod(gridZ - lowestZ, this.sectionGridSizeXZ);
57
58
				for (int gridY = 0; gridY < this.sectionGridSizeY; gridY++) {
59
					int newSectionY = this.minY + gridY;
60
					T value = this.nodes[this.getSectionIndex(gridX, gridY, gridZ)].value;
61
					long sectionNode = value.getSectionNode();
62
					if (sectionNode != SectionPos.asLong(newSectionX, newSectionY, newSectionZ)) {
63
						value.setSectionNode(SectionPos.asLong(newSectionX, newSectionY, newSectionZ));
64
					}
65
				}
66
			}
67
		}
68
69
		this.centerSectionPos = newCenterSectionPos;
70
		return true;
71
	}
72
73
	public int radius() {
74
		return this.radius;
75
	}
76
77
	public int minY() {
78
		return this.minY;
79
	}
80
81
	public int maxY() {
82
		return this.maxY;
83
	}
84
85
	public int height() {
86
		return this.sectionGridSizeY;
87
	}
88
89
	public SectionPos centerSectionPos() {
90
		return this.centerSectionPos;
91
	}
92
93
	public @Nullable T getValueAt(final BlockPos pos) {
94
		return this.getValue(SectionPos.asLong(pos));
95
	}
96
97
	public @Nullable T getValue(final long sectionNode) {
98
		int sectionX = SectionPos.x(sectionNode);
99
		int sectionY = SectionPos.y(sectionNode);
100
		int sectionZ = SectionPos.z(sectionNode);
101
		return this.getValue(sectionX, sectionY, sectionZ);
102
	}
103
104
	public @Nullable T getValue(final int sectionX, final int sectionY, final int sectionZ) {
105
		if (!this.containsSection(sectionX, sectionY, sectionZ)) {
106
			return null;
107
		}
108
109
		int y = sectionY - this.minY;
110
		int x = Math.floorMod(sectionX, this.sectionGridSizeXZ);
111
		int z = Math.floorMod(sectionZ, this.sectionGridSizeXZ);
112
		return this.nodes[this.getSectionIndex(x, y, z)].value;
113
	}
114
115
	private boolean containsSection(final int sectionX, final int sectionY, final int sectionZ) {
116
		if (sectionY >= this.minY && sectionY <= this.maxY) {
117
			return sectionX < this.centerSectionPos.x() - this.radius || sectionX > this.centerSectionPos.x() + this.radius
118
				? false
119
				: sectionZ >= this.centerSectionPos.z() - this.radius && sectionZ <= this.centerSectionPos.z() + this.radius;
120
		} else {
121
			return false;
122
		}
123
	}
124
125
	private int getSectionIndex(final int x, final int y, final int z) {
126
		return (z * this.sectionGridSizeY + y) * this.sectionGridSizeXZ + x;
127
	}
128
129
	@Override
130
	public Iterator<T> iterator() {
131
		return new Iterator<T>() {
132
			private int i;
133
134
			@Override
135
			public boolean hasNext() {
136
				return this.i < RotatingSectionStorage.this.nodes.length - 1;
137
			}
138
139
			public T next() {
140
				if (this.i >= RotatingSectionStorage.this.nodes.length) {
141
					throw new NoSuchElementException();
142
				} else {
143
					return RotatingSectionStorage.this.nodes[this.i++].value;
144
				}
145
			}
146
		};
147
	}
148
149
	@Override
150
	public void forEach(final Consumer<? super T> action) {
151
		for (RotatingSectionStorage.Node<T> node : this.nodes) {
152
			action.accept(node.value);
153
		}
154
	}
155
156
	@Override
157
	public Spliterator<T> spliterator() {
158
		return Spliterators.spliterator(this.iterator(), this.nodes.length, 0);
159
	}
160
161
	public int size() {
162
		return this.nodes.length;
163
	}
164
165
	@Environment(EnvType.CLIENT)
166
	private record Node<T>(T value) {
167
	}
168
169
	@Environment(EnvType.CLIENT)
170
	public interface Value {
171
		void setSectionNode(long sectionNode);
172
173
		long getSectionNode();
174
	}
175
176
	@Environment(EnvType.CLIENT)
177
	public interface ValueCreator<T extends RotatingSectionStorage.Value> {
178
		T createValue(int index, long sectionNode);
179
	}
180
}
181

Decompiled Minecraft 26.3 source of RotatingSectionStorage (net/minecraft/client/RotatingSectionStorage.java) with Mojang official mappings, stored verbatim on the site.