Skip to content

Commit

Permalink
add mother related sound
Browse files Browse the repository at this point in the history
- fix ambient sounds to mono channel
- play mother sound when boss bar is active
- add sounds to remains defender, ghost and vulnerability
  • Loading branch information
Cheaterpaul committed Oct 28, 2023
1 parent d137dba commit c9d2160
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.teamlapen.vampirism.blocks.mother.MotherTreeStructure;
import de.teamlapen.vampirism.core.*;
import de.teamlapen.vampirism.entity.GhostEntity;
import de.teamlapen.vampirism.network.ClientboundBossEventSoundPacket;
import de.teamlapen.vampirism.network.ClientboundPlayEventPacket;
import de.teamlapen.vampirism.particle.FlyingBloodParticleOptions;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -44,16 +45,9 @@


public class MotherBlockEntity extends BlockEntity {
/**
* Indicate whether a mother block is loaded in the world.
* Should be acceptably accurate as there is only every one mother nearby.
* But don't use it for anything important for gameplay
*/
public static boolean IS_A_MOTHER_LOADED_UNRELIABLE = false;

public static void serverTick(Level level, BlockPos blockPos, BlockState blockState, MotherBlockEntity e) {

IS_A_MOTHER_LOADED_UNRELIABLE = true;
//Handle fight ---------------------------------
if (e.isFrozen && e.freezeTimer-- <= 0) {
e.unFreezeFight(level, blockPos, blockState);
Expand Down Expand Up @@ -93,7 +87,7 @@ public static void serverTick(Level level, BlockPos blockPos, BlockState blockSt
if (level.getBlockState(p).getBlock() instanceof IRemainsBlock) {
level.setBlock(p, Blocks.AIR.defaultBlockState(), 3);
ModParticles.spawnParticlesServer(level, new DustParticleOptions(new Vector3f(0.7f, 0.7f, 0.7f), 1), p.getX() + 0.5, p.getY() + 0.5, p.getZ() + 0.5f, 20, 0.3, 0.3, 0.3, 0.01);
e.level.playSound(null, p, ModSounds.REMAINS_DESTROYED.get(), SoundSource.BLOCKS, 0.2f, 1f);
e.level.playSound(null, p, ModSounds.REMAINS_DEATH.get(), SoundSource.BLOCKS, 0.2f, 1f);
}
}
} else {
Expand All @@ -116,7 +110,7 @@ public static void serverTick(Level level, BlockPos blockPos, BlockState blockSt
if (e.isIntact()) {
level.getEntitiesOfClass(ServerPlayer.class, inflate2).forEach(e::addPlayer);
}
level.getEntitiesOfClass(ServerPlayer.class, inflate2.inflate(20,10,20)).forEach(e.bossEvent::addPlayer);
level.getEntitiesOfClass(ServerPlayer.class, inflate2.inflate(20,10,20)).forEach(e::addPlayerToBossEvent);
}
}

Expand Down Expand Up @@ -184,23 +178,26 @@ public void setRemoved() {
super.setRemoved();
this.bossEvent.removeAllPlayers();
this.activePlayers.clear();
IS_A_MOTHER_LOADED_UNRELIABLE = false;
}

@Override
public void onChunkUnloaded() {
super.onChunkUnloaded();
IS_A_MOTHER_LOADED_UNRELIABLE = false;
}

private void addPlayer(Player player) {
if (player instanceof ServerPlayer serverPlayer && !this.activePlayers.contains(serverPlayer)) {
updateFightStatus();
this.bossEvent.addPlayer(serverPlayer);
addPlayerToBossEvent(serverPlayer);
this.activePlayers.add(serverPlayer);
}
}

private void addPlayerToBossEvent(ServerPlayer player) {
this.bossEvent.addPlayer(player);
VampirismMod.dispatcher.sendTo(new ClientboundBossEventSoundPacket(this.bossEvent.getId(), ModSounds.MOTHER_AMBIENT.getKey()), player);
}

@Override
public @NotNull CompoundTag getUpdateTag() {
return saveWithoutMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import de.teamlapen.vampirism.blocks.mother.MotherTreeStructure;
import de.teamlapen.vampirism.core.ModBlocks;
import de.teamlapen.vampirism.core.ModEntities;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.core.ModTiles;
import de.teamlapen.vampirism.entity.VulnerableRemainsDummyEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
Expand Down Expand Up @@ -80,7 +78,6 @@ public void onDamageDealt(DamageSource src, double damage) {
this.health -= (int) damage;
if (this.level != null) {
this.lastDamage = this.level.getGameTime();
this.level.playSound(null, worldPosition, health > 0 ? ModSounds.REMAINS_HIT.get() : ModSounds.REMAINS_DESTROYED.get(), SoundSource.BLOCKS, 1f, 1f);
}
if (this.health <= 0) {
destroyVulnerability();
Expand Down Expand Up @@ -148,7 +145,10 @@ public void checkNeighbor(BlockPos pNeighborPos) {
public void setRemoved() {
super.setRemoved();
if (this.dummy_entity_id != null && this.level instanceof ServerLevel serverLevel) {
Optional.ofNullable(serverLevel.getEntity(dummy_entity_id)).ifPresent(entity -> entity.remove(Entity.RemovalReason.DISCARDED));
Optional.ofNullable(serverLevel.getEntity(dummy_entity_id)).ifPresent(entity -> {
entity.playSound(((VulnerableRemainsDummyEntity)entity).getDeathSound());
entity.remove(Entity.RemovalReason.DISCARDED);
});
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/main/java/de/teamlapen/vampirism/core/ModSounds.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.teamlapen.vampirism.core;

import de.teamlapen.vampirism.REFERENCE;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.Music;
import net.minecraft.sounds.Musics;
Expand All @@ -11,6 +12,9 @@
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
* Handle all sound related stuff
*/
Expand All @@ -34,11 +38,17 @@ public class ModSounds {
public static final RegistryObject<SoundEvent> TELEPORT_HERE = create("fx.teleport_here");
public static final RegistryObject<SoundEvent> FREEZE = create("fx.freeze");
public static final RegistryObject<SoundEvent> POTION_TABLE_CRAFTING = create("block.potion_table_crafting");
public static final RegistryObject<SoundEvent> REMAINS_HIT = create("block.remains_hit");
public static final RegistryObject<SoundEvent> REMAINS_DESTROYED = create("block.remains_destroyed");
public static final RegistryObject<SoundEvent> MOTHER_DEATH = create("fx.mother_death");
public static final RegistryObject<SoundEvent> MOTHER_AMBIENT = create("ambient.mother");
public static final RegistryObject<SoundEvent> VAMPIRE_FOREST_AMBIENT = create("ambient.forest");
public static final RegistryObject<SoundEvent> GHOST_AMBIENT = create("entity.ghost.ambient");
public static final RegistryObject<SoundEvent> GHOST_DEATH = create("entity.ghost.death");
public static final RegistryObject<SoundEvent> GHOST_HURT = create("entity.ghost.hurt");
public static final RegistryObject<SoundEvent> REMAINS_DEFENDER_AMBIENT = create("entity.remains_defender.ambient");
public static final RegistryObject<SoundEvent> REMAINS_DEFENDER_DEATH = create("entity.remains_defender.death");
public static final RegistryObject<SoundEvent> REMAINS_DEFENDER_HURT = create("entity.remains_defender.hit");
public static final RegistryObject<SoundEvent> REMAINS_DEATH = create("entity.remains.death");
public static final RegistryObject<SoundEvent> REMAINS_HURT = create("entity.remains.hurt");


static void register(IEventBus bus) {
Expand All @@ -50,12 +60,10 @@ private static RegistryObject<SoundEvent> create(@NotNull String soundNameIn) {
return SOUND_EVENTS.register(soundNameIn, () -> SoundEvent.createVariableRangeEvent(resourcelocation));
}

private static Music mother_music;
public static Music getMotherMusic(){
if(mother_music == null){
mother_music = ModSounds.MOTHER_AMBIENT.getHolder().map(music -> new Music(music, 0,0, true)).orElse(Musics.GAME);
}
return mother_music;
private static final Map<ResourceKey<SoundEvent>, Music> music = new HashMap<>();

public static Music getMusic(ResourceKey<SoundEvent> soundEvent) {
return music.computeIfAbsent(soundEvent, sound -> ForgeRegistries.SOUND_EVENTS.getHolder(sound).map(event -> new Music(event, 0,0, true)).orElse(Musics.GAME));
}

}
25 changes: 25 additions & 0 deletions src/main/java/de/teamlapen/vampirism/entity/GhostEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.VampirismAPI;
import de.teamlapen.vampirism.api.entity.IEntityLeader;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.core.ModTags;
import de.teamlapen.vampirism.entity.ai.goals.DefendLeaderGoal;
import de.teamlapen.vampirism.entity.ai.goals.FindLeaderGoal;
import de.teamlapen.vampirism.entity.ai.goals.NearestTargetGoalModifier;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EntityType;
Expand All @@ -29,6 +32,7 @@
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.ForgeMod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class GhostEntity extends VampirismEntity implements IRemainsEntity, IEntityFollower {

Expand Down Expand Up @@ -117,6 +121,27 @@ public float getPathfindingMalus(@NotNull BlockPathTypes pNodeType) {
return 0;
}

@Nullable
@Override
protected SoundEvent getAmbientSound() {
return ModSounds.GHOST_AMBIENT.get();
}

@Override
protected SoundEvent getDeathSound() {
return ModSounds.GHOST_DEATH.get();
}

@Override
protected @Nullable SoundEvent getHurtSound(DamageSource source) {
return ModSounds.GHOST_HURT.get();
}

@Override
public @NotNull SoundSource getSoundSource() {
return SoundSource.HOSTILE;
}

class GhostMeleeAttackGoal extends MeleeAttackGoal {

public GhostMeleeAttackGoal(double pSpeedModifier, boolean pFollowingTargetEvenIfNotSeen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.teamlapen.vampirism.blockentity.MotherBlockEntity;
import de.teamlapen.vampirism.blockentity.VulnerableRemainsBlockEntity;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.core.ModTags;
import de.teamlapen.vampirism.util.Helper;
import net.minecraft.core.BlockPos;
Expand All @@ -12,7 +13,6 @@
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.damagesource.DamageSource;
Expand All @@ -24,7 +24,6 @@
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.animal.AbstractGolem;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
Expand All @@ -35,7 +34,7 @@
import java.util.EnumSet;
import java.util.Optional;

public class RemainsDefenderEntity extends AbstractGolem implements IRemainsEntity {
public class RemainsDefenderEntity extends Mob implements IRemainsEntity {

protected static final EntityDataAccessor<Direction> DATA_ATTACH_FACE_ID = SynchedEntityData.defineId(RemainsDefenderEntity.class, EntityDataSerializers.DIRECTION);
private static final EntityDataAccessor<Integer> DATA_LIGHT_TICKS_REMAINING = SynchedEntityData.defineId(RemainsDefenderEntity.class, EntityDataSerializers.INT);
Expand Down Expand Up @@ -94,11 +93,18 @@ public boolean isInvulnerableTo(@NotNull DamageSource pSource) {
@Nullable
@Override
protected SoundEvent getAmbientSound() {
return SoundEvents.BLAZE_AMBIENT;
return ModSounds.REMAINS_DEFENDER_AMBIENT.get();
}

@Override
protected SoundEvent getDeathSound() {
return SoundEvents.SHULKER_DEATH;
return ModSounds.REMAINS_DEFENDER_DEATH.get();
}

@Nullable
@Override
protected SoundEvent getHurtSound(@NotNull DamageSource pDamageSource) {
return ModSounds.REMAINS_DEFENDER_HURT.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import de.teamlapen.vampirism.blockentity.VulnerableRemainsBlockEntity;
import de.teamlapen.vampirism.core.ModBlocks;
import de.teamlapen.vampirism.core.ModEntities;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.core.ModTags;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
Expand All @@ -19,6 +22,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -228,4 +232,21 @@ public boolean increaseFollowerCount() {
followerCount++;
return true;
}

@Nullable
@Override
public SoundEvent getDeathSound() {
return ModSounds.REMAINS_DEATH.get();
}

@Override
public @NotNull SoundSource getSoundSource() {
return SoundSource.HOSTILE;
}

@Nullable
@Override
protected SoundEvent getHurtSound(@NotNull DamageSource pDamageSource) {
return ModSounds.REMAINS_HURT.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.teamlapen.vampirism.mixin.client;

import net.minecraft.client.gui.components.BossHealthOverlay;
import net.minecraft.client.gui.components.LerpingBossEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Map;
import java.util.UUID;

@Mixin(BossHealthOverlay.class)
public interface BossHealthOverlayAccessor {

@Accessor("events")
Map<UUID, LerpingBossEvent> getEvents();
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package de.teamlapen.vampirism.mixin.client;

import de.teamlapen.vampirism.blockentity.MotherBlockEntity;
import de.teamlapen.vampirism.VampirismMod;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.proxy.ClientProxy;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.components.LerpingBossEvent;
import net.minecraft.sounds.Music;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Mixin(Minecraft.class)
public class MinecraftMixin {

@Final
@Shadow
public Gui gui;
@Inject(method="getSituationalMusic", at=@At(value="INVOKE", target = "Lnet/minecraft/world/level/Level;getBiome(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/core/Holder;"), cancellable = true)
private void getSituationalMusicVampirism(CallbackInfoReturnable<Music> cir){
if(MotherBlockEntity.IS_A_MOTHER_LOADED_UNRELIABLE && Minecraft.getInstance().gui.getBossOverlay().shouldPlayMusic()){
cir.setReturnValue(ModSounds.getMotherMusic());
}
Map<UUID, LerpingBossEvent> events = ((BossHealthOverlayAccessor) this.gui.getBossOverlay()).getEvents();
events.values().stream().map(s -> ((ClientProxy) VampirismMod.proxy).getBossEventSound(s.getId())).filter(Objects::nonNull).findFirst().ifPresent(s -> cir.setReturnValue(ModSounds.getMusic(s)));
}
}
Loading

0 comments on commit c9d2160

Please sign in to comment.