Making, Saving, Loading and Reading

Discussion in 'Plugin Development' started by Joshua Neicho, Mar 11, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Joshua Neicho

    Hi,
    I had a thread for this but I thought it would be better if I just made a new one rather then bumping the like 2-3 month old one.
    so I use this code

    Application.java
    Code:
    package com.JWNJWN.Application;
    
    import java.io.File;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    /**
    * Plugin for Bukkit
    *
    * @author JWNJWN
    */
    public class Application extends JavaPlugin {
        public static Property colors = null;
        public String pName = null;
        private Property config = null; // need at least one config option for non-OP use
    
        public void onDisable() {
            // TODO: Place any custom disable code here
    
            // NOTE: All registered events are automatically unregistered when a plugin is disabled
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            System.out.println("Application & Teams Is Disabled!");
        }
    
        public void onEnable() {
            System.out.println("Application & Teams Is Disabled!");
    
            PluginDescriptionFile pdf = this.getDescription();
            pName = pdf.getName();
    
            if (!this.getDataFolder().isDirectory()) {
                this.getDataFolder().mkdir();
            }
    
            }{
            if (config == null) {
                //Does the config exist, if not then make a new blank one
                if (!(new File(this.getDataFolder()+"Application.txt").exists())) {
                    config = new Property(this.getDataFolder()+"Application.txt", this);
                    config.setBoolean("OP", false); //OP only by choice
                } else {
                    config = new Property(this.getDataFolder()+"Application.txt", this);
                }
            }
            }
    }
    Property.java
    Code:
    package com.JWNJWN.Application;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public final class Property {
        private static final Logger log = Logger.getLogger("Minecraft");
        protected Application plugin;
        private Properties properties;
        private String fileName;
    
        public Property(String fileName, Application plugin) {
            this.plugin = plugin;
            this.fileName = fileName;
            this.properties = new Properties();
            File file = new File(fileName);
    
            if (file.exists()) {
                load();
            } else {
                save();
            }
        }
    
        public void load() {
            try {
                FileInputStream inFile = new FileInputStream(this.fileName);
                this.properties.load(inFile);
                inFile.close();
            } catch (IOException ex) {
                log.log(Level.SEVERE, "["+plugin.pName+"]: Unable to load "+this.fileName, ex);
            }
        }
    
        public void save() {
            try {
                FileOutputStream outFile = new FileOutputStream(this.fileName);
                this.properties.store(outFile, "Minecraft Properties File");
                outFile.close();
            } catch (IOException ex) {
                log.log(Level.SEVERE, "["+plugin.pName+"]: Unable to save "+this.fileName, ex);
            }
        }
    
        public Map<String, String> returnMap() throws Exception {
            Map<String, String> map = new HashMap<String, String>();
            BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                if ((line.trim().length() == 0) ||
                        (line.charAt(0) == '#')) {
                    continue;
                }
                int delimPosition = line.indexOf('=');
                String key = line.substring(0, delimPosition).trim();
                String value = line.substring(delimPosition + 1).trim();
                map.put(key, value);
            }
            reader.close();
            return map;
        }
    
        public boolean keyExists(String key) {
            return this.properties.containsKey(key);
        }
    
        public void remove(String key) {
            this.properties.remove(key);
            save();
        }
    
        // STRING
        public String getString(String key) {
            if (this.properties.containsKey(key)) {
                return this.properties.getProperty(key);
            }
            return "";
        }
        public void setString(String key, String value) {
            this.properties.setProperty(key, value);
            save();
        }
    
        // INT
        public int getInt(String key) {
            if (this.properties.containsKey(key)) {
                return Integer.parseInt(this.properties.getProperty(key));
            }
            return 0;
        }
        public void setInt(String key, int value) {
            this.properties.setProperty(key, String.valueOf(value));
            save();
        }
    
        // DOUBLE
        public double getDouble(String key) {
            if (this.properties.containsKey(key)) {
                return Double.parseDouble(this.properties.getProperty(key));
            }
            return 0.0D;
        }
        public void setDouble(String key, double value) {
            this.properties.setProperty(key, String.valueOf(value));
            save();
        }
    
        // LONG
        public long getLong(String key) {
            if (this.properties.containsKey(key)) {
                return Long.parseLong(this.properties.getProperty(key));
            }
            return 0L;
        }
        public void setLong(String key, long value) {
            this.properties.setProperty(key, String.valueOf(value));
            save();
        }
    
        // FLOAT
        public float getFloat(String key) {
            if (this.properties.containsKey(key)) {
                return Float.parseFloat(this.properties.getProperty(key));
            }
            return 0F;
        }
        public void setFloat(String key, float value) {
            this.properties.setProperty(key, String.valueOf(value));
            save();
        }
    
        // BOOLEAN
        public boolean getBoolean(String key) {
            if (this.properties.containsKey(key)) {
                return Boolean.parseBoolean(this.properties.getProperty(key));
            }
            return false;
        }
        public void setBoolean(String key, boolean value) {
            this.properties.setProperty(key, String.valueOf(value));
            save();
        }
    }
    it is supposed to make a Folder and a file called Application.java but only makes the folder.​
    please help, thanks​
     
  2. Offline

    nickguletskii

    Hmm, I don't think FileOutputStream automatically creates a file. Try adding a check if the file exists. If it doesn't, create a new file.
     
  3. Offline

    Joshua Neicho

    how would i make it add the file
    (I'm a complete bukkit noob)
     
  4. Offline

    nickguletskii

    Code:
    File f=new File("pathtofile");
    if(!f.exists()){
       f.createNewFile();
    }
    It throws an IO exception by the way.
     
  5. Offline

    Joshua Neicho

    would I put the folder where it needs to go or the file name in the folder where it needs to go
     
  6. Offline

    nickguletskii

    You would put a path to the target file...
     
  7. Offline

    Joshua Neicho

    doesn't you code say

    if f exists create a new file aswell
     
  8. Offline

    nickguletskii

    if not exists create new file

    ! = not operator (inverter)
     
  9. Offline

    Joshua Neicho

    k so i would put this in for the path?
    C:\Users\Joshua.Scrapbucket\Desktop\Server\plugins\Application\Application.txt
     
  10. Offline

    nickguletskii

    No, you put the relative path.
     
  11. Offline

    Joshua Neicho

    so what would that be?
     
  12. Offline

    nickguletskii

    \plugins\Application\Application.txt
     
  13. Offline

    Joshua Neicho

    I am not meant to have the speech marks am i?
     
  14. Offline

    nickguletskii

    You are... Are you even familiar with language basics?
     
  15. Offline

    Joshua Neicho

    sorry it was just giving me a error and it turned out the \ was meant to be /
    [MERGETIME="1299935645"][/MERGETIME]
    this still doesn't work it only creates the folder and not the file
    [MERGETIME="1299937532"][/MERGETIME]
    OK I got it to load a but i need help with it reading then printing
    [MERGETIME="1299967768"][/MERGETIME]
    Please i also need to know how it would print it and then choosing a colour for each word in the .txt file
    so like you would type Hello People Please do &4Blah Blah Blah and it would show the first Blah in the colour &4 which i think is dark red
     
  16. Offline

    Hohahihehu

    Look up Samkio's Intermediate Bukkit Tutorial, it should have what you need (i think?)
     
  17. Offline

    Joshua Neicho

    k that helped but it didnt tell me how i could choose the colours of words in my .txt file or now .properties file
     
Thread Status:
Not open for further replies.

Share This Page