[Help] Saving to a file

Discussion in 'Plugin Development' started by Web2201, Jan 7, 2015.

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

    Web2201


    1. Ok, so I have this class...

      Code:java
      1.  
      2. package me.Web2201.referral;
      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.ArrayList;
      10. import java.util.List;
      11. import java.util.logging.Logger;
      12.  
      13. import org.bukkit.ChatColor;
      14. import org.bukkit.Material;
      15. import org.bukkit.command.Command;
      16. import org.bukkit.command.CommandSender;
      17. import org.bukkit.entity.Player;
      18. import org.bukkit.event.EventHandler;
      19. import org.bukkit.event.Listener;
      20. import org.bukkit.event.player.PlayerJoinEvent;
      21. import org.bukkit.inventory.ItemStack;
      22. import org.bukkit.plugin.PluginDescriptionFile;
      23. import org.bukkit.plugin.PluginManager;
      24. import org.bukkit.plugin.java.JavaPlugin;
      25.  
      26. public class Referral extends JavaPlugin implements Listener {
      27.  
      28. public Referral plugin;
      29. public final Logger logger = this.getLogger();
      30. public List<String> referredPlayers = new ArrayList<String>();
      31.  
      32. @Override
      33. public void onEnable() {
      34.  
      35. PluginDescriptionFile pdf = this.getDescription();
      36. File dir = getDataFolder();
      37. if(!dir.exists())
      38. if(!dir.mkdir())
      39. System.out.println("Failed to create directory for plugin:" + pdf.getName());
      40.  
      41. referredPlayers = (List<String>) load(new File(getDataFolder(), "referredPlayers.dat"));
      42.  
      43.  
      44. this.logger.info(pdf.getName() + " Version " + pdf.getVersion()
      45. + " Enabled!");
      46. PluginManager pm = getServer().getPluginManager();
      47. pm.registerEvents(this, this);
      48.  
      49. }
      50.  
      51. @Override
      52. public void onDisable() {
      53.  
      54. PluginDescriptionFile pdf = this.getDescription();
      55. this.logger.info(pdf.getName() + " Version " + pdf.getVersion()
      56. + " Disabled!");
      57. save(referredPlayers, new File(getDataFolder(), "referredPlayers.dat"));
      58.  
      59. }
      60.  
      61. @Override
      62. public boolean onCommand(CommandSender sender, Command command,
      63. String commandLabel, String[] args) {
      64.  
      65. if (sender instanceof Player) {
      66.  
      67. Player player = (Player) sender;
      68. String targetPlayerName = args[0];
      69.  
      70. if (commandLabel.equalsIgnoreCase("refer")) {
      71. if(args.length == 1){
      72. referredPlayers.add(targetPlayerName);
      73. player.sendMessage(prefix(ChatColor.GREEN, ChatColor.GREEN)
      74. + "Successfully refered " + ChatColor.RED
      75. + targetPlayerName);
      76. player.sendMessage(ChatColor.GREEN
      77. + "When the referred player joins the server for the first time, they will receive a Tier 2 Crate!");
      78. }
      79. }
      80. }
      81.  
      82. return false;
      83. }
      84.  
      85. public void save(Object obj, File file){
      86. try {
      87. if(!file.exists())
      88. file.createNewFile();
      89. oos.writeObject(obj);
      90. oos.flush();
      91. oos.close();
      92. } catch (Exception e) {
      93. e.printStackTrace();
      94. }
      95. }
      96.  
      97. public Object load(File file){
      98.  
      99. try {
      100. Object result = ois.readObject();
      101. ois.close();
      102. return result;
      103. } catch (Exception e) {
      104. return null;
      105. }
      106.  
      107. }
      108.  
      109. @EventHandler
      110. public void onPlayerJoin(PlayerJoinEvent event) {
      111. Player player = event.getPlayer();
      112. String playerName = event.getPlayer().getName();
      113. if (referredPlayers.contains(playerName)) {
      114. if (!player.hasPlayedBefore()) {
      115. player.getInventory().setItem(1,
      116. new ItemStack(Material.COMPASS));
      117. player.sendMessage(ChatColor.GREEN
      118. + "You have received a Tier 2 Crate");
      119. referredPlayers.remove(playerName);
      120. }
      121. }
      122. }
      123.  
      124. public String prefix(ChatColor color, ChatColor color2) {
      125. return ChatColor.GRAY + "[" + color + "Referral" + ChatColor.GRAY + "]"
      126. + color2;
      127. }
      128.  
      129. }
      130.  


      and when I run the plugin its all fine.
      When I join the server I get an error caused by a NullPointerException
      And when I run the command I get an error.

      Here is the log:
      pastebin.com/CWsGB5WV
     
  2. Offline

    cnniillaa

    Wrong section! Please post in Plugin Development :)
     
  3. Offline

    eyamaz

    Please report the post in the future so that it can be moved instead of having a duplicate post made by the OP. Moved to Plugin Dev.
     
  4. Offline

    1Rogue Retired Staff

    Can you get an updated stacktrace? It doesn't match with the code you posted. Also it looks like you might have world corruption or somesuch re: the end-of-file exception.
     
  5. Offline

    CheesyFreezy

    In the onPlayerJoin you're checking if the player's name is in the ArrayList. Maybe your ArrayList is empty. Try to first add something in your ArrayList and than check it. Maybe this works
     
Thread Status:
Not open for further replies.

Share This Page