Skip to content

Commit 1eb1e0f

Browse files
committed
Kotlinize the sounds
1 parent f6924fb commit 1eb1e0f

File tree

8 files changed

+379
-380
lines changed

8 files changed

+379
-380
lines changed

src/main/kotlin/mods/eln/node/six/SixNodeElement.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ abstract class SixNodeElement(sixNode: SixNode, @JvmField var side: Direction, d
6464
}
6565

6666
open fun inventoryChanged() {}
67+
6768
override fun play(s: SoundCommand) {
6869
s.addUuid(getUuid())
6970
s.set(sixNode!!.coordinate)
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package mods.eln.sound;
1+
package mods.eln.sound
22

3-
public interface IPlayer {
4-
5-
void play(SoundCommand cmd);
6-
7-
void stop(int uuid);
3+
interface IPlayer {
4+
fun play(cmd: SoundCommand)
5+
fun stop(uuid: Int)
86
}
Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,75 @@
1-
package mods.eln.sound;
2-
3-
import mods.eln.client.ClientProxy;
4-
import mods.eln.client.SoundLoader;
5-
import mods.eln.misc.Utils;
6-
import net.minecraft.client.Minecraft;
7-
import net.minecraft.entity.player.EntityPlayer;
8-
9-
public class SoundClient {
10-
/*public static void playFromBlock(World world, int x, int y, int z, String track, float volume, float pitch, float rangeNominal, float rangeMax) {
11-
play(world, x + 0.5, y + 0.5, z + 0.5, track, volume, pitch, rangeNominal, rangeMax);
12-
}*/
13-
14-
public static void play(SoundCommand p) {
15-
ClientProxy.soundClientEventListener.currentUuid = p.uuid; //trolilole
16-
17-
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
18-
if (p.world.provider.dimensionId != player.dimension) return;
19-
double distance = Math.sqrt(Math.pow(p.x - player.posX, 2) + Math.pow(p.y - player.posY, 2) + Math.pow(p.z - player.posZ, 2));
20-
if (distance >= p.rangeMax) return;
21-
float distanceFactor = 1;
1+
package mods.eln.sound
2+
3+
import mods.eln.client.ClientProxy
4+
import mods.eln.client.SoundLoader
5+
import mods.eln.misc.Utils.TraceRayWeightOpaque
6+
import mods.eln.misc.Utils.println
7+
import mods.eln.misc.Utils.traceRay
8+
import net.minecraft.client.Minecraft
9+
import net.minecraft.entity.player.EntityPlayer
10+
import kotlin.math.pow
11+
import kotlin.math.sqrt
12+
13+
object SoundClient {
14+
fun play(p: SoundCommand) {
15+
ClientProxy.soundClientEventListener.currentUuid = p.uuid
16+
17+
val player: EntityPlayer = Minecraft.getMinecraft().thePlayer
18+
if (p.world!!.provider.dimensionId != player.dimension) return
19+
val distance = sqrt((p.x - player.posX).pow(2.0) + (p.y - player.posY).pow(2.0) + (p.z - player.posZ).pow(2.0))
20+
if (distance >= p.rangeMax) return
21+
var distanceFactor = 1f
2222
if (distance > p.rangeNominal) {
23-
distanceFactor = (float) ((p.rangeMax - distance) / (p.rangeMax - p.rangeNominal));
23+
distanceFactor = ((p.rangeMax - distance) / (p.rangeMax - p.rangeNominal)).toFloat()
2424
}
2525

26-
float blockFactor = Utils.traceRay(p.world, player.posX, player.posY, player.posZ, p.x, p.y, p.z, new Utils.TraceRayWeightOpaque()) * p.blockFactor;
26+
val blockFactor = traceRay(
27+
p.world!!,
28+
player.posX,
29+
player.posY,
30+
player.posZ,
31+
p.x,
32+
p.y,
33+
p.z,
34+
TraceRayWeightOpaque()
35+
) * p.blockFactor
2736

28-
int trackCount = SoundLoader.getTrackCount(p.track);
37+
val trackCount = SoundLoader.getTrackCount(p.track)
2938

3039
if (trackCount == 1) {
31-
float temp = 1.0f / (1 + blockFactor);
32-
p.volume *= Math.pow(temp, 2);
33-
p.volume *= distanceFactor;
34-
if (p.volume <= 0) return;
40+
val temp = 1.0f / (1 + blockFactor)
41+
p.volume *= temp.pow(2.0f)
42+
p.volume *= distanceFactor
43+
if (p.volume <= 0) return
3544

36-
p.world.playSound(player.posX + 2 * (p.x - player.posX) / distance, player.posY + 2 * (p.y - player.posY) / distance, player.posZ + 2 * (p.z - player.posZ) / distance, p.track, p.volume, p.pitch, false);
45+
p.world!!.playSound(
46+
player.posX + 2 * (p.x - player.posX) / distance,
47+
player.posY + 2 * (p.y - player.posY) / distance,
48+
player.posZ + 2 * (p.z - player.posZ) / distance,
49+
p.track,
50+
p.volume,
51+
p.pitch,
52+
false
53+
)
3754
} else {
38-
for (int idx = 0; idx < trackCount; idx++) {
39-
float bandVolume = p.volume;
40-
bandVolume *= distanceFactor;
41-
float normalizedBlockFactor = blockFactor;
42-
43-
bandVolume -= ((trackCount - 1 - idx) / (trackCount - 1f) + 0.2) * normalizedBlockFactor;
44-
Utils.println(bandVolume);
45-
p.world.playSound(player.posX + 2 * (p.x - player.posX) / distance, player.posY + 2 * (p.y - player.posY) / distance, player.posZ + 2 * (p.z - player.posZ) / distance, p.track + "_" + idx + "x", bandVolume, p.pitch, false);
55+
for (idx in 0 until trackCount) {
56+
var bandVolume = p.volume
57+
bandVolume *= distanceFactor
58+
59+
bandVolume -= (((trackCount - 1 - idx) / (trackCount - 1f) + 0.2) * blockFactor).toFloat()
60+
println(bandVolume)
61+
p.world!!.playSound(
62+
player.posX + 2 * (p.x - player.posX) / distance,
63+
player.posY + 2 * (p.y - player.posY) / distance,
64+
player.posZ + 2 * (p.z - player.posZ) / distance,
65+
p.track + "_" + idx + "x",
66+
bandVolume,
67+
p.pitch,
68+
false
69+
)
4670
}
4771
}
4872

49-
ClientProxy.soundClientEventListener.currentUuid = null;
73+
ClientProxy.soundClientEventListener.currentUuid = null
5074
}
5175
}
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
package mods.eln.sound;
1+
package mods.eln.sound
22

3-
import mods.eln.client.IUuidEntity;
4-
import mods.eln.misc.Utils;
5-
import net.minecraft.client.audio.ISound;
6-
import net.minecraft.client.audio.SoundManager;
3+
import mods.eln.client.IUuidEntity
4+
import mods.eln.misc.Utils.println
5+
import net.minecraft.client.audio.ISound
6+
import net.minecraft.client.audio.SoundManager
77

8-
public class SoundClientEntity implements IUuidEntity {
8+
class SoundClientEntity(var sm: SoundManager, var sound: ISound) : IUuidEntity {
9+
var borneTimer: Int = 5
910

10-
public ISound sound;
11-
public SoundManager sm;
12-
13-
int borneTimer = 5;
14-
15-
public SoundClientEntity(SoundManager sm, ISound sound) {
16-
this.sound = sound;
17-
this.sm = sm;
18-
}
19-
20-
@Override
21-
public boolean isAlive() {
11+
override fun isAlive(): Boolean {
2212
if (borneTimer != 0) {
23-
borneTimer--;
24-
return true;
13+
borneTimer--
14+
return true
2515
}
26-
return sm.isSoundPlaying(sound);
16+
return sm.isSoundPlaying(sound)
2717
}
2818

29-
@Override
30-
public void kill() {
31-
Utils.println("Sound deleted");
32-
sm.stopSound(sound);
19+
override fun kill() {
20+
println("Sound deleted")
21+
sm.stopSound(sound)
3322
}
3423
}
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
package mods.eln.sound;
1+
package mods.eln.sound
22

3-
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
4-
import mods.eln.client.UuidManager;
5-
import net.minecraft.client.audio.ISound;
6-
import net.minecraft.client.audio.SoundManager;
7-
import net.minecraftforge.client.event.sound.PlaySoundSourceEvent;
8-
import net.minecraftforge.common.MinecraftForge;
3+
import cpw.mods.fml.common.eventhandler.SubscribeEvent
4+
import mods.eln.client.UuidManager
5+
import net.minecraft.client.audio.ISound
6+
import net.minecraft.client.audio.SoundManager
7+
import net.minecraftforge.client.event.sound.PlaySoundSourceEvent
8+
import net.minecraftforge.common.MinecraftForge
99

10-
import java.util.ArrayList;
10+
class SoundClientEventListener(var uuidManager: UuidManager) {
11+
@JvmField
12+
var currentUuid: ArrayList<Int>? = null
1113

12-
public class SoundClientEventListener {
13-
14-
UuidManager uuidManager;
15-
ArrayList<Integer> currentUuid = null;
16-
17-
public SoundClientEventListener(UuidManager uuidManager) {
18-
this.uuidManager = uuidManager;
19-
MinecraftForge.EVENT_BUS.register(this);
14+
init {
15+
MinecraftForge.EVENT_BUS.register(this)
2016
}
2117

2218
@SubscribeEvent
23-
public void event(PlaySoundSourceEvent e) {
24-
if (currentUuid == null) return;
25-
uuidManager.add(currentUuid, new SoundClientEntity(e.manager, e.sound));
19+
fun event(e: PlaySoundSourceEvent) {
20+
if (currentUuid == null) return
21+
uuidManager.add(currentUuid!!, SoundClientEntity(e.manager, e.sound))
2622
}
2723

28-
static class KillSound {
29-
public ISound sound;
30-
public SoundManager sm;
24+
internal class KillSound {
25+
var sound: ISound? = null
26+
var sm: SoundManager? = null
3127

32-
public void kill() {
33-
sm.stopSound(sound);
28+
fun kill() {
29+
sm!!.stopSound(sound)
3430
}
3531
}
3632
}

0 commit comments

Comments
 (0)