ConfigurationSerializable help.

Discussion in 'Spigot Plugin Development' started by zDoeProgram, Feb 14, 2019 at 1:13 AM.

  1. Hey, Im going to try a new attempt at this, starting from scratch to use ConfigurationSerializable.

    I'm not sure how too create the save and load methods for my hashmap.

    Object:
    Code (Java):
    package me.beastcraft3.gcmchorses.object;

    import java.util.HashMap;
    import java.util.Map;

    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.entity.Horse;

    import me.beastcraft3.gcmchorses.enums.Status;

    public class NewHorseObject implements ConfigurationSerializable{
       
        private final Horse horse;
        private String name;
        private Status status;
       

        public NewHorseObject(Horse horse, String name, Status status) {
            this.horse = horse;
            this.name = name;
            this.status = status;
        }
       
        public NewHorseObject(Map<String, Object> map) {
            horse = (Horse) map.get("horse");
            name = (String) map.get("name");
            status = (Status) map.get("status");
        }



        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
             map.put("horse", getHorse());
             map.put("name", getName());
             map.put("status", getStatus());
            return map;
        }

        public Horse getHorse() {
            return horse;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Status getStatus() {
            return status;
        }

        public void setStatus(Status status) {
            this.status = status;
        }
    }
     
    Manager:
    Code (Java):
    package me.beastcraft3.gcmchorses.manager;

    import java.util.HashMap;
    import java.util.UUID;

    import org.apache.commons.lang.RandomStringUtils;
    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Horse;
    import org.bukkit.entity.Player;

    import me.beastcraft3.gcmchorses.GCMCHorses;
    import me.beastcraft3.gcmchorses.HorseFile.HorseFile;
    import me.beastcraft3.gcmchorses.enums.Status;
    import me.beastcraft3.gcmchorses.object.NewHorseObject;

    public class NewHorseManager {
       
        HashMap<UUID, NewHorseObject> horses = new HashMap<UUID, NewHorseObject>();
       
        public void createHorse(Player p, Location loc){
            //VARIABLES
            Horse horse = (Horse) p.getWorld().spawnEntity(loc, EntityType.HORSE);
            String horseName = givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect();
            Status alive = Status.Alive;
           
            //MECHANICS
            NewHorseObject horseObject = new NewHorseObject(horse, horseName, alive);
            horse.setCustomName(String.valueOf(horseName));
           
            //HORSE FILE
            HorseFile file = GCMCHorses.getPlugin().getHorseFile();
            file.getOrLoad(horse).set(String.valueOf(horse.getUniqueId()) + ".name", horseName);
            file.getOrLoad(horse).set(String.valueOf(horse.getUniqueId()) + ".status", String.valueOf(alive));
            file.save(horse);
           
            horses.put(horse.getUniqueId(), horseObject);
           
        }
       
        public void load(){
           
           
           
        }
       
        public void save(){
           
           
           
        }
       
       
        private String givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect() {
             
            int length = 16;
            boolean useLetters = true;
            boolean useNumbers = true;
            String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
           
            //FOR LOOP CHECK IF THE NAME ALRADY EXSIST IN THE CONFIG.
           
           
            return generatedString;
        }

    }
     
    I just want to save the horses in a hashmap, so when I reload the server, they are inside the hashmap.
     
  2. Use FileConfiguration#set(String path, Object value)
     
  3. I know that. Im just not sure how I should setup the load and save method, since I want to use them on the toggle methods(onEnable & onDisable)
     

Share This Page