Files not being created

Discussion in 'Plugin Development' started by xMakerx, Jun 27, 2013.

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

    xMakerx

    I'm trying to make a plugin that makes a file with a world name and .yml after it, but the file is never being created, here's the code:

    Code:
    public class WorldSpawn extends JavaPlugin implements Listener {
       
        WorldSpawn plugin;
        public static String pluginPrefix = (ChatColor.YELLOW + "[" + ChatColor.LIGHT_PURPLE + "WorldSpawn" + ChatColor.YELLOW + "] " + ChatColor.AQUA);
       
        public void onEnable() {
            if(!this.getDataFolder().exists()) { this.getDataFolder().mkdirs(); }
            getServer().getPluginManager().registerEvents(new Commands(this), this);
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("setspawn")) {
                    Player p = (Player)sender;
                    String worldName = p.getWorld().getName();
                    File[] dirlist = getDataFolder().listFiles();
                      for(int i = 0; i < dirlist.length; i++) {
                        if(dirlist[i].isFile()) {
                        if(!dirlist[i].getName().matches(worldName + ".yml")) {
                          File world;
                          world = new File(plugin.getDataFolder(), (worldName + ".yml"));
                          if(!world.exists()) {
                             
                              double X = p.getLocation().getX();
                              double Y = p.getLocation().getY();
                              double Z = p.getLocation().getZ();
                              double YAW = p.getLocation().getYaw();
                              double PITCH = p.getLocation().getPitch();
                             
                              try {
                                  world.createNewFile();
                                  FileWriter fw = new FileWriter(world, true);
                                  fw.write("# - " + worldName + " Spawn -\n");
                                  fw.write("World: " + worldName + "/n");
                                  fw.write("X: " + X + "/n");
                                  fw.write("Y: " + Y + "/n");
                                  fw.write("Z: " + Z + "/n");
                                  fw.write("YAW: " + YAW + "/n");
                                  fw.write("PITCH " + PITCH + "/n");
                                  fw.close();
                                 
                                  p.sendMessage(pluginPrefix + ChatColor.GREEN + "Spawn has been set for " + ChatColor.AQUA + worldName);
                                 
                                  return true;
                              }catch(IOException z) {
                                    z.getStackTrace();
                                }
                          }
                          }else {
                              p.sendMessage(pluginPrefix + ChatColor.RED + "Error: " + ChatColor.DARK_RED + "You cannot overwrite a previous set spawn, use /delspawn instead.");
                              return true;
                          }
                        }
                      }
                    return false;
        }
    }
    Could it be that it's compiled under Java 7 not 6? Let me know what to do as soon as possible.
     
  2. Offline

    xMakerx

  3. Offline

    MP5K

    Hello xMakerx,
    try make sure that the DataFolder exists, and try print out the boolean given by "File::createNewFile()";
     
  4. Offline

    xMakerx


    MP5K Okay, I set it up so it outputs if the data folder exists and it gave me true, what should I try next?


    MP5K
    I got the problem fixed by adding a test file called "test.yml" but for some reason if I call plugin.getDataFolder() it returns null, how could that be?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  5. Offline

    MP5K

    mm.. that is weired. Do you have permission to write/read the folder?
     
Thread Status:
Not open for further replies.

Share This Page