|
4 | 4 | import dev.dfonline.codeclient.Feature; |
5 | 5 | import dev.dfonline.codeclient.config.KeyBinds; |
6 | 6 | import dev.dfonline.codeclient.dev.overlay.ChestPeeker; |
7 | | -import dev.dfonline.codeclient.hypercube.item.Sound; |
| 7 | +import dev.dfonline.codeclient.hypercube.actiondump.Sound; |
8 | 8 | import dev.dfonline.codeclient.hypercube.item.VarItem; |
9 | 9 | import dev.dfonline.codeclient.hypercube.item.VarItems; |
10 | 10 | import dev.dfonline.codeclient.location.Dev; |
| 11 | +import net.minecraft.client.network.ClientPlayerEntity; |
11 | 12 | import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.sound.SoundCategory; |
12 | 14 | import net.minecraft.sound.SoundEvent; |
13 | | -import net.minecraft.sound.SoundEvents; |
| 15 | +import net.minecraft.util.Identifier; |
| 16 | +import net.minecraft.util.math.random.Random; |
14 | 17 |
|
15 | | -import java.util.Optional; |
| 18 | +import java.util.List; |
16 | 19 |
|
17 | 20 | public class PreviewSoundChest extends Feature { |
18 | 21 |
|
| 22 | + @SuppressWarnings("deprecation") |
| 23 | + private static final Random threadSafeRandom = Random.createThreadSafe(); |
| 24 | + |
19 | 25 | @Override |
20 | 26 | public void tick() { |
21 | 27 | if (KeyBinds.previewSounds.wasPressed() && CodeClient.MC.world != null && CodeClient.location instanceof Dev) { |
22 | 28 | if (CodeClient.MC.player == null) return; |
23 | | - ChestPeeker.pick(items -> { |
24 | | - for (ItemStack item : items) { |
25 | | - VarItem varItem = VarItems.parse(item); |
26 | | - if (!(varItem instanceof Sound sound)) continue; |
27 | | - Optional<dev.dfonline.codeclient.hypercube.actiondump.Sound> adSound = sound.getSoundId(); |
28 | | - if (adSound.isEmpty()) continue; |
29 | | - try { |
30 | | - CodeClient.MC.player.playSound( |
31 | | - (SoundEvent) SoundEvents.class.getDeclaredField(adSound.get().sound).get(null), |
32 | | - (float) sound.getVolume(), |
33 | | - (float) sound.getPitch() |
34 | | - ); |
35 | | - } catch (Exception ignored) { |
36 | | - |
37 | | - } |
38 | | - } |
| 29 | + ChestPeeker.pick(PreviewSoundChest::previewSounds); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static void previewSounds(List<ItemStack> items) { |
| 34 | + ClientPlayerEntity player = CodeClient.MC.player; |
| 35 | + if (player == null || CodeClient.MC.world == null) return; |
| 36 | + |
| 37 | + for (ItemStack item : items) { |
| 38 | + VarItem varItem = VarItems.parse(item); |
| 39 | + if (!(varItem instanceof dev.dfonline.codeclient.hypercube.item.Sound sound)) continue; |
| 40 | + |
| 41 | + sound.getSoundId().ifPresent(adSound -> { |
| 42 | + Sound.Variant variant = adSound.getVariantFromName(sound.getVariant()); |
| 43 | + Identifier id = Identifier.ofVanilla(adSound.soundId); |
| 44 | + SoundEvent event = SoundEvent.of(id); |
| 45 | + |
| 46 | + CodeClient.MC.world.playSound( |
| 47 | + player, |
| 48 | + player.getX(), |
| 49 | + player.getY(), |
| 50 | + player.getZ(), |
| 51 | + event, |
| 52 | + SoundCategory.MASTER, |
| 53 | + (float) sound.getVolume(), |
| 54 | + (float) sound.getPitch(), |
| 55 | + // I would copy-paste this entire method call for either variant or non-variant |
| 56 | + // to preserve the original random, but this is way cleaner. |
| 57 | + variant != null ? variant.seed() : threadSafeRandom.nextLong() |
| 58 | + ); |
39 | 59 | }); |
40 | 60 | } |
41 | 61 | } |
| 62 | + |
42 | 63 | } |
0 commit comments