Skip to content

Commit 1359c97

Browse files
authored
Merge pull request #672 from zyxkad/distant-detector-patch
make calculateDistance run on mainThread
2 parents ab8dca7 + 6f136c8 commit 1359c97

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

src/main/java/de/srendi/advancedperipherals/client/renderer/DistanceDetectorRenderer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ public DistanceDetectorRenderer(BlockEntityRendererProvider.Context pContext) {
3030
public void render(@NotNull DistanceDetectorEntity pBlockEntity, float pPartialTick, @NotNull PoseStack pPoseStack, MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
3131
if (pBlockEntity.getLaserVisibility()) {
3232
float distance = pBlockEntity.getCurrentDistance();
33+
float[] color = EnumColor.RED.getRgb();
3334
if (distance == -1) {
3435
distance = pBlockEntity.getMaxRange();
36+
color = EnumColor.DARK_RED.getRgb();
3537
}
36-
renderBeaconBeam(pBlockEntity, pPoseStack, pBufferSource, BeaconRenderer.BEAM_LOCATION, pPartialTick, 1, 0, distance + 0.5f, EnumColor.RED.getRgb(), 0.05f, 0.09f);
38+
renderBeaconBeam(pBlockEntity, pPoseStack, pBufferSource, BeaconRenderer.BEAM_LOCATION, pPartialTick, 1, 0, distance + 0.5f, color, 0.05f, 0.09f);
3739
}
3840
}
3941

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;
22

3+
import dan200.computercraft.api.lua.IArguments;
4+
import dan200.computercraft.api.lua.LuaException;
35
import dan200.computercraft.api.lua.LuaFunction;
46
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
57
import de.srendi.advancedperipherals.common.blocks.blockentities.DistanceDetectorEntity;
@@ -40,21 +42,38 @@ public final boolean ignoresTransparency() {
4042
}
4143

4244
@LuaFunction
43-
public final void setDetectionMode(int mode) {
44-
mode = Math.min(Math.max(mode, 0), 2);
45-
getPeripheralOwner().tileEntity.setDetectionType(DetectionType.values()[mode]);
45+
public final void setDetectionMode(IArguments args) throws LuaException {
46+
Object mode = args.get(0);
47+
if (mode == null) {
48+
throw new LuaException("arg #1 must provide a mode name or an index between [0, 2]");
49+
}
50+
DetectionType detectionType;
51+
if (mode instanceof Number modeInd) {
52+
int index = Math.min(Math.max(modeInd.intValue(), 0), 2);
53+
detectionType = DetectionType.values()[index];
54+
} else if (mode instanceof String modeStr) {
55+
detectionType = switch (modeStr.toUpperCase()) {
56+
case "BLOCK" -> DetectionType.BLOCK;
57+
case "ENTITY" -> DetectionType.ENTITY;
58+
case "BOTH" -> DetectionType.BOTH;
59+
default -> throw new LuaException("Unknown detection mode '" + mode + "'");
60+
};
61+
} else {
62+
throw new LuaException("arg #1 must be a string or a number");
63+
}
64+
getPeripheralOwner().tileEntity.setDetectionType(detectionType);
4665
}
4766

4867
@LuaFunction
4968
public final boolean detectsEntities() {
5069
DetectionType detectionType = getPeripheralOwner().tileEntity.getDetectionType();
51-
return detectionType == DetectionType.ENTITIES || detectionType == DetectionType.BOTH;
70+
return detectionType.detectEntity();
5271
}
5372

5473
@LuaFunction
5574
public final boolean detectsBlocks() {
5675
DetectionType detectionType = getPeripheralOwner().tileEntity.getDetectionType();
57-
return detectionType == DetectionType.BLOCK || detectionType == DetectionType.BOTH;
76+
return detectionType.detectBlock();
5877
}
5978

6079
@LuaFunction
@@ -68,7 +87,7 @@ public final double getDistance() {
6887
return getPeripheralOwner().tileEntity.getCurrentDistance();
6988
}
7089

71-
@LuaFunction
90+
@LuaFunction(mainThread = true)
7291
public final double calculateDistance() {
7392
return getPeripheralOwner().tileEntity.calculateAndUpdateDistance();
7493
}
@@ -94,9 +113,24 @@ public final double getMaxRange() {
94113
}
95114

96115
public enum DetectionType {
97-
BLOCK,
98-
ENTITIES,
99-
BOTH
116+
BLOCK(true, false),
117+
ENTITY(false, true),
118+
BOTH(true, true);
119+
120+
private final boolean block, entity;
121+
122+
private DetectionType(boolean block, boolean entity) {
123+
this.block = block;
124+
this.entity = entity;
125+
}
126+
127+
public boolean detectBlock() {
128+
return this.block;
129+
}
130+
131+
public boolean detectEntity() {
132+
return this.entity;
133+
}
100134
}
101135

102136
}

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@
44
import de.srendi.advancedperipherals.common.blocks.base.BaseBlock;
55
import de.srendi.advancedperipherals.common.blocks.base.PeripheralBlockEntity;
66
import de.srendi.advancedperipherals.common.configuration.APConfig;
7-
import de.srendi.advancedperipherals.common.network.APNetworking;
87
import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes;
98
import de.srendi.advancedperipherals.common.util.HitResultUtil;
109
import net.minecraft.core.BlockPos;
1110
import net.minecraft.core.Direction;
1211
import net.minecraft.nbt.CompoundTag;
1312
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
14-
import net.minecraft.server.level.ServerLevel;
1513
import net.minecraft.world.level.Level;
16-
import net.minecraft.world.level.block.SlabBlock;
1714
import net.minecraft.world.level.block.entity.BlockEntity;
1815
import net.minecraft.world.level.block.entity.BlockEntityType;
1916
import net.minecraft.world.level.block.state.BlockState;
20-
import net.minecraft.world.level.block.state.properties.SlabType;
2117
import net.minecraft.world.phys.*;
2218

2319
import org.jetbrains.annotations.NotNull;
@@ -136,9 +132,10 @@ public AABB getRenderBoundingBox() {
136132
if (currentDistance == -1) {
137133
currentDistance = this.getMaxRange();
138134
}
135+
currentDistance += 1.5f;
139136
Direction direction = getBlockState().getValue(BaseBlock.ORIENTATION).front();
140-
return AABB.ofSize(Vec3.atCenterOf(getBlockPos()), direction.getStepX() * currentDistance + 1, direction.getStepY() * currentDistance + 1, direction.getStepZ() * currentDistance + 1)
141-
.move(direction.getStepX() * currentDistance / 2, direction.getStepY() * currentDistance / 2, direction.getStepZ() * currentDistance / 2);
137+
Vec3 blockPos = Vec3.atCenterOf(getBlockPos());
138+
return new AABB(blockPos, blockPos.add(direction.getStepX() * currentDistance, direction.getStepY() * currentDistance, direction.getStepZ() * currentDistance));
142139
}
143140

144141
@Override
@@ -200,9 +197,9 @@ public double calculateAndUpdateDistance() {
200197
private HitResult getHitResult(Vec3 to, Vec3 from) {
201198
Level level = this.getLevel();
202199
return switch (this.detectionType) {
203-
case ENTITIES -> HitResultUtil.getEntityHitResult(to, from, level);
200+
case ENTITY -> HitResultUtil.getEntityHitResult(to, from, level);
204201
case BLOCK -> HitResultUtil.getBlockHitResult(to, from, level, this.ignoreTransparent);
205-
default -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent);
202+
case BOTH -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent);
206203
};
207204
}
208205

