Skip to content

Commit

Permalink
(Almost) Working sounds for thermal detonator,
Browse files Browse the repository at this point in the history
Beggining on placeble thermal detonator
  • Loading branch information
DeltaHelios committed Jan 22, 2024
1 parent f0d2f07 commit 9ade2be
Show file tree
Hide file tree
Showing 21 changed files with 408 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.parzivail.pswg.block;

import com.parzivail.pswg.container.SwgEntities;
import com.parzivail.pswg.entity.BlasterBoltEntity;
import com.parzivail.pswg.entity.ThermalDetonatorEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.text.Text;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;

public class ThermalDetonatorBlock extends Block
{

public ThermalDetonatorBlock(Settings settings)
{
super(settings);
}

@Override
public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity)
{
if (entity instanceof BlasterBoltEntity)
{
var tde = new ThermalDetonatorEntity(SwgEntities.Misc.ThermalDetonator, world);
tde.setPos(pos.getX(), pos.getY(), pos.getZ());
tde.setPrimed(true);
tde.setLife(0);
world.spawnEntity(tde);
}
if (entity instanceof PlayerEntity player)
{
player.sendMessage(Text.of("IHATEYOU"), true);
}
super.onEntityCollision(state, world, pos, entity);
}

@Override
public void onProjectileHit(World world, BlockState state, BlockHitResult hit, ProjectileEntity projectile)
{
if (projectile instanceof BlasterBoltEntity bbe)
{
explode(world, hit.getBlockPos());
}
super.onProjectileHit(world, state, hit, projectile);
}

@Override
public void onDestroyedByExplosion(World world, BlockPos pos, Explosion explosion)
{
explode(world, pos);
super.onDestroyedByExplosion(world, pos, explosion);
}

