Skip to content

Instantly share code, notes, and snippets.

@benj02
Created December 26, 2011 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benj02/1520373 to your computer and use it in GitHub Desktop.
Save benj02/1520373 to your computer and use it in GitHub Desktop.
franticupdate
package org.frantictools.franticupdatechooser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class FranticUpdate extends JavaPlugin
{
public static ArrayList<String> inventory;
public static ArrayList<String> schematic;
@Override
public void onEnable()
{
inventory = new ArrayList<String>();
schematic = new ArrayList<String>();
getCommand("schematic").setExecutor(new BaseCommand()
{
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("schematic"))
{
FranticUpdate.schematic.add(sender.getName());
if (FranticUpdate.inventory.contains(sender.getName()))
inventory.remove(sender.getName());
try {
FranticUpdate.updateFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
});
getCommand("inventory").setExecutor(new BaseCommand()
{
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("inventory"))
{
FranticUpdate.inventory.add(sender.getName());
if (FranticUpdate.schematic.contains(sender.getName()))
schematic.remove(sender.getName());
try {
FranticUpdate.updateFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
});
try {
loadFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("Frantic Schematic/Inventory thing enabled");
}
public static void loadFile() throws IOException
{
BufferedReader schematicReader = new BufferedReader(new FileReader(new File("schematic.txt")));
BufferedReader inventoryReader = new BufferedReader(new FileReader(new File("inventory.txt")));
String line;
while ((line = inventoryReader.readLine()) != null)
{
schematic.add(line);
}
while ((line = inventoryReader.readLine()) != null)
{
inventory.add(line);
}
schematicReader.close();
inventoryReader.close();
}
public static void updateFile() throws IOException
{
File newFile = new File("schematic.txt_new");
File oldFile = new File("schematic.txt_old");
File currentFile = new File("schematic.txt");
BufferedWriter schematicWriter = new BufferedWriter(new FileWriter(newFile));
for (String name : schematic)
{
schematicWriter.write(name);
schematicWriter.write('\n');
}
if(oldFile.exists())
{
oldFile.delete();
}
currentFile.renameTo(oldFile);
if(currentFile.exists())
{
currentFile.delete();
}
newFile.renameTo(currentFile);
if(newFile.exists())
{
newFile.delete();
}
File newFile2 = new File("inventory.txt_new");
File oldFile2 = new File("inventory.txt_old");
File currentFile2 = new File("inventory.txt");
BufferedWriter inventoryWriter = new BufferedWriter(new FileWriter(newFile2));
for (String name : inventory)
{
inventoryWriter.write(name);
inventoryWriter.write('\n');
}
if(oldFile2.exists())
{
oldFile2.delete();
}
currentFile2.renameTo(oldFile2);
if(currentFile2.exists())
{
currentFile2.delete();
}
newFile2.renameTo(currentFile2);
if(newFile2.exists())
{
newFile2.delete();
}
schematicWriter.close();
inventoryWriter.close();
}
public Logger log = Logger.getLogger("minecraft");
@Override
public void onDisable()
{
try {
updateFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("Frantic Schematic/Inventory thing disabled and file saved.");
}
private class BaseCommand implements CommandExecutor
{
@Override
public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment