Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speaker sound #10

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.client.audio.VelocityTickableSoundInstance;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

@Mixin(SpeakerSound.class)
public abstract class MixinSpeakerSound extends AbstractSoundInstance {
public abstract class MixinSpeakerSound extends AbstractSoundInstance implements VelocityTickableSoundInstance {
@Unique private SpeakerPosition speakerPosition;
@Unique private boolean isOnShip;
@Unique private Ship ship;

protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2, RandomSource arg3) {
super(arg, arg2, arg3);
Expand All @@ -30,8 +35,8 @@ protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2, RandomSource
)
private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
this.speakerPosition = position;
this.isOnShip = VSGameUtilsKt.getShipManagingPos(position.level(), position.position()) != null;
if (this.isOnShip) {
this.ship = VSGameUtilsKt.getShipManagingPos(position.level(), position.position());
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
Expand All @@ -44,11 +49,17 @@ private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
at = @At("HEAD")
)
private void updateWorldPos(CallbackInfo ci) {
if (this.isOnShip) {
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
}
}

@NotNull
@Override
public Vector3dc getVelocity() {
return ship != null ? ship.getVelocity() : new Vector3d();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.client.audio.VelocityTickableSoundInstance;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

@Mixin(SpeakerSound.class)
public abstract class MixinSpeakerSound extends AbstractSoundInstance {
public abstract class MixinSpeakerSound extends AbstractSoundInstance implements VelocityTickableSoundInstance {
@Unique private SpeakerPosition speakerPosition;
@Unique private boolean isOnShip;
@Unique private Ship ship;

protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2, RandomSource arg3) {
super(arg, arg2, arg3);
Expand All @@ -30,8 +35,8 @@ protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2, RandomSource
)
private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
this.speakerPosition = position;
this.isOnShip = VSGameUtilsKt.getShipManagingPos(position.level(), position.position()) != null;
if (this.isOnShip) {
this.ship = VSGameUtilsKt.getShipManagingPos(position.level(), position.position());
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
Expand All @@ -44,11 +49,17 @@ private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
at = @At("HEAD")
)
private void updateWorldPos(CallbackInfo ci) {
if (this.isOnShip) {
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
}
}

@NotNull
@Override
public Vector3dc getVelocity() {
return ship != null ? ship.getVelocity() : new Vector3d();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.xiewuzhiying.vs_addition.forge.mixin.netmusic;

import com.github.tartaricacid.netmusic.network.message.MusicToClientMessage;
import io.github.xiewuzhiying.vs_addition.forge.compats.NetMusicSoundOnShip;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.core.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

import java.net.URL;

@Mixin(MusicToClientMessage.class)
public abstract class MixinMusicToClientMessage {
@Inject(
method = "lambda$onHandle$2",
at = @At("HEAD"),
cancellable = true,
remap = false
)
private static void man(MusicToClientMessage message, URL url, final CallbackInfoReturnable<SoundInstance> cir) {
BlockPos pos = ((MusicToClientMessageAccessor)message).getPos();
final Ship ship = VSGameUtilsKt.getShipManagingPos(Minecraft.getInstance().level, pos.getX(), pos.getY(), pos.getZ());
if (ship != null) {
cir.setReturnValue(new NetMusicSoundOnShip(pos, url, ((MusicToClientMessageAccessor)message).getTimeSecond(), ship));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.xiewuzhiying.vs_addition.forge.mixin.netmusic;

import com.github.tartaricacid.netmusic.network.message.MusicToClientMessage;
import net.minecraft.core.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MusicToClientMessage.class)
public interface MusicToClientMessageAccessor {
@Accessor(remap = false)
BlockPos getPos();

@Accessor(remap = false)
int getTimeSecond();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.xiewuzhiying.vs_addition.forge.compats

import com.github.tartaricacid.netmusic.client.audio.NetMusicSound
import net.minecraft.core.BlockPos
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.Ship
import org.valkyrienskies.mod.client.audio.VelocityTickableSoundInstance
import java.net.URL

class NetMusicSoundOnShip(pos: BlockPos, songUrl: URL, timeSecond: Int, private val ship: Ship) : NetMusicSound(
pos,
songUrl,
timeSecond
), VelocityTickableSoundInstance {

private val originalPos = Vector3d(x, y, z)

override val velocity: Vector3dc
get() = ship.velocity

override fun tick() {
val newPos = ship.shipToWorld.transformPosition(originalPos, Vector3d())
this.x = newPos.x
this.y = newPos.y
this.z = newPos.z
super.tick()
}
}
5 changes: 3 additions & 2 deletions forge/src/main/resources/vs_addition.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
"create.HosePulley.MixinFluidFillingBehaviour",
"create.HosePulley.MixinHosePulleyBlockEntity",
"createaddition.MixinPortableEnergyInterfaceMovement",
"netmusic.MixinMusicToClientMessage",
"netmusic.MusicToClientMessageAccessor",
"tacz.MixinEntityKineticBullet"
],
"injectors": {
"defaultRequire": 1
},
"minVersion": "0.8",
"client": [
"computercraft.MixinSpeakerSound",
"netmusic.MixinNetMusicSound"
"computercraft.MixinSpeakerSound"
]
}
Loading