diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java b/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java index aae382d..eaac6bf 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java @@ -1,20 +1,27 @@ package com.oitsjustjose.charged_explosives.client; import com.oitsjustjose.charged_explosives.client.network.ClientOpenGuiPacket; +import com.oitsjustjose.charged_explosives.client.render.BlockDestroyRenderer; import com.oitsjustjose.charged_explosives.common.CommonProxy; import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket; import com.oitsjustjose.charged_explosives.common.network.UpdateNbtPacket; +import net.minecraft.core.BlockPos; +import net.minecraft.util.Tuple; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.network.NetworkDirection; import java.util.Optional; public class ClientProxy extends CommonProxy { + private BlockDestroyRenderer bdRenderer = new BlockDestroyRenderer(); + @Override public void init() { CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, OpenGuiPacket.class, OpenGuiPacket::encode, OpenGuiPacket::decode, ClientOpenGuiPacket::handleClient); CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, UpdateNbtPacket.class, UpdateNbtPacket::encode, UpdateNbtPacket::decode, UpdateNbtPacket::handle, Optional.of(NetworkDirection.PLAY_TO_SERVER)); + MinecraftForge.EVENT_BUS.register(bdRenderer); } @Override @@ -26,4 +33,14 @@ public class ClientProxy extends CommonProxy { public void openExplosiveGui(Player player, ItemStack stack) { super.openExplosiveGui(player, stack); } + + @Override + public void addBlockToRender(Tuple pos) { + bdRenderer.selectBlock(pos); + } + + @Override + public void removeBlockFromRender(Tuple pos) { + bdRenderer.unselectBlock(pos); + } } \ No newline at end of file diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java b/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java similarity index 99% rename from src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java rename to src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java index ae51ec8..27c083e 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java @@ -1,4 +1,4 @@ -package com.oitsjustjose.charged_explosives.client; +package com.oitsjustjose.charged_explosives.client.gui; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/network/ClientOpenGuiPacket.java b/src/main/java/com/oitsjustjose/charged_explosives/client/network/ClientOpenGuiPacket.java index ba3788f..11687e0 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/network/ClientOpenGuiPacket.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/network/ClientOpenGuiPacket.java @@ -1,6 +1,6 @@ package com.oitsjustjose.charged_explosives.client.network; -import com.oitsjustjose.charged_explosives.client.ExplosionConfigScreen; +import com.oitsjustjose.charged_explosives.client.gui.ExplosionConfigScreen; import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket; import net.minecraft.client.Minecraft; import net.minecraftforge.fml.LogicalSide; diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockDestroyRenderer.java b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockDestroyRenderer.java new file mode 100644 index 0000000..9fd9dbb --- /dev/null +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockDestroyRenderer.java @@ -0,0 +1,93 @@ +package com.oitsjustjose.charged_explosives.client.render; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.math.Matrix4f; +import com.oitsjustjose.charged_explosives.common.blocks.ChargedExplosiveBlock; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.core.BlockPos; +import net.minecraft.util.Tuple; +import net.minecraft.world.phys.HitResult; +import net.minecraft.world.phys.Vec3; +import net.minecraftforge.client.event.RenderLevelStageEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +import java.util.HashSet; +import java.util.Set; + +public class BlockDestroyRenderer { + protected Set> highlightedBlocks = new HashSet<>(); + + public void selectBlock(Tuple corners) { + this.highlightedBlocks.add(corners); + } + + public void unselectBlock(Tuple corners) { + this.highlightedBlocks.removeIf(pair -> pair.getA().equals(corners.getA()) && pair.getB().equals(corners.getB())); + } + + @SubscribeEvent + public void renderWorldLastEvent(RenderLevelStageEvent event) { + this.renderSelectedBlocks(event); + } + + protected void renderSelectedBlocks(RenderLevelStageEvent event) { + if (this.highlightedBlocks.isEmpty() || event.getStage() != RenderLevelStageEvent.Stage.AFTER_TRANSLUCENT_BLOCKS) { + return; + } + + Minecraft mc = Minecraft.getInstance(); + + this.highlightedBlocks.stream().filter(x -> { + return x.getA().distToCenterSqr(mc.gameRenderer.getMainCamera().getPosition()) < 100D; + }).forEach(pos -> { + PoseStack stack = event.getPoseStack(); + MultiBufferSource.BufferSource buf = mc.renderBuffers().bufferSource(); + VertexConsumer builder = buf.getBuffer(BlockOutlineRenderType.OVERLAY_LINES); + + stack.pushPose(); + + Vec3 proj = mc.gameRenderer.getMainCamera().getPosition(); + stack.translate(-proj.x, -proj.y, -proj.z); + + Matrix4f p = stack.last().pose(); + buildBlockOutline(builder, p, pos.getA(), pos.getB()); + + stack.popPose(); + RenderSystem.disableDepthTest(); + buf.endBatch(BlockOutlineRenderType.OVERLAY_LINES); + }); + } + + public static void buildBlockOutline(VertexConsumer buffer, Matrix4f positionMatrix, BlockPos start, BlockPos end) { + buffer.vertex(positionMatrix, start.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + + buffer.vertex(positionMatrix, start.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + + buffer.vertex(positionMatrix, end.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), start.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), end.getY(), start.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + + buffer.vertex(positionMatrix, start.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, end.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), start.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + buffer.vertex(positionMatrix, start.getX(), end.getY(), end.getZ()).color(1F, 0F, 0F, 1F).endVertex(); + } +} diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockOutlineRenderType.java b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockOutlineRenderType.java new file mode 100644 index 0000000..b0ca8f4 --- /dev/null +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockOutlineRenderType.java @@ -0,0 +1,28 @@ +package com.oitsjustjose.charged_explosives.client.render; + +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.VertexFormat; +import net.minecraft.client.renderer.RenderType; + +import java.util.OptionalDouble; + + +public class BlockOutlineRenderType extends RenderType { + private static final LineStateShard THICK_LINES = new LineStateShard(OptionalDouble.of(4.0D)); + public static final RenderType OVERLAY_LINES = create("overlay_lines", + DefaultVertexFormat.POSITION_COLOR, VertexFormat.Mode.LINES, 256, true, false, + CompositeState.builder().setLineState(THICK_LINES) + .setLayeringState(VIEW_OFFSET_Z_LAYERING) + .setShaderState(RENDERTYPE_LINES_SHADER) + .setTransparencyState(TRANSLUCENT_TRANSPARENCY) + .setTextureState(NO_TEXTURE) + .setDepthTestState(NO_DEPTH_TEST) + .setCullState(NO_CULL) + .setLightmapState(NO_LIGHTMAP) + .setWriteMaskState(COLOR_WRITE) + .createCompositeState(false)); + + public BlockOutlineRenderType(String name, VertexFormat vertexFormat, VertexFormat.Mode drawMode, int bufferSize, boolean useDelegate, boolean needsSorting, Runnable setupTaskIn, Runnable clearTaskIn) { + super(name, vertexFormat, drawMode, bufferSize, useDelegate, needsSorting, setupTaskIn, clearTaskIn); + } +} diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/CommonProxy.java b/src/main/java/com/oitsjustjose/charged_explosives/common/CommonProxy.java index bd31fde..f7fd6f2 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/CommonProxy.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/CommonProxy.java @@ -3,7 +3,9 @@ package com.oitsjustjose.charged_explosives.common; import com.oitsjustjose.charged_explosives.common.network.NetworkManager; import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket; import com.oitsjustjose.charged_explosives.common.network.UpdateNbtPacket; +import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.Tuple; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraftforge.network.NetworkDirection; @@ -32,4 +34,10 @@ public class CommonProxy { public void updateItemNbt(ItemStack stack) { // NOOP } + + public void addBlockToRender(Tuple pos) { + } + + public void removeBlockFromRender(Tuple pos) { + } } \ No newline at end of file diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/blocks/ChargedExplosiveBlock.java b/src/main/java/com/oitsjustjose/charged_explosives/common/blocks/ChargedExplosiveBlock.java index ae10640..c74e5a8 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/blocks/ChargedExplosiveBlock.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/blocks/ChargedExplosiveBlock.java @@ -1,11 +1,10 @@ package com.oitsjustjose.charged_explosives.common.blocks; import com.oitsjustjose.charged_explosives.ChargedExplosives; -import com.oitsjustjose.charged_explosives.common.registry.Registry; +import com.oitsjustjose.charged_explosives.common.items.ChargedExplosiveItem; import com.oitsjustjose.charged_explosives.common.tile.ChargedExplosiveBlockEntity; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.sounds.SoundEvents; @@ -17,7 +16,6 @@ import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; -import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -28,7 +26,6 @@ import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.AttachFace; -import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.phys.BlockHitResult; @@ -53,12 +50,11 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc public ChargedExplosiveBlock() { super(BlockBehaviour.Properties.of(Material.EXPLOSIVE, MaterialColor.COLOR_GREEN).dynamicShape().noOcclusion().isValidSpawn((w, x, y, z) -> false).isRedstoneConductor((x, y, z) -> false).isSuffocating((x, y, z) -> false).isViewBlocking((x, y, z) -> false)); - this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(FACE, AttachFace.WALL)); } @Override - public @NotNull VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) { + public @NotNull VoxelShape getShape(BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) { Direction direction = state.getValue(FACING); switch (state.getValue(FACE)) { case FLOOR: @@ -87,39 +83,14 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc } @Override - public @NotNull VoxelShape getCollisionShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) { - Direction direction = state.getValue(FACING); - switch (state.getValue(FACE)) { - case FLOOR: - if (direction.getAxis() == Direction.Axis.X) { - return FLOOR_AABB_X; - } - - return FLOOR_AABB_Z; - case WALL: - return switch (direction) { - case EAST -> EAST_AABB; - case WEST -> WEST_AABB; - case SOUTH -> SOUTH_AABB; - case DOWN -> FLOOR_AABB_X; - case UP -> CEILING_AABB_X; - case NORTH -> NORTH_AABB; - }; - case CEILING: - default: - if (direction.getAxis() == Direction.Axis.X) { - return CEILING_AABB_X; - } else { - return CEILING_AABB_Z; - } - } + public @NotNull VoxelShape getCollisionShape(BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) { + return this.getShape(state, level, pos, context); } protected void createBlockStateDefinition(StateDefinition.Builder Builder) { Builder.add(FACING, FACE); } - @Override public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, LivingEntity placer, @NotNull ItemStack stack) { super.setPlacedBy(level, pos, state, placer, stack); @@ -129,18 +100,15 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc } CompoundTag tag = stack.getOrCreateTag().copy(); - if (!tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth")) { - tag.putInt("explosionWidth", 0); - tag.putInt("explosionHeight", 0); - tag.putInt("explosionDepth", 0); - } - be.load(tag); } @Override public void onRemove(BlockState oldState, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean moving) { if (!newState.is(oldState.getBlock())) { + if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { + ChargedExplosives.getInstance().proxy.removeBlockFromRender(cebe.getCorners()); + } level.removeBlockEntity(pos); } super.onRemove(oldState, level, pos, newState, moving); @@ -170,108 +138,24 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc @Override public InteractionResult use(@Nonnull BlockState state, @Nonnull Level level, @Nonnull BlockPos pos, @Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull BlockHitResult hit) { - if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { - this.explode(level, pos, state, cebe.getExplosionWidth(), cebe.getExplosionHeight(), cebe.getExplosionDepth()); + if (level.isClientSide()) { + player.swing(hand); } + this.explode(level, pos); return InteractionResult.CONSUME; } - private void explode(Level level, BlockPos pos, BlockState state, int width, int height, int depth) { - Direction direction = state.getValue(FACING); - AttachFace face = state.getValue(FACE); - int startX, endX, startY, endY, startZ, endZ; - startX = endX = startY = endY = startZ = endZ = 0; - switch (face) { - case FLOOR -> { - if (direction.getAxis() == Direction.Axis.X) { - startX = pos.getX() - (int) Math.floor(height / 2F); - endX = pos.getX() + (int) Math.ceil(height / 2F); - startY = pos.getY() - depth; - endY = pos.getY(); - startZ = pos.getZ() - (int) Math.floor(width / 2F) + 1; - endZ = pos.getZ() + (int) Math.ceil(width / 2F) + 1; - } else { - startX = pos.getX() - (int) Math.floor(width / 2F); - endX = pos.getX() + (int) Math.ceil(width / 2F); - startY = pos.getY() - depth; - endY = pos.getY(); - startZ = pos.getZ() - (int) Math.floor(height / 2F); - endZ = pos.getZ() + (int) Math.ceil(height / 2F); + private void explode(Level level, BlockPos pos) { + if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { + level.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 1.0F); + cebe.getExplosions().forEach(p -> { + level.setBlockAndUpdate(p, Blocks.AIR.defaultBlockState()); + if (level.isClientSide()) { // TODO: packetize me captain + level.addParticle(ParticleTypes.EXPLOSION, (double) p.getX() + 0.5D, (double) p.getY() + 0.5D, (double) p.getZ() + 0.5D, 1.0D, 0.0D, 0.0D); + ChargedExplosives.getInstance().proxy.removeBlockFromRender(cebe.getCorners()); } - } - case WALL -> { - switch (direction) { - case NORTH -> { - startX = pos.getX() - (int) Math.floor(width / 2F); - endX = pos.getX() + (int) Math.ceil(width / 2F); - startY = pos.getY() - (int) Math.floor(height / 2F); - endY = pos.getY() + (int) Math.ceil(height / 2F); - startZ = pos.getZ(); - endZ = pos.getZ() + depth; - } - case SOUTH -> { - startX = pos.getX() - (int) Math.ceil(width / 2F) + 1; - endX = pos.getX() + (int) Math.floor(width / 2F) + 1; - startY = pos.getY() - (int) Math.ceil(height / 2F); - endY = pos.getY() + (int) Math.floor(height / 2F); - startZ = pos.getZ() - depth; - endZ = pos.getZ(); - } - case EAST -> { - startX = pos.getX() - depth; - endX = pos.getX(); - startY = pos.getY() - (int) Math.floor(height / 2F); - endY = pos.getY() + (int) Math.ceil(height / 2F); - startZ = pos.getZ() - (int) Math.floor(width / 2F); - endZ = pos.getZ() + (int) Math.ceil(width / 2F); - } - case WEST -> { - startX = pos.getX(); - endX = pos.getX() + depth; - startY = pos.getY() - (int) Math.ceil(height / 2F); - endY = pos.getY() + (int) Math.floor(height / 2F); - startZ = pos.getZ() - (int) Math.ceil(width / 2F) + 1; - endZ = pos.getZ() + (int) Math.floor(width / 2F) + 1; - } - default -> { - break; - } - } - } - case CEILING -> { - if (direction.getAxis() == Direction.Axis.X) { - startX = pos.getX() - (int) Math.floor(height / 2F); - endX = pos.getX() + (int) Math.ceil(height / 2F); - startY = pos.getY(); - endY = pos.getY() + depth; - startZ = pos.getZ() - (int) Math.floor(width / 2F); - endZ = pos.getZ() + (int) Math.ceil(width / 2F); - } else { - startX = pos.getX() - (int) Math.floor(width / 2F) + 1; - endX = pos.getX() + (int) Math.ceil(width / 2F) + 1; - startY = pos.getY(); - endY = pos.getY() + depth; - startZ = pos.getZ() - (int) Math.floor(height / 2F); - endZ = pos.getZ() + (int) Math.ceil(height / 2F); - } - } - } - this._explode(level, startX, startY, startZ, endX, endY, endZ); - level.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 1.0F); - } - - private void _explode(Level level, int startX, int startY, int startZ, int endX, int endY, int endZ) { - for (int x = startX; x < endX; x++) { - for (int z = startZ; z < endZ; z++) { - for (int y = startY; y < endY; y++) { - BlockPos pos = new BlockPos(x, y, z); - level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState()); - if (level.isClientSide()) { - level.addParticle(ParticleTypes.EXPLOSION, (double) x + 0.5D, (double) y + 0.5D, (double) z + 0.5D, 1.0D, 0.0D, 0.0D); - } - } - } + }); } } } diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/items/ChargedExplosiveItem.java b/src/main/java/com/oitsjustjose/charged_explosives/common/items/ChargedExplosiveItem.java index 0907204..2f254b2 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/items/ChargedExplosiveItem.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/items/ChargedExplosiveItem.java @@ -7,12 +7,14 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import org.jetbrains.annotations.NotNull; @@ -20,7 +22,6 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.util.List; import java.util.Objects; -import java.util.UUID; public class ChargedExplosiveItem extends BlockItem { public ChargedExplosiveItem(Block block) { @@ -36,6 +37,24 @@ public class ChargedExplosiveItem extends BlockItem { return super.use(level, player, hand); } + public static boolean isNbtInvalid(ItemStack stack) { + if (stack.hasTag()) { + CompoundTag tag = Objects.requireNonNull(stack.getTag()); + return !tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth"); + } + return true; + } + + @Override + public @NotNull InteractionResult place(BlockPlaceContext context) { + ItemStack stack = context.getItemInHand(); + if (isNbtInvalid(stack)) { + Objects.requireNonNull(context.getPlayer()).displayClientMessage(Util.translateOrFallback(ChargedExplosives.MODID + ".needs_init"), true); + return InteractionResult.FAIL; + } + return super.place(context); + } + @Override public void appendHoverText(@NotNull ItemStack stack, @Nullable Level level, @NotNull List components, @NotNull TooltipFlag flag) { if (!stack.hasTag()) { @@ -43,8 +62,7 @@ public class ChargedExplosiveItem extends BlockItem { } CompoundTag tag = Objects.requireNonNull(stack.getTag()); - - if (!tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth")) { + if (isNbtInvalid(stack)) { components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".not_yet_initialized")); return; } else { diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/tile/ChargedExplosiveBlockEntity.java b/src/main/java/com/oitsjustjose/charged_explosives/common/tile/ChargedExplosiveBlockEntity.java index 24b1fdb..0e8c9ce 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/tile/ChargedExplosiveBlockEntity.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/tile/ChargedExplosiveBlockEntity.java @@ -2,31 +2,47 @@ package com.oitsjustjose.charged_explosives.common.tile; import com.oitsjustjose.charged_explosives.ChargedExplosives; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.Tuple; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock; +import net.minecraft.world.level.block.HorizontalDirectionalBlock; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.AttachFace; + +import java.util.ArrayList; public class ChargedExplosiveBlockEntity extends BlockEntity { private int explosionWidth = 0; private int explosionHeight = 0; private int explosionDepth = 0; + private ArrayList explosionPositions; + private Tuple cornerToCorner; public ChargedExplosiveBlockEntity(BlockPos pos, BlockState state) { super(ChargedExplosives.getInstance().REGISTRY.CeBlockEntityType.get(), pos, state); + this.explosionPositions = new ArrayList<>(); } - // saveAdditional(CompoundTag tag) + @Override protected void saveAdditional(CompoundTag tag) { tag.putInt("explosionWidth", this.explosionWidth); tag.putInt("explosionHeight", this.explosionHeight); tag.putInt("explosionDepth", this.explosionDepth); } + @Override public void load(CompoundTag tag) { this.explosionWidth = tag.getInt("explosionWidth"); this.explosionHeight = tag.getInt("explosionHeight"); this.explosionDepth = tag.getInt("explosionDepth"); + calculateExplosions(); + + if (this.level != null && this.level.isClientSide()) { + ChargedExplosives.getInstance().proxy.addBlockToRender(cornerToCorner); + } } @Override @@ -41,6 +57,7 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { public int getExplosionWidth() { return explosionWidth; } + public int getExplosionDepth() { return explosionDepth; } @@ -48,4 +65,110 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { public int getExplosionHeight() { return explosionHeight; } + + public ArrayList getExplosions() { + return this.explosionPositions; + } + + public Tuple getCorners() { + return this.cornerToCorner; + } + + public void calculateExplosions() { + BlockPos pos = this.getBlockPos(); + BlockState state = this.getBlockState(); + ArrayList data = new ArrayList<>(); + Direction direction = state.getValue(HorizontalDirectionalBlock.FACING); + AttachFace face = state.getValue(FaceAttachedHorizontalDirectionalBlock.FACE); + + int startX, endX, startY, endY, startZ, endZ; + startX = endX = startY = endY = startZ = endZ = 0; + switch (face) { + case FLOOR -> { + if (direction.getAxis() == Direction.Axis.X) { + startX = pos.getX() - (int) Math.floor(explosionHeight / 2F); + endX = pos.getX() + (int) Math.ceil(explosionHeight / 2F); + startY = pos.getY() - explosionDepth; + endY = pos.getY(); + startZ = pos.getZ() - (int) Math.floor(explosionWidth / 2F) + 1; + endZ = pos.getZ() + (int) Math.ceil(explosionWidth / 2F) + 1; + } else { + startX = pos.getX() - (int) Math.floor(explosionWidth / 2F); + endX = pos.getX() + (int) Math.ceil(explosionWidth / 2F); + startY = pos.getY() - explosionDepth; + endY = pos.getY(); + startZ = pos.getZ() - (int) Math.floor(explosionHeight / 2F); + endZ = pos.getZ() + (int) Math.ceil(explosionHeight / 2F); + } + } + case WALL -> { + switch (direction) { + case NORTH -> { + startX = pos.getX() - (int) Math.floor(explosionWidth / 2F); + endX = pos.getX() + (int) Math.ceil(explosionWidth / 2F); + startY = pos.getY() - (int) Math.floor(explosionHeight / 2F); + endY = pos.getY() + (int) Math.ceil(explosionHeight / 2F); + startZ = pos.getZ() + 1; + endZ = pos.getZ() + explosionDepth + 1; + } + case SOUTH -> { + startX = pos.getX() - (int) Math.ceil(explosionWidth / 2F) + 1; + endX = pos.getX() + (int) Math.floor(explosionWidth / 2F) + 1; + startY = pos.getY() - (int) Math.ceil(explosionHeight / 2F); + endY = pos.getY() + (int) Math.floor(explosionHeight / 2F); + startZ = pos.getZ() - explosionDepth; + endZ = pos.getZ(); + } + case EAST -> { + startX = pos.getX() - explosionDepth; + endX = pos.getX(); + startY = pos.getY() - (int) Math.floor(explosionHeight / 2F); + endY = pos.getY() + (int) Math.ceil(explosionHeight / 2F); + startZ = pos.getZ() - (int) Math.floor(explosionWidth / 2F); + endZ = pos.getZ() + (int) Math.ceil(explosionWidth / 2F); + } + case WEST -> { + startX = pos.getX(); + endX = pos.getX() + explosionDepth; + startY = pos.getY() - (int) Math.ceil(explosionHeight / 2F); + endY = pos.getY() + (int) Math.floor(explosionHeight / 2F); + startZ = pos.getZ() - (int) Math.ceil(explosionWidth / 2F) + 1; + endZ = pos.getZ() + (int) Math.floor(explosionWidth / 2F) + 1; + } + default -> { + break; + } + } + } + case CEILING -> { + if (direction.getAxis() == Direction.Axis.X) { + startX = pos.getX() - (int) Math.floor(explosionHeight / 2F); + endX = pos.getX() + (int) Math.ceil(explosionHeight / 2F); + startY = pos.getY(); + endY = pos.getY() + explosionDepth; + startZ = pos.getZ() - (int) Math.floor(explosionWidth / 2F); + endZ = pos.getZ() + (int) Math.ceil(explosionWidth / 2F); + } else { + startX = pos.getX() - (int) Math.floor(explosionWidth / 2F) + 1; + endX = pos.getX() + (int) Math.ceil(explosionWidth / 2F) + 1; + startY = pos.getY(); + endY = pos.getY() + explosionDepth; + startZ = pos.getZ() - (int) Math.floor(explosionHeight / 2F); + endZ = pos.getZ() + (int) Math.ceil(explosionHeight / 2F); + } + } + } + + this.cornerToCorner = new Tuple<>(new BlockPos(startX, startY, startZ), new BlockPos(endX, endY, endZ)); + + for (int x = startX; x < endX; x++) { + for (int z = startZ; z < endZ; z++) { + for (int y = startY; y < endY; y++) { + data.add(new BlockPos(x, y, z)); + } + } + } + + this.explosionPositions = data; + } } diff --git a/src/main/resources/assets/charged_explosives/lang/en_us.json b/src/main/resources/assets/charged_explosives/lang/en_us.json index 6e535d4..153903f 100644 --- a/src/main/resources/assets/charged_explosives/lang/en_us.json +++ b/src/main/resources/assets/charged_explosives/lang/en_us.json @@ -7,5 +7,6 @@ "charged_explosives.width_tooltip": " §8Width: §8%s§r", "charged_explosives.depth_tooltip": " §8Depth: §8%s§r", "charged_explosives.height_tooltip": " §8Height: §8%s§r", + "charged_explosives.needs_init": "This Explosive Needs to be Configured", "block.charged_explosives.charged_explosive": "Explosive Charge" } \ No newline at end of file