Skip to content

Commit

Permalink
reimplement custom bomb explosion radius and add custom bomb fuse time
Browse files Browse the repository at this point in the history
  • Loading branch information
MoriyaShiine committed Oct 18, 2024
1 parent 1d7f66c commit 4e8a161
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 98 deletions.
10 changes: 4 additions & 6 deletions src/main/java/ladysnake/blast/common/Blast.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ladysnake.blast.common;

import ladysnake.blast.common.init.BlastBlocks;
import ladysnake.blast.common.init.BlastEntities;
import ladysnake.blast.common.init.BlastItems;
import ladysnake.blast.common.init.BlastSoundEvents;
import ladysnake.blast.common.init.*;
import net.fabricmc.api.ModInitializer;
import net.minecraft.entity.data.TrackedDataHandler;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
Expand All @@ -30,10 +27,11 @@ public Direction copy(Direction value) {
@Override
public void onInitialize() {
TrackedDataHandlerRegistry.register(FACING);
BlastSoundEvents.initialize();
BlastBlocks.init();
BlastComponentTypes.init();
BlastEntities.init();
BlastItems.init();
BlastBlocks.init();
BlastSoundEvents.initialize();
}

public static Identifier id(String value) {
Expand Down
125 changes: 54 additions & 71 deletions src/main/java/ladysnake/blast/common/entity/BombEntity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ladysnake.blast.common.entity;

import ladysnake.blast.common.init.BlastComponentTypes;
import ladysnake.blast.common.init.BlastItems;
import ladysnake.blast.common.world.CustomExplosion;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
Expand All @@ -15,6 +17,7 @@
import net.minecraft.network.packet.s2c.play.ExplosionS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;
Expand All @@ -39,32 +42,6 @@ public BombEntity(EntityType<? extends BombEntity> entityType, World world, Livi
ticksUntilRemoval = -1;
}

@Override
protected Item getDefaultItem() {
return BlastItems.BOMB;
}

protected CustomExplosion getExplosion() {
return new CustomExplosion(getWorld(), getOwner(), getX(), getY(), getZ(), getExplosionRadius(), null, Explosion.DestructionType.DESTROY);
}

@Override
protected void onCollision(HitResult hitResult) {
if (age > 1) {
// if (hitResult.getType() == HitResult.Type.ENTITY) {
// Entity entity = ((EntityHitResult) hitResult).getEntity();
// entity.damage(DamageSource.thrownProjectile(this, getOwner()), getDirectHitDamage());
// }

setVelocity(0, 0, 0);

if (getTriggerType() == BombTriggerType.IMPACT) {
explode();
}
}
}


@Override
public void tick() {
if (ticksUntilRemoval > 0) {
Expand All @@ -74,24 +51,20 @@ public void tick() {
}
} else {
super.tick();

if (getWorld().getBlockState(getBlockPos()).isFullCube(getWorld(), getBlockPos())) {
setPosition(prevX, prevY, prevZ);
}

// drop item if in water
if (isSubmergedInWater() && disableInLiquid()) {
getWorld().spawnEntity(new ItemEntity(getWorld(), getX(), getY(), getZ(), new ItemStack(getDefaultItem())));
remove(RemovalReason.DISCARDED);
}

// tick down the fuse, then blow up
if (getTriggerType() == BombTriggerType.FUSE) {
// smoke particle for lit fuse
if (getWorld().isClient) {
getWorld().addParticle(ParticleTypes.SMOKE, getX(), getY() + 0.3, getZ(), 0, 0, 0);
}

// shorten the fuse
setFuse(getFuse() - 1);
if (getFuse() <= 0) {
Expand All @@ -101,37 +74,72 @@ public void tick() {
}
}

@Override
protected Item getDefaultItem() {
return BlastItems.BOMB;
}

@Override
protected void onCollision(HitResult hitResult) {
if (age > 1) {
setVelocity(0, 0, 0);
if (getTriggerType() == BombTriggerType.IMPACT) {
explode();
}
}
}

@Override
public void onTrackedDataSet(TrackedData<?> trackedData) {
if (FUSE.equals(trackedData)) {
fuseTimer = getFuse();
}
}

@Override
protected void initDataTracker(DataTracker.Builder builder) {
super.initDataTracker(builder);
builder.add(FUSE, 40);
}

@Override
public void writeCustomDataToNbt(NbtCompound nbt) {
nbt.putInt("Fuse", getFuseTimer());
nbt.putFloat("ExplosionRadius", getExplosionRadius());
}

@Override
public void readCustomDataFromNbt(NbtCompound nbt) {
setFuse(nbt.getInt("Fuse"));
setExplosionRadius(nbt.getFloat("ExplosionRadius"));
}

public void explode() {
if (ticksUntilRemoval == -1) {
ticksUntilRemoval = 1;

CustomExplosion explosion = getExplosion();
explosion.collectBlocksAndDamageEntities();
explosion.affectWorld(true);

if (!getWorld().isClient()) {
for (net.minecraft.entity.player.PlayerEntity playerEntity : getWorld().getPlayers()) {
ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) playerEntity;
if (serverPlayerEntity.squaredDistanceTo(getX(), getY(), getZ()) < 4096.0D) {
serverPlayerEntity.networkHandler.sendPacket(new ExplosionS2CPacket(getX(), getY(), getZ(), explosion.getPower(), explosion.getAffectedBlocks(), explosion.getAffectedPlayers().get(serverPlayerEntity), explosion.getDestructionType(), explosion.getParticle(), explosion.getEmitterParticle(), explosion.getSoundEvent()));
for (ServerPlayerEntity player : PlayerLookup.world((ServerWorld) getWorld())) {
if (player.squaredDistanceTo(getX(), getY(), getZ()) < 4096) {
player.networkHandler.sendPacket(new ExplosionS2CPacket(getX(), getY(), getZ(), explosion.getPower(), explosion.getAffectedBlocks(), explosion.getAffectedPlayers().get(player), explosion.getDestructionType(), explosion.getParticle(), explosion.getEmitterParticle(), explosion.getSoundEvent()));
}
}
}
}
}

public boolean disableInLiquid() {
return true;
protected CustomExplosion getExplosion() {
return new CustomExplosion(getWorld(), getOwner(), getX(), getY(), getZ(), getExplosionRadius(), null, Explosion.DestructionType.DESTROY);
}

public BombTriggerType getTriggerType() {
protected BombTriggerType getTriggerType() {
return BombTriggerType.FUSE;
}

public void onTrackedDataSet(TrackedData<?> trackedData_1) {
if (FUSE.equals(trackedData_1)) {
fuseTimer = getFuse();
}
public boolean disableInLiquid() {
return true;
}

public int getFuse() {
Expand All @@ -157,34 +165,9 @@ public void setExplosionRadius(float explosionRadius) {

@Override
public void setItem(ItemStack item) {
super.setItem(new ItemStack(item.getItem()));
// todo explosion radius
// if (item.hasNbt() && item.getOrCreateNbt().contains("ExplosionRadius")) {
// setExplosionRadius(item.getOrCreateNbt().getFloat("ExplosionRadius"));
// }
}

@Override
protected void initDataTracker(DataTracker.Builder builder) {
super.initDataTracker(builder);
builder.add(FUSE, 40);
}

@Override
public void writeCustomDataToNbt(NbtCompound nbt) {
nbt.putShort("Fuse", (short) getFuseTimer());
nbt.putFloat("ExplosionRadius", getExplosionRadius());
}

@Override
public void readCustomDataFromNbt(NbtCompound nbt) {
setFuse(nbt.getShort("Fuse"));
setExplosionRadius(nbt.getFloat("ExplosionRadius"));
}

@Override
public ItemStack getStack() {
return getDefaultItem().getDefaultStack();
super.setItem(item.getItem().getDefaultStack());
setFuse(item.getOrDefault(BlastComponentTypes.FUSE, getFuse()));
setExplosionRadius(item.getOrDefault(BlastComponentTypes.EXPLOSION_RADIUS, getExplosionRadius()));
}

public enum BombTriggerType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;

public class ConfettiBombEntity extends BombEntity {
Expand All @@ -21,14 +20,6 @@ protected Item getDefaultItem() {
return BlastItems.CONFETTI_BOMB;
}

@Override
protected void onCollision(HitResult hitResult) {
setVelocity(0, 0, 0);
if (getTriggerType() == BombTriggerType.IMPACT) {
explode();
}
}

@Override
public void explode() {
if (getWorld().isClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;

public class ConfettiTriggerBombEntity extends TriggerBombEntity {
Expand All @@ -21,14 +20,6 @@ protected Item getDefaultItem() {
return BlastItems.CONFETTI_TRIGGER_BOMB;
}

@Override
protected void onCollision(HitResult hitResult) {
setVelocity(0, 0, 0);
if (getTriggerType() == BombTriggerType.IMPACT) {
explode();
}
}

@Override
public void explode() {
if (getWorld().isClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected CustomExplosion getExplosion() {
}

@Override
public BombTriggerType getTriggerType() {
protected BombTriggerType getTriggerType() {
return BombTriggerType.IMPACT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Item getDefaultItem() {
}

@Override
public BombTriggerType getTriggerType() {
protected BombTriggerType getTriggerType() {
return BombTriggerType.IMPACT;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ladysnake/blast/common/init/BlastComponentTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ladysnake.blast.common.init;

import ladysnake.blast.common.Blast;
import net.minecraft.component.ComponentType;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.dynamic.Codecs;

public class BlastComponentTypes {
public static final ComponentType<Integer> FUSE = new ComponentType.Builder<Integer>().codec(Codecs.POSITIVE_INT).packetCodec(PacketCodecs.VAR_INT).build();
public static final ComponentType<Float> EXPLOSION_RADIUS = new ComponentType.Builder<Float>().codec(Codecs.POSITIVE_FLOAT).packetCodec(PacketCodecs.FLOAT).build();

public static void init() {
Registry.register(Registries.DATA_COMPONENT_TYPE, Blast.id("fuse"), FUSE);
Registry.register(Registries.DATA_COMPONENT_TYPE, Blast.id("explosion_radius"), EXPLOSION_RADIUS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;

// todo teleport entities
public class EnderExplosion extends CustomExplosion {
public static final int PARTICLE_DISTANCE = 1;

Expand Down

0 comments on commit 4e8a161

Please sign in to comment.