Help with de-serialize

Discussion in 'Plugin Development' started by jusjus112, Sep 16, 2016.

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


    Hello guys,

    I'm stuck with deserialize my home string right from my database.
    So, here is an example string = "Appelboom:22:23:51,Home:31:53:634"

    So i have a hashmap. With <String, Location> so. i want the name "Appelboom" in the string section and that 3 integer's must be a location. But, i cant seperate that. I have this:

    String s = "Appelboom:22:23:51,Home:31:53:634";
            HashMap<String, Location> h = new HashMap<String, Location>();
            String str = "";
               for (String h : s.split(",")) {
    What i excactly want is this:

    String = "Home";
    Location = new Location blabla.. (31,54,634);

    My code gave me:

    - Home:31:53:634
    - Appelboom:22:23:51

    I hope you guys can help me..
  2. Offline


    Don't use strings for this. Create your own custom object to store all these values, and use that for the hashmap.

    I would recommend using something like the following to serialize a string.
    1. Use " h.split(":") " to split the subtring into smaller bits.
    2. Array[0] is the name
    3. Array[1] is the x
    4. Array[2] is the y
    5. Array[3] is the z
  3. Offline


    I would use - Apache commons lang 3
    and do something like this.
      String s = "Appelboom:22:23:51,Home:31:53:634";
      HashMap<String, Location> h = new HashMap<String, Location>();
      List<String> coords = new ArrayList<>();
      List<String> homeNames = new ArrayList<>();
      for (String getHomeNames : s.split(",")) {
      for (String homeCoords : getHomeNames.split(":")) {
      if (!StringUtils.isNumeric(homeCoords)){
      } else {
      int t = 0;
      for (String coordName : homeNames) {
      Location location = new Location(coords.get(t), coords.get(t++), coords.get(t++);
      h.put(coordName, location);
      /*System.out.println("Name: " + coordName);
      System.out.println("X: " + coords.get(t) + " Y: " + coords.get(t++) + " Z: " + coords.get(t++));*/
      if (t != coords.size()) {
      t *= 2;
      } else {
      t = 0;
    The reason for the two lists, is so that I can iterate over the Names of the homes and iterate over every 3 integers so that you can associate them together. Also to iterate over every 3 in the stored List for the coords, had to use a bit of math, and much easier to do so in it's own loop. This is only an example, and there might be a more cleaner way of doing this.

    Also note, you only have 3 coordinate integers for a location which means, if you tried to teleport a player based on these 3 numbers, it will do so only in the world they are in and not to the world they set the home. For example, if the coords were set while in the overworld, and the player is now in the ender world and then tried to go home. It would teleport them to those coords in the ender world and not the overworld. Just something to be aware of if you didn't know already.

    Also, if you are in control of the data being put into the database, you could make it easier on yourself by separating the home names from the coordinates to begin with. Anyways, this is an example of how to extract the names and coords from the information you provided. If you uncomment the commented System.out's and ran it, it would show you what each of the values are every time the for loop iterates.
    Last edited: Sep 17, 2016
  4. Offline



    That's a huge library to include just for that. Also, don't say you don't have to, not all implementations of Bukkit have it available, so you would have to include it. I don't see any reason you would need such a complicated mess of lists for this.


    Store this information in a yaml file, having a section for each player, and in that having keys that are the names of a home, and using the ConfigurationSerializable functionality of Location to save the Location as a value with it, so that it looks like this:

    home1: Location!<whatevergoeshere>
    home2: Location!<whatevergoeshere>

    The code to do this would be much simpler, as well.

    HashMap<String, Location> homes = new HashMap<>();
    ConfigurationFile file = .....;
    ConfigurationSection section = file.getSection("homes.alright2");
    for (String key : section.getKeys(false)) {
        homes.put(key, (Location) section.get(key));
  5. Offline


    @mythbusterma never said my way was the best and even stated there might be a more cleaner way, but given the limited information, I was giving a solution to the OP's problem. Also, if you use maven, you can shade the library in as well as use the minimize flag to only leave what is necessary from the dependency also note bukkit already has Lang 2.6 which also contains StringUtils, therefore OP could just use that dependency instead and not worry about shading at all. Also have to consider that the OP might not have control over the data being placed into the DB or needs such data to be cross server accessed. Anyways I only provided a solution based on the limited information provided. Also, only 2 lists were used lol
  6. Offline



    Bukkit is an API, and only includes Bukkit's classes. CraftBukkit does include it, but certain implementations (like Cauldron) do not, so you can't rely on it existing. Yes, you could minimize it using Maven, but that doesn't mean tacking unnecessary dependencies is a good thing, there are other reasons than the size of the shaded jarfile.

    If he is actually using a database, then he's got his schema quite, quite, wrong.

    A table with something like the following schema would be more appropriate:

    uuid (BIGINT) | name (VARCHAR(255) | world (BIGINT) | x (DECIMAL) | y (DECIMAL) | z (DECIMAL)

    Regardless, why would you want to share the homes across servers? One would assume that the two different servers have different worlds. I just assume he was using a database because "hurr dudurr MySQL everything."

    If he actually has no control over what's going into the database, then @Zombie_Striker is right.
  7. Offline


    You are wrong on this, Bukkit shades the entire commons lang 2.6 into the API Jar. Craftbukkit inherits it through the fact its shaded into the API jar.
  8. Offline



    Ah. Okay, I had bad version of the Bukkit artifact. My mistake.
Thread Status:
Not open for further replies.

Share This Page