Render explosion previews n stuff!

This commit is contained in:
oitsjustjose 2022-10-17 10:29:11 -05:00
commit 0ffedfb76b
10 changed files with 313 additions and 141 deletions

View file

@ -1,20 +1,27 @@
package com.oitsjustjose.charged_explosives.client; package com.oitsjustjose.charged_explosives.client;
import com.oitsjustjose.charged_explosives.client.network.ClientOpenGuiPacket; 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.CommonProxy;
import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket; import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket;
import com.oitsjustjose.charged_explosives.common.network.UpdateNbtPacket; 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.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.network.NetworkDirection; import net.minecraftforge.network.NetworkDirection;
import java.util.Optional; import java.util.Optional;
public class ClientProxy extends CommonProxy { public class ClientProxy extends CommonProxy {
private BlockDestroyRenderer bdRenderer = new BlockDestroyRenderer();
@Override @Override
public void init() { public void init() {
CommonProxy.netMgr.networkWrapper.registerMessage(CommonProxy.disc++, OpenGuiPacket.class, OpenGuiPacket::encode, OpenGuiPacket::decode, ClientOpenGuiPacket::handleClient); 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)); 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 @Override
@ -26,4 +33,14 @@ public class ClientProxy extends CommonProxy {
public void openExplosiveGui(Player player, ItemStack stack) { public void openExplosiveGui(Player player, ItemStack stack) {
super.openExplosiveGui(player, stack); super.openExplosiveGui(player, stack);
} }
@Override
public void addBlockToRender(Tuple<BlockPos, BlockPos> pos) {
bdRenderer.selectBlock(pos);
}
@Override
public void removeBlockFromRender(Tuple<BlockPos, BlockPos> pos) {
bdRenderer.unselectBlock(pos);
}
} }

View file

@ -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.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;

View file

@ -1,6 +1,6 @@
package com.oitsjustjose.charged_explosives.client.network; 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 com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.LogicalSide;

View file

@ -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<Tuple<BlockPos, BlockPos>> highlightedBlocks = new HashSet<>();
public void selectBlock(Tuple<BlockPos, BlockPos> corners) {
this.highlightedBlocks.add(corners);
}
public void unselectBlock(Tuple<BlockPos, BlockPos> 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();
}
}

View file

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

View file

@ -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.NetworkManager;
import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket; import com.oitsjustjose.charged_explosives.common.network.OpenGuiPacket;
import com.oitsjustjose.charged_explosives.common.network.UpdateNbtPacket; import com.oitsjustjose.charged_explosives.common.network.UpdateNbtPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Tuple;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkDirection; import net.minecraftforge.network.NetworkDirection;
@ -32,4 +34,10 @@ public class CommonProxy {
public void updateItemNbt(ItemStack stack) { public void updateItemNbt(ItemStack stack) {
// NOOP // NOOP
} }
public void addBlockToRender(Tuple<BlockPos, BlockPos> pos) {
}
public void removeBlockFromRender(Tuple<BlockPos, BlockPos> pos) {
}
} }

View file

@ -1,11 +1,10 @@
package com.oitsjustjose.charged_explosives.common.blocks; package com.oitsjustjose.charged_explosives.common.blocks;
import com.oitsjustjose.charged_explosives.ChargedExplosives; 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 com.oitsjustjose.charged_explosives.common.tile.ChargedExplosiveBlockEntity;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents; 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.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; 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.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; 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.AttachFace;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
@ -53,12 +50,11 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc
public ChargedExplosiveBlock() { 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)); 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)); this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(FACE, AttachFace.WALL));
} }
@Override @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); Direction direction = state.getValue(FACING);
switch (state.getValue(FACE)) { switch (state.getValue(FACE)) {
case FLOOR: case FLOOR:
@ -87,39 +83,14 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc
} }
@Override @Override
public @NotNull VoxelShape getCollisionShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) { public @NotNull VoxelShape getCollisionShape(BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) {
Direction direction = state.getValue(FACING); return this.getShape(state, level, pos, context);
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;
}
}
} }
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> Builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> Builder) {
Builder.add(FACING, FACE); Builder.add(FACING, FACE);
} }
@Override @Override
public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, LivingEntity placer, @NotNull ItemStack stack) { public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, LivingEntity placer, @NotNull ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack); super.setPlacedBy(level, pos, state, placer, stack);
@ -129,18 +100,15 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc
} }
CompoundTag tag = stack.getOrCreateTag().copy(); 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); be.load(tag);
} }
@Override @Override
public void onRemove(BlockState oldState, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean moving) { public void onRemove(BlockState oldState, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean moving) {
if (!newState.is(oldState.getBlock())) { if (!newState.is(oldState.getBlock())) {
if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) {
ChargedExplosives.getInstance().proxy.removeBlockFromRender(cebe.getCorners());
}
level.removeBlockEntity(pos); level.removeBlockEntity(pos);
} }
super.onRemove(oldState, level, pos, newState, moving); super.onRemove(oldState, level, pos, newState, moving);
@ -170,108 +138,24 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc
@Override @Override
public InteractionResult use(@Nonnull BlockState state, @Nonnull Level level, @Nonnull BlockPos pos, @Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull BlockHitResult hit) { 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) { if (level.isClientSide()) {
this.explode(level, pos, state, cebe.getExplosionWidth(), cebe.getExplosionHeight(), cebe.getExplosionDepth()); player.swing(hand);
} }
this.explode(level, pos);
return InteractionResult.CONSUME; 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; private void explode(Level level, BlockPos pos) {
startX = endX = startY = endY = startZ = endZ = 0; if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) {
switch (face) { level.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 1.0F);
case FLOOR -> { cebe.getExplosions().forEach(p -> {
if (direction.getAxis() == Direction.Axis.X) { level.setBlockAndUpdate(p, Blocks.AIR.defaultBlockState());
startX = pos.getX() - (int) Math.floor(height / 2F); if (level.isClientSide()) { // TODO: packetize me captain
endX = pos.getX() + (int) Math.ceil(height / 2F); 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);
startY = pos.getY() - depth; ChargedExplosives.getInstance().proxy.removeBlockFromRender(cebe.getCorners());
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);
} }
} });
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);
}
}
}
} }
} }
} }

