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

Screwed up last PR, gg #19

Open
wants to merge 2 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
101 changes: 55 additions & 46 deletions src/main/java/net/sradonia/bukkit/alphachest/AlphaChestPlugin.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
package net.sradonia.bukkit.alphachest;

import java.io.File;
import java.util.logging.Logger;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.bukkit.plugin.java.JavaPlugin;

import net.sradonia.bukkit.alphachest.commands.*;
import net.sradonia.bukkit.alphachest.listeners.PlayerListener;

public class AlphaChestPlugin extends JavaPlugin {

private Logger logger;

private VirtualChestManager chestManager;

public class AlphaChestPlugin extends JavaPlugin
{
private Teller teller;

@Override
public void onEnable() {
// Save a copy of the default config.yml if one doesn't already exist
saveDefaultConfig();

// Initialize some classes and objects
logger = getLogger();

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

teller = new Teller(this);

// Set the plugin's command executors
getCommand("chest").setExecutor(new ChestCommand(chestManager));
getCommand("clearchest").setExecutor(new ClearChestCommand(chestManager));
getCommand("disposal").setExecutor(new DisposalCommand(chestManager));
getCommand("savechests").setExecutor(new SaveChestsCommand(chestManager));
getCommand("workbench").setExecutor(new WorkbenchCommand());

// Register the plugin's events
getServer().getPluginManager().registerEvents(new PlayerListener(this, chestManager), this);

// Schedule an auto-save task
int autosaveInterval = getConfig().getInt("autosave") * 1200;
public void onEnable()
{
saveDefaultConfig();
File chestFolder = new File(getDataFolder(), "chests");
VirtualChestManager.initChestMan(chestFolder);
setTeller(new Teller(this));
getCommand("chest").setExecutor(new ChestCommand());
getCommand("clearchest").setExecutor(new ClearChestCommand());
getCommand("savechests").setExecutor(new SaveChestsCommand());
getCommand("workbench").setExecutor(new WorkbenchCommand());
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
int autosaveInterval = getConfig().getInt("autosave") * 1200;
if (autosaveInterval > 0)
{
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
{
public void run()
{
try
{
int savedChests = CompletableFuture.supplyAsync(() -> VirtualChestManager.saveAll()).get();
if (savedChests > 0 && !getConfig().getBoolean("silentAutosave"))
{
System.out.println("Auto-saved " + savedChests + " chests");
}
}
catch (InterruptedException | ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, autosaveInterval, autosaveInterval);
}
}

if (autosaveInterval > 0) {
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
int savedChests = chestManager.save();
@Override
public void onDisable()
{
System.out.println("[AlphaChest] Saving inventories!");
while (true)
{
VirtualChestManager.saveAll();
break;
}
}

if (savedChests > 0 && !getConfig().getBoolean("silentAutosave")) {
logger.info("Auto-saved " + savedChests + " chests");
}
}
}, autosaveInterval, autosaveInterval);
}
public Teller getTeller()
{
return teller;
}

@Override
public void onDisable() {
int savedChests = chestManager.save();
logger.info("Saved " + savedChests + " chests");
public void setTeller(Teller teller)
{
this.teller = teller;
}
}
60 changes: 60 additions & 0 deletions src/main/java/net/sradonia/bukkit/alphachest/ChestSaveQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.sradonia.bukkit.alphachest;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.bukkit.inventory.Inventory;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Map.Entry;

public class ChestSaveQueue extends BukkitRunnable
{
public File dataFolder;
private static final String ymlEx = ".chest.yml";

public ChestSaveQueue(File dataFolder)
{
this.dataFolder = dataFolder;
}

private static BlockingQueue<Entry<UUID, Inventory>> queue = new ArrayBlockingQueue(500);
int maximumIterations = 50;

public void saveChests()
{
int tracker = 0;
for (Iterator<Entry<UUID, Inventory>> iter = queue.iterator(); iter.hasNext();)
{
if (tracker > 50)
{
break;
}

tracker++;
}
}

public void saveChest(UUID playerUUID, Inventory chest)
{
final File chestFile = new File(dataFolder + "/chests/", playerUUID + ymlEx);
try
{
InventoryIO.saveToYaml(chest, chestFile);
}
catch (IOException e)
{
System.out.println("Couldn't save chest file: " + chestFile.getName());
}
}

@Override
public void run()
{

}
}
153 changes: 86 additions & 67 deletions src/main/java/net/sradonia/bukkit/alphachest/InventoryIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,103 +15,122 @@
/**
* Utility class to store inventories to file and read them back again.
*/
public class InventoryIO {
public class InventoryIO
{

/**
* Loads an inventory from a plain-text file.
*
* @param file the text file to load
* @param file
* the text file to load
* @return the loaded inventory
* @throws IOException if the file could not be read
* @throws IOException
* if the file could not be read
* @deprecated use {@link #loadFromYaml} instead
*/
@Deprecated
public static Inventory loadFromTextFile(File file) throws IOException {
final Inventory inventory = Bukkit.getServer().createInventory(null, 6 * 9);

final BufferedReader in = new BufferedReader(new FileReader(file));

String line;
int slot = 0;

while ((line = in.readLine()) != null) {
if (!line.equals("")) {
final String[] parts = line.split(":");

try {
int type = Integer.parseInt(parts[0]);
int amount = Integer.parseInt(parts[1]);
short damage = Short.parseShort(parts[2]);

if (type != 0) {
inventory.setItem(slot, new ItemStack(type, amount, damage));
}
} catch (NumberFormatException e) {
// ignore
}

++slot;
}
}

in.close();

return inventory;
public static Inventory loadFromTextFile(File file) throws IOException
{
final Inventory inventory = Bukkit.getServer().createInventory(null, 6 * 9);
final BufferedReader in = new BufferedReader(new FileReader(file));
String line;
int slot = 0;

while ((line = in.readLine()) != null)
{
if (!line.equals(""))
{
final String[] parts = line.split(":");

try
{
int type = Integer.parseInt(parts[0]);
int amount = Integer.parseInt(parts[1]);
short damage = Short.parseShort(parts[2]);

if (type != 0)
{
inventory.setItem(slot, new ItemStack(type, amount, damage));
}
}
catch (NumberFormatException e)
{
// ignore
}

++slot;
}
}

in.close();

return inventory;
}

/**
* Loads an inventory from a YAML configuration file.
*
* @param file the YAML file to load
* @param file
* the YAML file to load
* @return the loaded inventory
* @throws IOException if the file could not be read
* @throws InvalidConfigurationException if the file could not be parsed
* @throws IOException
* if the file could not be read
* @throws InvalidConfigurationException
* if the file could not be parsed
*/
public static Inventory loadFromYaml(File file) throws IOException, InvalidConfigurationException {
YamlConfiguration yaml = new YamlConfiguration();
yaml.load(file);

int inventorySize = yaml.getInt("size", 6 * 9);
public static Inventory loadFromYaml(File file) throws IOException, InvalidConfigurationException
{
YamlConfiguration yaml = new YamlConfiguration();
yaml.load(file);

Inventory inventory = Bukkit.getServer().createInventory(null, inventorySize);
int inventorySize = yaml.getInt("size", 6 * 9);

ConfigurationSection items = yaml.getConfigurationSection("items");
Inventory inventory = Bukkit.getServer().createInventory(null, inventorySize);

for (int slot = 0; slot < inventorySize; slot++) {
String slotString = String.valueOf(slot);
ConfigurationSection items = yaml.getConfigurationSection("items");

if (items.isItemStack(slotString)) {
ItemStack itemStack = items.getItemStack(slotString);
inventory.setItem(slot, itemStack);
}
}
for (int slot = 0; slot < inventorySize; slot++)
{
String slotString = String.valueOf(slot);

return inventory;
if (items.isItemStack(slotString))
{
ItemStack itemStack = items.getItemStack(slotString);
inventory.setItem(slot, itemStack);
}
}
return inventory;
}

/**
* Saves an inventory to a YAML configuration file.
*
* @param inventory the inventory to save
* @param file the YAML file to write
* @throws IOException if the file could not be written
* @param inventory
* the inventory to save
* @param file
* the YAML file to write
* @throws IOException
* if the file could not be written
*/
public static void saveToYaml(Inventory inventory, File file) throws IOException {
YamlConfiguration yaml = new YamlConfiguration();
public static void saveToYaml(Inventory inventory, File file) throws IOException
{
YamlConfiguration yaml = new YamlConfiguration();

int inventorySize = inventory.getSize();
yaml.set("size", inventorySize);
int inventorySize = inventory.getSize();
yaml.set("size", inventorySize);

ConfigurationSection items = yaml.createSection("items");
ConfigurationSection items = yaml.createSection("items");

for (int slot = 0; slot < inventorySize; slot++) {
ItemStack stack = inventory.getItem(slot);
for (int slot = 0; slot < inventorySize; slot++)
{
ItemStack stack = inventory.getItem(slot);

if (stack != null) {
items.set(String.valueOf(slot), stack);
}
}
if (stack != null)
{
items.set(String.valueOf(slot), stack);
}
}

yaml.save(file);
yaml.save(file);
}
}
Loading