Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ private void doAnchorAura() {

// Find best positions to place new anchors or break existing anchors
int iteratorRange = (int) Math.ceil(Math.max(placeRange.get(), breakRange.get()));
if (place.get()) BlockIterator.iCareAboutAirBlocks();
BlockIterator.register(iteratorRange, iteratorRange, (blockPos, blockState) -> {
boolean isPlacing = blockState.getBlock() != Blocks.RESPAWN_ANCHOR;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void onTick(TickEvent.Pre event) {
crossPool.freeAll(crosses);
crosses.clear();

BlockIterator.iCareAboutAirBlocks();
BlockIterator.register(horizontalRange.get(), verticalRange.get(), (blockPos, blockState) -> {
switch (BlockUtils.isValidMobSpawn(blockPos, blockState, lightLevel.get())) {
case Potential -> crosses.add(crossPool.get().set(blockPos, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void onTickPre(TickEvent.Pre event) {
spawnPool.freeAll(spawns);
spawns.clear();

BlockIterator.iCareAboutAirBlocks();
BlockIterator.register((int) Math.ceil(placeRange.get()), (int) Math.ceil(placeRange.get()), (blockPos, blockState) -> {
BlockUtils.MobSpawn spawn = BlockUtils.isValidMobSpawn(blockPos, blockState, lightLevel.get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import meteordevelopment.orbit.EventHandler;
import meteordevelopment.orbit.EventPriority;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;

import java.util.Iterator;
import java.util.List;
Expand All @@ -31,6 +34,7 @@ public class BlockIterator {
private static final BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
private static int hRadius, vRadius;

private static boolean iCareAboutAirBlocks;
private static boolean disableCurrent;

private BlockIterator() {
Expand All @@ -53,6 +57,7 @@ private static void onTick(TickEvent.Pre event) {

hRadius = 0;
vRadius = 0;
iCareAboutAirBlocks = false;

callbackPool.freeAll(callbacks);
callbacks.clear();
Expand All @@ -62,30 +67,69 @@ private static void onTick(TickEvent.Pre event) {
}

private static void scan(int px, int py, int pz) {
for (int x = px - hRadius; x <= px + hRadius; x++) {
for (int z = pz - hRadius; z <= pz + hRadius; z++) {
for (int y = Math.max(mc.level.getMinY(), py - vRadius); y <= py + vRadius; y++) {
if (callbacks.isEmpty()) return;
if (y >= mc.level.getMaxY()) break;

blockPos.set(x, y, z);
BlockState blockState = mc.level.getBlockState(blockPos);

int dx = Math.abs(x - px);
int dy = Math.abs(y - py);
int dz = Math.abs(z - pz);

Iterator<Callback> iterator = callbacks.iterator();
while (iterator.hasNext()) {
Callback callback = iterator.next();

if (dx <= callback.hRadius && dy <= callback.vRadius && dz <= callback.hRadius) {
disableCurrent = false;
callback.function.accept(blockPos, blockState);

if (disableCurrent) {
iterator.remove();
callbackPool.free(callback);
int x1 = px - hRadius;
int x2 = px + hRadius;
int z1 = pz - hRadius;
int z2 = pz + hRadius;
int y1 = Math.max(mc.level.getMinY(), py - vRadius);
int y2 = Math.min(mc.level.getMaxY(), py + vRadius);

int cx1 = SectionPos.blockToSectionCoord(x1);
int cx2 = SectionPos.blockToSectionCoord(x2);
int cz1 = SectionPos.blockToSectionCoord(z1);
int cz2 = SectionPos.blockToSectionCoord(z2);
int sy1 = mc.level.getSectionIndex(y1);
int sy2 = mc.level.getSectionIndex(y2);

for (int cx = cx1; cx <= cx2; cx++) {
for (int cz = cz1; cz <= cz2; cz++) {
LevelChunk chunk = mc.level.getChunk(cx, cz);
if (chunk.isEmpty()) continue;

int ox = SectionPos.sectionToBlockCoord(cx);
int oz = SectionPos.sectionToBlockCoord(cz);

int ix1 = SectionPos.sectionRelative(Math.max(x1, ox));
int ix2 = SectionPos.sectionRelative(Math.min(x2, ox + 15));
int iz1 = SectionPos.sectionRelative(Math.max(z1, oz));
int iz2 = SectionPos.sectionRelative(Math.min(z2, oz + 15));

for (int sy = sy1; sy <= sy2; sy++) {
LevelChunkSection section = chunk.getSection(sy);
if (!iCareAboutAirBlocks && section.hasOnlyAir()) continue;

int oy = SectionPos.sectionToBlockCoord(mc.level.getSectionYFromSectionIndex(sy));

int iy1 = SectionPos.sectionRelative(Math.max(y1, oy));
int iy2 = SectionPos.sectionRelative(Math.min(y2, oy + 15));

for (int y = iy1; y <= iy2; y++) {
for (int z = iz1; z <= iz2; z++) {
for (int x = ix1; x <= ix2; x++) {
blockPos.set(ox + x, oy + y, oz + z);
BlockState state = section.getBlockState(x, y, z);

int dx = Math.abs(ox + x - px);
int dy = Math.abs(oy + y - py);
int dz = Math.abs(oz + z - pz);

for (Iterator<Callback> it = callbacks.iterator(); it.hasNext();) {
Callback callback = it.next();

if (dx <= callback.hRadius && dy <= callback.vRadius && dz <= callback.hRadius) {
disableCurrent = false;
callback.function.accept(blockPos, state);

if (disableCurrent) {
it.remove();
callbackPool.free(callback);

if (callbacks.isEmpty()) {
return;
}
}
}
}
}
}
}
Expand All @@ -107,6 +151,10 @@ public static void register(int horizontalRadius, int verticalRadius, BiConsumer
callbacks.add(callback);
}

public static void iCareAboutAirBlocks() {
iCareAboutAirBlocks = true;
}

public static void disableCurrent() {
disableCurrent = true;
}
Expand Down
Loading