src/main/java/de/srendi/advancedperipherals/common/setup/APBlocks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected static void register() {
3939
public static final RegistryObject<Block> GEO_SCANNER = register("geo_scanner", () -> new APBlockEntityBlock<>(APBlockEntityTypes.GEO_SCANNER, false), () -> new APBlockItem(APBlocks.GEO_SCANNER.get(), APConfig.PERIPHERALS_CONFIG.enableGeoScanner));
4040
public static final RegistryObject<Block> COLONY_INTEGRATOR = register("colony_integrator", () -> new APBlockEntityBlock<>(APBlockEntityTypes.COLONY_INTEGRATOR, false), () -> new APBlockItem(APBlocks.COLONY_INTEGRATOR.get(), APConfig.PERIPHERALS_CONFIG.enableColonyIntegrator));
4141
public static final RegistryObject<Block> NBT_STORAGE = register("nbt_storage", () -> new APBlockEntityBlock<>(APBlockEntityTypes.NBT_STORAGE, false), () -> new APBlockItem(APBlocks.NBT_STORAGE.get(), APConfig.PERIPHERALS_CONFIG.enableNBTStorage));
42-
public static final RegistryObject<Block> DISTANCE_DETECTOR = register("distance_detector", () -> new APBlockEntityBlock<>(APBlockEntityTypes.DISTANCE_DETECTOR, BlockBehaviour.Properties.of(Material.METAL).noOcclusion(), true), () -> new APBlockItem(APBlocks.DISTANCE_DETECTOR.get(), APConfig.PERIPHERALS_CONFIG.enableNBTStorage));
42+
public static final RegistryObject<Block> DISTANCE_DETECTOR = register("distance_detector", () -> new APBlockEntityBlock<>(APBlockEntityTypes.DISTANCE_DETECTOR, true), () -> new APBlockItem(APBlocks.DISTANCE_DETECTOR.get(), APConfig.PERIPHERALS_CONFIG.enableNBTStorage));
4343

4444
private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) {
4545
return APRegistration.BLOCKS.register(name, block);

src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.DistanceDetectorPeripheral;
44
import net.minecraft.core.BlockPos;
55
import net.minecraft.core.Direction;
6-
import net.minecraft.core.Vec3i;
76
import net.minecraft.world.entity.Entity;
87
import net.minecraft.world.level.BlockGetter;
98
import net.minecraft.world.level.ClipContext;
@@ -41,7 +40,7 @@ public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, boolean ig
4140
return blockResult;
4241
} else if (blockResult.getType() == HitResult.Type.MISS) {
4342
return entityResult;
44-
}
43+
}
4544

4645
double blockDistance = from.distanceToSqr(blockResult.getLocation());
4746
double entityDistance = from.distanceToSqr(entityResult.getLocation());

0 commit comments

Comments
 (0)