Skip to content

Commit

Permalink
more work on lasers
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Sep 7, 2024
1 parent a438a6b commit 537b05e
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,112 @@
package com.portingdeadmods.modjam.api.blockentities;

import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.api.blocks.blockentities.LaserBlock;
import com.portingdeadmods.modjam.registries.MJItems;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

import java.util.List;

public abstract class LaserBlockEntity extends ContainerBlockEntity {
private int laserAnimTime;
private static final int MAX_DISTANCE = 16;

private float clientLaserTime;
private final Object2IntMap<Direction> laserDistances;
private int itemTransformTime;
public AABB box;

public LaserBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
super(blockEntityType, blockPos, blockState);
this.laserDistances = new Object2IntOpenHashMap<>();
this.box = new AABB(blockPos);
}

public abstract List<Direction> getLaserInputs();

public abstract List<Direction> getLaserOutputs();

public abstract int getLaserDistance(Direction direction);
public Object2IntMap<Direction> getLaserDistances() {
return laserDistances;
}

public int getLaserAnimTimeDuration() {
return 80;
}

public int getLaserAnimTime() {
return laserAnimTime;
public float getClientLaserTime() {
return clientLaserTime;
}

public float getLaserScale(float partialTick) {
return ((float) this.laserAnimTime + partialTick) / (float) this.getLaserAnimTimeDuration();
return (this.clientLaserTime + partialTick) / (float) this.getLaserAnimTimeDuration();
}

@Override
public void commonTick() {
super.commonTick();
if (laserAnimTime < getLaserAnimTimeDuration()) {
this.laserAnimTime++;
} else {
this.laserAnimTime = 0;
if (level.isClientSide()) {
if (clientLaserTime < getLaserAnimTimeDuration()) {
this.clientLaserTime += 0.5f;
} else {
this.clientLaserTime = 0;
}
}

if (level.getGameTime() % 10 == 0) {
checkConnections();
}
damageLiving();

transmitPower();
}

private void transmitPower() {

}

private void damageLiving() {
for (Direction direction : getLaserOutputs()) {
int distance = this.laserDistances.getInt(direction);
BlockPos pos = worldPosition.above().relative(direction, distance - 1);
Vec3 start = worldPosition.getCenter().subtract(0.1, 0, 0.1);
Vec3 end = pos.getCenter().add(0.1, 0, 0.1);
AABB box = new AABB(start, end);
this.box = box;
ModJam.LOGGER.debug("Distance: {}", box);
List<LivingEntity> livingEntities = level.getEntitiesOfClass(LivingEntity.class, box);
for (LivingEntity livingEntity : livingEntities) {
livingEntity.hurt(level.damageSources().inFire(), 3);
}
}
}

private void checkConnections() {
for (Direction direction : getLaserOutputs()) {
for (int i = 1; i < MAX_DISTANCE; i++) {
BlockPos pos = worldPosition.relative(direction, i);
BlockState state = level.getBlockState(pos);

if (state.getBlock() instanceof LaserBlock) {
laserDistances.put(direction, i);
break;
}

if (!state.canBeReplaced() || i == MAX_DISTANCE - 1) {
laserDistances.put(direction, 0);
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package com.portingdeadmods.modjam.api.blocks.blockentities;

public abstract class LaserBlock extends ContainerBlock{
import com.google.common.collect.ImmutableSet;
import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.registries.MJDataAttachments;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkAccess;

import java.util.Set;

public abstract class LaserBlock extends ContainerBlock {
public LaserBlock(Properties properties) {
super(properties);
}
Expand All @@ -9,4 +19,19 @@ public LaserBlock(Properties properties) {
public boolean tickingEnabled() {
return true;
}

@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
super.onPlace(state, level, pos, oldState, movedByPiston);
ChunkAccess chunk = level.getChunk(pos);
Set<BlockPos> chunkLasers = chunk.getData(MJDataAttachments.CHUNK_LASERS);
ImmutableSet<BlockPos> newSet = ImmutableSet.<BlockPos>builder().addAll(chunkLasers).add(pos).build();
chunk.setData(MJDataAttachments.CHUNK_LASERS, newSet);
ModJam.LOGGER.debug("Chunk lasers: {}", chunkLasers);
}

@Override
public void onRemove(BlockState p_60515_, Level level, BlockPos pos, BlockState p_60518_, boolean p_60519_) {
super.onRemove(p_60515_, level, pos, p_60518_, p_60519_);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.mojang.math.Axis;
import com.portingdeadmods.modjam.api.blockentities.LaserBlockEntity;
import com.portingdeadmods.modjam.utils.LaserRendererHelper;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.util.FastColor;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;
Expand All @@ -20,8 +22,9 @@ public LaserBlockEntityRenderer(BlockEntityRendererProvider.Context ctx) {
@Override
public void render(T blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
BlockPos originPos = blockEntity.getBlockPos();
Object2IntMap<Direction> laserDistances = blockEntity.getLaserDistances();
for (Direction direction : blockEntity.getLaserOutputs()) {
int laserDistance = blockEntity.getLaserDistance(direction);
int laserDistance = laserDistances.getOrDefault(direction, 0);
BlockPos targetPos = originPos.relative(direction, laserDistance);
LaserRendererHelper.renderOuterBeam(blockEntity, originPos, targetPos, direction, poseStack, bufferSource, partialTick);
poseStack.pushPose();
Expand Down Expand Up @@ -52,8 +55,9 @@ public boolean shouldRenderOffScreen(T blockEntity) {
public @NotNull AABB getRenderBoundingBox(T blockEntity) {
BlockPos blockPos = blockEntity.getBlockPos();
AABB box = new AABB(blockPos);
Object2IntMap<Direction> laserDistances = blockEntity.getLaserDistances();
for (Direction direction : blockEntity.getLaserOutputs()) {
int distance = blockEntity.getLaserDistance(direction);
int distance = laserDistances.getOrDefault(direction, 0);
BlockPos pos = blockPos.relative(direction, distance);
box = box.expandTowards(pos.getX(), pos.getY(), pos.getZ());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.portingdeadmods.modjam.client.renderer.blockentities;

import com.mojang.blaze3d.vertex.PoseStack;
import com.portingdeadmods.modjam.api.client.renderer.blockentities.LaserBlockEntityRenderer;
import com.portingdeadmods.modjam.content.blockentities.AquaticCatalystBlockEntity;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;

public class AquaticCatalystBERenderer extends LaserBlockEntityRenderer<AquaticCatalystBlockEntity> {
public AquaticCatalystBERenderer(BlockEntityRendererProvider.Context ctx) {
super(ctx);
}

@Override
public void render(AquaticCatalystBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
super.render(blockEntity, partialTick, poseStack, bufferSource, packedLight, packedOverlay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public List<Direction> getLaserOutputs() {
return List.of();
}

@Override
public int getLaserDistance(Direction direction) {
return 5;
}

@Override
public <T> ImmutableMap<Direction, Pair<IOActions, int[]>> getSidedInteractions(BlockCapability<T, @Nullable Direction> capability) {
return ImmutableMap.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ protected MapCodec<? extends BaseEntityBlock> codec() {
return simpleCodec(AquaticCatalystBlock::new);
}

@Override
public boolean tickingEnabled() {
return false;
}

@Override
public BlockEntityType<? extends ContainerBlockEntity> getBlockEntityType() {
return MJBlockEntityTypes.AQUATIC_CATALYST.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.mojang.serialization.Codec;
import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.utils.CodecUtils;
import net.minecraft.core.BlockPos;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

public class MJDataAttachments {
Expand All @@ -26,4 +30,9 @@ public class MJDataAttachments {
public static final Supplier<AttachmentType<Integer>> HEART_AUGMENTATION = ATTACHMENTS.register(
"heart_augment_id", ()-> AttachmentType.<Integer>builder(()->0).serialize(Codec.INT).copyOnDeath().build()
);

// Lasers in a chunk
public static final Supplier<AttachmentType<Set<BlockPos>>> CHUNK_LASERS = ATTACHMENTS.register(
"chunk_lasers", ()-> AttachmentType.<Set<BlockPos>>builder(()->Set.of()).serialize(CodecUtils.set(BlockPos.CODEC)).build()
);
}
16 changes: 16 additions & 0 deletions src/main/java/com/portingdeadmods/modjam/utils/CodecUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.portingdeadmods.modjam.utils;

import com.mojang.serialization.Codec;

import java.util.Set;

public final class CodecUtils {
public static <E> Codec<Set<E>> set(final Codec<E> elementCodec) {
return set(elementCodec, 0, Integer.MAX_VALUE);
}

public static <E> Codec<Set<E>> set(final Codec<E> elementCodec, final int minSize, final int maxSize) {
return new SetCodec<>(elementCodec, minSize, maxSize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import com.portingdeadmods.modjam.ModJam;
import com.portingdeadmods.modjam.api.blockentities.LaserBlockEntity;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
Expand All @@ -22,7 +21,7 @@ public final class LaserRendererHelper {

public static <T extends LaserBlockEntity> void renderOuterBeam(T blockEntity, BlockPos originPos, BlockPos targetPos, Direction direction, PoseStack poseStack, MultiBufferSource bufferSource, float partialTicks) {
float f = blockEntity.getLaserScale(partialTicks);
float f1 = blockEntity.getLaserAnimTime() + (partialTicks * 24);
float f1 = blockEntity.getClientLaserTime() + (partialTicks * 24);
float f2 = f1 * 0.5F % 1.0F;
float f3 = 0.5f;

Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/portingdeadmods/modjam/utils/SetBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.portingdeadmods.modjam.utils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.mojang.serialization.*;

import java.util.function.UnaryOperator;

public interface SetBuilder<T> {
DynamicOps<T> ops();

DataResult<T> build(T prefix);

SetBuilder<T> add(final T value);

SetBuilder<T> add(final DataResult<T> value);

SetBuilder<T> withErrorsFrom(final DataResult<?> result);

SetBuilder<T> mapError(UnaryOperator<String> onError);

default DataResult<T> build(final DataResult<T> prefix) {
return prefix.flatMap(this::build);
}

default <E> SetBuilder<T> add(final E value, final Encoder<E> encoder) {
return add(encoder.encodeStart(ops(), value));
}

default <E> SetBuilder<T> addAll(final Iterable<E> values, final Encoder<E> encoder) {
values.forEach(v -> encoder.encode(v, ops(), ops().empty()));
return this;
}

final class Builder<T> implements SetBuilder<T> {
private final DynamicOps<T> ops;
private DataResult<ImmutableSet.Builder<T>> builder = DataResult.success(ImmutableSet.builder(), Lifecycle.stable());

public Builder(final DynamicOps<T> ops) {
this.ops = ops;
}

@Override
public DynamicOps<T> ops() {
return ops;
}

@Override
public SetBuilder<T> add(final T value) {
builder = builder.map(b -> b.add(value));
return this;
}

@Override
public SetBuilder<T> add(final DataResult<T> value) {
builder = builder.apply2stable(ImmutableSet.Builder::add, value);
return this;
}

@Override
public SetBuilder<T> withErrorsFrom(final DataResult<?> result) {
builder = builder.flatMap(r -> result.map(v -> r));
return this;
}

@Override
public SetBuilder<T> mapError(final UnaryOperator<String> onError) {
builder = builder.mapError(onError);
return this;
}

@Override
public DataResult<T> build(final T prefix) {
final DataResult<T> result = builder.flatMap(b -> ops.mergeToList(prefix, b.build().asList()));
builder = DataResult.success(ImmutableSet.builder(), Lifecycle.stable());
return result;
}
}
}
Loading

0 comments on commit 537b05e

Please sign in to comment.