View file

@ -7,12 +7,14 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -20,7 +22,6 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID;
public class ChargedExplosiveItem extends BlockItem { public class ChargedExplosiveItem extends BlockItem {
public ChargedExplosiveItem(Block block) { public ChargedExplosiveItem(Block block) {
@ -36,6 +37,24 @@ public class ChargedExplosiveItem extends BlockItem {
return super.use(level, player, hand); 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 @Override
public void appendHoverText(@NotNull ItemStack stack, @Nullable Level level, @NotNull List<Component> components, @NotNull TooltipFlag flag) { public void appendHoverText(@NotNull ItemStack stack, @Nullable Level level, @NotNull List<Component> components, @NotNull TooltipFlag flag) {
if (!stack.hasTag()) { if (!stack.hasTag()) {
@ -43,8 +62,7 @@ public class ChargedExplosiveItem extends BlockItem {
} }
CompoundTag tag = Objects.requireNonNull(stack.getTag()); CompoundTag tag = Objects.requireNonNull(stack.getTag());
if (isNbtInvalid(stack)) {
if (!tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth")) {
components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".not_yet_initialized")); components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".not_yet_initialized"));
return; return;
} else { } else {

View file

@ -2,31 +2,47 @@ package com.oitsjustjose.charged_explosives.common.tile;
import com.oitsjustjose.charged_explosives.ChargedExplosives; import com.oitsjustjose.charged_explosives.ChargedExplosives;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Tuple;
import net.minecraft.world.item.ItemStack; 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.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; 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 { public class ChargedExplosiveBlockEntity extends BlockEntity {
private int explosionWidth = 0; private int explosionWidth = 0;
private int explosionHeight = 0; private int explosionHeight = 0;
private int explosionDepth = 0; private int explosionDepth = 0;
private ArrayList<BlockPos> explosionPositions;
private Tuple<BlockPos, BlockPos> cornerToCorner;
public ChargedExplosiveBlockEntity(BlockPos pos, BlockState state) { public ChargedExplosiveBlockEntity(BlockPos pos, BlockState state) {
super(ChargedExplosives.getInstance().REGISTRY.CeBlockEntityType.get(), pos, state); super(ChargedExplosives.getInstance().REGISTRY.CeBlockEntityType.get(), pos, state);
this.explosionPositions = new ArrayList<>();
} }
// saveAdditional(CompoundTag tag) @Override
protected void saveAdditional(CompoundTag tag) { protected void saveAdditional(CompoundTag tag) {
tag.putInt("explosionWidth", this.explosionWidth); tag.putInt("explosionWidth", this.explosionWidth);
tag.putInt("explosionHeight", this.explosionHeight); tag.putInt("explosionHeight", this.explosionHeight);
tag.putInt("explosionDepth", this.explosionDepth); tag.putInt("explosionDepth", this.explosionDepth);
} }
@Override
public void load(CompoundTag tag) { public void load(CompoundTag tag) {
this.explosionWidth = tag.getInt("explosionWidth"); this.explosionWidth = tag.getInt("explosionWidth");
this.explosionHeight = tag.getInt("explosionHeight"); this.explosionHeight = tag.getInt("explosionHeight");
this.explosionDepth = tag.getInt("explosionDepth"); this.explosionDepth = tag.getInt("explosionDepth");
calculateExplosions();
if (this.level != null && this.level.isClientSide()) {
ChargedExplosives.getInstance().proxy.addBlockToRender(cornerToCorner);
}
} }
@Override @Override
@ -41,6 +57,7 @@ public class ChargedExplosiveBlockEntity extends BlockEntity {
public int getExplosionWidth() { public int getExplosionWidth() {
return explosionWidth; return explosionWidth;
} }
public int getExplosionDepth() { public int getExplosionDepth() {
return explosionDepth; return explosionDepth;
} }
@ -48,4 +65,110 @@ public class ChargedExplosiveBlockEntity extends BlockEntity {
public int getExplosionHeight() { public int getExplosionHeight() {
return explosionHeight; return explosionHeight;
} }
public ArrayList<BlockPos> getExplosions() {
return this.explosionPositions;
}
public Tuple<BlockPos, BlockPos> getCorners() {
return this.cornerToCorner;
}
public void calculateExplosions() {
BlockPos pos = this.getBlockPos();
BlockState state = this.getBlockState();
ArrayList<BlockPos> 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;
}
} }

View file

@ -7,5 +7,6 @@
"charged_explosives.width_tooltip": " §8Width: §8%s§r", "charged_explosives.width_tooltip": " §8Width: §8%s§r",
"charged_explosives.depth_tooltip": " §8Depth: §8%s§r", "charged_explosives.depth_tooltip": " §8Depth: §8%s§r",
"charged_explosives.height_tooltip": " §8Height: §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" "block.charged_explosives.charged_explosive": "Explosive Charge"
} }