Fixes watchplugin not reloading when the file is instantly written
This commit is contained in:
parent
b51ff445dd
commit
0e1bee0ca1
1 changed files with 8 additions and 3 deletions
|
|
@ -41,6 +41,7 @@ public class PluginWatcherTask extends AbstractTask {
|
||||||
private final AtomicBoolean run;
|
private final AtomicBoolean run;
|
||||||
private File file;
|
private File file;
|
||||||
private String hash;
|
private String hash;
|
||||||
|
private long hashTimestamp = 0;
|
||||||
|
|
||||||
private WatchService watchService;
|
private WatchService watchService;
|
||||||
private Object task = null;
|
private Object task = null;
|
||||||
|
|
@ -69,14 +70,18 @@ public class PluginWatcherTask extends AbstractTask {
|
||||||
WatchKey key = watchService.take();
|
WatchKey key = watchService.take();
|
||||||
for (WatchEvent<?> event : key.pollEvents()) {
|
for (WatchEvent<?> event : key.pollEvents()) {
|
||||||
if (file.getName().equals(event.context().toString())) {
|
if (file.getName().equals(event.context().toString())) {
|
||||||
String previousHash = hash;
|
|
||||||
hash = FileUtils.getHash(file);
|
|
||||||
if (task != null) {
|
if (task != null) {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
taskManager.cancelTask(task);
|
taskManager.cancelTask(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String previousHash = hash;
|
||||||
|
long previousHashTimestamp = hashTimestamp;
|
||||||
|
|
||||||
|
hash = FileUtils.getHash(file);
|
||||||
|
hashTimestamp = System.currentTimeMillis();
|
||||||
task = ServerUtilsApp.getPlugin().getTaskManager().runTaskLater(() -> {
|
task = ServerUtilsApp.getPlugin().getTaskManager().runTaskLater(() -> {
|
||||||
if (hash.equals(previousHash)) {
|
if (hash.equals(previousHash) || previousHashTimestamp < hashTimestamp - 1000L) {
|
||||||
send(WatchResult.CHANGE);
|
send(WatchResult.CHANGE);
|
||||||
|
|
||||||
pluginManager.reloadPlugin(pluginName);
|
pluginManager.reloadPlugin(pluginName);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue