Skip to content

Commit 2d85b7f

Browse files
committed
Add auto-save task to periodically save network data and improve chunk loading checks
1 parent 3eee3cc commit 2d85b7f

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/main/java/com/dermoha/networkstorage/NetworkStoragePlugin.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void onEnable() {
6767

6868
// Schedule repeating tasks
6969
startSenderChestTask();
70+
startAutoSaveTask();
7071

7172
getLogger().info("NetworkStorage Plugin has been enabled!");
7273
}
@@ -133,8 +134,7 @@ private void startSenderChestTask() {
133134
while (iterator.hasNext()) {
134135
Location senderLoc = iterator.next();
135136

136-
// Skip if chunk is not loaded to prevent forced chunk loading
137-
if (!senderLoc.getChunk().isLoaded()) {
137+
if (!senderLoc.getWorld().isChunkLoaded(senderLoc.getBlockX() >> 4, senderLoc.getBlockZ() >> 4)) {
138138
continue;
139139
}
140140

@@ -153,14 +153,23 @@ private void startSenderChestTask() {
153153
}
154154
}
155155
} else {
156-
// If the block is no longer a chest, remove it from the network
157156
iterator.remove();
158-
networkManager.saveNetworks(); // Save changes after pruning
159-
getLogger().info("Pruned non-chest block at " + senderLoc.toString() + " from network.");
157+
getLogger().info("Pruned non-chest block at " + senderLoc.toString() + " from a network because it was no longer a chest.");
160158
}
161159
}
162160
}
163-
}, 0L, interval);
161+
}, 100L, interval);
162+
}
163+
164+
private void startAutoSaveTask() {
165+
int interval = configManager.getAutoSaveInterval() * 60 * 20; // Convert minutes to ticks
166+
if (interval > 0) {
167+
Bukkit.getScheduler().runTaskTimer(this, () -> {
168+
getLogger().info("Auto-saving network data...");
169+
networkManager.saveNetworks();
170+
getLogger().info("Auto-save complete.");
171+
}, interval, interval);
172+
}
164173
}
165174

166175
public static NetworkStoragePlugin getInstance() {

0 commit comments

Comments
 (0)