Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash during active scan with no more world #25

Merged
merged 1 commit into from
Oct 7, 2024
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
4 changes: 2 additions & 2 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Next steps

- [ ] only one setting at a time is saved (bug: #20, pr: #24)
- [ ] `NullPointerException` when a scanner is active and world disappears (bug: #21, pr: ?)
- [x] only one setting at a time is saved (bug: #20, pr: #24)
- [x] `NullPointerException` when a scanner is active and world disappears (bug: #21, pr: #25)
- [ ] clumps of blocks (bug for internal walls: #22, issue: #23, pr: ?)
- [ ] bounds for frustum
- [ ] walls no longer flicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class FabricAnimationHost extends AbstractAnimatorHost {
public void initialize(Sonar source) {
this.source = source;
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
if (client.world == null)
return;

this.tick(Clock.currentTimeMillis());
this.source.remove(this.source.oldEchoes(new MinecraftClientCore(client)));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

public class MinecraftClientCore implements ClientCore {
private final MinecraftClient client;
Expand All @@ -21,16 +22,20 @@ public MinecraftClientCore(MinecraftClient client) {
}

@Override
public BlockInfo getBlockInfo(V3i pos) {
assert client.world != null;
public @Nullable BlockInfo getBlockInfo(V3i pos) {
if (client.world == null) {
return null;
}
BlockState state = client.world.getBlockState(new BlockPos(Minecraft.vec3iOf(pos)));
Block block = state == null ? null : state.getBlock();
return new MinecraftBlockInfo(state, block);
}

@Override
public V3i getPlayerPos() {
assert client.player != null;
public @Nullable V3i getPlayerPos() {
if (client.player == null) {
return null;
}
return Minecraft.v3iOf(client.player.getBlockPos());
}

Expand Down
5 changes: 3 additions & 2 deletions scanner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ repositories {
}

dependencies {
compileOnly 'org.jetbrains:annotations:16.0.2'
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'org.joml:joml:1.10.8'
implementation 'org.slf4j:slf4j-api:1.7.+'
implementation 'org.slf4j:slf4j-log4j12:1.7.29'
implementation 'org.slf4j:slf4j-api:2.0.+'
implementation 'org.slf4j:slf4j-log4j13:1.0.+'
implementation 'org.apache.commons:commons-text:1.12.0'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import com.midnightbits.scanner.rt.math.V3i;
import com.midnightbits.scanner.rt.text.Text;
import org.jetbrains.annotations.Nullable;

public interface ClientCore {
BlockInfo getBlockInfo(V3i pos);
@Nullable BlockInfo getBlockInfo(V3i pos);

V3i getPlayerPos();
@Nullable V3i getPlayerPos();

float getCameraPitch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public Predicate<BlockEcho> oldEchoes(ClientCore client) {
}

final var info = client.getBlockInfo(block.position());
if (info == null) {
return true;
}
return !block.id().equals(info.getId());
});
}
Expand Down
13 changes: 11 additions & 2 deletions scanner/src/main/java/com/midnightbits/scanner/sonar/Sonar.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public boolean sendPing(ClientCore client, SlicePacer pacer, ScanWaveConsumer wa
if (reflections != null)
return false;
reflections = Reflections.fromPlayerPov(client, blockDistance, blockRadius);
if (reflections == null)
return false;
pacer.registerCallback((now) -> {
if (!reflections.hasNextSlice()) {
reflections = null;
Expand Down Expand Up @@ -137,6 +139,9 @@ public void processSlice(ScanWaveConsumer waveConsumer, Set<Id> blocks) {

for (final var pos : slice.items()) {
final var info = client.getBlockInfo(pos);
if (info == null) {
break;
}
if (info.isAir()) {
LOGGER.debug("({}) is air", pos);
continue;
Expand Down Expand Up @@ -167,8 +172,12 @@ public void processSlice(ScanWaveConsumer waveConsumer, Set<Id> blocks) {
waveConsumer.advance(slice.items(), echoes.stream().toList());
}

static Reflections fromPlayerPov(ClientCore client, int blockDistance, int blockRadius) {
return new Reflections(client, client.getPlayerPos(), blockDistance, blockRadius);
static @Nullable Reflections fromPlayerPov(ClientCore client, int blockDistance, int blockRadius) {
final var pos = client.getPlayerPos();
if (pos == null) {
return null;
}
return new Reflections(client, pos, blockDistance, blockRadius);
}

private ConeOfBlocks coneOfBlocksFromCamera(int blockDistance, int blockRadius) {
Expand Down
Loading