From 7b60248514a095fa71daa7752aabde9177c9d5ac Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 26 Sep 2022 21:13:03 +0200 Subject: [PATCH] Standardise sound playing --- .../unicopia/ability/magic/Caster.java | 8 ++--- .../unicopia/entity/Living.java | 14 ++++++-- .../behaviour/RangedAttackBehaviour.java | 5 ++- .../unicopia/item/ProjectileItem.java | 3 +- .../unicopia/util/SoundEmitter.java | 34 +++++++++++++++++++ 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/util/SoundEmitter.java diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java index 607c9b24..eb19918f 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java @@ -12,12 +12,12 @@ import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.entity.Physics; import com.minelittlepony.unicopia.entity.PonyContainer; import com.minelittlepony.unicopia.particle.ParticleSource; +import com.minelittlepony.unicopia.util.SoundEmitter; import com.minelittlepony.unicopia.util.VecHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.sound.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.GameRules; @@ -26,7 +26,7 @@ import net.minecraft.world.World; /** * Interface for any magically capable entities that can cast or persist spells. */ -public interface Caster extends Owned, Levelled, Affine, ParticleSource { +public interface Caster extends Owned, Levelled, Affine, ParticleSource, SoundEmitter { Physics getPhysics(); @@ -74,10 +74,6 @@ public interface Caster extends Owned, Levelled, Affi return getReferenceWorld().getGameRules().getBoolean(GameRules.DO_MOB_GRIEFING); } - default void playSound(SoundEvent sound, float volume, float pitch) { - getReferenceWorld().playSound(null, getEntity().getX(), getEntity().getY(), getEntity().getZ(), sound, getEntity().getSoundCategory(), volume, pitch); - } - /** * Removes the desired amount of mana or health from this caster in exchange for a spell's benefits. *

diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index c33f58a3..880c77af 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -30,7 +30,6 @@ import net.minecraft.entity.projectile.ProjectileEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.particle.ParticleTypes; -import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; @@ -183,7 +182,7 @@ public abstract class Living implements Equine, Caste ItemStack broken = UItems.BROKEN_SUNGLASSES.getDefaultStack(); broken.setNbt(glasses.getNbt()); TrinketsDelegate.getInstance().setEquippedStack(entity, TrinketsDelegate.FACE, broken); - entity.world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.BLOCK_GLASS_BREAK, SoundCategory.PLAYERS, 1, 1); + playSound(SoundEvents.BLOCK_GLASS_BREAK, 1, 1); } } } @@ -232,4 +231,15 @@ public abstract class Living implements Equine, Caste public void fromNBT(NbtCompound compound) { enchants.fromNBT(compound); } + + public void updateVelocity() { + updateVelocity(entity); + } + + public static void updateVelocity(Entity entity) { + entity.velocityModified = true; + //if (entity instanceof ServerPlayerEntity ply) { + // ply.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(ply)); + //} + } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/RangedAttackBehaviour.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/RangedAttackBehaviour.java index e0cd3c80..9fa24feb 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/RangedAttackBehaviour.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/RangedAttackBehaviour.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity.behaviour; import java.util.function.BiFunction; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.util.SoundEmitter; import net.minecraft.entity.Entity; import net.minecraft.entity.ai.RangedAttackMob; @@ -35,9 +36,7 @@ public class RangedAttackBehaviour extends E spit.setOwner(player.getMaster()); if (!entity.isSilent()) { - entity.world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), - sound, entity.getSoundCategory(), 1, - 1 + (entity.world.random.nextFloat() - entity.world.random.nextFloat()) * 0.2F); + SoundEmitter.playSoundAt(entity, sound, 1, 1 + (entity.world.random.nextFloat() - entity.world.random.nextFloat()) * 0.2F); } entity.world.spawnEntity(spit); diff --git a/src/main/java/com/minelittlepony/unicopia/item/ProjectileItem.java b/src/main/java/com/minelittlepony/unicopia/item/ProjectileItem.java index b75270c9..5b436c00 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ProjectileItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ProjectileItem.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.projectile.MagicProjectileEntity; import com.minelittlepony.unicopia.projectile.ProjectileDelegate; +import com.minelittlepony.unicopia.util.SoundEmitter; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.projectile.ProjectileEntity; @@ -34,7 +35,7 @@ abstract class ProjectileItem extends Item implements ProjectileDelegate { ItemStack stack = player.getStackInHand(hand); - world.playSound(null, player.getX(), player.getY(), player.getZ(), + SoundEmitter.playSoundAt(player, getThrowSound(stack), SoundCategory.NEUTRAL, 0.5F, 0.4F / (world.random.nextFloat() * 0.4F + 0.8F)); diff --git a/src/main/java/com/minelittlepony/unicopia/util/SoundEmitter.java b/src/main/java/com/minelittlepony/unicopia/util/SoundEmitter.java new file mode 100644 index 00000000..e3cba35e --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/util/SoundEmitter.java @@ -0,0 +1,34 @@ +package com.minelittlepony.unicopia.util; + +import net.minecraft.entity.Entity; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvent; +import net.minecraft.world.World; + +public interface SoundEmitter { + + World getReferenceWorld(); + + Entity getEntity(); + + + default void playSound(SoundEvent sound, float volume, float pitch) { + playSoundAt(getEntity(), sound, volume, pitch); + } + + default void playSound(SoundEvent sound, float volume) { + playSound(sound, volume, getRandomPitch()); + } + + default float getRandomPitch() { + return (float)getReferenceWorld().getRandom().nextTriangular(0.5F, 0.2F); + } + + static void playSoundAt(Entity entity, SoundEvent sound, float pitch, float volume) { + playSoundAt(entity, sound, entity.getSoundCategory(), volume, pitch); + } + + static void playSoundAt(Entity entity, SoundEvent sound, SoundCategory category, float pitch, float volume) { + entity.world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), sound, category, volume, pitch); + } +}