added repulsive tnt

This commit is contained in:
Jenny committed 2025-01-31 04:38:18 +01:00
1 parent b8240c61c2
commit 871c1f9888
12 files changed
+188 -1

No files matched your search

@@ -56,6 +56,9 @@ public class blocks {
public static final RegistryObject<Block> TNT_ENDER = BLOCKS.register("tnt_ender", () -> new enderTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80));
public static final RegistryObject<Item> TNT_ENDER_ITEM = ITEMS.register("tnt_ender", () -> new BlockItemTooltip(TNT_ENDER.get(), new Item.Properties()));
public static final RegistryObject<Block> TNT_REPULSIVE = BLOCKS.register("tnt_repulsive", () -> new repulsiveTNTBlock(BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED), 4.0f, 80, 1));
public static final RegistryObject<Item> TNT_REPULSIVE_ITEM = ITEMS.register("tnt_repulsive", () -> new BlockItemTooltip(TNT_REPULSIVE.get(), new Item.Properties()));
public static void register(IEventBus bus) {
BLOCKS.register(bus);
@@ -0,0 +1,60 @@
package com.jenny.enhancedexplosives.blocks;
import com.jenny.enhancedexplosives.entities.tnt.blackHolePrimedTNT;
import com.jenny.enhancedexplosives.entities.tnt.repulsivePrimedTNT;
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 repulsiveTNTBlock extends TntBlock {
public final float pRadius, speed;
public final int fuseTime;
public repulsiveTNTBlock(Properties p_57422_, float pRadius, int fuseTime, float speed) {
super(p_57422_);
this.pRadius = pRadius;
this.fuseTime = fuseTime;
this.speed = speed;
}
@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.speed);
}
@Deprecated
public static void explode(Level p_57434_, BlockPos p_57435_, float pRadius, int fuseTime, float speed) {
explode(p_57434_, p_57435_, (LivingEntity)null, pRadius, fuseTime, speed);
}
@Deprecated
private static void explode(Level level, BlockPos blockPos, @Nullable LivingEntity entity, float pRadius, int fuseTime, float speed) {
if (!level.isClientSide) {
repulsivePrimedTNT primedtnt = new repulsivePrimedTNT(level, (double)blockPos.getX() + (double)0.5F, (double)blockPos.getY(), (double)blockPos.getZ() + (double)0.5F, entity, pRadius, fuseTime, speed);
level.addFreshEntity(primedtnt);
level.playSound((Player)null, primedtnt.getX(), primedtnt.getY(), primedtnt.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0F, 1.0F);
level.gameEvent(entity, GameEvent.PRIME_FUSE, blockPos);
}
}
@Override
public void wasExploded(Level level, @NotNull BlockPos blockPos, @NotNull Explosion pExplosion) {
if (!level.isClientSide) {
repulsivePrimedTNT primedtnt = new repulsivePrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, pExplosion.getIndirectSourceEntity(), pRadius, fuseTime, speed);
int i = primedtnt.getFuse();
primedtnt.setFuse((short) (level.random.nextInt(i / 4) + i / 8));
level.addFreshEntity(primedtnt);
}
}
}
@@ -26,6 +26,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_REPULSIVE.get());
output.accept(blocks.TNT_CLAYMORE.get());
output.accept(blocks.TNT_SELECTIVE.get());
output.accept(blocks.TNT_ENDER.get());
@@ -29,6 +29,7 @@ public class ModBlockStateProvider extends BlockStateProvider {
sideTopBottom(blocks.TNT_ENDER);
sideTopBottom(blocks.TNT_CLAYMORE);
SideTop(blocks.TNT_BLACK_HOLE);
SideTop(blocks.TNT_REPULSIVE);
SideOnlyTNT(blocks.TNT_CLUSTER_2);
SideOnlyTNT(blocks.TNT_CLUSTER_4);
SideOnlyTNT(blocks.TNT_CLUSTER_8);
@@ -47,6 +47,10 @@ public class entities {
ENTITY_TYPES.register("tnt_ender", () -> EntityType.Builder.<enderPrimedTNT>of(enderPrimedTNT::new, MobCategory.MISC)
.sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_ender"));
public static final RegistryObject<EntityType<repulsivePrimedTNT>> TNT_REPULSIVE =
ENTITY_TYPES.register("tnt_repulsive", () -> EntityType.Builder.<repulsivePrimedTNT>of(repulsivePrimedTNT::new, MobCategory.MISC)
.sized(0.98F, 0.7F).fireImmune().clientTrackingRange(8).build("tnt_repulsive"));
public static final RegistryObject<EntityType<tntArrow>> ARROW_TNT =
ENTITY_TYPES.register("arrow_tnt", () -> EntityType.Builder.<tntArrow>of(tntArrow::new, MobCategory.MISC)
.sized(0.48F, 0.48F).clientTrackingRange(64).build("arrow_tnt"));
@@ -85,6 +89,8 @@ public class entities {
EntityRenderers.register(TNT_BLACK_HOLE.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_CLAYMORE.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_ENDER.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_REPULSIVE.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_SELECTIVE.get(), SelectiveTNTRenderer::new);
EntityRenderers.register(TNT_CLUSTER.get(), clusterTNTRenderer::new);
@@ -95,6 +101,7 @@ public class entities {
EntityRenderers.register(ARROW_CARPET.get(), TNTArrowRenderer::new);
EntityRenderers.register(ARROW_CARPT_PART.get(), TNTArrowRenderer::new);
EntityRenderers.register(ARROW_TUNNEL.get(), TNTArrowRenderer::new);
EntityRenderers.register(ARROW_CLAYMORE.get(), baseArrowRenderer::new);
}
}
@@ -0,0 +1,99 @@
package com.jenny.enhancedexplosives.entities.tnt;
import com.jenny.enhancedexplosives.blocks.blocks;
import com.jenny.enhancedexplosives.entities.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.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import java.util.List;
public class repulsivePrimedTNT extends basePrimedTNT {
private static final EntityDataAccessor<Float> DATA_SPEED_ID = SynchedEntityData.defineId(repulsivePrimedTNT.class, EntityDataSerializers.FLOAT);
public repulsivePrimedTNT(Level pLevel, double pX, double pY, double pZ, @Nullable LivingEntity pOwner, float power, int fuse, float speed) {
super(entities.TNT_REPULSIVE.get(), pLevel, pOwner, new Vec3(pX, pY, pZ), fuse, power);
this.setSpeed(speed);
}
public repulsivePrimedTNT(EntityType<repulsivePrimedTNT> entityType, Level level) {
super(entityType, level, null);
this.setSpeed(this.getSpeed());
}
private Vec3 targetVector(Entity target) {
double dist = getTargetDist(target);
float speed = getSpeed();
Vec3 mult = new Vec3(speed / dist, speed / dist, speed / dist);
Vec3 ret = target.position().subtract(this.position()).normalize().multiply(mult);
return ret;
}
public double getTargetDist(Entity target) {
return target.position().subtract(this.position()).length();
}
public List<Entity> findTargets() {
Vec3 corner1 = this.position().subtract(15, 15, 15);
Vec3 corner2 = this.position().add(15, 15, 15);
AABB boundingBox = new AABB(corner1, corner2);
return this.level().getEntities(this, boundingBox);
}
@Override
public void tick() {
for (Entity e : findTargets()) {
e.addDeltaMovement(targetVector(e));
}
super.tick();
}
@Override
public void explode() {
for (Entity e : findTargets()) {
e.addDeltaMovement(targetVector(e).scale(3));
}
super.explode();
}
@Override
protected void addAdditionalSaveData(CompoundTag pCompound) {
pCompound.putFloat("Speed", this.getSpeed());
super.addAdditionalSaveData(pCompound);
}
@Override
protected void readAdditionalSaveData(CompoundTag pCompound) {
this.setSpeed(pCompound.getFloat("Speed"));
super.readAdditionalSaveData(pCompound);
}
public void setSpeed(float speed) {
this.entityData.set(DATA_SPEED_ID, speed);
}
public float getSpeed() {
return this.entityData.get(DATA_SPEED_ID);
}
@Override
protected void defineSynchedData() {
this.entityData.define(DATA_SPEED_ID, 4.0f);
super.defineSynchedData();
}
@Override
public Block renderBlock() {
return blocks.TNT_REPULSIVE.get();
}
}