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 @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -68,7 +87,7 @@ public final double getDistance() {
return getPeripheralOwner().tileEntity.getCurrentDistance();
}

@LuaFunction
@LuaFunction(mainThread = true)
public final double calculateDistance() {
return getPeripheralOwner().tileEntity.calculateAndUpdateDistance();
}
Expand All @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected static void register() {
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));
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));
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));
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));
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));

private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) {
return APRegistration.BLOCKS.register(name, block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Loading