Fixes watchplugin not reloading when the file is instantly written

This commit is contained in:
Frank van der Heijden 2021-03-25 16:45:42 +01:00
parent b51ff445dd
commit 0e1bee0ca1
No known key found for this signature in database
GPG key ID: 26DA56488D314D11

View file

@ -41,6 +41,7 @@ public class PluginWatcherTask extends AbstractTask {
private final AtomicBoolean run;
private File file;
private String hash;
private long hashTimestamp = 0;
private WatchService watchService;
private Object task = null;
@ -69,14 +70,18 @@ public class PluginWatcherTask extends AbstractTask {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
if (file.getName().equals(event.context().toString())) {
String previousHash = hash;
hash = FileUtils.getHash(file);
if (task != null) {
//noinspection unchecked
taskManager.cancelTask(task);
}
String previousHash = hash;
long previousHashTimestamp = hashTimestamp;
hash = FileUtils.getHash(file);
hashTimestamp = System.currentTimeMillis();
task = ServerUtilsApp.getPlugin().getTaskManager().runTaskLater(() -> {
if (hash.equals(previousHash)) {
if (hash.equals(previousHash) || previousHashTimestamp < hashTimestamp - 1000L) {
send(WatchResult.CHANGE);
pluginManager.reloadPlugin(pluginName);