aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--soundchan.properties.example4
-rw-r--r--src/main/java/soundchan/BotListener/BotListener.java15
-rw-r--r--src/main/java/soundchan/BotListener/UserSoundWatcher.java69
-rw-r--r--src/main/java/soundchan/LocalAudioManager.java28
4 files changed, 105 insertions, 11 deletions
diff --git a/soundchan.properties.example b/soundchan.properties.example
index 8780021..b600032 100644
--- a/soundchan.properties.example
+++ b/soundchan.properties.example
@@ -24,3 +24,7 @@ audioOnUserJoin=FLAG_CONDITION
//The file where users and sound clips are related, see usersound.properties.example for more info
//If this is not set, it will default to usersounds.properties
userAudioFilePath=C:\\PATH\\TO\\USER\\SOUND\\FILE
+
+//Flag for watching the user sound file for changes
+//If audioOnUserJoin is not enabled, then this will do nothing
+watchUserSoundFile=FLAG_CONDITION \ No newline at end of file
diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java
index 0dad6c0..02c3acd 100644
--- a/src/main/java/soundchan/BotListener/BotListener.java
+++ b/src/main/java/soundchan/BotListener/BotListener.java
@@ -64,15 +64,20 @@ public class BotListener extends ListenerAdapter{
if(audioOnUserJoin) {
String userAudioPath = properties.getProperty("userAudioFilePath");
if(userAudioPath == null || userAudioPath.contentEquals("")) {
- localManager = new LocalAudioManager(localFilePath, "usersound.properties");
+ userAudioPath = "usersound.properties";
}
- else {
- localManager = new LocalAudioManager(localFilePath, userAudioPath);
+ localManager = new LocalAudioManager(localFilePath, userAudioPath);
+
+ if(settingEnableCheck(properties.getProperty("watchUserSoundFile"))) {
+ ExecutorService executorService = Executors.newSingleThreadExecutor();
+ UserSoundWatcher userSoundWatcher = new UserSoundWatcher(localManager, userAudioPath);
+ future = executorService.submit(userSoundWatcher);
+ executorService.shutdown();
}
}
else
localManager = new LocalAudioManager(localFilePath);
-
+
if(settingEnableCheck(properties.getProperty("watchLocalFilePath"))) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
DirectoryWatcher directoryWatcher = new DirectoryWatcher(localManager, localFilePath);
@@ -379,6 +384,8 @@ public class BotListener extends ListenerAdapter{
* @return True if it matches a value to enable, False otherwise
*/
private static boolean settingEnableCheck(String value) {
+ if(value == null)
+ return false;
value = value.toLowerCase();
if(value.contentEquals("true") || value.contentEquals("1") ||
value.contentEquals("yes") || value.contentEquals("on") ||
diff --git a/src/main/java/soundchan/BotListener/UserSoundWatcher.java b/src/main/java/soundchan/BotListener/UserSoundWatcher.java
new file mode 100644
index 0000000..4c10cef
--- /dev/null
+++ b/src/main/java/soundchan/BotListener/UserSoundWatcher.java
@@ -0,0 +1,69 @@
+package soundchan.BotListener;
+
+import soundchan.LocalAudioManager;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.*;
+
+import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
+
+public class UserSoundWatcher implements Runnable {
+
+ private LocalAudioManager localAudioManager;
+ private String userSoundFile;
+ private Path soundFileDirectory;
+ private WatchService watchService;
+ private WatchKey watchKey;
+
+ @SuppressWarnings("unchecked")
+ static <T> WatchEvent<T> cast(WatchEvent<?> event) {
+ return (WatchEvent<T>) event;
+ }
+
+ public UserSoundWatcher(LocalAudioManager audioManager, String filepath) {
+ this.localAudioManager = audioManager;
+ File soundFile = new File(filepath);
+ this.userSoundFile = soundFile.getName();
+ try {
+ this.soundFileDirectory = soundFile.getCanonicalFile().getParentFile().toPath();
+ } catch(IOException e) {
+ System.out.println("Error getting parent path of " + userSoundFile);
+ }
+ try {
+ this.watchService = FileSystems.getDefault().newWatchService();
+ this.watchKey = soundFileDirectory.register(watchService, ENTRY_MODIFY);
+ } catch(IOException e) {
+ System.out.println(e.getMessage());
+ System.out.println("Error setting up watcher for " + filepath);
+ }
+ }
+
+ /**
+ * Called by an executor, checks for changes of the userSoundFile
+ */
+ public void run() {
+ try {
+ while(true) {
+ WatchKey key = watchService.take();
+ if(this.watchKey != key) {
+ System.out.println("Error with WatchKey");
+ continue;
+ }
+
+ for(WatchEvent<?> event : key.pollEvents()) {
+ WatchEvent<Path> pathEvent = cast(event);
+ if(pathEvent.context().endsWith(userSoundFile)) {
+ localAudioManager.UpdateUserAudio();
+ }
+ }
+
+ if(!key.reset()) {
+ break;
+ }
+ }
+ } catch(InterruptedException e) {
+ return;
+ }
+ }
+}
diff --git a/src/main/java/soundchan/LocalAudioManager.java b/src/main/java/soundchan/LocalAudioManager.java
index 036b38d..b125e73 100644
--- a/src/main/java/soundchan/LocalAudioManager.java
+++ b/src/main/java/soundchan/LocalAudioManager.java
@@ -17,9 +17,11 @@ public class LocalAudioManager {
public Map<String, String> filenameDict;
public Map<String, String> usernameDict;
private String filepath;
+ private String userSoundFilepath;
public LocalAudioManager(String filepath_in){
filepath = filepath_in;
+ userSoundFilepath = null;
filenameDict = PopulateFiles();
}
@@ -30,9 +32,9 @@ public class LocalAudioManager {
*/
public LocalAudioManager(String filepath_in, String userSoundFile) {
filepath = filepath_in;
+ userSoundFilepath = userSoundFile;
filenameDict = PopulateFiles();
- usernameDict = new HashMap<>();
- MapUserAudio(userSoundFile);
+ usernameDict = MapUserAudio();
}
/**
@@ -93,6 +95,15 @@ public class LocalAudioManager {
}
/**
+ * Updates the map of usernames to sound files
+ */
+ public void UpdateUserAudio() {
+ if(userSoundFilepath != null | userSoundFilepath.contentEquals("")) {
+ usernameDict = MapUserAudio();
+ }
+ }
+
+ /**
* Creates a map of the sounds in the sound directory
* @return A map with the filename (without extension) is the key for the filename (with extension)
*/
@@ -114,16 +125,19 @@ public class LocalAudioManager {
/**
* Reads in users and their respective sounds from file, then builds a map of users to the filenames. This assumes
* filenames for the sounds are valid, but doesn't check for them.
- * @param userSoundFile The file (with path if required) with listing of users and the sounds to play when they join
+ * @return A map with the usernames as the keys for the filename of the sound
*/
- private void MapUserAudio(String userSoundFile) {
- Properties userSoundProp = LoadProperties(userSoundFile);
+ private Map<String, String> MapUserAudio() {
+ Properties userSoundProp = LoadProperties(userSoundFilepath);
Set<String> users = userSoundProp.stringPropertyNames();
+
+ Map<String, String> userDict = new HashMap<>();
+
for(String user : users) {
String soundFile = userSoundProp.getProperty(user);
- usernameDict.put(user, soundFile);
+ userDict.put(user, soundFile);
}
-
+ return userDict;
}
/**