Solved My data won't store!

Discussion in 'Plugin Development' started by mcrazmouze, Sep 18, 2016.

Thread Status:
Not open for further replies.
  1. Code:text
    1.  
    2. package me.bukkit.melonos;
    3.  
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. import java.io.ObjectInputStream;
    8. import java.io.ObjectOutputStream;
    9. import java.util.HashMap;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class WHUmain extends JavaPlugin {
    16.  
    17. //Variables!
    18. String fname = "plugins/" + getDataFolder().getName()+ "/" + "data.dat";
    19. File f = new File( fname);
    20. HashMap<Integer,String> data;
    21.  
    22. //OnEnable!
    23. @Override
    24. public void onEnable() {
    25. File dir = getDataFolder();
    26. if(!dir.exists())
    27. if(dir.mkdir())
    28. getLogger().info("Could not make the folder!");
    29.  
    30. if( data == null ) {
    31. data = new HashMap<Integer, String>();
    32. try { ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(fname) ); oos.writeObject(data); oos.flush(); oos.close();} catch(Exception e){e.printStackTrace();}}
    33.  
    34. createFile();
    35.  
    36. getLogger().info( getDescription().getName() + " Is running on version " + getDescription().getVersion() );}
    37.  
    38. //Creating file!
    39. private void createFile() {
    40. if( !f.exists() )
    41. try {f.createNewFile(); }catch(Exception e){e.printStackTrace();}}
    42.  
    43.  
    44. @SuppressWarnings("unchecked")
    45. private void savePlayer( Player ply ) {
    46. try {
    47. ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(fname) );
    48. ObjectInputStream ois = new ObjectInputStream( new FileInputStream(fname));
    49.  
    50. data = (HashMap<Integer, String>) ois.readObject();
    51. ois.close();
    52.  
    53. data.put(data.size()+1, ply.getName());
    54.  
    55. oos.writeObject(data);
    56. oos.flush(); oos.close();
    57. ply.sendMessage("You've been saved!");
    58. ply.sendMessage(data.get(data.size()));
    59. return;
    60. }catch( Exception e){e.printStackTrace(); return;}}
    61.  
    62.  
    63. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    64. if( sender instanceof Player ) {
    65. Player player = (Player) sender;
    66. if( cmd.getName().equalsIgnoreCase("saveme") ) {
    67. savePlayer(player);
    68. } else { player.sendMessage("WRONG!!");}
    69. } else { getLogger().info("You have to be a player for getting saved");}
    70. return true;}
    71.  
    72. }
    73.  


    My error is here:
    http://pastebin.com/XzsWyKG9

    bump

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

    Tecno_Wizard

    @mcrazmouze That's a pretty complex error you have there. It's caused by improperly reading data/corrupted data, and that's as specific as I can say it without confusing anyone who isn't a Computer Science major. (It isn't finding a full byte at the end of the stream, so something is terribly wrong here)

    Rather than tackling the cause, I'd go to using the Bukkit Configuration API where this won't happen.
     
  3. @Tecno_Wizard yeah, I get what you mean.
    I followed this tutorial( Kinda ):
     
  4. Offline

    JanTuck

    Dont. Go to Pogostick or use his(BCBroz's) new tutorials about the config ;) He doesnt explain it 100℅ perfect in that one.

    If this is solved mark the thread as solved
    Sent from Tapatalk
     
  5. Offline

    CraftCreeper6

    @mcrazmouze
    Just as I thought, please never ever use TheBCBroz.
     
  6. Offline

    ArsenArsen

    Kinda off topic but is that BcBroz? Oh God..
    @bwfcwalshy im speechless speak for me.
     
  7. Okay, but his work.
     
  8. Offline

    ArsenArsen

    Will work true, but it is bad as all hell and also very misleading.
     
  9. Offline

    CraftCreeper6

    @mcrazmouze
    Yeah, looks like it's working too. Sarcasm intended.
     
  10. Finally made it work!

    "HashMap<Integer,String> data = (HashMap<Integer, String>) ois.readObject();"

    Just needed to add HashMap<Integer,String> at the start.
    - is there some better tutorials out there?
     
  11. Offline

    I Al Istannen

  12. Offline

    JanTuck

    Json can be red by web applications too.

    Sent from Tapatalk
     
  13. Offline

    I Al Istannen

    @JanTuck
    Yaml too. Json is just more common as it is very easy to use in JavaScript (JSON = Java Script Object Notation).

    But editing YAML is certainly easier than editing Json. Similarily, if the user shouldn't edit the file, Json may be easier for you.
     
  14. Offline

    JanTuck

    Well he wont be acessing from web i think so i would recommned yaml it is just that much easier.

    Sent from Tapatalk
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page