HashMap Serilization

Discussion in 'Plugin Development' started by Kwright02, Nov 25, 2016.

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

    Kwright02

    I have a plugin for my prison server i'm making and It allows for the placement of blocks when in "Place" mode to be regognized as prisonBlocks. So I havea hashmap that stores the location of a block as the key, and the typeid of the block as the value. This information allows the schedueler to reset the block after a certian ammount of time. What I need help with is figuring out how to seralize this data into a config and load it back into the hashmap. This is my code for the place method and i'll be happy to give other code if asked for.

    Code:
    @EventHandler
    
            public void onBlockPlaceEvent(BlockPlaceEvent e){
    
                if(!blockplacemode == false){
    
                    Location blockcord = e.getBlock().getLocation();
    
                        blocks.put(blockcord, e.getBlock().getTypeId());
    
                        e.getPlayer().sendMessage(alertchatprefix + noprefix + "PrisonOre  type, " +e.getBlock().getTypeId() + ", added sucessfully at: " + e.getBlock().getX() + " " + e.getBlock().getY() + " " + e.getBlock().getZ());
    
                }
    
            }
    [Code]
     
  2. Offline

    Lordloss

    A simple and good solution would be adding the Block objects to a list, and store it into a FileConfiguration.
     
  3. Offline

    Zombie_Striker

    @Lordloss
    Blocks do not implement ConfigurationSerializable. That means that blocks cannot be stored in configs. However, locations can.

    @Kwright02
    Do what Lord suggested, but instead of storing blocks, store their location.
     
    DoggyCodeâ„¢ likes this.
  4. Offline

    Kwright02

    ok but how do I get them out of the list and back into the hashmap? I sill need the cordinates and the blocktype the block is
     
  5. Offline

    bwfcwalshy Retired Staff

    @Kwright02 Save the location and type, then just get them from the yaml file
     
  6. Offline

    Kwright02

    Yea but how do I set and get them?
     
  7. Offline

    mythbusterma

    @Kwright02

    You read them one by one and put them into the map.
     
  8. Offline

    Kwright02

    I don't meant to be rude but your stating the obvious. I asking how I would do it from a code standpoint. Like what I would do in a for loop. @mythbusterma
     
  9. Offline

    Lordloss

    No one is going to write the code for you. Use google to lookup how to use a YamlConfiguration and how you get/set values, then iterate your list of blocks, get the needed data from the blocks and put it into your config...
     
  10. Offline

    A5H73Y

    Code:
    Area1:
        1:
            x: 1
            y: 2
            z: 3
            material: STONE
        2:
            "
    
    You'd then get the keys of "Area1" for example, having a list of each entry with its coordinate and Material type.
     
  11. Offline

    mythbusterma

    @Kwright02

    Honestly, you'd be better off doing this with schematics. You can use WorldEdit to read and utilise them.

    Otherwise, create a list of values that contain locations and a type, and the block data. Keep in mind this won't work for things like chests, which have more values attached to them, or signs.

    I'm sure someone has already written code to take blocks and put them into a YAML file, so look around.
     
  12. Offline

    Kwright02

    look I wouldn't be asking for help If I hadn't already tried to use google. You don't need to be so rude to me. You're defeating the entire porpouse of this froum section. I'm not asking for someone to write the entire code for me, i'm asking for someone to show me where to start.

    @mythbusterma With the size of what I need to to schematics wont work. They're too slow and cause too much lag, also I don't see how it would be letting me do what my plugin already does.


    @A5H73Y I know this. I'm just askin for some help on where to start because I couldn't get the location and block information to be able to be pulled out of the config. I really need someone to show me a little bit of code for how I would get the info back out of the config. I'm not asking for an entire plugin's code, just a line or 2 that is like the start of what I need to do. Keep In mind i've lookd a lot of places for help and I didn't find any that worked. That is why I'm here.
     
  13. Offline

    timtower Moderator Moderator

  14. Offline

    mythbusterma

    @Kwright02

    Errr, I don't think you understand what schematics are. They are just ways of storing block data in the native Minecraft.

    There's nothing inherently slow about that, it's probably faster than storing it in a YAML file, and would be much smaller.

    You were asking how to store this information, and I told you a schematic would be the best way to store this information, I didn't say to repaste the entire thing when you wanted change blocks. I was only talking about how storing them.
     
  15. Offline

    Kwright02


    Ok sorry I miss understood that. But I know nothing about these Schematics wheras I know a little about the config system already. It would benifet me muhc more to use the system I already know a bit about versus trying to use a whole new system.
     
  16. Offline

    A5H73Y

    To further assist you (not entirely give you working code), assuming you have access to a FileConfiguration (you could use .getConfig() on your JavaPlugin class.
    Code:
    List<String> keys = getConfig().getConfigurationSection("Area1").getKeys(false);
    List<Location> blockLocations = ArrayList<Location>();
    
    for (String key : keys){
        int x = getConfig().getInt("Area1." + key + ".X");
        //do the same for each coord
    
        //Now you have access to each location of each block, and the material type.
    }
     
  17. Offline

    mythbusterma

    @Kwright02

    Right, it is more difficult. I have a project on GitHub called TerrainManager that does this, and you can use the code from it to load and save schematics, but it's a little out of date.
     
  18. Offline

    Kwright02

    @A5H73Y coo thanks! I just have one more question. How do you put a string with a value inside a config section?
     
  19. Offline

    Zombie_Striker

    @Kwright02
    Config#set(PATH + ".String" , Value)
     
  20. Offline

    Kwright02

    thanks! :)

    So I am able to succesfuly store the data in config, however i'm having some issues. here is my code:

    Code:
    public void onEnable(){
    
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
                log.info("Prison Physics is coded by Kwright02");
    
                loadBlocks();
    
            }
    
        
    
            public void onDisable(){
    
                saveConfig();
    
            }
    
        
    
            public void loadBlocks(){
    
                Bukkit.broadcastMessage("Worked Block Set");
    
                Set<String> keys = getConfig().getConfigurationSection("blocks").getKeys(false);
    
                for (String key : keys){
    
                    String keyn = key.toString();
    
                    String array[] = keyn.split(" ");
    
                    int x = Integer.parseInt(array[1]);
    
                    int y = Integer.parseInt(array[1]);
    
                    int z = Integer.parseInt(array[1]);
    
                    int typeId = Integer.parseInt(getConfig().getString(x + " " + y + " " + z).toString());
    
                    Location locc = new Location(Bukkit.getWorld("world"), x, y, z);
    
                    blocks.put(locc, typeId);
    
              
    
                    //Now you have access to each location of each block, and the material type.
    
                }
    
            }
    
    @EventHandler
    
            public void onBlockPlaceEvent(BlockPlaceEvent e){
    
                if(!blockplacemode == false){
    
                    String cordset = e.getBlock().getX() + " " + e.getBlock().getY() + " " + e.getBlock().getZ() + " ";
    
                    Location blockcord = e.getBlock().getLocation();
    
                        blocks.put(blockcord, e.getBlock().getTypeId());
    
                        this.getConfig().set("blocks." + cordset, e.getBlock().getTypeId());
    
                        e.getPlayer().sendMessage(alertchatprefix + noprefix + "PrisonOre  type, " +e.getBlock().getTypeId() + ", added sucessfully at: " + e.getBlock().getX() + " " + e.getBlock().getY() + " " + e.getBlock().getZ());
    
                }
    
            }
    
    

    The location that is being put into the hash map isn't sucesfully put in for some reason. Any Ideas?

    ==== The moderator that exited my post removed the last sentence. This is why you failed to see the question I asked. this is not my fault. ====
     
    Last edited: Dec 5, 2016
  21. Offline

    Zombie_Striker

    @Kwright02
    If your problem has been solved, mark this thread as solved.
     
  22. Offline

    Kwright02

    @Zombie_Striker
    It's not solved if you would read the above message. Therfore because it's not solved, it's not marked as solved. :)
     
  23. Offline

    bwfcwalshy Retired Staff

    Why do you say if it's not false? Why not just do if(blockplacemode)?
     
  24. Offline

    Kwright02

    instead of replying about this could you mabey reply with an answer to the question I asked? Not to be rude but your response you gave is pretty usless.
     
  25. Offline

    bwfcwalshy Retired Staff

    @Kwright02 helping you to have better logic and more efficient programming is not useful? I strong disagree, I think logic overpowers an error especially since logic can cause errors.
     
    Lordloss likes this.
  26. Offline

    Kwright02

    although it can cause errors, you still didn't answer my origonal question.
     
  27. Offline

    mythbusterma

    @Kwright02

    You didn't tell us what issue you're having. Instead of being smarmy, you could actually tell us what your problem is.
     
    Lordloss likes this.
  28. Offline

    A5H73Y

    Without testing your code in game, I can spot a few problems which may help resolve your issue.
    Code:
    for (String key : keys){
    
    String keyn = key.toString(); <-- You're setting a String to a String.toString(); completely redundant...
    
    String array[] = keyn.split(" "); <-- just split key 
    
    int x = Integer.parseInt(array[1]); <-- What is in the first index of the array..?
    
    int y = Integer.parseInt(array[1]); <-- You're using the second index for each coordinate
    
    int z = Integer.parseInt(array[1]); <-- same here... they should be 1, 2, 3 etc.
    
    
    Other than that I can see what you're trying to do and as far as I can see it should work... debug into the code to see what values you are getting back and it should help you narrow down the problem.
    Although I've just spotted it could be this line:
    Code:
    String cordset = e.getBlock().getX() + " " + e.getBlock().getY() + " " + e.getBlock().getZ() + " ";
    you're adding a space at the end, which would mean it wouldn't match it on retrieval.
     
  29. Offline

    Kwright02

    Thank you. I will try fixing these issues and see what happens
     
Thread Status:
Not open for further replies.

Share This Page