DiscreteVoxelShape .java

net.minecraft.world.phys.shapes.DiscreteVoxelShape (class): members: IntFaceConsumer, IntLineConsumer, forAllFaces, isFullWide, getXSize, getYSize, xSize.

net.minecraft.world.phys.shapesclass267 linescommon8.2 KB

API Surface (14)

DiscreteVoxelShape rotate(final OctahedralGroup rotation)
boolean isFullWide(final AxisCycle transform, final int x, final int y, final int z)
boolean isFullWide(final int x, final int y, final int z)
boolean isFull(final AxisCycle transform, final int x, final int y, final int z)
boolean isEmpty()
int firstFull(final Direction.Axis aAxis, final int b, final int c)
int lastFull(final Direction.Axis aAxis, final int b, final int c)
int getSize(final Direction.Axis axis)
int getXSize()
int getYSize()
int getZSize()
void forAllEdges(final DiscreteVoxelShape.IntLineConsumer consumer, final boolean mergeNeighbors)
void forAllBoxes(final DiscreteVoxelShape.IntLineConsumer consumer, final boolean mergeNeighbors)
void forAllFaces(final DiscreteVoxelShape.IntFaceConsumer consumer)

Fields & Constants (4)

AXIS_VALUES
xSize
ySize
zSize

String literals (1)

"Need all positive sizes: x: "

Code Graph: Dependencies (4)

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

Full Source

DiscreteVoxelShape.java267 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.phys.shapes;
2
3
import com.mojang.math.OctahedralGroup;
4
import net.minecraft.core.AxisCycle;
5
import net.minecraft.core.Direction;
6
import org.joml.Vector3i;
7
8
public abstract class DiscreteVoxelShape {
9
	private static final Direction.Axis[] AXIS_VALUES = Direction.Axis.values();
10
	protected final int xSize;
11
	protected final int ySize;
12
	protected final int zSize;
13
14
	protected DiscreteVoxelShape(final int xSize, final int ySize, final int zSize) {
15
		if (xSize >= 0 && ySize >= 0 && zSize >= 0) {
16
			this.xSize = xSize;
17
			this.ySize = ySize;
18
			this.zSize = zSize;
19
		} else {
20
			throw new IllegalArgumentException("Need all positive sizes: x: " + xSize + ", y: " + ySize + ", z: " + zSize);
21
		}
22
	}
23
24
	public DiscreteVoxelShape rotate(final OctahedralGroup rotation) {
25
		if (rotation == OctahedralGroup.IDENTITY) {
26
			return this;
27
		}
28
29
		Vector3i v = rotation.rotate(new Vector3i(this.xSize, this.ySize, this.zSize));
30
		int shiftX = fixupCoordinate(v, 0);
31
		int shiftY = fixupCoordinate(v, 1);
32
		int shiftZ = fixupCoordinate(v, 2);
33
		DiscreteVoxelShape newShape = new BitSetDiscreteVoxelShape(v.x, v.y, v.z);
34
35
		for (int x = 0; x < this.xSize; x++) {
36
			for (int y = 0; y < this.ySize; y++) {
37
				for (int z = 0; z < this.zSize; z++) {
38
					if (this.isFull(x, y, z)) {
39
						Vector3i newPos = rotation.rotate(v.set(x, y, z));
40
						int newX = shiftX + newPos.x;
41
						int newY = shiftY + newPos.y;
42
						int newZ = shiftZ + newPos.z;
43
						newShape.fill(newX, newY, newZ);
44
					}
45
				}
46
			}
47
		}
48
49
		return newShape;
50
	}
51
52
	private static int fixupCoordinate(final Vector3i v, final int index) {
53
		int value = v.get(index);
54
		if (value < 0) {
55
			v.setComponent(index, -value);
56
			return -value - 1;
57
		} else {
58
			return 0;
59
		}
60
	}
61
62
	public boolean isFullWide(final AxisCycle transform, final int x, final int y, final int z) {
63
		return this.isFullWide(transform.cycle(x, y, z, Direction.Axis.X), transform.cycle(x, y, z, Direction.Axis.Y), transform.cycle(x, y, z, Direction.Axis.Z));
64
	}
65
66
	public boolean isFullWide(final int x, final int y, final int z) {
67
		if (x < 0 || y < 0 || z < 0) {
68
			return false;
69
		} else {
70
			return x < this.xSize && y < this.ySize && z < this.zSize ? this.isFull(x, y, z) : false;
71
		}
72
	}
73
74
	public boolean isFull(final AxisCycle transform, final int x, final int y, final int z) {
75
		return this.isFull(transform.cycle(x, y, z, Direction.Axis.X), transform.cycle(x, y, z, Direction.Axis.Y), transform.cycle(x, y, z, Direction.Axis.Z));
76
	}
77
78
	public abstract boolean isFull(final int x, final int y, final int z);
79
80
	public abstract void fill(final int x, final int y, final int z);
81
82
	public boolean isEmpty() {
83
		for (Direction.Axis axis : AXIS_VALUES) {
84
			if (this.firstFull(axis) >= this.lastFull(axis)) {
85
				return true;
86
			}
87
		}
88
89
		return false;
90
	}
91
92
	public abstract int firstFull(final Direction.Axis axis);
93
94
	public abstract int lastFull(final Direction.Axis axis);
95
96
	public int firstFull(final Direction.Axis aAxis, final int b, final int c) {
97
		int aSize = this.getSize(aAxis);
98
		if (b >= 0 && c >= 0) {
99
			Direction.Axis bAxis = AxisCycle.FORWARD.cycle(aAxis);
100
			Direction.Axis cAxis = AxisCycle.BACKWARD.cycle(aAxis);
101
			if (b < this.getSize(bAxis) && c < this.getSize(cAxis)) {
102
				AxisCycle transform = AxisCycle.between(Direction.Axis.X, aAxis);
103
104
				for (int a = 0; a < aSize; a++) {
105
					if (this.isFull(transform, a, b, c)) {
106
						return a;
107
					}
108
				}
109
110
				return aSize;
111
			} else {
112
				return aSize;
113
			}
114
		} else {
115
			return aSize;
116
		}
117
	}
118
119
	public int lastFull(final Direction.Axis aAxis, final int b, final int c) {
120
		if (b >= 0 && c >= 0) {
121
			Direction.Axis bAxis = AxisCycle.FORWARD.cycle(aAxis);
122
			Direction.Axis cAxis = AxisCycle.BACKWARD.cycle(aAxis);
123
			if (b < this.getSize(bAxis) && c < this.getSize(cAxis)) {
124
				int aSize = this.getSize(aAxis);
125
				AxisCycle transform = AxisCycle.between(Direction.Axis.X, aAxis);
126
127
				for (int a = aSize - 1; a >= 0; a--) {
128
					if (this.isFull(transform, a, b, c)) {
129
						return a + 1;
130
					}
131
				}
132
133
				return 0;
134
			} else {
135
				return 0;
136
			}
137
		} else {
138
			return 0;
139
		}
140
	}
141
142
	public int getSize(final Direction.Axis axis) {
143
		return axis.choose(this.xSize, this.ySize, this.zSize);
144
	}
145
146
	public int getXSize() {
147
		return this.getSize(Direction.Axis.X);
148
	}
149
150
	public int getYSize() {
151
		return this.getSize(Direction.Axis.Y);
152
	}
153
154
	public int getZSize() {
155
		return this.getSize(Direction.Axis.Z);
156
	}
157
158
	public void forAllEdges(final DiscreteVoxelShape.IntLineConsumer consumer, final boolean mergeNeighbors) {
159
		this.forAllAxisEdges(consumer, AxisCycle.NONE, mergeNeighbors);
160
		this.forAllAxisEdges(consumer, AxisCycle.FORWARD, mergeNeighbors);
161
		this.forAllAxisEdges(consumer, AxisCycle.BACKWARD, mergeNeighbors);
162
	}
163
164
	private void forAllAxisEdges(final DiscreteVoxelShape.IntLineConsumer consumer, final AxisCycle transform, final boolean mergeNeighbors) {
165
		AxisCycle inverse = transform.inverse();
166
		int aSize = this.getSize(inverse.cycle(Direction.Axis.X));
167
		int bSize = this.getSize(inverse.cycle(Direction.Axis.Y));
168
		int cSize = this.getSize(inverse.cycle(Direction.Axis.Z));
169
170
		for (int a = 0; a <= aSize; a++) {
171
			for (int b = 0; b <= bSize; b++) {
172
				int lastStart = -1;
173
174
				for (int c = 0; c <= cSize; c++) {
175
					int fullSectors = 0;
176
					int oddSectors = 0;
177
178
					for (int da = 0; da <= 1; da++) {
179
						for (int db = 0; db <= 1; db++) {
180
							if (this.isFullWide(inverse, a + da - 1, b + db - 1, c)) {
181
								fullSectors++;
182
								oddSectors ^= da ^ db;
183
							}
184
						}
185
					}
186
187
					if (fullSectors == 1 || fullSectors == 3 || fullSectors == 2 && (oddSectors & 1) == 0) {
188
						if (mergeNeighbors) {
189
							if (lastStart == -1) {
190
								lastStart = c;
191
							}
192
						} else {
193
							consumer.consume(
194
								inverse.cycle(a, b, c, Direction.Axis.X),
195
								inverse.cycle(a, b, c, Direction.Axis.Y),
196
								inverse.cycle(a, b, c, Direction.Axis.Z),
197
								inverse.cycle(a, b, c + 1, Direction.Axis.X),
198
								inverse.cycle(a, b, c + 1, Direction.Axis.Y),
199
								inverse.cycle(a, b, c + 1, Direction.Axis.Z)
200
							);
201
						}
202
					} else if (lastStart != -1) {
203
						consumer.consume(
204
							inverse.cycle(a, b, lastStart, Direction.Axis.X),
205
							inverse.cycle(a, b, lastStart, Direction.Axis.Y),
206
							inverse.cycle(a, b, lastStart, Direction.Axis.Z),
207
							inverse.cycle(a, b, c, Direction.Axis.X),
208
							inverse.cycle(a, b, c, Direction.Axis.Y),
209
							inverse.cycle(a, b, c, Direction.Axis.Z)
210
						);
211
						lastStart = -1;
212
					}
213
				}
214
			}
215
		}
216
	}
217
218
	public void forAllBoxes(final DiscreteVoxelShape.IntLineConsumer consumer, final boolean mergeNeighbors) {
219
		BitSetDiscreteVoxelShape.forAllBoxes(this, consumer, mergeNeighbors);
220
	}
221
222
	public void forAllFaces(final DiscreteVoxelShape.IntFaceConsumer consumer) {
223
		this.forAllAxisFaces(consumer, AxisCycle.NONE);
224
		this.forAllAxisFaces(consumer, AxisCycle.FORWARD);
225
		this.forAllAxisFaces(consumer, AxisCycle.BACKWARD);
226
	}
227
228
	private void forAllAxisFaces(final DiscreteVoxelShape.IntFaceConsumer consumer, final AxisCycle transform) {
229
		AxisCycle inverse = transform.inverse();
230
		Direction.Axis cAxis = inverse.cycle(Direction.Axis.Z);
231
		int aSize = this.getSize(inverse.cycle(Direction.Axis.X));
232
		int bSize = this.getSize(inverse.cycle(Direction.Axis.Y));
233
		int cSize = this.getSize(cAxis);
234
		Direction negative = Direction.fromAxisAndDirection(cAxis, Direction.AxisDirection.NEGATIVE);
235
		Direction positive = Direction.fromAxisAndDirection(cAxis, Direction.AxisDirection.POSITIVE);
236
237
		for (int a = 0; a < aSize; a++) {
238
			for (int b = 0; b < bSize; b++) {
239
				boolean lastFull = false;
240
241
				for (int c = 0; c <= cSize; c++) {
242
					boolean full = c != cSize && this.isFull(inverse, a, b, c);
243
					if (!lastFull && full) {
244
						consumer.consume(negative, inverse.cycle(a, b, c, Direction.Axis.X), inverse.cycle(a, b, c, Direction.Axis.Y), inverse.cycle(a, b, c, Direction.Axis.Z));
245
					}
246
247
					if (lastFull && !full) {
248
						consumer.consume(
249
							positive, inverse.cycle(a, b, c - 1, Direction.Axis.X), inverse.cycle(a, b, c - 1, Direction.Axis.Y), inverse.cycle(a, b, c - 1, Direction.Axis.Z)
250
						);
251
					}
252
253
					lastFull = full;
254
				}
255
			}
256
		}
257
	}
258
259
	public interface IntFaceConsumer {
260
		void consume(Direction direction, int x, int y, int z);
261
	}
262
263
	public interface IntLineConsumer {
264
		void consume(int x1, int y1, int z1, int x2, int y2, int z2);
265
	}
266
}
267

Decompiled Minecraft 26.3 source of DiscreteVoxelShape (net/minecraft/world/phys/shapes/DiscreteVoxelShape.java) with Mojang official mappings, stored verbatim on the site.