|
| 1 | +package dev.kir.sync.easteregg.mixin.technoblade; |
| 2 | + |
| 3 | +import dev.kir.sync.easteregg.technoblade.Technoblade; |
| 4 | +import dev.kir.sync.easteregg.technoblade.TechnobladeTransformable; |
| 5 | +import net.fabricmc.api.EnvType; |
| 6 | +import net.fabricmc.api.Environment; |
| 7 | +import net.minecraft.client.MinecraftClient; |
| 8 | +import net.minecraft.client.sound.EntityTrackingSoundInstance; |
| 9 | +import net.minecraft.client.sound.PositionedSoundInstance; |
| 10 | +import net.minecraft.client.world.ClientWorld; |
| 11 | +import net.minecraft.entity.Entity; |
| 12 | +import net.minecraft.entity.mob.MobEntity; |
| 13 | +import net.minecraft.entity.player.PlayerEntity; |
| 14 | +import net.minecraft.sound.SoundCategory; |
| 15 | +import net.minecraft.sound.SoundEvent; |
| 16 | +import net.minecraft.util.Identifier; |
| 17 | +import net.minecraft.util.math.Box; |
| 18 | +import net.minecraft.util.math.random.Random; |
| 19 | +import net.minecraft.util.profiler.Profiler; |
| 20 | +import net.minecraft.util.registry.Registry; |
| 21 | +import net.minecraft.util.registry.RegistryEntry; |
| 22 | +import net.minecraft.util.registry.RegistryKey; |
| 23 | +import net.minecraft.world.MutableWorldProperties; |
| 24 | +import net.minecraft.world.World; |
| 25 | +import net.minecraft.world.dimension.DimensionType; |
| 26 | +import org.jetbrains.annotations.Nullable; |
| 27 | +import org.spongepowered.asm.mixin.Final; |
| 28 | +import org.spongepowered.asm.mixin.Mixin; |
| 29 | +import org.spongepowered.asm.mixin.Shadow; |
| 30 | +import org.spongepowered.asm.mixin.injection.At; |
| 31 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 32 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import java.util.function.Supplier; |
| 36 | + |
| 37 | +@Environment(EnvType.CLIENT) |
| 38 | +@Mixin(ClientWorld.class) |
| 39 | +abstract class ClientWorldMixin extends World { |
| 40 | + @Shadow |
| 41 | + private @Final MinecraftClient client; |
| 42 | + |
| 43 | + private ClientWorldMixin(MutableWorldProperties properties, RegistryKey<World> registryRef, RegistryEntry<DimensionType> dimension, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long seed, int maxChainedNeighborUpdates) { |
| 44 | + super(properties, registryRef, dimension, profiler, isClient, debugWorld, seed, maxChainedNeighborUpdates); |
| 45 | + } |
| 46 | + |
| 47 | + @Inject(method = "playSound(DDDLnet/minecraft/sound/SoundEvent;Lnet/minecraft/sound/SoundCategory;FFZJ)V", at = @At("HEAD"), cancellable = true) |
| 48 | + private void playSound(double x, double y, double z, SoundEvent sound, SoundCategory category, float volume, float pitch, boolean useDistance, long seed, CallbackInfo ci) { |
| 49 | + if (category != SoundCategory.NEUTRAL) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + List<MobEntity> Technoblades = this.getEntitiesByClass(MobEntity.class, new Box(x - 0.1, y - 0.1, z - 0.1, x + 0.1, y + 0.1, z + 0.1), e -> e instanceof TechnobladeTransformable && ((TechnobladeTransformable)e).isTechnoblade()); |
| 54 | + MobEntity entity = Technoblades.size() == 0 ? null : Technoblades.get(0); |
| 55 | + if (entity == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + Technoblade Technoblade = ((TechnobladeTransformable)entity).asTechnoblade(); |
| 60 | + sound = this.getTechnobladeSound(sound); |
| 61 | + if (sound == null) { |
| 62 | + ci.cancel(); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + double distance = this.client.gameRenderer.getCamera().getPos().squaredDistanceTo(x, y, z); |
| 67 | + PositionedSoundInstance positionedSoundInstance = new PositionedSoundInstance(sound, Technoblade.getSoundCategory(), volume, pitch, Random.create(seed), x, y, z); |
| 68 | + if (useDistance && distance > 100) { |
| 69 | + this.client.getSoundManager().play(positionedSoundInstance, (int)(Math.sqrt(distance) * 0.5)); |
| 70 | + } else { |
| 71 | + this.client.getSoundManager().play(positionedSoundInstance); |
| 72 | + } |
| 73 | + ci.cancel(); |
| 74 | + } |
| 75 | + |
| 76 | + @Inject(method = "playSoundFromEntity", at = @At("HEAD"), cancellable = true) |
| 77 | + private void playSoundFromEntity(PlayerEntity except, Entity entity, SoundEvent sound, SoundCategory category, float volume, float pitch, long seed, CallbackInfo ci) { |
| 78 | + if (except != this.client.player || !(entity instanceof TechnobladeTransformable) || !((TechnobladeTransformable)entity).isTechnoblade()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + Technoblade Technoblade = ((TechnobladeTransformable)entity).asTechnoblade(); |
| 83 | + sound = this.getTechnobladeSound(sound); |
| 84 | + if (sound == null) { |
| 85 | + ci.cancel(); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + this.client.getSoundManager().play(new EntityTrackingSoundInstance(sound, Technoblade.getSoundCategory(), volume, pitch, entity, seed)); |
| 90 | + ci.cancel(); |
| 91 | + } |
| 92 | + |
| 93 | + private @Nullable SoundEvent getTechnobladeSound(SoundEvent sound) { |
| 94 | + Identifier originalSoundId = sound.getId(); |
| 95 | + if (originalSoundId.getPath().endsWith(".ambient") || originalSoundId.getPath().endsWith(".death")) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + SoundEvent fixedSound = Registry.SOUND_EVENT.get(new Identifier(originalSoundId.getNamespace(), originalSoundId.getPath().replaceFirst("\\.[^.]+", ".player"))); |
| 100 | + if (fixedSound == null) { |
| 101 | + fixedSound = sound; |
| 102 | + } |
| 103 | + return fixedSound; |
| 104 | + } |
| 105 | +} |
0 commit comments