diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java b/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java index d026562..ae51ec8 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/ExplosionConfigScreen.java @@ -31,9 +31,9 @@ public class ExplosionConfigScreen extends Screen { private EditBox widthBox; private EditBox depthBox; private EditBox heightBox; - private Component widthTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.width.title"); - private Component depthTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.depth.title"); - private Component heightTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.height.title"); + private final Component widthTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.width.title"); + private final Component depthTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.depth.title"); + private final Component heightTitle = Util.translateOrFallback(ChargedExplosives.MODID + ".explosion.height.title"); /* Constants */ private final int TEXTURE_WIDTH = 176; @@ -74,48 +74,48 @@ public class ExplosionConfigScreen extends Screen { + (LW_MOD - EDIT_BOX_WIDTH - EB_MOD) /* Offset for the right btn */; /* Width Adj */ - this.widthBox = this.addRenderableWidget(new EditBox(this.font, BG_X + (rowWidth / 2) + EB_MOD, BG_Y + 40, EDIT_BOX_WIDTH, 20, Component.literal(""))); + this.widthBox = this.addRenderableWidget(new EditBox(this.font, BG_X + ((rowWidth / 2) - 1) + EB_MOD, BG_Y + 40, EDIT_BOX_WIDTH, 20, Component.literal(""))); this.widthBox.setValue("" + this.explosionWidth); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + FW_MOD, BG_Y + 40, DECR_W, 20, DECR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + FW_MOD, BG_Y + 40, DECR_W, 20, DECR, onPress -> { if (this.explosionWidth > 0) { this.explosionWidth--; this.dirty = true; this.widthBox.setValue("" + this.explosionWidth); } })); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + LW_MOD, BG_Y + 40, INCR_W, 20, INCR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + LW_MOD, BG_Y + 40, INCR_W, 20, INCR, onPress -> { this.explosionWidth++; this.dirty = true; this.widthBox.setValue("" + this.explosionWidth); })); /* Depth Adj */ - this.depthBox = this.addRenderableWidget(new EditBox(this.font, BG_X + (rowWidth / 2) + EB_MOD, BG_Y + 85, EDIT_BOX_WIDTH, 20, Component.literal(""))); + this.depthBox = this.addRenderableWidget(new EditBox(this.font, BG_X + ((rowWidth / 2) - 1) + EB_MOD, BG_Y + 85, EDIT_BOX_WIDTH, 20, Component.literal(""))); this.depthBox.setValue("" + this.explosionDepth); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + FW_MOD, BG_Y + 85, DECR_W, 20, DECR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + FW_MOD, BG_Y + 85, DECR_W, 20, DECR, onPress -> { if (this.explosionDepth > 0) { this.explosionDepth--; this.dirty = true; this.depthBox.setValue("" + this.explosionDepth); } })); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + LW_MOD, BG_Y + 85, INCR_W, 20, INCR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + LW_MOD, BG_Y + 85, INCR_W, 20, INCR, onPress -> { this.explosionDepth++; this.dirty = true; this.depthBox.setValue("" + this.explosionDepth); })); /* Height Adj */ - this.heightBox = this.addRenderableWidget(new EditBox(this.font, BG_X + (rowWidth / 2) + EB_MOD, BG_Y + 130, EDIT_BOX_WIDTH, 20, Component.literal(""))); + this.heightBox = this.addRenderableWidget(new EditBox(this.font, BG_X + ((rowWidth / 2) - 1) + EB_MOD, BG_Y + 130, EDIT_BOX_WIDTH, 20, Component.literal(""))); this.heightBox.setValue("" + this.explosionHeight); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + FW_MOD, BG_Y + 130, DECR_W, 20, DECR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + FW_MOD, BG_Y + 130, DECR_W, 20, DECR, onPress -> { if (this.explosionHeight > 0) { this.explosionHeight--; this.dirty = true; this.heightBox.setValue("" + this.explosionHeight); } })); - this.addRenderableWidget(new Button(BG_X + (rowWidth / 2) + LW_MOD, BG_Y + 130, INCR_W, 20, INCR, onPress -> { + this.addRenderableWidget(new Button(BG_X + ((rowWidth / 2) - 1) + LW_MOD, BG_Y + 130, INCR_W, 20, INCR, onPress -> { this.explosionHeight++; this.dirty = true; this.heightBox.setValue("" + this.explosionHeight); 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 5973f99..90459c8 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,32 +1,44 @@ 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.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.server.level.ServerLevel; -import net.minecraft.world.Containers; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.LivingEntity; +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; import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock; -import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity; import net.minecraft.world.level.block.entity.BlockEntity; 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.Vec3; +import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.annotation.Nonnull; + public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBlock implements EntityBlock { @@ -40,7 +52,7 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc protected static final VoxelShape EAST_AABB = Block.box(0D, 4D, 2D, 3D, 12D, 14D); public ChargedExplosiveBlock() { - super(BlockBehaviour.Properties.of(Material.EXPLOSIVE, MaterialColor.COLOR_RED).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)); } @@ -131,6 +143,7 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc if (!newState.is(oldState.getBlock())) { level.removeBlockEntity(pos); } + super.onRemove(oldState, level, pos, newState, moving); } @Nullable @@ -138,5 +151,135 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) { return new ChargedExplosiveBlockEntity(pos, state); } + + @Override + public void playerWillDestroy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull Player player) { + if (level.isClientSide() || player.isCreative()) { + return; + } + + BlockEntity be = level.getBlockEntity(pos); + if (be instanceof ChargedExplosiveBlockEntity expBe) { + ItemStack stack = new ItemStack(ChargedExplosives.getInstance().REGISTRY.CeItem.get()); + expBe.saveToItem(stack); + ItemEntity ent = new ItemEntity(level, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, stack); + ent.setDefaultPickUpDelay(); + level.addFreshEntity(ent); + } + } + + @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()); + } + 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; + switch (face) { + case FLOOR -> { + if (direction.getAxis() == Direction.Axis.X) { + startX = pos.getX() - height; + endX = pos.getX() + height + 1; + startY = pos.getY() - depth; + endY = pos.getY(); + startZ = pos.getZ() - width; + endZ = pos.getZ() + width + 1; + } else { + startX = pos.getX() - width; + endX = pos.getX() + width + 1; + startY = pos.getY() - depth; + endY = pos.getY(); + startZ = pos.getZ() - height; + endZ = pos.getZ() + height + 1; + } + this._explode(level, startX, startY, startZ, endX, endY, endZ); + } + case WALL -> { + switch (direction) { + case NORTH -> { + startX = pos.getX() - width; + endX = pos.getX() + width + 1; + startY = pos.getY() - height; + endY = pos.getY() + height + 1; + startZ = pos.getZ() + 1; + endZ = pos.getZ() + depth + 1; + } + case SOUTH -> { + startX = pos.getX() - width; + endX = pos.getX() + width + 1; + startY = pos.getY() - height; + endY = pos.getY() + height + 1; + startZ = pos.getZ() - depth; + endZ = pos.getZ(); + } + case EAST -> { + startX = pos.getX() - depth; + endX = pos.getX(); + startY = pos.getY() - height; + endY = pos.getY() + height + 1; + startZ = pos.getZ() - width; + endZ = pos.getZ() + width + 1; + } + case WEST -> { + startX = pos.getX() + 1; + endX = pos.getX() + depth + 1; + startY = pos.getY() - height; + endY = pos.getY() + height + 1; + startZ = pos.getZ() - width; + endZ = pos.getZ() + width + 1; + } + default -> { + //NOOP + startX = 0; + endX = 0; + startY = 0; + endY = 0; + startZ = 0; + endZ = 0; + } + } + this._explode(level, startX, startY, startZ, endX, endY, endZ); + } + case CEILING -> { + if (direction.getAxis() == Direction.Axis.X) { + startX = pos.getX() - height; + endX = pos.getX() + height + 1; + startY = pos.getY(); + endY = pos.getY() + depth + 1; + startZ = pos.getZ() - width; + endZ = pos.getZ() + width + 1; + } else { + startX = pos.getX() - width; + endX = pos.getX() + width + 1; + startY = pos.getY(); + endY = pos.getY() + depth + 1; + startZ = pos.getZ() - height; + endZ = pos.getZ() + height + 1; + } + 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 3fa70fa..0907204 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 @@ -1,17 +1,25 @@ package com.oitsjustjose.charged_explosives.common.items; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.oitsjustjose.charged_explosives.ChargedExplosives; +import com.oitsjustjose.charged_explosives.common.Util; 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.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.level.Level; import net.minecraft.world.level.block.Block; 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 { @@ -23,15 +31,35 @@ public class ChargedExplosiveItem extends BlockItem { public @NotNull InteractionResultHolder use(@NotNull Level level, Player player, @NotNull InteractionHand hand) { // Set NBT if it hasn't been already ItemStack stack = player.getItemInHand(hand); - CompoundTag tag = stack.getOrCreateTag(); - UUID uuid; - if (tag.contains("explosive_id")) { - uuid = tag.getUUID("explosive_id"); - } else { - uuid = UUID.randomUUID(); - tag.putUUID("explosive_id", uuid); - } + stack.getOrCreateTag(); ChargedExplosives.getInstance().proxy.openExplosiveGui(player, stack); return super.use(level, player, hand); } + + @Override + public void appendHoverText(@NotNull ItemStack stack, @Nullable Level level, @NotNull List components, @NotNull TooltipFlag flag) { + if (!stack.hasTag()) { + return; + } + + CompoundTag tag = Objects.requireNonNull(stack.getTag()); + + if (!tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth")) { + components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".not_yet_initialized")); + return; + } else { + int w = tag.getInt("explosionWidth"); + int d = tag.getInt("explosionDepth"); + int h = tag.getInt("explosionHeight"); + try { + TranslatableContents wc = new TranslatableContents(ChargedExplosives.MODID + ".width_tooltip", w); + components.add(wc.resolve(null, null, 0)); + TranslatableContents dc = new TranslatableContents(ChargedExplosives.MODID + ".depth_tooltip", d); + components.add(dc.resolve(null, null, 0)); + TranslatableContents hc = new TranslatableContents(ChargedExplosives.MODID + ".height_tooltip", h); + components.add(hc.resolve(null, null, 0)); + } catch (CommandSyntaxException ignored) { + } + } + } } diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/network/UpdateNbtPacket.java b/src/main/java/com/oitsjustjose/charged_explosives/common/network/UpdateNbtPacket.java index 7d6f848..89a3688 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/network/UpdateNbtPacket.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/network/UpdateNbtPacket.java @@ -2,12 +2,15 @@ package com.oitsjustjose.charged_explosives.common.network; import com.oitsjustjose.charged_explosives.ChargedExplosives; +import com.oitsjustjose.charged_explosives.common.items.ChargedExplosiveItem; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.item.ItemStack; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.network.NetworkEvent; +import java.util.Arrays; +import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; @@ -41,20 +44,15 @@ public class UpdateNbtPacket { } CompoundTag newTag = Objects.requireNonNull(pkt.stack.getTag()); - // Search for the item to update - Optional stack = Objects.requireNonNull(ctx.get().getSender()).getInventory().items.stream().filter(x -> { - return x.hasTag() - && x.getTag() != null - && x.getTag().contains("explosive_id") - && x.getTag().getUUID("explosive_id").equals(newTag.getUUID("explosive_id")); - }).findFirst(); + ItemStack main = Objects.requireNonNull(ctx.get().getSender()).getMainHandItem(); + ItemStack off = Objects.requireNonNull(ctx.get().getSender()).getOffhandItem(); - if (stack.isPresent()) { - // We know there's a tag present because we used it earlier in the filter() - CompoundTag tag = Objects.requireNonNull(stack.get().getTag()); - tag.putInt("explosionWidth", newTag.getInt("explosionWidth")); - tag.putInt("explosionDepth", newTag.getInt("explosionDepth")); - tag.putInt("explosionHeight", newTag.getInt("explosionHeight")); + if (off.getItem() instanceof ChargedExplosiveItem) { + off.setTag(newTag); + } else if (main.getItem() instanceof ChargedExplosiveItem) { + main.setTag(newTag); + } else { + ChargedExplosives.getInstance().LOGGER.warn("ItemStack in players main and off hands are NOT charged explosives..."); } }); ctx.get().setPacketHandled(true); 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 a52151b..24b1fdb 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 @@ -3,6 +3,7 @@ package com.oitsjustjose.charged_explosives.common.tile; import com.oitsjustjose.charged_explosives.ChargedExplosives; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; @@ -28,5 +29,23 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { this.explosionDepth = tag.getInt("explosionDepth"); } + @Override + public void saveToItem(ItemStack stack) { + CompoundTag tag = stack.getOrCreateTag(); + tag.putInt("explosionWidth", explosionWidth); + tag.putInt("explosionHeight", explosionHeight); + tag.putInt("explosionDepth", explosionDepth); + super.saveToItem(stack); + } + public int getExplosionWidth() { + return explosionWidth; + } + public int getExplosionDepth() { + return explosionDepth; + } + + public int getExplosionHeight() { + return explosionHeight; + } } 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 a3974ab..1b54638 100644 --- a/src/main/resources/assets/charged_explosives/lang/en_us.json +++ b/src/main/resources/assets/charged_explosives/lang/en_us.json @@ -1,6 +1,11 @@ { - "charged_explosives.explosion.configuration.title": "Configure Blast", - "charged_explosives.explosion.width.title": "Explosion Width:", - "charged_explosives.explosion.height.title": "Explosion Height:", - "charged_explosives.explosion.depth.title": "Explosion Depth:" + "charged_explosives.explosion.configuration.title": "Configure Blast Radius", + "charged_explosives.explosion.width.title": "Explosion Width:", + "charged_explosives.explosion.height.title": "Explosion Height:", + "charged_explosives.explosion.depth.title": "Explosion Depth:", + "charged_explosives.not_yet_initialized": "§8Unconfigured§r", + "charged_explosives.width_tooltip": " §8Width: §8%s§r", + "charged_explosives.depth_tooltip": " §8Depth: §8%s§r", + "charged_explosives.height_tooltip": " §8Height: §8%s§r", + "block.charged_explosives.charged_explosive": "Explosive Charge" } \ No newline at end of file