[Request] Reading from flatfiles programming assistance

Discussion in 'Plugin Development' started by eagledude4, Jan 29, 2011.

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

    eagledude4

    I'm new to the minecraft plugin development scene and reading and writing to a flatfile seems kind of difficult for a noob. I seen a tutorial but it was dedicated to another script with a different scheme that I have, so I couldn't get the most out of it.

    The script's purpose is to spawn players at group specific spawn points set in the config file, using the Permissions plugin.

    I have the outline of the script finished, with most methods complete, I just need some assistance with the config file reading.

    I would really appreciate assistance via teamviewer, but if not that's fine as well.

    Class files:
    GroupSpawns.java
    Code:
    package com.bukkit.HOMEPC_Jordan.GroupSpawns;
    
    import java.io.*;
    import java.util.Hashtable;
    import java.util.logging.*;
    import java.sql.*;
    
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    public class GroupSpawns extends JavaPlugin {
        private final GSPlayerListener playerListener = new GSPlayerListener(this);
        public static Logger log;
        public static Permissions Permissions = null;
        static String maindirectory = "GroupSpawns/";
        static File Config= new File(maindirectory + "config");
    
        public GroupSpawns(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
            super(pluginLoader, instance, desc, folder, plugin, cLoader);
        }
    
        public void onEnable() {
        new File(maindirectory).mkdirs();
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.High, this);
            pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
            log.info( "[GroupSpawns] loaded" );
            if (!Config.exists())
            try {
                Config.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        LoadSettings.loadMain();
        }
    
        public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
            log.info( "[GroupSpawns] version [" + pdfFile.getVersion() + "] unloaded" );
        }
    }

    GSPlayerListener.java
    Code:
    package com.bukkit.HOMEPC_Jordan.GroupSpawns;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    public class GSPlayerListener extends PlayerListener {
        private final GroupSpawns plugin;
    
        public GSPlayerListener(GroupSpawns instance) {
            plugin = instance;
        }
    
        public void onPlayerCommand(PlayerChatEvent e) {
        String[] cmd = e.getMessage().split(" ");
        double X = e.getPlayer().getLocation().getX();
        double Y = e.getPlayer().getLocation().getX();
        double Z = e.getPlayer().getLocation().getZ();
        double Pitch = e.getPlayer().getLocation().getPitch();
        double Yaw = e.getPlayer().getLocation().getYaw();
            if(cmd[0].equalsIgnoreCase("/getloc")) {
                e.getPlayer().sendMessage("Your Location is "+X+": "+Y+": "+Z+"");
            } else if(cmd[0].equalsIgnoreCase("/pitchyaw")) {
                e.getPlayer().sendMessage("Your Pitch is "+Pitch+"");
                e.getPlayer().sendMessage("Your Yaw is "+Yaw+"");
            }
        }
    
        public void onPlayerJoin(PlayerEvent e) {
        Location Spawn = Default line of config file;
        Location ModSpawn = Moderator line of config file;
        Location AdminSpawn = Admin line of config file;
        Player player = e.getPlayer();
            if(Permissions.Security.getGroup(e.getPlayer().getName()).equalsIgnoreCase("Default")) {
                Player.teleportTo(Spawn);
            } else if(Permissions.Security.getGroup(e.getPlayer().getName()).equalsIgnoreCase("Moderators")) {
                Player.teleportTo(ModSpawn);
            } else if(Permissions.Security.getGroup(e.getPlayer().getName()).equalsIgnoreCase("Admins")) {
                Player.teleportTo(AdminSpawn);
            }
        }
    }

    LoadSettings.java
    Code:
    package com.bukkit.HOMEPC_Jordan.GroupSpawns;
    public class LoadSettings {
        static int startingBalance;
        static String credit;
        public static void loadMain() {
        String propertiesFile = GroupSpawns.maindirectory  + "MainConfig.properties";
            PluginProperties properties = new PluginProperties(propertiesFile);
            properties.load();
        }
    }
    

    PluginProperties.java
    Code:
    package com.bukkit.HOMEPC_Jordan.GroupSpawns;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class PluginProperties extends Properties {
        static final long serialVersionUID = 0L;
        static final Logger log = Logger.getLogger("minecraft");
        private String fileName;
    
        public PluginProperties(String file) {
            this.fileName = file;
        }
    
        public void load() {
            File file = new File(this.fileName);
            if (file.exists()) {
                try {
                    load(new FileInputStream(this.fileName));
                } catch (IOException ex) {
                    log.log(Level.SEVERE, "Unable to load " + this.fileName, ex);
                }
            }
        }
    
        public void save(String start) {
            try {
                store(new FileOutputStream(this.fileName), start);
            } catch (IOException ex) {
                log.log(Level.SEVERE, "Unable to save " + this.fileName, ex);
            }
        }
    
        public int getInteger(String key, int value) {
            if (containsKey(key)) {
                return Integer.parseInt(getProperty(key));
            }
    
            put(key, String.valueOf(value));
            return value;
        }
    
        public double getDouble(String key, double value) {
            if (containsKey(key)) {
                return Double.parseDouble(getProperty(key));
            }
    
            put(key, String.valueOf(value));
            return value;
        }
    
        public String getString(String key, String value) {
            if (containsKey(key)) {
                return getProperty(key);
            }
    
            put(key, value);
            return value;
        }
    
        public boolean getBoolean(String key, boolean value) {
            if (containsKey(key)) {
                String boolString = getProperty(key);
                return (boolString.length() > 0)
                        && (boolString.toLowerCase().charAt(0) == 't');
            }
            put(key, value ? "true" : "false");
            return value;
        }
    
    }
     
  2. Offline

    TheBeefiest

  3. Offline

    eagledude4

    I dont need a new class, I need my already configured class to be integrated with the rest of my script.Thanks though, iProperties was definitly written better than what I got, but I believe what I have works.
     
  4. Offline

    fullwall

    I'm not sure of your exact needs, but I'll give you some examples on loading/storing from properties.

    Code:
    //loading
    
    Properties propsinput = new Properties();
    try
    {
    propsinput.load(new FileInputStream("directory/to/something.properties"));
        if (propsinput.containsKey("your-key"))
            variableHere = Integer.parseInteger(propsinput.getProperty("your-key"));
    // to store other types of values, just use Valuetype.parseValuetype
    //storing
    
    Properties propsoutput =new Properties();
    propsoutput.setProperty("your-key-name","your-value" );
    try
    {
    propsoutput.store(new FileOutputStream("your_directory_string_or_variable_here"),null);
    }
    
    catch (IOException ioe)
    {
    
    log.info( [Plugin name]: Can't create or find directory-name for writing.");
    }
    
     
  5. Offline

    eagledude4

    Thanks for the help, but can I make a key for co-ordinates, yaw and pitch? for example, x,y,z,yaw,pitch as a single key, and assign it as Location location? If so, would it be too much to ask for an example so I can understand a little better? And since I'm going to need 3 seperate similar keys in format, how would the script be able to tell the difference?
     
  6. Offline

    eisental

    You would need to add the parsing by yourself. For ex. to save a block coordinates you need to save it as string
    Code:
    Properties.setProperty(propName, Block.getX() + "," + Block.getY() + "," + Block.getZ());
    
    And then parse it like so:
    [CODE]
    String sprop = Properties.get(propKey);
    String[] split = sprop.split(",");
    x = Integer.decode(split[0]);
    y = Integer.decode(split[1]);
    z = .......
    
    Of course you can use any delimiter you want.
    I think though that this would work much better with Yaml. You should google it. It can automatically parse lists and maps into whatever datatype you need. And its already included in Bukkit...
     
  7. Offline

    eagledude4

    I'm not sure how or where to implement that. Can you show me where to add the parsing? I really have no idea how to setup a properties loading environment. (The classes, and the methods) I have iProperty now, but I think I need another class to load iProperty no?

    YAML Looks like something that I would like to get into. Although I have next to no idea of how to use it, or where to get started.
     
Thread Status:
Not open for further replies.

Share This Page