ConfigurationSerializable help.

Discussion in 'Spigot Plugin Development' started by zDoeProgram, Feb 14, 2019.

  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.

    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) {
   = horse;
   = 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");

        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) {
   = name;

        public Status getStatus() {
            return status;

        public void setStatus(Status status) {
            this.status = status;
    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){
            Horse horse = (Horse) p.getWorld().spawnEntity(loc, EntityType.HORSE);
            String horseName = givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect();
            Status alive = Status.Alive;
            NewHorseObject horseObject = new NewHorseObject(horse, horseName, alive);
            //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));
            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);
            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