From 4e8a1612c501f9cc13aa8e43b93e701eb1169b2c Mon Sep 17 00:00:00 2001 From: MoriyaShiine <45156123+MoriyaShiine@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:24:13 -0500 Subject: [PATCH] reimplement custom bomb explosion radius and add custom bomb fuse time --- .../java/ladysnake/blast/common/Blast.java | 10 +- .../blast/common/entity/BombEntity.java | 125 ++++++++---------- .../common/entity/ConfettiBombEntity.java | 9 -- .../entity/ConfettiTriggerBombEntity.java | 9 -- .../blast/common/entity/NavalMineEntity.java | 2 +- .../common/entity/TriggerBombEntity.java | 2 +- .../common/init/BlastComponentTypes.java | 18 +++ .../blast/common/world/EnderExplosion.java | 1 - 8 files changed, 78 insertions(+), 98 deletions(-) create mode 100644 src/main/java/ladysnake/blast/common/init/BlastComponentTypes.java diff --git a/src/main/java/ladysnake/blast/common/Blast.java b/src/main/java/ladysnake/blast/common/Blast.java index 4d05d19..1e23d80 100644 --- a/src/main/java/ladysnake/blast/common/Blast.java +++ b/src/main/java/ladysnake/blast/common/Blast.java @@ -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; @@ -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) { diff --git a/src/main/java/ladysnake/blast/common/entity/BombEntity.java b/src/main/java/ladysnake/blast/common/entity/BombEntity.java index 5436b6f..be37817 100644 --- a/src/main/java/ladysnake/blast/common/entity/BombEntity.java +++ b/src/main/java/ladysnake/blast/common/entity/BombEntity.java @@ -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; @@ -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; @@ -39,32 +42,6 @@ public BombEntity(EntityType 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) { @@ -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) { @@ -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() { @@ -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 { diff --git a/src/main/java/ladysnake/blast/common/entity/ConfettiBombEntity.java b/src/main/java/ladysnake/blast/common/entity/ConfettiBombEntity.java index 2da5e6d..24e50e5 100644 --- a/src/main/java/ladysnake/blast/common/entity/ConfettiBombEntity.java +++ b/src/main/java/ladysnake/blast/common/entity/ConfettiBombEntity.java @@ -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 { @@ -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) { diff --git a/src/main/java/ladysnake/blast/common/entity/ConfettiTriggerBombEntity.java b/src/main/java/ladysnake/blast/common/entity/ConfettiTriggerBombEntity.java index a1df8be..9818589 100644 --- a/src/main/java/ladysnake/blast/common/entity/ConfettiTriggerBombEntity.java +++ b/src/main/java/ladysnake/blast/common/entity/ConfettiTriggerBombEntity.java @@ -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 { @@ -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) { diff --git a/src/main/java/ladysnake/blast/common/entity/NavalMineEntity.java b/src/main/java/ladysnake/blast/common/entity/NavalMineEntity.java index 3b17880..2951af4 100644 --- a/src/main/java/ladysnake/blast/common/entity/NavalMineEntity.java +++ b/src/main/java/ladysnake/blast/common/entity/NavalMineEntity.java @@ -27,7 +27,7 @@ protected CustomExplosion getExplosion() { } @Override - public BombTriggerType getTriggerType() { + protected BombTriggerType getTriggerType() { return BombTriggerType.IMPACT; } diff --git a/src/main/java/ladysnake/blast/common/entity/TriggerBombEntity.java b/src/main/java/ladysnake/blast/common/entity/TriggerBombEntity.java index d6454e9..14395e0 100644 --- a/src/main/java/ladysnake/blast/common/entity/TriggerBombEntity.java +++ b/src/main/java/ladysnake/blast/common/entity/TriggerBombEntity.java @@ -19,7 +19,7 @@ protected Item getDefaultItem() { } @Override - public BombTriggerType getTriggerType() { + protected BombTriggerType getTriggerType() { return BombTriggerType.IMPACT; } diff --git a/src/main/java/ladysnake/blast/common/init/BlastComponentTypes.java b/src/main/java/ladysnake/blast/common/init/BlastComponentTypes.java new file mode 100644 index 0000000..7e379c7 --- /dev/null +++ b/src/main/java/ladysnake/blast/common/init/BlastComponentTypes.java @@ -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 FUSE = new ComponentType.Builder().codec(Codecs.POSITIVE_INT).packetCodec(PacketCodecs.VAR_INT).build(); + public static final ComponentType EXPLOSION_RADIUS = new ComponentType.Builder().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); + } +} diff --git a/src/main/java/ladysnake/blast/common/world/EnderExplosion.java b/src/main/java/ladysnake/blast/common/world/EnderExplosion.java index 4588e6f..4dbf926 100644 --- a/src/main/java/ladysnake/blast/common/world/EnderExplosion.java +++ b/src/main/java/ladysnake/blast/common/world/EnderExplosion.java @@ -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;