Fix for HotDropWorldSVC.java not working

This commit is contained in:
RePixelatedMC
2023-11-17 12:17:46 +01:00
parent 15a90edb03
commit 74e87a7fae
@@ -5,6 +5,10 @@ import static java.nio.file.StandardWatchEventKinds.*;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
@@ -16,20 +20,21 @@ import com.volmit.iris.util.scheduling.Looper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class HotDropWorldSVC extends Looper implements IrisService { public class HotDropWorldSVC implements IrisService {
private WatchService watchService; private WatchService watchService;
private JavaPlugin plugin; private JavaPlugin plugin;
public Looper ticker;
@Override @Override
public void onEnable() { public void onEnable() {
this.plugin = Iris.instance; // Assuming Iris.instance is your plugin instance Iris.info("hotDropSVC");
this.plugin = Iris.instance;
initializeWatchService(); initializeWatchService();
} }
@Override @Override
public void onDisable() { public void onDisable() {
ticker.interrupt();
} }
private void initializeWatchService() { private void initializeWatchService() {
@@ -37,47 +42,54 @@ public class HotDropWorldSVC extends Looper implements IrisService {
this.watchService = FileSystems.getDefault().newWatchService(); this.watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get(Bukkit.getWorldContainer().getAbsolutePath()); Path path = Paths.get(Bukkit.getWorldContainer().getAbsolutePath());
path.register(watchService, ENTRY_CREATE); path.register(watchService, ENTRY_CREATE);
this.startLoop();
ticker.start();
} catch (Exception e) { } catch (Exception e) {
Iris.reportError(e); Iris.reportError(e);
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override public void startLoop() {
protected long loop() { final JavaPlugin finalPlugin = this.plugin;
WatchKey key; ticker = new Looper() {
try { @Override
key = watchService.poll(); protected long loop() {
if (key != null) { WatchKey key;
for (WatchEvent<?> event : key.pollEvents()) { try {
WatchEvent.Kind<?> kind = event.kind(); key = watchService.poll();
if (key != null) {
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == ENTRY_CREATE) { if (kind == ENTRY_CREATE) {
WatchEvent<Path> ev = (WatchEvent<Path>) event; WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context(); Path filename = ev.context();
File newDir = new File(Bukkit.getWorldContainer(), filename.toString()); File newDir = new File(Bukkit.getWorldContainer(), filename.toString());
File irisFolder = new File(newDir, "iris"); File irisFolder = new File(newDir, "iris");
if (irisFolder.exists() && irisFolder.isDirectory()) { if (irisFolder.exists() && irisFolder.isDirectory()) {
Iris.info("World HotDrop Detected!"); Iris.info("World HotDrop Detected!");
String worldName = newDir.getName(); String worldName = newDir.getName();
String version = getVersionFromIrisFolder(irisFolder); String version = getVersionFromIrisFolder(irisFolder);
if (Bukkit.getWorld(worldName) == null && isPackValid(worldName, version)) { if (Bukkit.getWorld(worldName) == null && isPackValid(worldName, version)) {
Bukkit.getScheduler().runTask(this.plugin, () -> WorldHandlerSFG.LoadWorld(worldName)); Bukkit.getScheduler().runTask(finalPlugin, () -> WorldHandlerSFG.LoadWorld(worldName));
}
}
} }
} }
key.reset();
} }
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
return -1;
} }
key.reset();
}
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
return -1;
}
return 1000; return 1000;
}
};
} }
private String getVersionFromIrisFolder(File irisFolder) { private String getVersionFromIrisFolder(File irisFolder) {