aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/CustomMusic.java
blob: 717a3abb9c6f5ae9de67560a215e4cdf15c7ecff (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package me.Danker.features;

import me.Danker.DankersSkyblockMod;
import me.Danker.commands.ToggleCommand;
import me.Danker.handlers.ScoreboardHandler;
import me.Danker.utils.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.StringUtils;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.sound.PlaySoundEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CustomMusic {

    static boolean cancelNotes;

    public static Song dungeonboss;
    public static int dungeonbossVolume;
    public static Song bloodroom;
    public static int bloodroomVolume;
    public static Song dungeon;
    public static int dungeonVolume;
    public static Song phase2;
    public static int phase2Volume;
    public static Song phase3;
    public static int phase3Volume;
    public static Song phase4;
    public static int phase4Volume;
    public static Song phase5;
    public static int phase5Volume;
    public static Song hub;
    public static int hubVolume;
    public static Song island;
    public static int islandVolume;
    public static Song dungeonHub;
    public static int dungeonHubVolume;
    public static Song farmingIslands;
    public static int farmingIslandsVolume;
    public static Song goldMine;
    public static int goldMineVolume;
    public static Song deepCaverns;
    public static int deepCavernsVolume;
    public static Song dwarvenMines;
    public static int dwarvenMinesVolume;
    public static Song crystalHollows;
    public static int crystalHollowsVolume;
    public static Song spidersDen;
    public static int spidersDenVolume;
    public static Song crimsonIsle;
    public static int crimsonIsleVolume;
    public static Song end;
    public static int endVolume;
    public static Song park;
    public static int parkVolume;


    @SubscribeEvent
    public void onWorldChange(WorldEvent.Load event) {
        reset();
    }

    @SubscribeEvent(priority = EventPriority.LOW)
    public void onTick(TickEvent.ClientTickEvent event) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
        if (event.phase != TickEvent.Phase.START) return;

        Minecraft mc = Minecraft.getMinecraft();
        EntityPlayerSP player = mc.thePlayer;
        World world = mc.theWorld;
        if (DankersSkyblockMod.tickAmount % 10 == 0) {
            if (world != null && player != null) {
                if (Utils.inDungeons) {
                    List<String> scoreboard = ScoreboardHandler.getSidebarLines();
                    if (scoreboard.size() > 2) {
                        String firstLine = ScoreboardHandler.cleanSB(scoreboard.get(scoreboard.size() - 1));
                        String secondLine = ScoreboardHandler.cleanSB(scoreboard.get(scoreboard.size() - 2));
                        if (firstLine.contains("30,30") || // F1
                            firstLine.contains("30,125") || // F2
                            firstLine.contains("30,225") || // F3
                            secondLine.contains("- Healthy") || // F3
                            firstLine.contains("30,344") || // F4
                            firstLine.contains("livid") || // F5
                            firstLine.contains("sadan") || // F6
                            firstLine.contains("maxor")) { // F7

                            if (ToggleCommand.dungeonBossMusic) dungeonboss.start();
                        }
                    }
                } else {
                    switch (Utils.tabLocation) {
                        case "Hub":
                            if (ToggleCommand.hubMusic) hub.start();
                            break;
                        case "Private Island":
                            if (ToggleCommand.islandMusic) island.start();
                            break;
                        case "Dungeon Hub":
                            if (ToggleCommand.dungeonHubMusic) dungeonHub.start();
                            break;
                        case "The Farming Islands":
                            if (ToggleCommand.farmingIslandsMusic) farmingIslands.start();
                            break;
                        case "Gold Mine":
                            if (ToggleCommand.goldMineMusic) goldMine.start();
                            break;
                        case "Deep Caverns":
                            if (ToggleCommand.deepCavernsMusic) deepCaverns.start();
                            break;
                        case "Dwarven Mines":
                            if (ToggleCommand.dwarvenMinesMusic) dwarvenMines.start();
                            break;
                        case "Crystal Hollows":
                            if (ToggleCommand.crystalHollowsMusic) crystalHollows.start();
                            break;
                        case "Spider's Den":
                            if (ToggleCommand.spidersDenMusic) spidersDen.start();
                            break;
                        case "Crimson Isle":
                            if (ToggleCommand.crimsonIsleMusic) crimsonIsle.start();
                            break;
                        case "The End":
                            if (ToggleCommand.endMusic) end.start();
                            break;
                        case "The Park":
                            if (ToggleCommand.parkMusic) park.start();
                            break;
                    }
                }
            }
        }
    }

    @SubscribeEvent(receiveCanceled = true)
    public void onChat(ClientChatReceivedEvent event) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
        String message = StringUtils.stripControlCodes(event.message.getUnformattedText());

        if (ToggleCommand.dungeonMusic && Utils.inDungeons) {
            if (message.contains("[NPC] Mort: Here, I found this map when I first entered the dungeon.")) {
                dungeon.start();
            }
        }

        if (message.contains(":")) return;

        if (Utils.inDungeons) {
            if (ToggleCommand.dungeonBossMusic) {
                if (message.startsWith("[BOSS] Storm: Pathetic Maxor")) {
                    phase2.start();
                } else if (message.startsWith("[BOSS] Goldor: Who dares trespass into my domain?")) {
                    phase3.start();
                } else if (message.startsWith("[BOSS] Necron: You went further than any human before")) {
                    phase4.start();
                } else if (message.startsWith("[BOSS] ") && message.endsWith("You.. again?")) {
                    phase5.start();
                }
            }

            if (message.contains("EXTRA STATS ")) {
                dungeonboss.stop();
                bloodroom.stop();
                dungeon.stop();
                phase2.stop();
                phase3.stop();
                phase4.stop();
                phase5.stop();
            } else if (message.contains("The BLOOD DOOR has been opened!")) {
                dungeon.stop();
                if (ToggleCommand.bloodRoomMusic) bloodroom.start();
            }
        }
    }

    @SubscribeEvent
    public void onSound(PlaySoundEvent event) {
        if (cancelNotes && event.name.startsWith("note.")) {
            event.result = null;
        }
    }

    public static void init(String configDirectory) {
        if (configDirectory == null) return;
        File directory = new File(configDirectory + "/dsmmusic");
        if (!directory.exists()) directory.mkdir();

        reset();

        dungeonboss = new Song(directory, "dungeonboss", dungeonbossVolume);
        bloodroom = new Song(directory, "bloodroom", bloodroomVolume);
        dungeon = new Song(directory, "dungeon", dungeonVolume);
        phase2 = new Song(directory, "phasetwo", phase2Volume);
        phase3 = new Song(directory, "phasethree", phase3Volume);
        phase4 = new Song(directory, "phasefour", phase4Volume);
        phase5 = new Song(directory, "phasefive", phase5Volume);
        hub = new Song(directory, "hub", hubVolume);
        island = new Song(directory, "island", hubVolume);
        dungeonHub = new Song(directory, "dungeonhub", dungeonHubVolume);
        farmingIslands = new Song(directory, "farmingislands", farmingIslandsVolume);
        goldMine = new Song(directory, "goldmine", goldMineVolume);
        deepCaverns = new Song(directory, "deepcaverns", deepCavernsVolume);
        dwarvenMines = new Song(directory, "dwarvenmines", dwarvenMinesVolume);
        crystalHollows = new Song(directory, "crystalhollows", crystalHollowsVolume);
        spidersDen = new Song(directory, "spidersden", spidersDenVolume);
        crimsonIsle = new Song(directory, "crimsonisle", crimsonIsleVolume);
        end = new Song(directory, "end", endVolume);
        park = new Song(directory, "park", parkVolume);
    }

    public static void reset() {
        if (dungeonboss != null) dungeonboss.stop();
        if (bloodroom != null) bloodroom.stop();
        if (dungeon != null) dungeon.stop();
        if (phase2 != null) phase2.stop();
        if (phase3 != null) phase3.stop();
        if (phase4 != null) phase4.stop();
        if (phase5 != null) phase5.stop();
        if (hub != null) hub.stop();
        if (island != null) island.stop();
        if (dungeonHub != null) dungeonHub.stop();
        if (farmingIslands != null) farmingIslands.stop();
        if (goldMine != null) goldMine.stop();
        if (deepCaverns != null) deepCaverns.stop();
        if (dwarvenMines != null) dwarvenMines.stop();
        if (crystalHollows != null) crystalHollows.stop();
        if (spidersDen != null) spidersDen.stop();
        if (crimsonIsle != null) crimsonIsle.stop();
        if (end != null) end.stop();
        if (park != null) park.stop();
    }

    public static class Song {

        public Clip music;
        private final List<File> playlist = new ArrayList<>();
        private int volume;

        public Song(File directory, String songName, int volume) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (!file.isDirectory() && file.getName().matches(songName + "\\d*(?:\\.wav)?\\.wav")) { // .wav.wav moment
                        playlist.add(file);
                        System.out.println("Added " + file.getName() + " to " + songName + " playlist.");
                    }
                }
            }

            this.volume = volume;
        }

        public void start() throws UnsupportedAudioFileException, LineUnavailableException, IOException {

            try {
                if (music == null) music = AudioSystem.getClip();
                if (!music.isRunning()) {
                    reset();
                    shuffle();
                    setVolume(volume);
                    cancelNotes = true;
                    music.setMicrosecondPosition(0);
                    music.start();
                }
            } catch (UnsupportedAudioFileException ex) {
                ex.printStackTrace();

                EntityPlayer player = Minecraft.getMinecraft().thePlayer;
                if (player != null) {
                    player.addChatMessage(new ChatComponentText(DankersSkyblockMod.ERROR_COLOUR + "Attempted to play non .wav file. Please use a .wav converter instead of renaming the file."));
                }
            }
        }

        public void stop() {
            cancelNotes = false;
            if (music != null) {
                music.stop();
                if (music.isOpen()) music.close();
            }
        }

        public void shuffle() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
            if (playlist.size() > 0) {
                File file = playlist.get(new Random().nextInt(playlist.size()));
                music = AudioSystem.getClip();
                AudioInputStream ais = AudioSystem.getAudioInputStream(file);
                music.open(ais);
            }
        }

        public boolean setVolume(int volume) {
            this.volume = volume;
            if (playlist.size() < 1) return false;
            if (volume <= 0 || volume > 100) {
                EntityPlayer player = Minecraft.getMinecraft().thePlayer;
                if (player != null) player.addChatMessage(new ChatComponentText(DankersSkyblockMod.ERROR_COLOUR + "Volume can only be set between 0% and 100%."));
                return false;
            }

            float decibels = (float) (20 * Math.log(volume / 100.0));
            FloatControl control = (FloatControl) music.getControl(FloatControl.Type.MASTER_GAIN);
            if (decibels <= control.getMinimum() || decibels >= control.getMaximum()) return false;
            control.setValue(decibels);

            return true;
        }

    }

}