Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Async Autosaving #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class AlphaChestPlugin extends JavaPlugin {

private Logger logger;
public static boolean useAsync;

private VirtualChestManager chestManager;

Expand All @@ -26,7 +27,7 @@ public void onEnable() {

File chestFolder = new File(getDataFolder(), "chests");
chestManager = new VirtualChestManager(chestFolder, logger);

useAsync = getConfig().getBoolean("useAsync");
teller = new Teller(this);

// Set the plugin's command executors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ public static void saveToYaml(Inventory inventory, File file) throws IOException

yaml.save(file);
}

}
104 changes: 77 additions & 27 deletions src/main/java/net/sradonia/bukkit/alphachest/VirtualChestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -81,36 +82,85 @@ public boolean accept(File dir, String name) {
* @return the number of successfully written chests
*/
public int save() {
int savedChests = 0;

dataFolder.mkdirs();

Iterator<Entry<UUID, Inventory>> chestIterator = chests.entrySet().iterator();

while (chestIterator.hasNext()) {
final Entry<UUID, Inventory> entry = chestIterator.next();
final UUID playerUUID = entry.getKey();
final Inventory chest = entry.getValue();

final File chestFile = new File(dataFolder, playerUUID.toString() + YAML_CHEST_EXTENSION);

if (chest == null) {
// Chest got removed, so we have to delete the file.
chestFile.delete();
chestIterator.remove();
} else {
try {
// Write the chest file in YAML format
InventoryIO.saveToYaml(chest, chestFile);
final Set<Entry<UUID, Inventory>> chestIterator = Collections.synchronizedSet(chests.entrySet());

if (AlphaChestPlugin.useAsync){
//AsyncSaver saver = new AsyncSaver(dataFolder, chestIterator);
//new Thread(saver).start();

//return saver.getSavedChests();

ExecutorService pool = Executors.newFixedThreadPool(2); // creates a pool of threads for the Future to draw from

Future<Integer> value = pool.submit(new Callable<Integer>() {
@Override
public Integer call() {
int savedChests = 0;
Iterator<Entry<UUID,Inventory>> iterator = chestIterator.iterator();
while (iterator.hasNext()) {
final Map.Entry<UUID, Inventory> entry = iterator.next();
final UUID playerUUID = entry.getKey();
final Inventory chest = entry.getValue();

final File chestFile = new File(dataFolder, playerUUID.toString() + ".chest.yml");

if (chest == null) {
// Chest got removed, so we have to delete the file.
chestFile.delete();
iterator.remove();
} else {
try {
// Write the chest file in YAML format
InventoryIO.saveToYaml(chest, chestFile);

savedChests++;
} catch (IOException e) {
System.out.println("Couldn't save chest file: " + chestFile.getName());
}
}
}
return savedChests;
}
});

savedChests++;
} catch (IOException e) {
logger.log(Level.WARNING, "Couldn't save chest file: " + chestFile.getName(), e);
try{
return value.get();
}catch(Exception e){
Bukkit.getLogger().severe("Could not save chests!");
e.printStackTrace();
}
return 0;
}
else{
int savedChests = 0;
Iterator<Entry<UUID,Inventory>> iterator = chestIterator.iterator();
while (iterator.hasNext()) {
final Map.Entry<UUID, Inventory> entry = iterator.next();
final UUID playerUUID = entry.getKey();
final Inventory chest = entry.getValue();

final File chestFile = new File(dataFolder, playerUUID.toString() + ".chest.yml");

if (chest == null) {
// Chest got removed, so we have to delete the file.
chestFile.delete();
iterator.remove();
} else {
try {
// Write the chest file in YAML format
InventoryIO.saveToYaml(chest, chestFile);

savedChests++;
} catch (IOException e) {
System.out.println("Couldn't save chest file: " + chestFile.getName());
}
}
}
return savedChests;
}

return savedChests;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ clearOnDeath: false

# Set this to true to drop the contents of the player's chest on death if he does not have the "alphachest.keepOnDeath" permission.
dropOnDeath: false

useAsync: true