diff --git a/gradle.properties b/gradle.properties index 947a256..9ea3e74 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.2.0 +mod_version=0.3.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 1190605..7ee6e54 100644 --- a/src/main/java/com/jenny/compressedtnt/blocks/blocks.java +++ b/src/main/java/com/jenny/compressedtnt/blocks/blocks.java @@ -46,6 +46,9 @@ public class blocks { public static final RegistryObject 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 TNT_BLACK_HOLE_ITEM = ITEMS.register("tnt_black_hole", () -> new BlockItem(TNT_BLACK_HOLE.get(), new Item.Properties())); + public static final RegistryObject TNT_CLAYMORE= BLOCKS.register("tnt_claymore", () -> new claymoreTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 2.0f, 20, 256)); + public static final RegistryObject TNT_CLAYMORE_ITEM = ITEMS.register("tnt_claymore", () -> new BlockItem(TNT_CLAYMORE.get(), new Item.Properties())); + public static void register(IEventBus bus) { BLOCKS.register(bus); diff --git a/src/main/java/com/jenny/compressedtnt/blocks/claymoreTNTBlock.java b/src/main/java/com/jenny/compressedtnt/blocks/claymoreTNTBlock.java new file mode 100644 index 0000000..4b67c0c --- /dev/null +++ b/src/main/java/com/jenny/compressedtnt/blocks/claymoreTNTBlock.java @@ -0,0 +1,59 @@ +package com.jenny.compressedtnt.blocks; + +import com.jenny.compressedtnt.entities.claymorePrimedTNT; +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 claymoreTNTBlock extends TntBlock { + public final float pRadius; + public final int fuseTime; + public final int projectileCount; + + public claymoreTNTBlock(Properties p_57422_, float pRadius, int fuseTime, int projectileCount) { + super(p_57422_); + this.pRadius = pRadius; + this.fuseTime = fuseTime; + this.projectileCount = projectileCount; + } + + @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, this.projectileCount); + } + + @Deprecated + public static void explode(Level p_57434_, BlockPos p_57435_, float pRadius, int fuseTime, int projectileCount) { + explode(p_57434_, p_57435_, (LivingEntity)null, pRadius, fuseTime, projectileCount); + } + + @Deprecated + private static void explode(Level level, BlockPos blockPos, @Nullable LivingEntity entity, float pRadius, int fuseTime, int projectileCount) { + if (!level.isClientSide) { + claymorePrimedTNT primedtnt = new claymorePrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, entity, pRadius, fuseTime, projectileCount); + level.addFreshEntity(primedtnt); + level.gameEvent(entity, GameEvent.PRIME_FUSE, blockPos); + level.playSound((Player) null, blockPos.getX(), blockPos.getY(), blockPos.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0F, 1.0F); + } + } + + @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); + claymorePrimedTNT primedtnt = new claymorePrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, pExplosion.getIndirectSourceEntity(), pRadius, ft, projectileCount); + level.addFreshEntity(primedtnt); + } + } +} diff --git a/src/main/java/com/jenny/compressedtnt/creativeTab.java b/src/main/java/com/jenny/compressedtnt/creativeTab.java index 8e61f06..5bb4827 100644 --- a/src/main/java/com/jenny/compressedtnt/creativeTab.java +++ b/src/main/java/com/jenny/compressedtnt/creativeTab.java @@ -25,6 +25,7 @@ public class creativeTab { output.accept(blocks.TNT_CLUSTER_8.get()); output.accept(blocks.TNT_HOMING.get()); output.accept(blocks.TNT_BLACK_HOLE.get()); + output.accept(blocks.TNT_CLAYMORE.get()); }).title(Component.literal("Compressed TNT")).build()); public static void register(IEventBus bus) { diff --git a/src/main/java/com/jenny/compressedtnt/entities/claymorePrimedTNT.java b/src/main/java/com/jenny/compressedtnt/entities/claymorePrimedTNT.java new file mode 100644 index 0000000..858359c --- /dev/null +++ b/src/main/java/com/jenny/compressedtnt/entities/claymorePrimedTNT.java @@ -0,0 +1,76 @@ +package com.jenny.compressedtnt.entities; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.syncher.EntityDataAccessor; +import net.minecraft.network.syncher.EntityDataSerializers; +import net.minecraft.network.syncher.SynchedEntityData; +import net.minecraft.util.RandomSource; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.projectile.Arrow; +import net.minecraft.world.entity.projectile.Projectile; +import net.minecraft.world.level.Level; +import net.minecraft.world.phys.Vec3; + +import javax.annotation.Nullable; + +public class claymorePrimedTNT extends basePrimedTNT { + private static final EntityDataAccessor DATA_PCOUNT_ID = SynchedEntityData.defineId(claymorePrimedTNT.class, EntityDataSerializers.INT); + + public claymorePrimedTNT(Level pLevel, double pX, double pY, double pZ, @Nullable LivingEntity pOwner, float power, int fuse, int projectileCount) { + super(entities.TNT_CLAYMORE.get(), pLevel, pOwner, new Vec3(pX, pY, pZ), fuse, power, "tnt_claymore"); + setPCount(projectileCount); + } + + public claymorePrimedTNT(EntityType entityType, Level level) { + super(entityType, level, null); + setRenderID("tnt_claymore"); + } + + public Vec3 targetVector(RandomSource rng) { + return new Vec3( + (double) rng.nextInt(-10, 11) / 10, + (double) rng.nextInt( 11) / 10, + (double) rng.nextInt(-10, 11) / 10); + } + + @Override + public void explode() { + super.explode(); + RandomSource rng = level().getRandom(); + for (int i = 0; i < getPCount(); i++) { + Vec3 target = targetVector(rng); + Vec3 pos = position().add(target); + Projectile e = new Arrow(level(),pos.x, pos.y + 1, pos.z); + e.setDeltaMovement(target.multiply(5, 0.1, 5)); + level().addFreshEntity(e); + } + + } + + @Override + protected void addAdditionalSaveData(CompoundTag pCompound) { + pCompound.putFloat("ProjectileCount", this.getPCount()); + super.addAdditionalSaveData(pCompound); + } + + @Override + protected void readAdditionalSaveData(CompoundTag pCompound) { + this.setPCount(pCompound.getInt("ProjectileCount")); + super.readAdditionalSaveData(pCompound); + } + + public void setPCount(int pCount) { + this.entityData.set(DATA_PCOUNT_ID, pCount); + } + + public int getPCount() { + return this.entityData.get(DATA_PCOUNT_ID); + } + + @Override + protected void defineSynchedData() { + this.entityData.define(DATA_PCOUNT_ID, 16); + super.defineSynchedData(); + } +} diff --git a/src/main/java/com/jenny/compressedtnt/entities/entities.java b/src/main/java/com/jenny/compressedtnt/entities/entities.java index 7be737b..77d61ee 100644 --- a/src/main/java/com/jenny/compressedtnt/entities/entities.java +++ b/src/main/java/com/jenny/compressedtnt/entities/entities.java @@ -29,8 +29,12 @@ public class entities { .sized(0.48F, 0.48F).fireImmune().clientTrackingRange(8).build("tnt_cluster")); public static final RegistryObject> TNT_BLACK_HOLE = - ENTITY_TYPES.register("tnt_chunk", () -> EntityType.Builder.of(blackHolePrimedTNT::new, MobCategory.MISC) - .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_chunk")); + ENTITY_TYPES.register("tnt_blackhole", () -> EntityType.Builder.of(blackHolePrimedTNT::new, MobCategory.MISC) + .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_blackhole")); + + public static final RegistryObject> TNT_CLAYMORE = + ENTITY_TYPES.register("tnt_claymore", () -> EntityType.Builder.of(claymorePrimedTNT::new, MobCategory.MISC) + .sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_claymore")); public static void register(IEventBus eventBus) { ENTITY_TYPES.register(eventBus);