Solved Need help with saving Locations.

Discussion in 'Plugin Development' started by ItsOneAndTwo, Dec 29, 2013.

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

    ItsOneAndTwo

    So I made a plugin, where you would type a command, right click on a block, and the block that I clicked on gets added to a Location ArrayList.
    Ok cool, everything works, but i need a way to save those locations to a file, for an example .dat file.
    I have this:
    Code:java
    1. public ArrayList<Location> blockloc = new ArrayList<Location>();

    Is there a way i can save blockloc to a .dat file, and then load it again? If so, please help!
     
  2. Offline

    CubieX

    You can use a custom .yml file. Here is an explanation on how to create and use a custom .yml file to store data.
    Especially have a look at the section "Arbitrary Configurations".
     
  3. Offline

    ItsOneAndTwo

    CubieX
    Did you even read my post?
    I'm trying to save a bunch of locations in an ArrayList into a .dat or .data file, so that users wont be able to mess up the locations.
    And I also need a way to load location data from a .dat/.data file and set them to an ArrayList.
     
  4. Offline

    BillyGalbreath

    Why are you so stuck on .dat files? YAML is built into the API for us to save our plugin's data to..
     
  5. Offline

    ItsOneAndTwo

    @BillyGalbreath
    1. If I were to use YAML, then users would be able to mess with the coords (not a big problem).
    2. I believe Location can't be serialized, which I probably have to do.
    How would I save all the locations from an ArrayList to a file then? Help.
     
  6. Offline

    xTrollxDudex

    ItsOneAndTwo
    ...
    1.Users can mess around all they want with the file but if you don't save it, you're good.
    2. Yes they can:
    PHP:
    public String locToString(Location l) {
        return 
    l.getWorld().getName ":" l.getBlockX() + ":" l.getBlockY() + ":" l.getBlockZ();
    }

    public 
    Location stringToLoc(String s) {
        
    String[] st s.split(":");
        return new 
    Location(Bukkit.getServer().getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    }
     
    MOMOTHEREAL likes this.
  7. Offline

    ItsOneAndTwo

    xTrollxDudex
    Ok, awesome.
    But how am I going to save and load my current ArrayList, which is:
    Code:java
    1. public ArrayList<Location> blockloc = new ArrayList<Location>();

    ?
     
  8. Offline

    BillyGalbreath

    By iterating over your list.
     
  9. Offline

    DarkBladee12

    ItsOneAndTwo That's how you would do it with the methods xTrollxDudex provided:

    Code:java
    1. public String listToString(List<Location> list) {
    2. StringBuilder s = new StringBuilder();
    3. for(Location l : list) {
    4. if(s.length() > 0)
    5. s.append("#");
    6. s.append(locToString(l));
    7. }
    8. return s.toString();
    9. }
    10.  
    11. public List<Location> stringToList(String s) {
    12. List<Location> list = new ArrayList<Location>();
    13. for(String p : s.split("#"))
    14. list.add(stringToLoc(p));
    15. return list;
    16. }
     
  10. Offline

    ItsOneAndTwo

    DarkBladee12
    Gonna give it a try. By the looks of the code it seems to work :D

    DarkBladee12
    How can i call the save and load?

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

    xTrollxDudex

  12. Offline

    ItsOneAndTwo

  13. Offline

    DarkBladee12

    ItsOneAndTwo You could utilize a BufferedWriter/BufferedReader to save/load the string from file.
     
  14. Offline

    ItsOneAndTwo

    DarkBladee12 I can't save the string, because i can't access it.
    I tried putting this code into a onDisable() method, and got an error, so i had to put this into the main class, and now i cant access the string from onDisable() method.
     
  15. Offline

    DarkBladee12

    You can access a file from every class and method... Please google how to save and load strings to/from file, I think this well help you.
     
  16. Offline

    ItsOneAndTwo

    I know how to save strings to a file, i just cant access the string itself.
     
  17. Offline

    xTrollxDudex

    ItsOneAndTwo
    Your onDisable is supposed to be in the main class... Show the code.
     
  18. Offline

    ItsOneAndTwo

    xTrollxDudex
    Here's my main class:
    Code:java
    1. public class InfiniteBlocks extends JavaPlugin {
    2.  
    3. public Logger logger = Logger.getLogger("Mineraft");
    4. //public ArrayList<Location> blockloc = new ArrayList<Location>();
    5. public ArrayList<String> cansetblockloc = new ArrayList<String>();
    6.  
    7. public String locToString(Location l) {
    8. return l.getWorld().getName() + ":" + l.getBlockX() + ":" + l.getBlockY() + ":" + l.getBlockZ();
    9. }
    10.  
    11. public Location stringToLoc(String s) {
    12. String[] st = s.split(":");
    13. return new Location(Bukkit.getServer().getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    14. }
    15.  
    16. public String listToString(List<Location> blockloc) {
    17. StringBuilder s = new StringBuilder();
    18. for(Location l : blockloc) {
    19. if(s.length() > 0)
    20. s.append("#");
    21. s.append(locToString(l));
    22. }
    23. return s.toString();
    24. }
    25.  
    26. public List<Location> stringToList(String s) {
    27. List<Location> blockloc = new ArrayList<Location>();
    28. for(String p : s.split("#"))
    29. blockloc.add(stringToLoc(p));
    30. return blockloc;
    31. }
    32.  
    33. public void onEnable() {
    34. PluginManager pm = getServer().getPluginManager();
    35. pm.registerEvents(new InfiniteBlocksListener(this), this);
    36. PluginDescriptionFile pdfFile = getDescription();
    37. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Enabled!");
    38. }
    39.  
    40. public void onDisable() {
    41. PluginDescriptionFile pdfFile = getDescription();
    42. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Disabled!");
    43. }
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    46. if(cmd.getName().equalsIgnoreCase("infblock")){
    47. Player player = (Player) sender;
    48. if(sender.hasPermission("infiniteblocks.set")) {
    49. if(!cansetblockloc.contains(sender.getName())) {
    50. cansetblockloc.add(sender.getName());
    51. sender.sendMessage("§ePlease click on the block you wish to make infinite.");
    52. }
    53. }
    54. return true;
    55. }
    56. return false;
    57. }
    58. }
    59.  
     
  19. Offline

    xTrollxDudex

  20. Offline

    ItsOneAndTwo

    xTrollxDudex
    Locations are stored in
    Code:java
    1. public ArrayList<Location> blockloc = new ArrayList<Location>();

    Whenever i type /infblock, i'm added to
    Code:
    public ArrayList<String> cansetblockloc = new ArrayList<String>();
    And then im clicking on a block, which removes me from cansetblockloc, and the block i clicked on gets added to blockloc.
    everything works, except whenever i reload/restart the server, blockloc resets.
    I just need to save and load blockloc.
     
  21. Offline

    xTrollxDudex

    ItsOneAndTwo
    Well then use a static method to get the blockloc
     
  22. Offline

    ItsOneAndTwo

  23. Offline

    xTrollxDudex

    ItsOneAndTwo
    PHP:
    public static ArrayList<StringgetList() { return blockloc; }
     
  24. Offline

    ItsOneAndTwo

    xTrollxDudex
    How would i implement that into my current code?
    btw, blockloc is ArrayList<Location>, not ArrayList<String>.
     
  25. Offline

    xTrollxDudex

    ItsOneAndTwo
    Sorry, change that to Location...

    Note to self: don't multitask if you have a difficult problem and an easy problem, you'll obviously get the easy one wrong.
     
    BillyGalbreath likes this.
  26. Offline

    ItsOneAndTwo

    xTrollxDudex
    I'm new to Java, I can do basic things with it, but where on earth am I going to put the code U gave me?

    I tried putting this into onDisable/Enable, and I tried to put this to main class, but I'm getting an error.

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

    xTrollxDudex

    ItsOneAndTwo
    Well, you can't put methods into another method. You would put that code I gave you in your listener class or whatever class that has your ArrayList in it.

    Then, onEnable, you would do:
    PHP:
    saveDefaultConfig();
    <
    Listener class name>.getList().addAll(0stringToList(getConfig().getString("Locations")));
    And in your onDisable, do
    PHP:
    getConfig.set("Locations"listToString(<Your listener class name>.getList()));
    saveConfig()
     
  28. Offline

    ItsOneAndTwo

    xTrollxDudex
    Now i have this:
    Code:java
    1. public void onEnable() {
    2. saveDefaultConfig();
    3. <InfiniteBlocks>.getList().addAll(0, stringToList(getConfig().getString("Locations")));
    4. PluginManager pm = getServer().getPluginManager();
    5. pm.registerEvents(new InfiniteBlocksListener(this), this);
    6. PluginDescriptionFile pdfFile = getDescription();
    7. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Enabled!");
    8. }
    9.  
    10. //onDisable
    11. public void onDisable() {
    12. getConfig().set("Locations", listToString(InfiniteBlocks.getList()));
    13. saveConfig();
    14. PluginDescriptionFile pdfFile = getDescription();
    15. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Disabled!");
    16. }

    And i'm getting error on these two:
    Code:java
    1. getConfig().set("Locations", listToString(InfiniteBlocks.getList()));

    Error: "The method listToString(List<Location>) in the type InfiniteBlocks is not applicable for the arguments (ArrayList<String>)"
    And on this one:
    Code:java
    1. <InfiniteBlocks>.getList().addAll(0, stringToList(getConfig().getString("Locations")));

    I'm getting this error: "Multiple markers at this line
    - The method addAll(int, Collection<? extends String>) in the type ArrayList<String> is not applicable for the arguments (int,
    List<Location>)
    - Syntax error on token ">", delete this token
    - Syntax error on token(s), misplaced construct(s)"

    //EDIT:
    onEnable i fixed the code and i got this error:
    The method addAll(int, Collection<? extends String>) in the type ArrayList<String> is not applicable for the arguments (int, List<Location>)

    Nvm i got it fixed, gonna try the plugin now.

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

    xTrollxDudex

    ItsOneAndTwo
    Ah, now we're getting somewhere.
    1. Use rhis instead:
    PHP:
    public String listToString(ArrayList<Location> list) {
        
    StringBuilder s = new StringBuilder($;
        for(
    Location loc : list) 
            
    s.append("#").append(locToString(loc));
        return 
    s.toString();
    }
    2/3. Use this instead:
    PHP:
    public ArrayList<LocationstringToList(String s) {
        
    ArrayList<Locationblockloc = new ArrayList<Location>();
        for(
    String p s.split("#"))
            
    blockloc.add(stringToLoc(p));
        return 
    blockloc;
    }
    4. Remove all the < and the > from your code

    5. Profit.

    Wait, show me your listener class please.
     
  30. Offline

    ItsOneAndTwo

    xTrollxDudex
    I removed all the code you guys gave me, now the plugin is working, but without the saving and loading feature.
    If i ever decide to reload/restart the server then all of my locations in the arraylist will be gone.
    Ok, here i have my main class:
    Code:java
    1. package com.gmail.nyyd.ronn.ib;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class InfiniteBlocks extends JavaPlugin {
    15.  
    16. //Logger
    17. public Logger logger = Logger.getLogger("Mineraft");
    18.  
    19. //ArrayList stuff
    20. public ArrayList<Location> blockloc = new ArrayList<Location>();
    21. public ArrayList<String> cansetblockloc = new ArrayList<String>();
    22.  
    23. //onEnable
    24. public void onEnable() {
    25. PluginManager pm = getServer().getPluginManager();
    26. pm.registerEvents(new InfiniteBlocksListener(this), this);
    27. PluginDescriptionFile pdfFile = getDescription();
    28. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Enabled!");
    29. }
    30.  
    31. //onDisable
    32. public void onDisable() {
    33. PluginDescriptionFile pdfFile = getDescription();
    34. this.logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion() + " has been Disabled!");
    35. }
    36.  
    37. //Command
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    39. if(cmd.getName().equalsIgnoreCase("infblock")){
    40. Player player = (Player) sender;
    41. if(sender.hasPermission("infiniteblocks.set")) {
    42. if(!cansetblockloc.contains(sender.getName())) {
    43. cansetblockloc.add(sender.getName());
    44. sender.sendMessage("§ePlease click on the block you wish to make infinite.");
    45. }
    46. }
    47. return true;
    48. }
    49. return false;
    50. }
    51. }
    52.  


    And here i have my Listener class:
    Code:java
    1. package com.gmail.nyyd.ronn.ib;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11. public class InfiniteBlocksListener implements Listener {
    12. public static InfiniteBlocks plugin;
    13. public InfiniteBlocksListener(InfiniteBlocks instance) {
    14. plugin = instance;
    15. }
    16.  
    17. //Add to blockloc
    18. @EventHandler
    19. public void onPlayerInteractEvent(PlayerInteractEvent event) {
    20. Block block = event.getClickedBlock();
    21. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    22. event.getPlayer().sendMessage("§eBlock ID: " + block.getTypeId() + ":" + block.getData() + " (" + block.getType() + ")");
    23. if(plugin.cansetblockloc.contains(event.getPlayer().getName())) {
    24. event.setCancelled(true);
    25. if(block.getType() == Material.ANVIL) {
    26. if(!plugin.blockloc.contains(block.getLocation())) {
    27. plugin.blockloc.add(block.getLocation());
    28. plugin.cansetblockloc.remove(event.getPlayer().getName());
    29. event.getPlayer().sendMessage("§aThis " + block.getType().toString().toLowerCase() + " §ais now infinite!");
    30. }else{
    31. event.getPlayer().sendMessage("§eThis " + block.getType().toString().toLowerCase() + " is no longer infinite!");
    32. plugin.cansetblockloc.remove(event.getPlayer().getName());
    33. plugin.blockloc.remove(block.getLocation());
    34. }
    35. }else{
    36. event.getPlayer().sendMessage("§4This " + block.getType().toString().toLowerCase() + " can't be set as infinite!");
    37. plugin.cansetblockloc.remove(event.getPlayer().getName());
    38. }
    39. }
    40.  
    41. if(plugin.blockloc.contains(block.getLocation())) {
    42. if(block.getType() == Material.ANVIL) {
    43. if(block.getData() == (byte) 4 || block.getData() == (byte) 8) {
    44. block.setData((byte) 0);
    45. }
    46. if(block.getData() == (byte) 7 || block.getData() == (byte) 11) {
    47. block.setData((byte) 3);
    48. }
    49. if(block.getData() == (byte) 6 || block.getData() == (byte) 10) {
    50. block.setData((byte) 2);
    51. }
    52. if(block.getData() == (byte) 5 || block.getData() == (byte) 9) {
    53. block.setData((byte) 1);
    54. }
    55. }
    56. }
    57. }
    58. }
    59.  
    60. //Remove from blockloc
    61. @EventHandler
    62. public void onBlockBreakEvent(BlockBreakEvent event) {
    63. Block block = event.getBlock();
    64. if(plugin.blockloc.contains(block.getLocation())) {
    65. plugin.blockloc.remove(block.getLocation());
    66. event.getPlayer().sendMessage("§eThis " + block.getType().toString().toLowerCase() + " is no longer infinite!");
    67. }
    68. }
    69.  
    70. }
    71.  
     
Thread Status:
Not open for further replies.

Share This Page