Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit b13c787

Browse files
authored
bresenham
1 parent abbd250 commit b13c787

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

src/main/java/com/jelly/MightyMiner/utils/AngleUtils.java

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import net.minecraft.client.Minecraft;
77
import net.minecraft.entity.Entity;
88
import net.minecraft.init.Blocks;
9-
import net.minecraft.util.BlockPos;
10-
import net.minecraft.util.Tuple;
11-
import net.minecraft.util.Vec3;
9+
import net.minecraft.util.*;
1210

1311
import java.util.ArrayList;
1412
import java.util.List;
@@ -324,4 +322,104 @@ public static int getPitchRotationTime(float target_pitch, int angle_per_unit, i
324322
return (int) Math.max(min, Math.abs(target_pitch - mc.thePlayer.rotationPitch) / angle_per_unit * time_per_unit);
325323
}
326324

325+
public static BlockPos bresenham(Vec3 start, Vec3 end) {
326+
int x1 = MathHelper.floor_double(end.xCoord);
327+
int y1 = MathHelper.floor_double(end.yCoord);
328+
int z1 = MathHelper.floor_double(end.zCoord);
329+
int x0 = MathHelper.floor_double(start.xCoord);
330+
int y0 = MathHelper.floor_double(start.yCoord);
331+
int z0 = MathHelper.floor_double(start.zCoord);
332+
333+
if (mc.theWorld.getBlockState(new BlockPos(x0, y0, z0)).getBlock() != Blocks.air) {
334+
return new BlockPos(x0, y0, z0);
335+
}
336+
337+
int iterations = 200;
338+
339+
while (iterations-- >= 0) {
340+
if (x0 == x1 && y0 == y1 && z0 == z1) {
341+
return new BlockPos(end);
342+
}
343+
344+
boolean hasNewX = true;
345+
boolean hasNewY = true;
346+
boolean hasNewZ = true;
347+
348+
double newX = 999.0;
349+
double newY = 999.0;
350+
double newZ = 999.0;
351+
352+
if (x1 > x0) {
353+
newX = (double) x0 + 1.0;
354+
} else if (x1 < x0) {
355+
newX = (double) x0 + 0.0;
356+
} else {
357+
hasNewX = false;
358+
}
359+
if (y1 > y0) {
360+
newY = (double) y0 + 1.0;
361+
} else if (y1 < y0) {
362+
newY = (double) y0 + 0.0;
363+
} else {
364+
hasNewY = false;
365+
}
366+
if (z1 > z0) {
367+
newZ = (double) z0 + 1.0;
368+
} else if (z1 < z0) {
369+
newZ = (double) z0 + 0.0;
370+
} else {
371+
hasNewZ = false;
372+
}
373+
374+
double stepX = 999.0;
375+
double stepY = 999.0;
376+
double stepZ = 999.0;
377+
378+
379+
double dx = end.xCoord - start.xCoord;
380+
double dy = end.yCoord - start.yCoord;
381+
double dz = end.zCoord - start.zCoord;
382+
383+
if (hasNewX) {
384+
stepX = (newX - start.xCoord) / dx;
385+
}
386+
if (hasNewY) {
387+
stepY = (newY - start.yCoord) / dy;
388+
}
389+
if (hasNewZ) {
390+
stepZ = (newZ - start.zCoord) / dz;
391+
}
392+
if (stepX == -0.0) {
393+
stepX = -1.0E-4;
394+
}
395+
if (stepY == -0.0) {
396+
stepY = -1.0E-4;
397+
}
398+
if (stepZ == -0.0) {
399+
stepZ = -1.0E-4;
400+
}
401+
402+
EnumFacing enumfacing;
403+
if (stepX < stepY && stepX < stepZ) {
404+
enumfacing = x1 > x0 ? EnumFacing.WEST : EnumFacing.EAST;
405+
start = new Vec3(newX, start.yCoord + dy * stepX, start.zCoord + dz * stepX);
406+
} else if (stepY < stepZ) {
407+
enumfacing = y1 > y0 ? EnumFacing.DOWN : EnumFacing.UP;
408+
start = new Vec3(start.xCoord + dx * stepY, newY, start.zCoord + dz * stepY);
409+
} else {
410+
enumfacing = z1 > z0 ? EnumFacing.NORTH : EnumFacing.SOUTH;
411+
start = new Vec3(start.xCoord + dx * stepZ, start.yCoord + dy * stepZ, newZ);
412+
}
413+
x0 = MathHelper.floor_double(start.xCoord) - (enumfacing == EnumFacing.EAST ? 1 : 0);
414+
y0 = MathHelper.floor_double(start.yCoord) - (enumfacing == EnumFacing.UP ? 1 : 0);
415+
z0 = MathHelper.floor_double(start.zCoord) - (enumfacing == EnumFacing.SOUTH ? 1 : 0);
416+
417+
if (mc.theWorld.getBlockState(new BlockPos(x0, y0, z0)).getBlock() != Blocks.air) {
418+
return new BlockPos(x0, y0, z0);
419+
}
420+
}
421+
422+
return null;
423+
}
424+
327425
}

0 commit comments

Comments
 (0)