public void explode(World world, BlockPos blockPos)
{
world.setBlockState(blockPos, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL | Block.REDRAW_ON_MAIN_THREAD);
var tde = new ThermalDetonatorEntity(SwgEntities.Misc.ThermalDetonator, world);
tde.setPos(blockPos.getX(), blockPos.getY(), blockPos.getZ());
tde.setPrimed(true);
tde.setLife(0);

world.spawnEntity(tde);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.parzivail.pswg.features.lightsabers.client.ThrownLightsaberEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundEvent;

public class SoundHelper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.parzivail.pswg.client.sound;

import com.parzivail.pswg.container.SwgSounds;
import com.parzivail.pswg.entity.ThermalDetonatorEntity;
import com.parzivail.pswg.item.ThermalDetonatorItem;
import com.parzivail.pswg.item.ThermalDetonatorTag;
import com.parzivail.util.sound.DopplerSoundInstance;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.sound.MovingSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class ThermalDetonatorEntitySoundInstance extends MovingSoundInstance
{
private final ThermalDetonatorEntity detonatorEntity;

public ThermalDetonatorEntitySoundInstance(ThermalDetonatorEntity detonatorEntity)
{
super(SwgSounds.Explosives.THERMAL_DETONATOR_BEEP, SoundCategory.PLAYERS, SoundInstance.createRandom());
this.detonatorEntity = detonatorEntity;
this.repeat = true;
this.repeatDelay = 0;
this.volume = 1F;
this.x = (float)detonatorEntity.getX();
this.y = (float)detonatorEntity.getY();
this.z = (float)detonatorEntity.getZ();
}

@Override
public void tick()
{

if (detonatorEntity.isRemoved() || !areConditionsMet(detonatorEntity))
{
setDone();
return;
}
x = (float)this.detonatorEntity.getX();
y = (float)this.detonatorEntity.getY();
z = (float)this.detonatorEntity.getZ();
float distanceToPlayer32 = 32;
if (detonatorEntity.getWorld().getClosestPlayer(detonatorEntity, 32) != null)
{
distanceToPlayer32 = detonatorEntity.getWorld().getClosestPlayer(detonatorEntity, 32).distanceTo(detonatorEntity);
}
volume = ((32 - distanceToPlayer32) / 32);
//detonatorEntity.getWorld().getClosestPlayer(detonatorEntity, 32).sendMessage(Text.of(""+volume), true);

}

public static boolean areConditionsMet(ThermalDetonatorEntity tde)
{
if (tde.isPrimed())
{
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.parzivail.pswg.client.sound;

import com.parzivail.pswg.container.SwgSounds;
import com.parzivail.pswg.features.lightsabers.LightsaberItem;
import com.parzivail.pswg.features.lightsabers.data.LightsaberTag;
import com.parzivail.pswg.item.ThermalDetonatorItem;
import com.parzivail.pswg.item.ThermalDetonatorTag;
import com.parzivail.util.sound.DopplerSoundInstance;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;

@Environment(EnvType.CLIENT)
public class ThermalDetonatorItemSoundInstance extends DopplerSoundInstance
{
private final PlayerEntity player;

public ThermalDetonatorItemSoundInstance(PlayerEntity player)
{
super(player, SwgSounds.Explosives.THERMAL_DETONATOR_BEEP, SoundCategory.PLAYERS, SoundInstance.createRandom());
this.player = player;
this.repeat = true;
this.repeatDelay = 0;
this.volume = 0.75F;
this.x = (float)player.getX();
this.y = (float)player.getY();
this.z = (float)player.getZ();
}

@Override
public boolean canPlay()
{
return !this.player.isSilent();
}

@Override
public boolean shouldAlwaysPlay()
{
return true;
}

@Override
public void tick()
{
super.tick();

if (this.player.isRemoved())
{
this.setDone();
return;
}

var foundDetonator = false;

if (player.getMainHandStack().getItem() instanceof ThermalDetonatorItem)
foundDetonator = true;

if (!foundDetonator && player.getOffHandStack().getItem() instanceof ThermalDetonatorItem)
foundDetonator = true;

if (!areConditionsMet(player))
{
this.setDone();
return;
}
if (foundDetonator)
{
volume = 0.75f;
}
else
{
volume = 0.25f;
}

this.x = (float)this.player.getX();
this.y = (float)this.player.getY();
this.z = (float)this.player.getZ();
}

public static boolean areConditionsMet(PlayerEntity player)
{
//return isPrimed(player.getMainHandStack()) || isPrimed(player.getOffHandStack());
for (int i = 0; i < player.getInventory().size(); i++)
if (player.getInventory().getStack(i).getItem() instanceof ThermalDetonatorItem)
{
return isPrimed(player.getInventory().getStack(i));
}
return false;
}

private static boolean isPrimed(ItemStack stack)
{
if (!(stack.getItem() instanceof ThermalDetonatorItem))
return false;

var tdt = new ThermalDetonatorTag(stack.getOrCreateNbt());
return tdt.primed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,14 @@ public static class Cage
public static final BlockEntityType<CreatureCageBlockEntity> CreatureCageBlockEntityType = FabricBlockEntityTypeBuilder.create(CreatureCageBlockEntity::new, BlockUtil.concat(DyedCreatureTerrarium, CreatureTerrarium, Creature)).build();
}

@RegistryOrder(26)
public static class Misc
{
@RegistryName("thermal_detonator_block")
@ClientBlockRegistryData(renderLayer = RenderLayerHint.CUTOUT_MIPPED)
public static final ThermalDetonatorBlock ThermalDetonatorBlock = new ThermalDetonatorBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).nonOpaque().strength(0.5F));
}

public static void register()
{
RegistryHelper.registerAutoId(Resources.MODID, SwgBlocks.class, Object.class, SwgBlocks::tryRegisterBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void register()
Lightsaber.register();
Blaster.register();
Ship.register();
Explosives.register();

for (var pair : SOUND_EVENTS.entrySet())
Registry.register(Registries.SOUND_EVENT, pair.getKey(), pair.getValue());
Expand Down Expand Up @@ -118,4 +119,14 @@ private static void register()
{
}
}

public static class Explosives
{
public static final SoundEvent THERMAL_DETONATOR_BEEP = of(Resources.id("explosives.thermaldetonator.beep"));
public static final SoundEvent THERMAL_DETONATOR_ARM = of(Resources.id("explosives.thermaldetonator.arm"));
public static final SoundEvent THERMAL_DETONATOR_EXPLOSION = of(Resources.id("explosives.thermaldetonator.explode"));
private static void register()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.parzivail.pswg.entity;

import com.parzivail.pswg.Resources;
import com.parzivail.pswg.block.ThermalDetonatorBlock;
import com.parzivail.pswg.container.SwgDamageTypes;
import com.parzivail.pswg.container.SwgPackets;
import com.parzivail.pswg.container.SwgParticles;
Expand Down Expand Up @@ -307,7 +308,10 @@ protected void onCollision(HitResult hitResult)
var state = getWorld().getBlockState(blockPos);
if(state.getBlock() instanceof TargetBlock targetBlock){
targetBlock.onProjectileHit(getWorld(), state, blockHit, this);

}
else if (state.getBlock() instanceof ThermalDetonatorBlock tdb)
{
tdb.explode(getWorld(), blockPos);
}

if (state.isIn(SwgTags.Blocks.BLASTER_REFLECT))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
package com.parzivail.pswg.entity;

import com.parzivail.pswg.client.sound.ThermalDetonatorEntitySoundInstance;
import com.parzivail.pswg.client.sound.ThermalDetonatorItemSoundInstance;
import com.parzivail.pswg.container.SwgItems;
import com.parzivail.pswg.container.SwgParticles;
import com.parzivail.pswg.container.SwgSounds;
import com.parzivail.util.entity.IPrecisionSpawnEntity;
import com.parzivail.util.entity.IPrecisionVelocityEntity;
import com.parzivail.util.entity.collision.IComplexEntityHitbox;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.Objects;

public class ThermalDetonatorEntity extends ThrowableExplosive implements IPrecisionSpawnEntity, IPrecisionVelocityEntity
{
public int texturePhase = 0;
public ThermalDetonatorEntitySoundInstance soundInstance = new ThermalDetonatorEntitySoundInstance(this);

public ThermalDetonatorEntity(EntityType<ThermalDetonatorEntity> type, World world)
{
super(type, world);
setExplosionPower(5f);
if (getWorld() instanceof ClientWorld)
{
MinecraftClient.getInstance().getSoundManager().play(soundInstance);
}
}

@Override
public void explode()
{
getWorld().playSound(null, getBlockPos(), SwgSounds.Explosives.THERMAL_DETONATOR_EXPLOSION, SoundCategory.PLAYERS, 1f, 1f);
MinecraftClient.getInstance().getSoundManager().stop(soundInstance);
super.explode();
}

@Override
Expand Down Expand Up @@ -78,4 +117,22 @@ protected void onCollision(HitResult hitResult)

super.onCollision(hitResult);
}

@Override
public ActionResult interact(PlayerEntity player, Hand hand)
{
if (!isPrimed() && age > 30)
{
getWorld().spawnEntity(dropItem(SwgItems.Explosives.ThermalDetonator));

this.discard();
}
return super.interact(player, hand);
}

@Override
public boolean canHit()
{
return true;
}
}
Loading

0 comments on commit 9ade2be

Please sign in to comment.