From 9ab8c6495f3583ea8375a7d9225d6cafbb2dec73 Mon Sep 17 00:00:00 2001
From: Jenny <JennyTSonline@proton.me>
Date: Sat, 25 Jan 2025 02:44:51 +0100
Subject: [PATCH] added selective TNT

---
 gradle.properties                             |  2 +-
 .../jenny/compressedtnt/blocks/blocks.java    |  8 ++-
 .../blocks/selectiveTNTBlock.java             | 59 +++++++++++++++++++
 .../com/jenny/compressedtnt/creativeTab.java  |  1 +
 .../compressedtnt/entities/entities.java      |  5 ++
 .../entities/tnt/selectivePrimedTNT.java      | 47 +++++++++++++++
 .../assets/compressedtnt/lang/en_us.json      |  2 +
 7 files changed, 120 insertions(+), 4 deletions(-)
 create mode 100644 src/main/java/com/jenny/compressedtnt/blocks/selectiveTNTBlock.java
 create mode 100644 src/main/java/com/jenny/compressedtnt/entities/tnt/selectivePrimedTNT.java

diff --git a/gradle.properties b/gradle.properties
index 67d018d..00881a6 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -38,7 +38,7 @@ mod_name=Compressed TNT
 # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
 mod_license=All Rights Reserved
 # The mod version. See https://semver.org/
-mod_version=0.6.0
+mod_version=0.7.0
 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
 # This should match the base package used for the mod sources.
 # See https://maven.apache.org/guides/mini/guide-naming-conventions.html
diff --git a/src/main/java/com/jenny/compressedtnt/blocks/blocks.java b/src/main/java/com/jenny/compressedtnt/blocks/blocks.java
index dbe0b6a..0e53ca1 100644
--- a/src/main/java/com/jenny/compressedtnt/blocks/blocks.java
+++ b/src/main/java/com/jenny/compressedtnt/blocks/blocks.java
@@ -42,15 +42,17 @@ public class blocks {
     public static final RegistryObject<Block> TNT_CLUSTER_8 = BLOCKS.register("tnt_cluster_8", () -> new ClusterTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 8, 10));
     public static final RegistryObject<Item> TNT_CLUSTER_8_ITEM = ITEMS.register("tnt_cluster_8", () -> new BlockItemTooltip(TNT_CLUSTER_8.get(), new Item.Properties()));
 
-    public static final RegistryObject<Block> TNT_HOMING= BLOCKS.register("tnt_homing", () -> new homingTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 1));
+    public static final RegistryObject<Block> TNT_HOMING = BLOCKS.register("tnt_homing", () -> new homingTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 1));
     public static final RegistryObject<Item> TNT_HOMING_ITEM = ITEMS.register("tnt_homing", () -> new BlockItemTooltip(TNT_HOMING.get(), new Item.Properties()));
 
-    public static final RegistryObject<Block> TNT_BLACK_HOLE= BLOCKS.register("tnt_black_hole", () -> new blackHoleTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 1));
+    public static final RegistryObject<Block> TNT_BLACK_HOLE = BLOCKS.register("tnt_black_hole", () -> new blackHoleTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 1));
     public static final RegistryObject<Item> TNT_BLACK_HOLE_ITEM = ITEMS.register("tnt_black_hole", () -> new BlockItemTooltip(TNT_BLACK_HOLE.get(), new Item.Properties()));
 
-    public static final RegistryObject<Block> TNT_CLAYMORE= BLOCKS.register("tnt_claymore", () -> new claymoreTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 2.0f, 20, 256));
+    public static final RegistryObject<Block> TNT_CLAYMORE =  BLOCKS.register("tnt_claymore", () -> new claymoreTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 2.0f, 20, 256));
     public static final RegistryObject<Item> TNT_CLAYMORE_ITEM = ITEMS.register("tnt_claymore", () -> new BlockItemTooltip(TNT_CLAYMORE.get(), new Item.Properties()));
 
+    public static final RegistryObject<Block> TNT_SELECTIVE = BLOCKS.register("tnt_selective", () -> new selectiveTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 32.0f, 80));
+    public static final RegistryObject<Item> TNT_SELECTIVE_ITEM = ITEMS.register("tnt_selective", () -> new BlockItemTooltip(TNT_SELECTIVE.get(), new Item.Properties()));
 
     public static void register(IEventBus bus) {
         BLOCKS.register(bus);
diff --git a/src/main/java/com/jenny/compressedtnt/blocks/selectiveTNTBlock.java b/src/main/java/com/jenny/compressedtnt/blocks/selectiveTNTBlock.java
new file mode 100644
index 0000000..daf853c
--- /dev/null
+++ b/src/main/java/com/jenny/compressedtnt/blocks/selectiveTNTBlock.java
@@ -0,0 +1,59 @@
+package com.jenny.compressedtnt.blocks;
+
+import com.jenny.compressedtnt.entities.tnt.selectivePrimedTNT;
+import com.jenny.compressedtnt.entities.tnt.StrongerPrimedTNT;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
+import net.minecraft.sounds.SoundEvents;
+import net.minecraft.sounds.SoundSource;
+import net.minecraft.world.entity.LivingEntity;
+import net.minecraft.world.entity.player.Player;
+import net.minecraft.world.level.Explosion;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.block.TntBlock;
+import net.minecraft.world.level.block.state.BlockState;
+import net.minecraft.world.level.gameevent.GameEvent;
+import org.jetbrains.annotations.NotNull;
+
+import javax.annotation.Nullable;
+
+public class selectiveTNTBlock extends TntBlock {
+    public final float pRadius;
+    public final int fuseTime;
+
+    public selectiveTNTBlock(Properties p_57422_, float pRadius, int fuseTime) {
+        super(p_57422_);
+        this.pRadius = pRadius;
+        this.fuseTime = fuseTime;
+    }
+
+    @Override
+    public void onCaughtFire(@NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, @Nullable Direction face, @Nullable LivingEntity igniter) {
+        explode(world, pos, igniter, this.pRadius, this.fuseTime);
+    }
+
+    @Deprecated
+    public static void explode(Level p_57434_, BlockPos p_57435_, float pRadius, int fuseTime) {
+        explode(p_57434_, p_57435_, (LivingEntity)null, pRadius, fuseTime);
+    }
+
+    @Deprecated
+    private static void explode(Level p_57437_, BlockPos p_57438_, @Nullable LivingEntity p_57439_, float pRadius, int fuseTime) {
+        if (!p_57437_.isClientSide) {
+            selectivePrimedTNT primedtnt = new selectivePrimedTNT(p_57437_, (double)p_57438_.getX() + (double)0.5F, (double)p_57438_.getY(), (double)p_57438_.getZ() + (double)0.5F, p_57439_, pRadius, fuseTime);
+            p_57437_.addFreshEntity(primedtnt);
+            p_57437_.playSound((Player)null, primedtnt.getX(), primedtnt.getY(), primedtnt.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0F, 1.0F);
+            p_57437_.gameEvent(p_57439_, GameEvent.PRIME_FUSE, p_57438_);
+        }
+
+    }
+
+    @Override
+    public void wasExploded(Level level, @NotNull BlockPos blockPos, @NotNull Explosion pExplosion) {
+        if (!level.isClientSide) {
+            int ft = (short) (level.random.nextInt(fuseTime / 4) + fuseTime / 8);
+            selectivePrimedTNT primedtnt = new selectivePrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, pExplosion.getIndirectSourceEntity(), pRadius, ft);
+            level.addFreshEntity(primedtnt);
+        }
+    }
+}
diff --git a/src/main/java/com/jenny/compressedtnt/creativeTab.java b/src/main/java/com/jenny/compressedtnt/creativeTab.java
index 4be7301..b5f897f 100644
--- a/src/main/java/com/jenny/compressedtnt/creativeTab.java
+++ b/src/main/java/com/jenny/compressedtnt/creativeTab.java
@@ -27,6 +27,7 @@ public class creativeTab {
         output.accept(blocks.TNT_HOMING.get());
         output.accept(blocks.TNT_BLACK_HOLE.get());
         output.accept(blocks.TNT_CLAYMORE.get());
+        output.accept(blocks.TNT_SELECTIVE.get());
         output.accept(items.TNT_ARROW.get());
         output.accept(items.CONCUSSIVE_ARROW.get());
         output.accept(items.CARPET_ARROW.get());
diff --git a/src/main/java/com/jenny/compressedtnt/entities/entities.java b/src/main/java/com/jenny/compressedtnt/entities/entities.java
index b6c3289..04d4eea 100644
--- a/src/main/java/com/jenny/compressedtnt/entities/entities.java
+++ b/src/main/java/com/jenny/compressedtnt/entities/entities.java
@@ -37,6 +37,10 @@ public class entities {
             ENTITY_TYPES.register("tnt_blackhole", () -> EntityType.Builder.<blackHolePrimedTNT>of(blackHolePrimedTNT::new, MobCategory.MISC)
                     .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_blackhole"));
 
+    public static final RegistryObject<EntityType<selectivePrimedTNT>> TNT_SELECTIVE =
+            ENTITY_TYPES.register("tnt_selective", () -> EntityType.Builder.<selectivePrimedTNT>of(selectivePrimedTNT::new, MobCategory.MISC)
+                    .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_selective"));
+
     public static final RegistryObject<EntityType<claymorePrimedTNT>> TNT_CLAYMORE =
             ENTITY_TYPES.register("tnt_claymore", () -> EntityType.Builder.<claymorePrimedTNT>of(claymorePrimedTNT::new, MobCategory.MISC)
                     .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_claymore"));
@@ -66,6 +70,7 @@ public class entities {
         EntityRenderers.register(TNT_HOMING.get(), BaseTNTRenderer::new);
         EntityRenderers.register(TNT_BLACK_HOLE.get(), BaseTNTRenderer::new);
         EntityRenderers.register(TNT_CLAYMORE.get(), BaseTNTRenderer::new);
+        EntityRenderers.register(TNT_SELECTIVE.get(), BaseTNTRenderer::new);
         EntityRenderers.register(TNT_CLUSTER.get(), clusterTNTRenderer::new);
 
         EntityRenderers.register(ARROW_TNT.get(), TNTArrowRenderer::new);
diff --git a/src/main/java/com/jenny/compressedtnt/entities/tnt/selectivePrimedTNT.java b/src/main/java/com/jenny/compressedtnt/entities/tnt/selectivePrimedTNT.java
new file mode 100644
index 0000000..26b4e42
--- /dev/null
+++ b/src/main/java/com/jenny/compressedtnt/entities/tnt/selectivePrimedTNT.java
@@ -0,0 +1,47 @@
+package com.jenny.compressedtnt.entities.tnt;
+
+import com.jenny.compressedtnt.entities.entities;
+import net.minecraft.core.BlockPos;
+import net.minecraft.world.entity.EntityType;
+import net.minecraft.world.entity.LivingEntity;
+import net.minecraft.world.level.BlockGetter;
+import net.minecraft.world.level.Explosion;
+import net.minecraft.world.level.ExplosionDamageCalculator;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.block.state.BlockState;
+import net.minecraft.world.phys.Vec3;
+import org.jetbrains.annotations.NotNull;
+
+import javax.annotation.Nullable;
+
+public class selectivePrimedTNT extends basePrimedTNT {
+    static class SelectiveExplosionCalculator extends ExplosionDamageCalculator {
+        private final String block;
+
+        public SelectiveExplosionCalculator(String block) {
+            this.block = block;
+        }
+
+        public boolean shouldBlockExplode(@NotNull Explosion pExplosion, @NotNull BlockGetter pReader, @NotNull BlockPos pPos, BlockState pState, float pPower) {
+            return pState.getBlock().toString().equals(block);
+        }
+    }
+
+    public selectivePrimedTNT(Level pLevel, double pX, double pY, double pZ, @Nullable LivingEntity pOwner, float power, int fuse) {
+        super(entities.TNT_SELECTIVE.get(), pLevel, pOwner, new Vec3(pX, pY, pZ), fuse, power, "tnt_selective");
+    }
+
+    public selectivePrimedTNT(EntityType<selectivePrimedTNT> entityType, Level level) {
+        super(entityType, level, null);
+        this.setRenderID("tnt_selective");
+    }
+    @Override
+    protected void explode() {
+        SelectiveExplosionCalculator dmgCalc = new SelectiveExplosionCalculator(getBlock());
+        this.level().explode(this, null, dmgCalc, position(), getPower(), false, Level.ExplosionInteraction.TNT);
+    }
+
+    public String getBlock() {
+        return level().getBlockState(new BlockPos((int) Math.round(getX()), (int) Math.round(getY() - 1), (int) Math.round(getZ()))).getBlock().toString();
+    }
+ }
diff --git a/src/main/resources/assets/compressedtnt/lang/en_us.json b/src/main/resources/assets/compressedtnt/lang/en_us.json
index 4e75f29..8d133b3 100644
--- a/src/main/resources/assets/compressedtnt/lang/en_us.json
+++ b/src/main/resources/assets/compressedtnt/lang/en_us.json
@@ -10,6 +10,7 @@
   "block.compressedtnt.tnt_homing": "Homing TNT",
   "block.compressedtnt.tnt_black_hole": "Black Hole TNT",
   "block.compressedtnt.tnt_claymore": "Claymore TNT",
+  "block.compressedtnt.tnt_selective": "Selective TNT",
 
   "item.compressedtnt.arrow_tnt": "TNT Arrow",
   "item.compressedtnt.arrow_concussive": "Concussive Arrow",
@@ -22,6 +23,7 @@
   "tooltip.compressedtnt.tnt_homing": "follows the closest entity",
   "tooltip.compressedtnt.tnt_black_hole": "pulls all entities towards itself",
   "tooltip.compressedtnt.tnt_claymore": "throws arrows into all direcctions upon explosion",
+  "tooltip.compressedtnt.tnt_selective": "only destroys the type of block it explodes on",
 
   "tooltip.compressedtnt.arrow_tnt": "explodes on impact",
   "tooltip.compressedtnt.arrow_concussive": "explodes on impact without block damage",