This repository was archived by the owner on Jan 12, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSoundManager.java
More file actions
121 lines (107 loc) · 4.2 KB
/
SoundManager.java
File metadata and controls
121 lines (107 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package dev.stashy.extrasounds;
import dev.stashy.extrasounds.debug.DebugUtils;
import dev.stashy.extrasounds.sounds.SoundType;
import dev.stashy.extrasounds.sounds.Sounds;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SoundManager
{
private static final Logger LOGGER = LogManager.getLogger();
private static long lastPlayed = System.currentTimeMillis();
public static void playSound(ItemStack stack, SoundType type)
{
var itemId = Registries.ITEM.getId(stack.getItem());
String idString = ExtraSounds.getClickId(itemId, type);
if (!Identifier.isValid(idString))
{
LOGGER.error("Unable to parse sound from ID: " + idString);
return;
}
Identifier id = Identifier.tryParse(idString);
Registries.SOUND_EVENT.getOrEmpty(id).ifPresentOrElse(
(snd) -> playSound(snd, type),
() -> LOGGER.error("Sound cannot be found in registry: " + id));
}
public static void playSound(StatusEffect effect, boolean add)
{
DebugUtils.effectLog(effect, add);
SoundEvent e = add ?
switch (effect.getCategory())
{
case HARMFUL -> Sounds.EFFECT_ADD_NEGATIVE;
case NEUTRAL, BENEFICIAL -> Sounds.EFFECT_ADD_POSITIVE;
}
:
switch (effect.getCategory())
{
case HARMFUL -> Sounds.EFFECT_REMOVE_NEGATIVE;
case NEUTRAL, BENEFICIAL -> Sounds.EFFECT_REMOVE_POSITIVE;
};
playSound(e, SoundType.EFFECT);
}
public static void playSound(SoundEvent snd, SoundType type)
{
playSound(snd, type, type.category);
}
public static void playSound(SoundEvent snd, SoundType type, SoundCategory cat)
{
playSound(snd, type.pitch, cat);
}
public static void playSound(SoundEvent snd, float pitch, SoundCategory cat)
{
playSound(new PositionedSoundInstance(snd.getId(), cat, getMasterVol(), pitch, ExtraSounds.mcRandom,
false, 0, SoundInstance.AttenuationType.NONE, 0.0D, 0.0D, 0.0D,
true));
DebugUtils.soundLog(snd);
}
public static void playSound(SoundEvent snd, SoundType type, BlockPos position)
{
playSound(new PositionedSoundInstance(snd, type.category, getMasterVol(), type.pitch,
ExtraSounds.mcRandom,
position.getX() + 0.5,
position.getY() + 0.5,
position.getZ() + 0.5));
DebugUtils.soundLog(snd);
}
public static void playSound(PositionedSoundInstance instance)
{
throttle(() -> {
var client = MinecraftClient.getInstance();
client.send(() -> {
client.getSoundManager().play(instance);
});
});
}
public static void stopSound(SoundEvent e, SoundType type)
{
MinecraftClient.getInstance().getSoundManager().stopSounds(e.getId(), type.category);
}
private static void throttle(Runnable r)
{
try
{
long now = System.currentTimeMillis();
if (now - lastPlayed > 5) r.run();
lastPlayed = now;
}
catch (Exception e)
{
System.err.println("Failed to play sound:");
e.printStackTrace();
}
}
private static float getMasterVol()
{
return MinecraftClient.getInstance().options.getSoundVolume(Mixers.MASTER);
}
}