Still can't write to files.

Discussion in 'Plugin Development' started by Razorcane, Dec 15, 2011.

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

    Razorcane

    I've been working for days trying to figure this out. No matter what I do, no matter what code I write, it simply won't write to the files. I've literally done everything that I can think of, and the files still remain blank after forcing them to be written in. Here is my code:

    Code:
        public void writeGuests(){
            File f = new File("plugins/Origins/groups/guests.txt");
            try {
                FileWriter fw = new FileWriter(f, true);
                BufferedWriter bw = new BufferedWriter(fw);
                f.delete();
                f.createNewFile();
                for(String str : plugin.members){
                    bw.append(str + "\r\n");
                }
                bw.close();
            }
            catch(Exception e){
                plugin.log.info(plugin.name + "Error writing to guests.txt");
            }
        }
    This is but the latest revision among several dozens of other revisions where I tried to get this to work. There doesn't seem to be any errors from the console, so I honestly don't understand why it doesn't write.
     
  2. Offline

    Chiller

    Why don't you use Bukkit's config file system?
     
  3. Offline

    Razorcane

    Because it's derpy. The YML config system frequently derps and deletes everything in the file. Txt files don't do that. I appreciate the thought, but I'm not looking for even worse code.
     
  4. Offline

    Chiller

    Oh well ive never worked with config files... So I cannot help.
     
  5. Offline

    Razorcane

    It's not the config files. I just need to figure out why this isn't writing to files.
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    @Razorcane
    are you on windows or *nix
    file paths use different characters, make sure your using the right one.
    or just append File.seperator

    EDIT:
    and for the record, the yml parser and writer does not understand in line comments, but if comments are at the top of the file, it will respect it.
     
  7. Offline

    halley

    Basic assumptions: is the current working directory where you think it is? Check if the subdirectories you think are there, are really there.

    I have never had the config system delete or empty out stuff. None of my plugins or hundreds of others out there in the marketplace. It's not derpy, there's probably a simple mistake made somewhere.

    While I won't dissuade anyone from using File.separator, the ONLY part of Windows which demands a backslash is the command line interpreter CMD.EXE - all of the Windows file APIs accept the forward slash, just like Linux and Unix and OSX. Try it sometime; this is true in Java or C++ or Perl or Python. If you ever have to support Mac OS9 or VAX VMS, then you need to pay attention.
     
  8. Offline

    Razorcane

    I'm using Windows, but the problem is it doesn't write at all.
    I would just prefer txt files. I don't see why it's a problem that I want to use txt files. And yes, it's in the proper directory. The console doesn't report any exceptions or errors when I call the methods, they just don't write. At first I thought it was because my computer didn't have the proper write permissions, but it does. So I don't really understand why it's doing this.
     
  9. Offline

    aidancbrady

    I am afraid to say I am having the EXACT SAME PROBLEM. EXACT. I set up an onPlayerJoin() method to write out the player name and IP in a text file, and it won't freaking generate!

    Code? Here.
    MAIN class:
    Code:
    package org.orecraft.orecraftcore;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class OreCraftCore extends JavaPlugin {
       
        public OreCraftCore()
        {
            super();
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if (sender instanceof Player) {
                String command = cmd.getName().toLowerCase();
                Player player = (Player)sender;
                if (command.equals("oc") || command.equals("occ") || command.equals("orecraft") || command.equals("orecraftcore")) {
                    String param1 = args.length > 0 ? args[0] : "";
                    String param2 = args.length > 1 ? args[1] : "";
     
                    //Info Command
                   
                    if (param1.equals("info")) {
                        if (player.hasPermission("orecraft.info")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore------------");
                            player.sendMessage(ChatColor.GRAY + "  You are running" + ChatColor.DARK_GRAY + " OreCraftCore" + ChatColor.GRAY + " version" + ChatColor.GOLD + " 1.0.0.");
                            player.sendMessage(ChatColor.GRAY + "  Plugin is currently " + ChatColor.GREEN + "enabled.");
                            player.sendMessage(ChatColor.GRAY + "  Type " + ChatColor.DARK_GRAY + "/orecraft help " + ChatColor.GRAY + "for a list of commands.");
                            player.sendMessage(ChatColor.BLUE + "----------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                   
                    //Credits Command
                   
                    else if (param1.equals("credits")) {
                        if (player.hasPermission("orecraft.credits")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                            player.sendMessage(ChatColor.GRAY + "  Programmed and compiled by " + ChatColor.DARK_GRAY + "Aidan Brady.");
                            player.sendMessage(ChatColor.GRAY + "  Made using " + ChatColor.DARK_GRAY + "Eclipse build 20110916-0149");
                            player.sendMessage(ChatColor.GRAY + "  Referenced with " + ChatColor.DARK_GRAY + "Bukkit 1.2.3-R0.2 API Developer Snapshot");
                            player.sendMessage(ChatColor.BLUE + "--------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                   
                    //Help Command
                   
                    else if (param1.equals("help")) {
                        if (player.hasPermission("orecraft.help")) {
                            if (param2.equals("")) {
                                player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                                player.sendMessage(ChatColor.DARK_GRAY + "  /orecraft help " + ChatColor.GRAY + "- displays help for OreCraftCore.");
                                player.sendMessage(ChatColor.DARK_GRAY + "  /orecraft help <cmd> " + ChatColor.GRAY + "- displays help for a command.");
                                player.sendMessage(ChatColor.DARK_GRAY + "  /orecraft credits " + ChatColor.GRAY + "- displays credits for OreCraftCore.");
                                player.sendMessage(ChatColor.DARK_GRAY + "  /orecraft stats " + ChatColor.GRAY + "- displays stats for OreCraftCore.");
                                player.sendMessage(ChatColor.DARK_GRAY + "  /orecraft info " + ChatColor.GRAY + "- displays info for OreCraftCore.");
                                player.sendMessage(ChatColor.BLUE + "--------------------------------");
                                return true;
                            }
                           
                            else if (param2.equals("help")) {
                                player.sendMessage(ChatColor.DARK_GRAY + "/orecraft help " + ChatColor.GRAY + "- displays help for OreCraftCore.");
                                return true;
                            }
                            else if (param2.equals("stats")) {
                                player.sendMessage(ChatColor.DARK_GRAY + "/orecraft stats " + ChatColor.GRAY + "- displays stats for OreCraftCore.");
                                return true;
                            }
                            else if (param2.equals("info")) {
                                player.sendMessage(ChatColor.DARK_GRAY + "/orecraft info " + ChatColor.GRAY + "- displays info for OreCraftCore.");
                                return true;   
                            }
                            else if (param2.equals("credits")) {
                                player.sendMessage(ChatColor.DARK_GRAY + "/orecraft credits " + ChatColor.GRAY + "- displays credits for OreCraftCore.");
                            return true;
                            }
                            else {
                                player.sendMessage(ChatColor.GRAY + "Command not recognized. Try another.");
                                return true;
                            }
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                   
                    //Stats Command
                   
                    else if (param1.equals("stats")) {
                        if (player.hasPermission("orecraft.stats")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                            player.sendMessage(ChatColor.GRAY + "  Maximum Memory: " + ChatColor.DARK_GRAY + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + ChatColor.GRAY + " MB");
                            player.sendMessage(ChatColor.GRAY + "  Total Memory: " + ChatColor.DARK_GRAY + (Runtime.getRuntime().totalMemory() / 1024 / 1024) + ChatColor.GRAY + " MB");
                            player.sendMessage(ChatColor.GRAY + "  Free Memory: " + ChatColor.DARK_GRAY + (Runtime.getRuntime().freeMemory() / 1024 / 1024) + ChatColor.GRAY + " MB");
                            player.sendMessage(ChatColor.BLUE + "--------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                   
                    //Player Command
                   
                    else if (param1.equals("player")) {
                        if (args.length < 2) {
                            if (player.hasPermission("orecraft.player")) {
                                player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                                player.sendMessage(ChatColor.GRAY + "  Hello, " + ChatColor.DARK_BLUE + player.getDisplayName() + "!");
                                player.sendMessage(ChatColor.GRAY + "  You are playing in gamemode '" + ChatColor.DARK_GRAY + player.getGameMode() + ChatColor.GRAY + ".'");
                                player.sendMessage(ChatColor.GRAY + "  You are playing OreCraft on IP " + ChatColor.DARK_GRAY + player.getAddress() + ChatColor.GRAY + ".");
                                player.sendMessage(ChatColor.GRAY + "  You have " + ChatColor.DARK_GRAY + player.getHealth() + ChatColor.GRAY + " out of " + ChatColor.DARK_GRAY + player.getMaxHealth() + ChatColor.GRAY + " health.");
                                player.sendMessage(ChatColor.BLUE + "--------------------------------");
                                return true;
                            }
                            else {
                                player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                            }
                       
                        }
                        Player otherPlayer = player.getServer().getPlayer(args[1]);
                        if (player.hasPermission("orecraft.player.lookup")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                            player.sendMessage(ChatColor.GRAY + "  " + ChatColor.DARK_BLUE + otherPlayer.getDisplayName() + "'s stats:");
                            player.sendMessage(ChatColor.GRAY + "  OP: " + ChatColor.DARK_GRAY + otherPlayer.isOp());
                            player.sendMessage(ChatColor.GRAY + "  Online: " + ChatColor.DARK_GRAY + otherPlayer.isOnline());
                            player.sendMessage(ChatColor.GRAY + "  Gamemode: " + ChatColor.DARK_GRAY + otherPlayer.getGameMode() + ChatColor.GRAY);
                            player.sendMessage(ChatColor.GRAY + "  IP: " + ChatColor.DARK_GRAY + otherPlayer.getAddress());
                            player.sendMessage(ChatColor.GRAY + "  Health: " + ChatColor.DARK_GRAY + otherPlayer.getHealth());
                            player.sendMessage(ChatColor.GRAY + "  Location: " + ChatColor.DARK_GRAY + "(" + otherPlayer.getWorld().getName() + ", " + Math.round(otherPlayer.getLocation().getX()) + ", " + Math.round(otherPlayer.getLocation().getY()) + ", " + Math.round(otherPlayer.getLocation().getZ()) + ")");
                            player.sendMessage(ChatColor.BLUE + "--------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                       
                    }
                   
                    //Null Command
                   
                    else if (param1.equals("")) {
                        if (player.hasPermission("orecraft.null")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                            player.sendMessage(ChatColor.GRAY + "  OreCraftCore version 1.0.0 loaded.");
                            player.sendMessage(ChatColor.GRAY + "  Type " + ChatColor.DARK_GRAY + "/orecraft help " + ChatColor.GRAY + "for a list of commands.");
                            player.sendMessage(ChatColor.BLUE + "--------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                   
                    //Unknown Command
                   
                    else {
                        if (player.hasPermission("orecraft.unknown")) {
                            player.sendMessage(ChatColor.BLUE + "----------OreCraftCore----------");
                            player.sendMessage(ChatColor.GRAY + "  Command not recognized.");
                            player.sendMessage(ChatColor.GRAY + "  Type " + ChatColor.DARK_GRAY + "/orecraft help " + ChatColor.GRAY + "for a list of commands.");
                            player.sendMessage(ChatColor.BLUE + "--------------------------------");
                            return true;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
                        }
                    }
                }
            }
            return false;
        }
       
        Logger log;
       
        public void onEnable() {
           
            if (!new File(getDataFolder().toString()).exists()) {
                new File(getDataFolder().toString()).mkdir();
              }
           
            File file = new File(getDataFolder() + "/players.txt");
           
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("Unable to create 'players' file in " + file.getPath() + "/" + file.getName());
                }
            }
           
            log = this.getLogger();
            log.info("Plugin enabled.");
        }
       
        public void onDisable(){
            log.info("Plugin disabled.");
        }   
     
    }
    
    ***********
    PLAYER LISTENER class:
    ***********

    Code:
    package org.orecraft.orecraftcore;
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Calendar;
    import java.util.logging.Logger;
     
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class IPListener implements Listener
    {
        public static OreCraftCore plugin;
       
        public IPListener(OreCraftCore instance) {
            plugin = instance;
        }
       
        public void onPlayerJoin(PlayerJoinEvent event) {
            String playerIP = event.getPlayer().getAddress().toString();
            String playerName = event.getPlayer().getName();
           
            File playerLogger = new File(plugin.getDataFolder() + "/players.txt");
           
            //Write on that damn file!
           
            try {
                //File, Y U NO EXIST?
                if (!playerLogger.exists()) {
                    try {
                        Logger log = Logger.getLogger("Minecraft");
                        log.info("'players.txt' file does not exist, creating now...");
                        playerLogger.createNewFile();
                    } catch (IOException e) {
                        System.out.println("Unable to create 'players' file in " + playerLogger.getPath() + "/" + playerLogger.getName());
                    }
                }
               
                //Writer, Y U NO WRITE?
               
                Logger log = Logger.getLogger("Minecraft");
                log.info("Creating database for player '" + playerName + ".'");
                BufferedWriter writer = new BufferedWriter(new FileWriter(playerLogger, true));
                writer.write("[" + getDate() + "] " + playerName + "," + playerIP);
                writer.newLine();
                writer.flush();
                writer.close();
            }
            catch (Exception e) {
                Logger log = Logger.getLogger("Minecraft");
                log.info("OreCraftCore was unable to create a database for player '" + playerName + ".'");
                log.info("The error was:");
                e.printStackTrace();
            }
        }
       
        //getDate() method
       
        public static String getDate() {
            Calendar c = Calendar.getInstance();
            int month = c.get(2) + 1;
            String date = Integer.toString(month);
            date = date + "/";
            date = date + c.get(5) + "/";
            date = date + c.get(1) + " ";
            date = date + c.get(11) + ":";
            date = date + c.get(12) + ".";
            date = date + c.get(13);
            return date;
        }
    }
    
    Anyone? This needs to be fixed :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page