No matter what I do, I just can't seem to be able to fix this!

Discussion in 'Plugin Development' started by football70500, Jul 14, 2014.

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

    football70500

    I have been trying for about a month now to figure out how to save my inventory with this plugin and I just can't figure it out. In a previous post of about 2 weeks ago, I was told to add this to my SaveInventory method and there is an error thrown right at the } under c.save(file). It says "Syntax error, insert "Finally" to complete BlockStatements" I dont get what it means by Insert Finally.

    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2.  
    3.  
    4. public static Inventory enderone = Bukkit.createInventory(null, 9, "EnderChest");
    5. public static Inventory endertwo = Bukkit.createInventory(null, 18, "EnderChest");
    6. public static Inventory enderthree = Bukkit.createInventory(null, 27, "EnderChest");
    7. public static Inventory enderfour = Bukkit.createInventory(null, 36, "EnderChest");
    8. public static Inventory enderfive = Bukkit.createInventory(null, 45, "EnderChest");
    9. public static Inventory endersix = Bukkit.createInventory(null, 54, "EnderChest");
    10. public static Inventory backpackone = Bukkit.createInventory(null, 9, "Inventory");
    11.  
    12. @Override
    13. public void onEnable(){
    14. getLogger().info("JumboChest has been enabled!");
    15. Bukkit.getPluginManager().registerEvents(this, this);
    16. }
    17.  
    18. @Override
    19. public void onDisable(){
    20. getLogger().info("JumboChest has been disabled!");
    21. }
    22.  
    23.  
    24. @EventHandler
    25. public void onOpen(PlayerInteractEvent e){
    26. Player player = e.getPlayer();
    27. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    28. if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
    29. e.setCancelled(true);
    30. if(player.hasPermission("jc.ender.one")){
    31. player.openInventory(enderone);
    32. }
    33. if(player.hasPermission("jc.ender.two")){
    34. player.openInventory(endertwo);
    35. }
    36. if(player.hasPermission("jc.ender.three")){
    37. player.openInventory(enderthree);
    38. }
    39. if(player.hasPermission("jc.ender.four")){
    40. player.openInventory(enderfour);
    41. }
    42. if(player.hasPermission("jc.ender.five")){
    43. player.openInventory(enderfive);
    44. }
    45. if(player.hasPermission("jc.ender.six")){
    46. player.openInventory(endersix);
    47. }
    48. }
    49. }
    50. }
    51.  
    52. @EventHandler
    53. public void onClose(InventoryCloseEvent e){
    54. saveInventory(e.getPlayer().getInventory().getContents(), e.getPlayer().getInventory().getArmorContents(), e.getPlayer().getUniqueId());
    55.  
    56. }
    57.  
    58.  
    59.  
    60. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    61. try {
    62. YamlConfiguration c = new YamlConfiguration();
    63. File file = new File(this.getDataFolder() + "/data/", uuid + ".yml");
    64. if (!file.exists()) {
    65. try {
    66. file.createNewFile();
    67. }
    68. catch (IOException e) {
    69. e.printStackTrace();
    70. }
    71. }
    72. c.save(file);
    73. }
    74. }
    75.  
    76.  
    77.  
    78. }
    79.  
     
  2. Offline

    Necrodoom

  3. Offline

    football70500

    Necrodoom

    When I add a finally statement at the end, c and file get underlined

    The error on file says: The field JavaPlugin.file is not visible

    The error on c says: c cannot be resolved. Where can I put my File and my YAMLConfiguration at for this to not show up?
    Code:java
    1. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    2. try {
    3. YamlConfiguration c = new YamlConfiguration();
    4. File file = new File(this.getDataFolder() + "/data/", uuid + ".yml");
    5. if (!file.exists()) {
    6. try {
    7. file.createNewFile();
    8. }
    9. catch (IOException e) {
    10. e.printStackTrace();
    11. }
    12.  
    13. }
    14. c.save(file);
    15. }
    16. finally{
    17. c.save(file);
    18. }
    19.  
    20. }
     
  4. Offline

    Necrodoom

    football70500 after the try closes?
    Edit: comment above was edited while I post replying, original post: "where do I put the catch?"
     
  5. Offline

    football70500

    My bad on that one. I derped for a sec. If I do it like this, in my finally statement, It says c.save(file) is "Unhandled exception type IOException"

    Code:java
    1. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    2. YamlConfiguration c = new YamlConfiguration();
    3. File file = new File(this.getDataFolder() + "/data/", uuid + ".yml");
    4. try {
    5.  
    6. if (!file.exists()) {
    7. try {
    8. file.createNewFile();
    9. }
    10. catch (IOException e) {
    11. e.printStackTrace();
    12. }
    13.  
    14. }
    15. }
    16. finally{
    17. c.save(file);
    18. }
    19. }

    And if i try to catch it it throws another error

    Necrodoom

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

    CorrieKay

    Well, you'll want to try/catch the c.save(file), as that can throw an exception. (edit: You could probably just move the save method up into the first try block)

    By the way, are you requiring this to be human readable? I know you've been working on it for quite a while, but I've got a bit of experience with saving inventories to file, and I've always found it far easier to use an NBT tag to make the save. If you're interested I've got two methods, one for writing to a compound tag and one to read an inventory from a compound tag for reference, if you wanted to learn how to do that :]
     
  7. Offline

    football70500

    CorrieKay
    Will this work?
    Code:java
    1. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    2. YamlConfiguration c = new YamlConfiguration();
    3. File file = new File(this.getDataFolder() + "/data/", uuid + ".yml");
    4. try {
    5.  
    6.  
    7. if (!file.exists()) {
    8. try {
    9. file.createNewFile();
    10. }
    11. catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14.  
    15. }
    16. }
    17. finally{
    18. try {
    19. c.save(file);
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. }
    25. }
     
  8. Offline

    CorrieKay

    Well, to be honest, you really ought to not have so many try catch blocks. You could compress all 3 into a single one, and cut down on the number of lines you've got (increasing the readability of this)

    Code:java
    1. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    2. YamlConfiguration c = new YamlConfiguration();
    3. File file = new File(this.getDataFolder() + "/data/" + uuid.toString() + ".yml");
    4. try{
    5. if(!file.exists()){
    6. file.createNewFile();
    7. }
    8. c.save(file);
    9. } catch (IOException e){
    10. e.printStackTrace();
    11. }
    12. }


    Be aware that this'll just save a new yml configuration file, you dont yet do anything with the item stack arrays :p
     
  9. Offline

    football70500

    Thanks for pointing that out. I already made it do this thoughL

    Code:java
    1. @EventHandler
    2. public void onClose(InventoryCloseEvent e){
    3. saveInventory(e.getPlayer().getInventory().getContents(), e.getPlayer().getInventory().getArmorContents(), e.getPlayer().getUniqueId());
    4.  
    5. }


    EDIT: The Inventory still gets erased


    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2.  
    3.  
    4.  
    5. public static Inventory enderone = Bukkit.createInventory(null, 9, "EnderChest");
    6. public static Inventory endertwo = Bukkit.createInventory(null, 18, "EnderChest");
    7. public static Inventory enderthree = Bukkit.createInventory(null, 27, "EnderChest");
    8. public static Inventory enderfour = Bukkit.createInventory(null, 36, "EnderChest");
    9. public static Inventory enderfive = Bukkit.createInventory(null, 45, "EnderChest");
    10. public static Inventory endersix = Bukkit.createInventory(null, 54, "EnderChest");
    11. public static Inventory backpackone = Bukkit.createInventory(null, 9, "Inventory");
    12.  
    13. @Override
    14. public void onEnable(){
    15. getLogger().info("JumboChest has been enabled!");
    16. Bukkit.getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. getLogger().info("JumboChest has been disabled!");
    22. }
    23.  
    24.  
    25. @EventHandler
    26. public void onOpen(PlayerInteractEvent e){
    27. Player player = e.getPlayer();
    28. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    29. if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
    30. e.setCancelled(true);
    31. if(player.hasPermission("jc.ender.one")){
    32. player.openInventory(enderone);
    33. }
    34. if(player.hasPermission("jc.ender.two")){
    35. player.openInventory(endertwo);
    36. }
    37. if(player.hasPermission("jc.ender.three")){
    38. player.openInventory(enderthree);
    39. }
    40. if(player.hasPermission("jc.ender.four")){
    41. player.openInventory(enderfour);
    42. }
    43. if(player.hasPermission("jc.ender.five")){
    44. player.openInventory(enderfive);
    45. }
    46. if(player.hasPermission("jc.ender.six")){
    47. player.openInventory(endersix);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. @EventHandler
    54. public void onClose(InventoryCloseEvent e){
    55. saveInventory(e.getPlayer().getInventory().getContents(), e.getPlayer().getInventory().getArmorContents(), e.getPlayer().getUniqueId());
    56.  
    57. }
    58.  
    59.  
    60.  
    61. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    62. YamlConfiguration c = new YamlConfiguration();
    63. File file = new File(this.getDataFolder() + "/data/" + uuid.toString() + ".yml");
    64. try{
    65. if(!file.exists()){
    66. file.createNewFile();
    67. }
    68. c.save(file);
    69. } catch (IOException e){
    70. e.printStackTrace();
    71. }
    72. }
    73. }


    Is it because I didn't actually save it upon InventoryClose?
     
  10. Offline

    CorrieKay

    Right, no, i see that the saveInventory method is being called, but the actual method itself doesnt save the inventory, or armor arrays to the yaml file. Its literally just creating an empty configuration, and saving it to a file with the name of the players UUID.
     
  11. Offline

    football70500

    I see. Now how do I make it actually save the inventory to the file??
     
  12. Offline

    CorrieKay

    considering at Item Stacks implement ConfigurationSerializeable, you should be able to save them directly to the configuration file.

    Hmm.. What happens if you put these two lines between the create new file, and the save?

    Code:java
    1. c.set("contents", inventory);
    2. c.set("armor", armor);


    go ahead and do that, get the inventory to save, and paste the newly created yml file here
     
  13. Offline

    football70500

    The inventory still doesn't get saved.

    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2.  
    3.  
    4.  
    5. public static Inventory enderone = Bukkit.createInventory(null, 9, "EnderChest");
    6. public static Inventory endertwo = Bukkit.createInventory(null, 18, "EnderChest");
    7. public static Inventory enderthree = Bukkit.createInventory(null, 27, "EnderChest");
    8. public static Inventory enderfour = Bukkit.createInventory(null, 36, "EnderChest");
    9. public static Inventory enderfive = Bukkit.createInventory(null, 45, "EnderChest");
    10. public static Inventory endersix = Bukkit.createInventory(null, 54, "EnderChest");
    11. public static Inventory backpackone = Bukkit.createInventory(null, 9, "Inventory");
    12.  
    13. @Override
    14. public void onEnable(){
    15. getLogger().info("JumboChest has been enabled!");
    16. Bukkit.getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. getLogger().info("JumboChest has been disabled!");
    22. }
    23.  
    24.  
    25. @EventHandler
    26. public void onOpen(PlayerInteractEvent e){
    27. Player player = e.getPlayer();
    28. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    29. if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
    30. e.setCancelled(true);
    31. if(player.hasPermission("jc.ender.one")){
    32. player.openInventory(enderone);
    33. }
    34. if(player.hasPermission("jc.ender.two")){
    35. player.openInventory(endertwo);
    36. }
    37. if(player.hasPermission("jc.ender.three")){
    38. player.openInventory(enderthree);
    39. }
    40. if(player.hasPermission("jc.ender.four")){
    41. player.openInventory(enderfour);
    42. }
    43. if(player.hasPermission("jc.ender.five")){
    44. player.openInventory(enderfive);
    45. }
    46. if(player.hasPermission("jc.ender.six")){
    47. player.openInventory(endersix);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. @EventHandler
    54. public void onClose(InventoryCloseEvent e){
    55. saveInventory(e.getPlayer().getInventory().getContents(), e.getPlayer().getInventory().getArmorContents(), e.getPlayer().getUniqueId());
    56.  
    57. }
    58.  
    59.  
    60.  
    61. public void saveInventory(ItemStack[] inventory, ItemStack[] armor, UUID uuid) {
    62. YamlConfiguration c = new YamlConfiguration();
    63. File file = new File(this.getDataFolder() + "/data/" + uuid.toString() + ".yml");
    64. try{
    65. if(!file.exists()){
    66. file.createNewFile();
    67. }
    68. c.set("contents", inventory);
    69. c.set("armor", armor);
    70. c.save(file);
    71. } catch (IOException e){
    72. e.printStackTrace();
    73. }
    74. }
    75. }
     
  14. Offline

    CorrieKay

    Is there a stacktrace/error you're getting, or does that code just literally do nothing? :confused:
     
  15. Offline

    football70500

    It doesn't save the inventory. No error it just doesn't save the inventory after I reload/restar tthe server

    CorrieKay

    Anyone help?
     
Thread Status:
Not open for further replies.

Share This Page