2727
2828import java .io .Closeable ;
2929import java .io .IOException ;
30- import java .nio .file .ClosedWatchServiceException ;
31- import java .nio .file .Files ;
32- import java .nio .file .Path ;
33- import java .nio .file .StandardWatchEventKinds ;
34- import java .nio .file .WatchEvent ;
35- import java .nio .file .WatchKey ;
36- import java .nio .file .WatchService ;
37- import java .util .Optional ;
30+ import java .nio .file .*;
3831import java .util .function .Consumer ;
3932
4033/**
@@ -98,13 +91,14 @@ private RecursiveDirectoryWatcher(Path root, WatchService watchService) {
9891 * @return a new instance that will monitor the given root folder
9992 * @throws IOException If creating the watcher failed, e.g. due to root not existing.
10093 */
101- public static Optional <RecursiveDirectoryWatcher > create (Path root ) throws IOException {
94+ public static RecursiveDirectoryWatcher create (Path root ) throws IOException {
95+ WatchService watchService ;
10296 try {
103- WatchService watchService = root .getFileSystem ().newWatchService ();
104- return Optional .of (new RecursiveDirectoryWatcher (root , watchService ));
105- } catch (UnsupportedOperationException ignored ) {
106- return Optional .empty ();
97+ watchService = root .getFileSystem ().newWatchService ();
98+ } catch (UnsupportedOperationException e ) {
99+ throw new IllegalArgumentException ("Root must support file watching" , e );
107100 }
101+ return new RecursiveDirectoryWatcher (root , watchService );
108102 }
109103
110104 private void registerFolderWatcher (Path root ) throws IOException {
@@ -116,12 +110,14 @@ private void registerFolderWatcher(Path root) throws IOException {
116110 private void triggerInitialEvents (Path root ) throws IOException , FilenameException {
117111 Path schematicRoot = WorldEdit .getInstance ().getSchematicsManager ().getRoot ();
118112 eventConsumer .accept (new DirectoryCreatedEvent (root ));
119- for (Path path : Files .newDirectoryStream (root )) {
120- path = WorldEdit .getInstance ().getSafeOpenFile (null , schematicRoot .toFile (), schematicRoot .relativize (path ).toString (), null ).toPath ();
121- if (Files .isDirectory (path )) {
122- triggerInitialEvents (path );
123- } else {
124- eventConsumer .accept (new FileCreatedEvent (path ));
113+ try (DirectoryStream <Path > directoryStream = Files .newDirectoryStream (root )) {
114+ for (Path path : directoryStream ) {
115+ path = WorldEdit .getInstance ().getSafeOpenFile (null , schematicRoot .toFile (), schematicRoot .relativize (path ).toString (), null ).toPath ();
116+ if (Files .isDirectory (path )) {
117+ triggerInitialEvents (path );
118+ } else {
119+ eventConsumer .accept (new FileCreatedEvent (path ));
120+ }
125121 }
126122 }
127123 }
@@ -136,6 +132,12 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {
136132 Path schematicRoot = WorldEdit .getInstance ().getSchematicsManager ().getRoot ();
137133
138134 this .eventConsumer = eventConsumer ;
135+
136+ if (watchThread != null ) {
137+ // We have an existing thread, interrupt it first to trigger a clean shutdown
138+ watchThread .interrupt ();
139+ }
140+
139141 watchThread = new Thread (() -> {
140142 LOGGER .debug ("RecursiveDirectoryWatcher::EventConsumer started" );
141143
@@ -148,10 +150,12 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {
148150
149151 try {
150152 WatchKey watchKey ;
151- while (true ) {
153+ while (! Thread . interrupted () ) {
152154 try {
153155 watchKey = watchService .take ();
154- } catch (InterruptedException e ) { break ; }
156+ } catch (InterruptedException e ) {
157+ break ;
158+ }
155159
156160 for (WatchEvent <?> event : watchKey .pollEvents ()) {
157161 WatchEvent .Kind <?> kind = event .kind ();
@@ -205,7 +209,7 @@ public void start(Consumer<DirEntryChangeEvent> eventConsumer) {
205209 }
206210 LOGGER .debug ("RecursiveDirectoryWatcher::EventConsumer exited" );
207211 });
208- watchThread .setName ("RecursiveDirectoryWatcher" );
212+ watchThread .setName ("WorldEdit- RecursiveDirectoryWatcher" );
209213 watchThread .start ();
210214 }
211215
0 commit comments