From 3bdc361fdf71b857e9e4c648393f70910b8a4b9f Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 15:17:22 -0700 Subject: [PATCH 01/10] make calculateDistance run on mainThread --- .../computercraft/peripheral/DistanceDetectorPeripheral.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..ae6d22f20 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 @@ -68,7 +68,7 @@ public final double getDistance() { return getPeripheralOwner().tileEntity.getCurrentDistance(); } - @LuaFunction + @LuaFunction(mainThread = true) public final double calculateDistance() { return getPeripheralOwner().tileEntity.calculateAndUpdateDistance(); } From 9a504837ebe6ca0c0d0d78405a063c3f4123a427 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 15:37:54 -0700 Subject: [PATCH 02/10] allow setDetectionMode also accept string as mode --- .../DistanceDetectorPeripheral.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) 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 ae6d22f20..79ef8581d 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 @@ -40,9 +40,26 @@ 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 "ENTITIES" -> DetectionType.ENTITIES; + 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 From 4dcdc9eb0c883a371d48a24e930694992df9a038 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:21:55 -0700 Subject: [PATCH 03/10] fix styles --- .../common/blocks/blockentities/DistanceDetectorEntity.java | 4 ---- .../srendi/advancedperipherals/common/util/HitResultUtil.java | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) 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..b2bdd6b4f 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; 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()); From 02d88edcbd116d11dd465bb878c9ab627f644ef1 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:25:45 -0700 Subject: [PATCH 04/10] add missing semicolons --- .../computercraft/peripheral/DistanceDetectorPeripheral.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 79ef8581d..da5994bb9 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 @@ -48,14 +48,14 @@ public final void setDetectionMode(IArguments args) throws LuaException { DetectionType detectionType; if (mode instanceof Number modeInd) { int index = Math.min(Math.max(modeInd.intValue(), 0), 2); - detectionType = DetectionType.values()[index] + detectionType = DetectionType.values()[index]; } else if (mode instanceof String modeStr) { detectionType = switch (modeStr.toUpperCase()) { case "BLOCK" -> DetectionType.BLOCK; case "ENTITIES" -> DetectionType.ENTITIES; 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"); } From 1a08c6a5bc1a4b711bb07c202fe35163a26783bc Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:26:38 -0700 Subject: [PATCH 05/10] add missing imports --- .../computercraft/peripheral/DistanceDetectorPeripheral.java | 2 ++ 1 file changed, 2 insertions(+) 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 da5994bb9..362ed4ec1 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; From 0c0eb0cea9bcbaae0828ea0c7b87eac46b728c2a Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:31:55 -0700 Subject: [PATCH 06/10] rename DetectionType.ENTITIES -> DetectionType.ENTITY --- .../DistanceDetectorPeripheral.java | 27 ++++++++++++++----- .../blockentities/DistanceDetectorEntity.java | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) 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 362ed4ec1..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 @@ -54,7 +54,7 @@ public final void setDetectionMode(IArguments args) throws LuaException { } else if (mode instanceof String modeStr) { detectionType = switch (modeStr.toUpperCase()) { case "BLOCK" -> DetectionType.BLOCK; - case "ENTITIES" -> DetectionType.ENTITIES; + case "ENTITY" -> DetectionType.ENTITY; case "BOTH" -> DetectionType.BOTH; default -> throw new LuaException("Unknown detection mode '" + mode + "'"); }; @@ -67,13 +67,13 @@ public final void setDetectionMode(IArguments args) throws LuaException { @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 @@ -113,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 b2bdd6b4f..78176d017 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 @@ -196,7 +196,7 @@ 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); }; From 44ff763743007965e2a8682daabacd2bcfcec25f Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:49:48 -0700 Subject: [PATCH 07/10] remove noOcclusion for distance detector --- .../de/srendi/advancedperipherals/common/setup/APBlocks.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 763e0679ca1f6c77996c535f4fc4dbb2596cb6d5 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 17:58:24 -0700 Subject: [PATCH 08/10] make undetected color to dark red --- .../client/renderer/DistanceDetectorRenderer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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); } } From bea9dd3675be8a905d07719806f373e2b0cc369d Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 18:14:18 -0700 Subject: [PATCH 09/10] fix render bounding box --- .../common/blocks/blockentities/DistanceDetectorEntity.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 78176d017..c68caf610 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 @@ -132,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 From 6f136c8f35382bb54bd9536d88e133ec5579cfd2 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Dec 2024 18:16:46 -0700 Subject: [PATCH 10/10] default -> case BOTH --- .../common/blocks/blockentities/DistanceDetectorEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c68caf610..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 @@ -199,7 +199,7 @@ private HitResult getHitResult(Vec3 to, Vec3 from) { return switch (this.detectionType) { 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); }; }