Hopefully fix some weirdness

This commit is contained in:
grimmauld 2021-04-23 21:35:01 +02:00
parent a0f01b733d
commit 0c89eb0279
5 changed files with 62 additions and 11 deletions

View file

@ -34,7 +34,7 @@ public class BeamSegment {
private final ILightHandler handler;
@Nullable
private Quaternion beaconBeamModifier;
private int length;
private double length;
public BeamSegment(ILightHandler handler, @Nonnull float[] color, Vector3d start, Vector3d direction) {
this.handler = handler;
@ -86,7 +86,7 @@ public class BeamSegment {
return this.colors;
}
public int getLength() {
public double getLength() {
return this.length;
}
@ -153,4 +153,8 @@ public class BeamSegment {
stacker.pop()
.pop();
}
public void setLength(double length) {
this.length = length;
}
}

View file

@ -7,14 +7,19 @@ import java.util.stream.Stream;
import javax.annotation.Nullable;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.utility.BeaconHelper;
import com.simibubi.create.foundation.utility.VecHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.item.DyeColor;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
@ -34,31 +39,57 @@ public interface ILightHandler {
return beam;
float[] segmentColor = parent == null ? colorComponentValues : parent.getColorAt(testBlockPos);
Vector3d direction = VecHelper.step(beamDirection);
Vector3d direction = VecHelper.step(beamDirection).normalize();
Vector3d testPos = VecHelper.getCenterOf(testBlockPos);
BeamSegment segment = new BeamSegment(this, segmentColor, testPos, direction);
beam.add(segment);
BlockPos lastChecked = testBlockPos;
Vector3d rayEnd = testPos.add(direction.scale(getMaxScanRange()));
for (int i = 0; i < getMaxScanRange(); i++) {
testPos = testPos.add(direction); // check next block
testBlockPos = new BlockPos(testPos.x, testPos.y, testPos.z);
BlockRayTraceResult raytrace = world
.rayTraceBlocks(new RayTraceContext(testPos, rayEnd, RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, null));
testBlockPos = raytrace.getPos();
if (raytrace.getType() == RayTraceResult.Type.MISS || !world.isBlockPresent(testBlockPos)) {
segment.setLength(getMaxScanRange());
break;
}
double scale =Math.sqrt(lastChecked.distanceSq(testBlockPos.getX(), testBlockPos.getY(), testBlockPos.getZ(), false));
testPos = testPos.add(direction.scale(Math.max(0, scale - 1)));
segment.setLength(segment.getLength() + Math.max(scale / direction.length() - 1, 0));
lastChecked = testBlockPos;
BlockState testState = world.getBlockState(testBlockPos);
Block testBlock = testState.getBlock();
float[] newColor = BeaconHelper.getBeaconColorFor(testState.getBlock());
TileEntity te = testState.hasTileEntity() ? world.getTileEntity(testBlockPos) : null;
ILightHandler lightHandler = te instanceof ILightHandlerProvider ? ((ILightHandlerProvider) te).getHandler() : null;
ILightHandler lightHandler = null;
// if possible, don't read for tile entities as often. Only read TEs that we actually know exist and are relevant.
if (testBlock instanceof ITE && ILightHandlerProvider.class.isAssignableFrom(((ITE<?>) testBlock).getTileEntityClass())) {
TileEntity te = world.getTileEntity(testBlockPos);
if (te instanceof ILightHandlerProvider)
lightHandler = ((ILightHandlerProvider) te).getHandler();
}
if (lightHandler != this)
beam.addListener(lightHandler);
if (newColor == null) {
if (testState.getOpacity(world, testBlockPos) >= 15 && testState.getBlock() != Blocks.BEDROCK || (lightHandler != null && lightHandler.absorbsLight())) {
if (testState.getOpacity(world, testBlockPos) >= 15 && testBlock != Blocks.BEDROCK || (lightHandler != null && lightHandler.absorbsLight())) {
break;
}
} else if (!Arrays.equals(segmentColor, newColor)) {
segmentColor = new float[]{(segment.colors[0] + newColor[0]) / 2.0F, (segment.colors[1] + newColor[1]) / 2.0F, (segment.colors[2] + newColor[2]) / 2.0F};
segment = new BeamSegment(this, newColor, testPos, direction);
segment = new BeamSegment(this, newColor, segment.getStart().add(segment.getDirection().scale(segment.getLength())), direction);
beam.add(segment);
continue;
}

View file

@ -3,6 +3,7 @@ package com.simibubi.create.content.optics.aligner;
import java.util.List;
import com.simibubi.create.content.optics.ILightHandler;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.tileEntity.SmartTileEntity;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
@ -11,7 +12,7 @@ import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public class AlignerTileEntity extends SmartTileEntity implements ILightHandler.ILightHandlerProvider {
public class AlignerTileEntity extends SmartTileEntity implements ILightHandler.ILightHandlerProvider, ITE<AlignerTileEntity> {
protected AlignerBehaviour aligner;
public AlignerTileEntity(TileEntityType<?> tileEntityTypeIn) {
@ -40,4 +41,9 @@ public class AlignerTileEntity extends SmartTileEntity implements ILightHandler.
public double getMaxRenderDistanceSquared() {
return 256.0D;
}
@Override
public Class<AlignerTileEntity> getTileEntityClass() {
return AlignerTileEntity.class;
}
}

View file

@ -4,6 +4,7 @@ import java.util.List;
import com.simibubi.create.content.optics.ILightHandler;
import com.simibubi.create.content.optics.behaviour.LightEmittingBehaviour;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.tileEntity.SmartTileEntity;
import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour;
@ -12,7 +13,7 @@ import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public class RadiantBeaconTileEntity extends SmartTileEntity implements ILightHandler.ILightHandlerProvider {
public class RadiantBeaconTileEntity extends SmartTileEntity implements ILightHandler.ILightHandlerProvider, ITE<RadiantBeaconTileEntity> {
LightEmittingBehaviour<RadiantBeaconTileEntity> lightEmittingBehaviour;
public RadiantBeaconTileEntity(TileEntityType<?> tileEntityTypeIn) {
@ -41,4 +42,9 @@ public class RadiantBeaconTileEntity extends SmartTileEntity implements ILightHa
public double getMaxRenderDistanceSquared() {
return 256.0D;
}
@Override
public Class<RadiantBeaconTileEntity> getTileEntityClass() {
return RadiantBeaconTileEntity.class;
}
}

View file

@ -227,4 +227,8 @@ public class VecHelper {
}
return n;
}
public static Vector3d getRandom() {
return new Vector3d(Math.random(), Math.random(), Math.random()).normalize();
}
}