diff --git a/src/main/java/de/srendi/advancedperipherals/client/renderer/DistanceDetectorRenderer.java b/src/main/java/de/srendi/advancedperipherals/client/renderer/DistanceDetectorRenderer.java index 2bb048706..8c571b8f5 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/renderer/DistanceDetectorRenderer.java +++ b/src/main/java/de/srendi/advancedperipherals/client/renderer/DistanceDetectorRenderer.java @@ -30,10 +30,12 @@ public DistanceDetectorRenderer(BlockEntityRendererProvider.Context pContext) { public void render(@NotNull DistanceDetectorEntity pBlockEntity, float pPartialTick, @NotNull PoseStack pPoseStack, MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) { if (pBlockEntity.getLaserVisibility()) { float distance = pBlockEntity.getCurrentDistance(); + float[] color = EnumColor.RED.getRgb(); if (distance == -1) { distance = pBlockEntity.getMaxRange(); + color = EnumColor.DARK_RED.getRgb(); } - renderBeaconBeam(pBlockEntity, pPoseStack, pBufferSource, BeaconRenderer.BEAM_LOCATION, pPartialTick, 1, 0, distance + 0.5f, EnumColor.RED.getRgb(), 0.05f, 0.09f); + renderBeaconBeam(pBlockEntity, pPoseStack, pBufferSource, BeaconRenderer.BEAM_LOCATION, pPartialTick, 1, 0, distance + 0.5f, color, 0.05f, 0.09f); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java index 105292147..706579f7e 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java @@ -1,5 +1,7 @@ package de.srendi.advancedperipherals.common.addons.computercraft.peripheral; +import dan200.computercraft.api.lua.IArguments; +import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner; import de.srendi.advancedperipherals.common.blocks.blockentities.DistanceDetectorEntity; @@ -40,21 +42,38 @@ public final boolean ignoresTransparency() { } @LuaFunction - public final void setDetectionMode(int mode) { - mode = Math.min(Math.max(mode, 0), 2); - getPeripheralOwner().tileEntity.setDetectionType(DetectionType.values()[mode]); + public final void setDetectionMode(IArguments args) throws LuaException { + Object mode = args.get(0); + if (mode == null) { + throw new LuaException("arg #1 must provide a mode name or an index between [0, 2]"); + } + DetectionType detectionType; + if (mode instanceof Number modeInd) { + int index = Math.min(Math.max(modeInd.intValue(), 0), 2); + detectionType = DetectionType.values()[index]; + } else if (mode instanceof String modeStr) { + detectionType = switch (modeStr.toUpperCase()) { + case "BLOCK" -> DetectionType.BLOCK; + case "ENTITY" -> DetectionType.ENTITY; + case "BOTH" -> DetectionType.BOTH; + default -> throw new LuaException("Unknown detection mode '" + mode + "'"); + }; + } else { + throw new LuaException("arg #1 must be a string or a number"); + } + getPeripheralOwner().tileEntity.setDetectionType(detectionType); } @LuaFunction public final boolean detectsEntities() { DetectionType detectionType = getPeripheralOwner().tileEntity.getDetectionType(); - return detectionType == DetectionType.ENTITIES || detectionType == DetectionType.BOTH; + return detectionType.detectEntity(); } @LuaFunction public final boolean detectsBlocks() { DetectionType detectionType = getPeripheralOwner().tileEntity.getDetectionType(); - return detectionType == DetectionType.BLOCK || detectionType == DetectionType.BOTH; + return detectionType.detectBlock(); } @LuaFunction @@ -68,7 +87,7 @@ public final double getDistance() { return getPeripheralOwner().tileEntity.getCurrentDistance(); } - @LuaFunction + @LuaFunction(mainThread = true) public final double calculateDistance() { return getPeripheralOwner().tileEntity.calculateAndUpdateDistance(); } @@ -94,9 +113,24 @@ public final double getMaxRange() { } public enum DetectionType { - BLOCK, - ENTITIES, - BOTH + BLOCK(true, false), + ENTITY(false, true), + BOTH(true, true); + + private final boolean block, entity; + + private DetectionType(boolean block, boolean entity) { + this.block = block; + this.entity = entity; + } + + public boolean detectBlock() { + return this.block; + } + + public boolean detectEntity() { + return this.entity; + } } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java b/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java index db5b451cc..3c51cfb97 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java +++ b/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java @@ -4,20 +4,16 @@ import de.srendi.advancedperipherals.common.blocks.base.BaseBlock; import de.srendi.advancedperipherals.common.blocks.base.PeripheralBlockEntity; import de.srendi.advancedperipherals.common.configuration.APConfig; -import de.srendi.advancedperipherals.common.network.APNetworking; import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes; import de.srendi.advancedperipherals.common.util.HitResultUtil; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; -import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.SlabBlock; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.SlabType; import net.minecraft.world.phys.*; import org.jetbrains.annotations.NotNull; @@ -136,9 +132,10 @@ public AABB getRenderBoundingBox() { if (currentDistance == -1) { currentDistance = this.getMaxRange(); } + currentDistance += 1.5f; Direction direction = getBlockState().getValue(BaseBlock.ORIENTATION).front(); - return AABB.ofSize(Vec3.atCenterOf(getBlockPos()), direction.getStepX() * currentDistance + 1, direction.getStepY() * currentDistance + 1, direction.getStepZ() * currentDistance + 1) - .move(direction.getStepX() * currentDistance / 2, direction.getStepY() * currentDistance / 2, direction.getStepZ() * currentDistance / 2); + Vec3 blockPos = Vec3.atCenterOf(getBlockPos()); + return new AABB(blockPos, blockPos.add(direction.getStepX() * currentDistance, direction.getStepY() * currentDistance, direction.getStepZ() * currentDistance)); } @Override @@ -200,9 +197,9 @@ public double calculateAndUpdateDistance() { private HitResult getHitResult(Vec3 to, Vec3 from) { Level level = this.getLevel(); return switch (this.detectionType) { - case ENTITIES -> HitResultUtil.getEntityHitResult(to, from, level); + case ENTITY -> HitResultUtil.getEntityHitResult(to, from, level); case BLOCK -> HitResultUtil.getBlockHitResult(to, from, level, this.ignoreTransparent); - default -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent); + case BOTH -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent); }; } diff --git a/src/main/java/de/srendi/advancedperipherals/common/setup/APBlocks.java b/src/main/java/de/srendi/advancedperipherals/common/setup/APBlocks.java index aab4cdd0b..971a4faf9 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/setup/APBlocks.java +++ b/src/main/java/de/srendi/advancedperipherals/common/setup/APBlocks.java @@ -39,7 +39,7 @@ protected static void register() { public static final RegistryObject GEO_SCANNER = register("geo_scanner", () -> new APBlockEntityBlock<>(APBlockEntityTypes.GEO_SCANNER, false), () -> new APBlockItem(APBlocks.GEO_SCANNER.get(), APConfig.PERIPHERALS_CONFIG.enableGeoScanner)); public static final RegistryObject COLONY_INTEGRATOR = register("colony_integrator", () -> new APBlockEntityBlock<>(APBlockEntityTypes.COLONY_INTEGRATOR, false), () -> new APBlockItem(APBlocks.COLONY_INTEGRATOR.get(), APConfig.PERIPHERALS_CONFIG.enableColonyIntegrator)); public static final RegistryObject NBT_STORAGE = register("nbt_storage", () -> new APBlockEntityBlock<>(APBlockEntityTypes.NBT_STORAGE, false), () -> new APBlockItem(APBlocks.NBT_STORAGE.get(), APConfig.PERIPHERALS_CONFIG.enableNBTStorage)); - public static final RegistryObject 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)); + public static final RegistryObject DISTANCE_DETECTOR = register("distance_detector", () -> new APBlockEntityBlock<>(APBlockEntityTypes.DISTANCE_DETECTOR, true), () -> new APBlockItem(APBlocks.DISTANCE_DETECTOR.get(), APConfig.PERIPHERALS_CONFIG.enableNBTStorage)); private static RegistryObject registerNoItem(String name, Supplier block) { return APRegistration.BLOCKS.register(name, block); diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java index d3d0c9c1e..4231a15df 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java @@ -3,7 +3,6 @@ import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.DistanceDetectorPeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.core.Vec3i; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.ClipContext; @@ -41,7 +40,7 @@ public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, boolean ig return blockResult; } else if (blockResult.getType() == HitResult.Type.MISS) { return entityResult; - } + } double blockDistance = from.distanceToSqr(blockResult.getLocation()); double entityDistance = from.distanceToSqr(entityResult.getLocation());