From 466903d15627f120b69308093bdbab3de18f9fbd Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 18 Oct 2022 11:06:01 -0500 Subject: [PATCH] Configs, redstone fixes, concussive damage and more --- .../charged_explosives/ChargedExplosives.java | 13 +++ .../client/ClientProxy.java | 2 - .../client/config/ClientConfig.java | 35 ++++++ .../client/gui/ExplosionConfigScreen.java | 97 +++++++-------- .../client/render/BlockDestroyRenderer.java | 10 +- .../client/render/BlockOutlineRenderType.java | 1 - .../common/TickScheduler.java | 16 ++- .../common/blocks/ChargedExplosiveBlock.java | 110 +++++++----------- .../common/config/CommonConfig.java | 55 +++++++++ .../common/items/ChargedExplosiveItem.java | 63 ++++++---- .../tile/ChargedExplosiveBlockEntity.java | 26 ++++- .../assets/charged_explosives/lang/en_us.json | 6 +- .../models/block/charged_explosive.json | 5 +- 13 files changed, 285 insertions(+), 154 deletions(-) create mode 100644 src/main/java/com/oitsjustjose/charged_explosives/client/config/ClientConfig.java create mode 100644 src/main/java/com/oitsjustjose/charged_explosives/common/config/CommonConfig.java diff --git a/src/main/java/com/oitsjustjose/charged_explosives/ChargedExplosives.java b/src/main/java/com/oitsjustjose/charged_explosives/ChargedExplosives.java index 470d320..fb16376 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/ChargedExplosives.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/ChargedExplosives.java @@ -1,16 +1,21 @@ package com.oitsjustjose.charged_explosives; import com.oitsjustjose.charged_explosives.client.ClientProxy; +import com.oitsjustjose.charged_explosives.client.config.ClientConfig; import com.oitsjustjose.charged_explosives.common.CommonProxy; import com.oitsjustjose.charged_explosives.common.TickScheduler; +import com.oitsjustjose.charged_explosives.common.config.CommonConfig; import com.oitsjustjose.charged_explosives.common.registry.Registry; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.AddReloadListenerEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.fml.loading.FMLPaths; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -32,6 +37,7 @@ public class ChargedExplosives { MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(SCHEDULER); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); + this.configSetup(); } public static ChargedExplosives getInstance() { @@ -42,6 +48,13 @@ public class ChargedExplosives { PROXY.init(); } + private void configSetup() { + ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, CommonConfig.COMMON_CONFIG); + ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ClientConfig.CLIENT_CONFIG); + CommonConfig.loadConfig(CommonConfig.COMMON_CONFIG, FMLPaths.CONFIGDIR.get().resolve(MODID + "-common.toml")); + ClientConfig.loadConfig(ClientConfig.CLIENT_CONFIG, FMLPaths.CONFIGDIR.get().resolve(MODID + "-client.toml")); + } + @SubscribeEvent public void onSlashReload(AddReloadListenerEvent evt) { } 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 9ec19a1..51b3fba 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/ClientProxy.java @@ -39,14 +39,12 @@ public class ClientProxy extends CommonProxy { public void startPreviewExplosion(Tuple corners) { if(corners != null && corners.getA() != null && corners.getB() != null) { bdRenderer.addExplosion(corners); - } } public void endPreviewExplosion(Tuple corners) { if(corners != null && corners.getA() != null && corners.getB() != null) { bdRenderer.removeExplosion(corners); - } } } \ No newline at end of file diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/config/ClientConfig.java b/src/main/java/com/oitsjustjose/charged_explosives/client/config/ClientConfig.java new file mode 100644 index 0000000..9de0879 --- /dev/null +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/config/ClientConfig.java @@ -0,0 +1,35 @@ +package com.oitsjustjose.charged_explosives.client.config; + +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.electronwill.nightconfig.core.io.WritingMode; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.common.ForgeConfigSpec.Builder; +import net.minecraftforge.fml.common.Mod; + +import java.nio.file.Path; + +@Mod.EventBusSubscriber +public class ClientConfig { + public static final ForgeConfigSpec CLIENT_CONFIG; + private static final Builder CLIENT_BUILDER = new Builder(); + + public static ForgeConfigSpec.BooleanValue ENABLE_EXPLOSION_PREVIEW_RENDER; + private static final String CATEGORY_CLIENT = "client_configs"; + + static { + init(); + CLIENT_CONFIG = CLIENT_BUILDER.build(); + } + + public static void loadConfig(ForgeConfigSpec spec, Path path) { + final CommentedFileConfig configData = CommentedFileConfig.builder(path).sync().autosave().writingMode(WritingMode.REPLACE).build(); + configData.load(); + spec.setConfig(configData); + } + + private static void init() { + CLIENT_BUILDER.comment("Client Configuration Settings").push(CATEGORY_CLIENT); + ENABLE_EXPLOSION_PREVIEW_RENDER = CLIENT_BUILDER.comment("Enable a red box showing which blocks will be destroyed?").define("enableExplosionPreviewRender", true); + CLIENT_BUILDER.pop(); + } +} \ No newline at end of file diff --git a/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java b/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java index 0459ae5..11122ee 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/gui/ExplosionConfigScreen.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import com.oitsjustjose.charged_explosives.ChargedExplosives; import com.oitsjustjose.charged_explosives.common.Util; +import com.oitsjustjose.charged_explosives.common.config.CommonConfig; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.screens.Screen; @@ -69,56 +70,60 @@ public class ExplosionConfigScreen extends Screen { final int LW_MOD = 65; final int EDIT_BOX_WIDTH = 40; - int rowWidth =/* Width of actual els */ INCR_W + DECR_W + EDIT_BOX_WIDTH - + (LW_MOD - EDIT_BOX_WIDTH) /* Offset for the left btn */ - + (LW_MOD - EDIT_BOX_WIDTH - EB_MOD) /* Offset for the right btn */; + int rowWidth =/* Width of actual els */ INCR_W + DECR_W + EDIT_BOX_WIDTH + (LW_MOD - EDIT_BOX_WIDTH) /* Offset for the left btn */ + (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) - 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) - 1) + FW_MOD, BG_Y + 40, DECR_W, 20, DECR, onPress -> { - if (this.explosionWidth > 0) { + if (this.explosionWidth > 1) { this.explosionWidth--; this.dirty = true; this.widthBox.setValue("" + this.explosionWidth); } })); + 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) - 1) + LW_MOD, BG_Y + 40, INCR_W, 20, INCR, onPress -> { - this.explosionWidth++; - this.dirty = true; - this.widthBox.setValue("" + this.explosionWidth); + if (this.explosionWidth < CommonConfig.MAX_EXPLOSION_WIDTH.get()) { + this.explosionWidth++; + this.dirty = true; + this.widthBox.setValue("" + this.explosionWidth); + } })); /* Depth Adj */ - 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) - 1) + FW_MOD, BG_Y + 85, DECR_W, 20, DECR, onPress -> { - if (this.explosionDepth > 0) { + if (this.explosionDepth > 1) { this.explosionDepth--; this.dirty = true; this.depthBox.setValue("" + this.explosionDepth); } })); + 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) - 1) + LW_MOD, BG_Y + 85, INCR_W, 20, INCR, onPress -> { - this.explosionDepth++; - this.dirty = true; - this.depthBox.setValue("" + this.explosionDepth); + if (this.explosionDepth < CommonConfig.MAX_EXPLOSION_DEPTH.get()) { + this.explosionDepth++; + this.dirty = true; + this.depthBox.setValue("" + this.explosionDepth); + } })); /* Height Adj */ - 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) - 1) + FW_MOD, BG_Y + 130, DECR_W, 20, DECR, onPress -> { - if (this.explosionHeight > 0) { + if (this.explosionHeight > 1) { this.explosionHeight--; this.dirty = true; this.heightBox.setValue("" + this.explosionHeight); } })); + 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) - 1) + LW_MOD, BG_Y + 130, INCR_W, 20, INCR, onPress -> { - this.explosionHeight++; - this.dirty = true; - this.heightBox.setValue("" + this.explosionHeight); + if (this.explosionHeight < CommonConfig.MAX_EXPLOSION_HEIGHT.get()) { + this.explosionHeight++; + this.dirty = true; + this.heightBox.setValue("" + this.explosionHeight); + } })); } @@ -142,10 +147,14 @@ public class ExplosionConfigScreen extends Screen { try { this.widthBox.tick(); int value = Integer.parseInt(this.widthBox.getValue()); - this.widthBox.setTextColor(WHITE); - if (value != explosionWidth) { - this.explosionWidth = value; - this.dirty = true; + if (value <= 0 || value > CommonConfig.MAX_EXPLOSION_WIDTH.get()) { + this.widthBox.setTextColor(RED); + } else { + this.widthBox.setTextColor(WHITE); + if (value != explosionWidth) { + this.explosionWidth = value; + this.dirty = true; + } } } catch (NumberFormatException ex) { this.widthBox.setTextColor(RED); @@ -154,10 +163,14 @@ public class ExplosionConfigScreen extends Screen { try { this.depthBox.tick(); int value = Integer.parseInt(this.depthBox.getValue()); - this.depthBox.setTextColor(WHITE); - if (value != explosionDepth) { - this.explosionDepth = value; - this.dirty = true; + if (value <= 0 || value > CommonConfig.MAX_EXPLOSION_DEPTH.get()) { + this.depthBox.setTextColor(RED); + } else { + this.depthBox.setTextColor(WHITE); + if (value != explosionDepth) { + this.explosionDepth = value; + this.dirty = true; + } } } catch (NumberFormatException ex) { this.depthBox.setTextColor(RED); @@ -166,29 +179,20 @@ public class ExplosionConfigScreen extends Screen { try { this.heightBox.tick(); int value = Integer.parseInt(this.heightBox.getValue()); - this.heightBox.setTextColor(WHITE); - if (value != explosionHeight) { - this.explosionHeight = value; - this.dirty = true; + if (value <= 0 || value > CommonConfig.MAX_EXPLOSION_HEIGHT.get()) { + this.heightBox.setTextColor(RED); + } else { + this.heightBox.setTextColor(WHITE); + if (value != explosionHeight) { + this.explosionHeight = value; + this.dirty = true; + } } } catch (NumberFormatException ex) { this.heightBox.setTextColor(RED); } } - private void tickEditBox(EditBox box, int comp, Function update) { - box.tick(); - try { - int value = Integer.parseInt(box.getValue()); - box.setTextColor(16777215); - if (value != comp) { - update.apply(value); - } - } catch (NumberFormatException ex) { - box.setTextColor(16711680); - } - } - @Override public boolean shouldCloseOnEsc() { return true; @@ -226,6 +230,5 @@ public class ExplosionConfigScreen extends Screen { @Override public void renderBackground(@NotNull PoseStack stack, int _unused) { this.fillGradient(stack, 0, 0, this.width, this.height, -1072689136, -804253680); - net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new ScreenEvent.BackgroundRendered(this, stack)); } } 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 index 61291bb..6a12179 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockDestroyRenderer.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockDestroyRenderer.java @@ -5,6 +5,7 @@ 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.client.config.ClientConfig; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.core.BlockPos; @@ -13,7 +14,6 @@ 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 { @@ -29,7 +29,9 @@ public class BlockDestroyRenderer { @SubscribeEvent public void renderWorldLastEvent(RenderLevelStageEvent event) { - this.renderSelectedBlocks(event); + if (ClientConfig.ENABLE_EXPLOSION_PREVIEW_RENDER.get()) { + this.renderSelectedBlocks(event); + } } protected void renderSelectedBlocks(RenderLevelStageEvent event) { @@ -39,9 +41,7 @@ public class BlockDestroyRenderer { Minecraft mc = Minecraft.getInstance(); - this.highlightedBlocks.stream().filter(x -> { - return x.getA().distToCenterSqr(mc.gameRenderer.getMainCamera().getPosition()) < 256D; - }).forEach(pos -> { + this.highlightedBlocks.stream().filter(x -> x.getA().distToCenterSqr(mc.gameRenderer.getMainCamera().getPosition()) < 256D).forEach(pos -> { PoseStack stack = event.getPoseStack(); MultiBufferSource.BufferSource buf = mc.renderBuffers().bufferSource(); VertexConsumer builder = buf.getBuffer(BlockOutlineRenderType.OVERLAY_LINES); 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 index b0ca8f4..9c313f8 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockOutlineRenderType.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/client/render/BlockOutlineRenderType.java @@ -6,7 +6,6 @@ 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", diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/TickScheduler.java b/src/main/java/com/oitsjustjose/charged_explosives/common/TickScheduler.java index 044a443..e76e603 100644 --- a/src/main/java/com/oitsjustjose/charged_explosives/common/TickScheduler.java +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/TickScheduler.java @@ -6,6 +6,7 @@ import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import java.util.Set; +import java.util.UUID; public class TickScheduler { protected Set tasks = Sets.newConcurrentHashSet(); @@ -14,22 +15,29 @@ public class TickScheduler { @SubscribeEvent public void registerEvent(TickEvent.ServerTickEvent event) { long now = System.currentTimeMillis(); - tasks.removeIf(x -> x.ready(now)); + this.tasks.removeIf(x -> x.ready(now)); } - public void addTask(ScheduledTask task) { + public UUID addTask(ScheduledTask task) { this.tasks.add(task); + return task.getUuid(); + } + + public void cancelTask(UUID uuid) { + this.tasks.removeIf(x -> x.uuid.equals(uuid)); } public static class ScheduledTask { public final Runnable exec; public final long startTime; public final int delayTimeSeconds; + private final UUID uuid; public ScheduledTask(Runnable exec, int delay) { this.exec = exec; this.delayTimeSeconds = delay; this.startTime = System.currentTimeMillis(); + this.uuid = UUID.randomUUID(); } public boolean ready(long time) { @@ -37,5 +45,9 @@ public class TickScheduler { if (ready) this.exec.run(); return ready; } + + public UUID getUuid() { + return this.uuid; + } } } 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 a1ebde3..c62717f 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 @@ -2,6 +2,7 @@ package com.oitsjustjose.charged_explosives.common.blocks; import com.oitsjustjose.charged_explosives.ChargedExplosives; import com.oitsjustjose.charged_explosives.common.TickScheduler; +import com.oitsjustjose.charged_explosives.common.config.CommonConfig; import com.oitsjustjose.charged_explosives.common.tile.ChargedExplosiveBlockEntity; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -10,12 +11,12 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; +import net.minecraft.world.damagesource.DamageSource; 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.item.Items; -import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; @@ -26,8 +27,10 @@ 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.entity.EntityTypeTest; import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.MaterialColor; +import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; @@ -35,7 +38,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.List; +import java.util.Objects; public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBlock implements EntityBlock { @@ -84,7 +88,7 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc } @Override - public @NotNull VoxelShape getCollisionShape(BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) { + public @NotNull VoxelShape getCollisionShape(@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) { return this.getShape(state, level, pos, context); } @@ -104,17 +108,23 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc be.load(tag); if (placer.getOffhandItem().getItem() == Items.REDSTONE_TORCH) { this.explode(level, pos); + } else if (level.hasNeighborSignal(pos)) { + this.explode(level, pos); } } @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.endPreviewExplosion(cebe.getCorners()); - } - level.removeBlockEntity(pos); + public void onRemove(@NotNull BlockState oldState, @NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState newState, boolean moving) { + if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { + ChargedExplosives.getInstance().PROXY.endPreviewExplosion(cebe.getCorners()); + ItemStack stack = new ItemStack(ChargedExplosives.getInstance().REGISTRY.CeItem.get()); + cebe.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); } + + level.removeBlockEntity(pos); super.onRemove(oldState, level, pos, newState, moving); } @@ -125,55 +135,26 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc } @Override - // TODO: apply same logic if block is just *poof'd* - public void playerWillDestroy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull Player player) { - if (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); - } - // TODO (MAYBE): ability to store upcoming tasks for this tile entity and cancel them on break - } - - @Override - public InteractionResult use(@Nonnull BlockState state, @Nonnull Level level, @Nonnull BlockPos pos, @Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull BlockHitResult hit) { + public @NotNull InteractionResult use(@Nonnull BlockState state, @Nonnull Level level, @Nonnull BlockPos pos, @Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull BlockHitResult hit) { if (level.isClientSide()) { player.swing(hand); return InteractionResult.SUCCESS; } - explode(level, pos); + this.explode(level, pos); return InteractionResult.CONSUME; } @Override - public void neighborChanged(BlockState state, Level level, BlockPos pos, Block block, BlockPos fromPos, boolean isMoving) { + public void neighborChanged(@NotNull BlockState state, Level level, @NotNull BlockPos pos, @NotNull Block block, @NotNull BlockPos fromPos, boolean isMoving) { if (level.hasNeighborSignal(pos)) { if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { if (!cebe.hasBeenActivated()) { - cebe.setActivated(); - explode(level, pos); + this.explode(level, pos); } } } } - @Nullable - @Override - public BlockState getStateForPlacement(BlockPlaceContext ctx) { - if (ctx.getLevel().hasNeighborSignal(ctx.getClickedPos())) { - this.explode(ctx.getLevel(), ctx.getClickedPos()); - } - return super.getStateForPlacement(ctx); -// return (BlockState)this\.defaultBlockState().setValue(LIT, p_55659_.getLevel().hasNeighborSignal(p_55659_.getClickedPos())); - } - private void explode(Level level, BlockPos pos) { if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { if (cebe.hasBeenActivated()) { @@ -181,32 +162,32 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc } else { cebe.setActivated(); } + + + cebe.addScheduledTask(ChargedExplosives.getInstance().SCHEDULER.addTask(new TickScheduler.ScheduledTask(() -> this.explodeTask(level, pos), CommonConfig.EXPLOSION_COUNTDOWN_TIME.get()))); + for (int i = 0; i < CommonConfig.NUM_BEEPS.get(); i++) { + cebe.addScheduledTask(ChargedExplosives.getInstance().SCHEDULER.addTask(new TickScheduler.ScheduledTask(() -> { + if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity) { // check that it's still there + level.playSound(null, pos, ChargedExplosives.getInstance().REGISTRY.BeepBoop.get(), SoundSource.BLOCKS, 0.5F, 1.0F); + } + }, (CommonConfig.EXPLOSION_COUNTDOWN_TIME.get() / CommonConfig.NUM_BEEPS.get()) * i))); + } } - // TODO: configs - final int DELAY_TIME_SECONDS = 5; - final int NUM_WARNING_BEEPS = 5; - - - ChargedExplosives.getInstance().SCHEDULER.addTask(new TickScheduler.ScheduledTask(() -> this.explodeTask(level, pos), DELAY_TIME_SECONDS)); - for (int i = 0; i < NUM_WARNING_BEEPS; i++) { - ChargedExplosives.getInstance().SCHEDULER.addTask(new TickScheduler.ScheduledTask( - () -> { - if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity) { // check that it's still there - level.playSound(null, pos, ChargedExplosives.getInstance().REGISTRY.BeepBoop.get(), SoundSource.BLOCKS, 0.5F, 1.0F); - } - }, - (DELAY_TIME_SECONDS / NUM_WARNING_BEEPS) * i - )); - } } private void explodeTask(Level level, BlockPos pos) { if (level.getBlockEntity(pos) instanceof ChargedExplosiveBlockEntity cebe) { /* ↥ WARNING: the below block will *destroy* cebe, so anything using should go here ↥ */ - AtomicInteger explodedCount = new AtomicInteger(); + level.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 1.0F); + ChargedExplosives.getInstance().PROXY.endPreviewExplosion(cebe.getCorners()); + level.removeBlockEntity(pos); + level.removeBlock(pos, false); + + AABB aabb = new AABB(cebe.getCorners().getA(), cebe.getCorners().getB()).inflate(5.0D); + float dmgAmt = cebe.calculateConcussiveDamage(); cebe.getExplosions().forEach(p -> { BlockState state = level.getBlockState(p); if (!state.hasBlockEntity()) { @@ -215,22 +196,15 @@ public class ChargedExplosiveBlock extends FaceAttachedHorizontalDirectionalBloc level.removeBlock(p, false); /* Ignore the yellow squiggles below - p_49886 is passed to a func where it's Nullable */ Block.dropResources(state, level, p, null, null, ItemStack.EMPTY); - explodedCount.getAndIncrement(); if (level.getRandom().nextBoolean()) { ChargedExplosives.getInstance().PROXY.spawnExplosionParticle(p); } } } }); + List entities = level.getEntities(EntityTypeTest.forClass(LivingEntity.class), aabb, Objects::nonNull); - -// TODO: if explosion doesn't happen, make a new sound - if (explodedCount.get() > 0) { - level.playSound(null, pos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 1.0F, 1.0F); - ChargedExplosives.getInstance().PROXY.endPreviewExplosion(cebe.getCorners()); - level.removeBlockEntity(pos); - level.removeBlock(pos, false); - } + entities.forEach(x -> x.hurt(DamageSource.GENERIC, dmgAmt)); } } } diff --git a/src/main/java/com/oitsjustjose/charged_explosives/common/config/CommonConfig.java b/src/main/java/com/oitsjustjose/charged_explosives/common/config/CommonConfig.java new file mode 100644 index 0000000..be0cc83 --- /dev/null +++ b/src/main/java/com/oitsjustjose/charged_explosives/common/config/CommonConfig.java @@ -0,0 +1,55 @@ +package com.oitsjustjose.charged_explosives.common.config; + +import java.nio.file.Path; + +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.electronwill.nightconfig.core.io.WritingMode; + +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.common.ForgeConfigSpec.Builder; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber +public class CommonConfig { + public static final ForgeConfigSpec COMMON_CONFIG; + private static final Builder COMMON_BUILDER = new Builder(); + + public static ForgeConfigSpec.DoubleValue CONCUSSIVE_DAMAGE_SCALE; + public static ForgeConfigSpec.IntValue MAX_EXPLOSION_WIDTH; + public static ForgeConfigSpec.IntValue MAX_EXPLOSION_HEIGHT; + public static ForgeConfigSpec.IntValue MAX_EXPLOSION_DEPTH; + public static ForgeConfigSpec.IntValue EXPLOSION_COUNTDOWN_TIME; + public static ForgeConfigSpec.IntValue NUM_BEEPS; + private static final String CATEGORY_MISC = "common_configuration"; + + static { + init(); + COMMON_CONFIG = COMMON_BUILDER.build(); + } + + public static void loadConfig(ForgeConfigSpec spec, Path path) { + final CommentedFileConfig configData = CommentedFileConfig.builder(path).sync().autosave() + .writingMode(WritingMode.REPLACE).build(); + configData.load(); + spec.setConfig(configData); + } + + private static void init() { + COMMON_BUILDER.comment("Miscellaneous").push(CATEGORY_MISC); + + CONCUSSIVE_DAMAGE_SCALE = COMMON_BUILDER.comment("A percentage (decimal from 0-1) of how much concussive damage should be done by charges. 0 disables concussive damage.", + "Values greater than 0 will hurt anything in a radius of the blast by an amount (calculated by the explosive's width, depth and height) times this value.") + .defineInRange("concussiveDamageScale", 0.1, 0.0, 1.0); + + MAX_EXPLOSION_WIDTH = COMMON_BUILDER.comment("The max explosion width a user can set in their explosive").defineInRange("maxExplosionWidth", 11, 1, 128); + MAX_EXPLOSION_HEIGHT = COMMON_BUILDER.comment("The max explosion height a user can set in their explosive").defineInRange("maxExplosionHeight", 11, 1, 128); + MAX_EXPLOSION_DEPTH = COMMON_BUILDER.comment("The max explosion depth a user can set in their explosive").defineInRange("maxExplosionDepth", 11, 1, 128); + + EXPLOSION_COUNTDOWN_TIME = COMMON_BUILDER.comment("The amount of time between activating an explosive and when it detonates, in seconds. Setting to 0 makes it instant") + .defineInRange("detonationCountdownTime", 5, 0, Integer.MAX_VALUE); + NUM_BEEPS=COMMON_BUILDER.comment("The number of warning beeps given after setting an explosive off to detonate. Time is distributed evenly across the value configured in 'detonationCountdownTime'") + .defineInRange("detonationBeepCount", 5, 0, Integer.MAX_VALUE); + + COMMON_BUILDER.pop(); + } +} \ No newline at end of file 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 46c25e2..3630236 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 @@ -3,6 +3,7 @@ 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 com.oitsjustjose.charged_explosives.common.config.CommonConfig; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; @@ -40,52 +41,70 @@ public class ChargedExplosiveItem extends BlockItem { return super.use(level, player, hand); } - public static boolean isNbtInvalid(ItemStack stack) { + public static boolean isNbtInitialized(ItemStack stack) { if (stack.hasTag()) { CompoundTag tag = Objects.requireNonNull(stack.getTag()); - return !tag.contains("explosionWidth") || !tag.contains("explosionHeight") || !tag.contains("explosionDepth"); + return tag.contains("explosionWidth") && tag.contains("explosionHeight") && tag.contains("explosionDepth"); } - return true; + return false; + } + + public static boolean isNbtValid(ItemStack stack) { + if (stack.hasTag() && isNbtInitialized(stack)) { + CompoundTag tag = Objects.requireNonNull(stack.getTag()); + int w = tag.getInt("explosionWidth"); + int h = tag.getInt("explosionHeight"); + int d = tag.getInt("explosionDepth"); + boolean zeroCheck = w > 0 && h > 0 && d > 0; + boolean configCheck = w <= CommonConfig.MAX_EXPLOSION_WIDTH.get() && h <= CommonConfig.MAX_EXPLOSION_HEIGHT.get() && d <= CommonConfig.MAX_EXPLOSION_DEPTH.get(); + return zeroCheck && configCheck; + } + return false; } @Override public @NotNull InteractionResult place(BlockPlaceContext context) { ItemStack stack = context.getItemInHand(); - if (isNbtInvalid(stack)) { + // TODO: noises + if (!isNbtInitialized(stack)) { Objects.requireNonNull(context.getPlayer()).displayClientMessage(Util.translateOrFallback(ChargedExplosives.MODID + ".needs_init"), true); return InteractionResult.FAIL; } + if (!isNbtValid(stack)) { + Objects.requireNonNull(context.getPlayer()).displayClientMessage(Util.translateOrFallback(ChargedExplosives.MODID + ".invalid_config"), true); + return InteractionResult.FAIL; + } return super.place(context); } @Override protected boolean updateCustomBlockEntityTag(@Nonnull BlockPos p, @Nonnull Level l, @Nullable Player pl, @Nonnull ItemStack s, @Nonnull BlockState st) { - return true; + return false; } @Override public void appendHoverText(@NotNull ItemStack stack, @Nullable Level level, @NotNull List components, @NotNull TooltipFlag flag) { - if (!stack.hasTag()) { + if (!isNbtInitialized(stack)) { + components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".needs_init_tooltip")); + return; + } + if (!isNbtValid(stack)) { + components.add(Util.translateOrFallback(ChargedExplosives.MODID + ".invalid_configuration_tooltip")); return; } CompoundTag tag = Objects.requireNonNull(stack.getTag()); - if (isNbtInvalid(stack)) { - 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) { - } + 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/tile/ChargedExplosiveBlockEntity.java b/src/main/java/com/oitsjustjose/charged_explosives/common/tile/ChargedExplosiveBlockEntity.java index 5f95b53..1bda27c 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 @@ -1,6 +1,7 @@ package com.oitsjustjose.charged_explosives.common.tile; import com.oitsjustjose.charged_explosives.ChargedExplosives; +import com.oitsjustjose.charged_explosives.common.config.CommonConfig; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -11,8 +12,10 @@ 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 org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.UUID; public class ChargedExplosiveBlockEntity extends BlockEntity { private int explosionWidth = 0; @@ -20,11 +23,13 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { private int explosionDepth = 0; private boolean goingOffNow = false; private ArrayList explosionPositions; + private final ArrayList scheduledTasks; private Tuple cornerToCorner; public ChargedExplosiveBlockEntity(BlockPos pos, BlockState state) { super(ChargedExplosives.getInstance().REGISTRY.CeBlockEntityType.get(), pos, state); this.explosionPositions = new ArrayList<>(); + this.scheduledTasks = new ArrayList<>(); } @Override @@ -35,7 +40,7 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { } @Override - public void load(CompoundTag tag) { + public void load(@NotNull CompoundTag tag) { super.load(tag); this.explosionWidth = tag.getInt("explosionWidth"); this.explosionHeight = tag.getInt("explosionHeight"); @@ -51,7 +56,13 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { tag.putInt("explosionWidth", explosionWidth); tag.putInt("explosionHeight", explosionHeight); tag.putInt("explosionDepth", explosionDepth); - super.saveToItem(stack); + } + + public float calculateConcussiveDamage() { + return (((float) this.explosionWidth / (CommonConfig.MAX_EXPLOSION_WIDTH.get() / 2F)) + + ((float) this.explosionHeight / (CommonConfig.MAX_EXPLOSION_HEIGHT.get() / 2F)) + + ((float) this.explosionDepth / (CommonConfig.MAX_EXPLOSION_DEPTH.get() / 2F))) * + CommonConfig.CONCUSSIVE_DAMAGE_SCALE.get().floatValue(); } public ArrayList getExplosions() { @@ -124,7 +135,6 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { endZ = pos.getZ() + (int) Math.ceil(explosionWidth / 2F); } default -> { - break; } } } @@ -166,4 +176,14 @@ public class ChargedExplosiveBlockEntity extends BlockEntity { public void setActivated() { this.goingOffNow = true; } + + public void addScheduledTask(UUID uuid) { + this.scheduledTasks.add(uuid); + } + + @Override + public void setRemoved() { + this.scheduledTasks.forEach(ChargedExplosives.getInstance().SCHEDULER::cancelTask); + super.setRemoved(); + } } 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 446ed06..1108bad 100644 --- a/src/main/resources/assets/charged_explosives/lang/en_us.json +++ b/src/main/resources/assets/charged_explosives/lang/en_us.json @@ -3,11 +3,13 @@ "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.needs_init_tooltip": "§8Unconfigured§r", + "charged_explosives.invalid_configuration_tooltip": "§8Invalid Config§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", - "charged_explosives.needs_init": "This Explosive Needs to be Configured", + "charged_explosives.needs_init": "Explosive Needs to be Configured", + "charged_explosives.invalid_config": "Explosive Has Invalid Configuration", "charge.beep.boops": "§cCharged Explosive Beeps Aggressively§r", "block.charged_explosives.charged_explosive": "Explosive Charge" } \ No newline at end of file diff --git a/src/main/resources/assets/charged_explosives/models/block/charged_explosive.json b/src/main/resources/assets/charged_explosives/models/block/charged_explosive.json index a26f139..7ede13f 100644 --- a/src/main/resources/assets/charged_explosives/models/block/charged_explosive.json +++ b/src/main/resources/assets/charged_explosives/models/block/charged_explosive.json @@ -36,8 +36,9 @@ ], "display": { "thirdperson_righthand": { - "translation": [0, 3, 1], - "scale": [0.55, 0.55, 0.55] + "rotation": [65, 0, 0], + "translation": [0, 3, 4.75], + "scale": [0.75, 0.75, 0.75] }, "thirdperson_lefthand": { "rotation": [65, 0, 0],