Facebook
From StefTheDev, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 231
  1. public class Manager {
  2.  
  3.     //You use this set to cache (store) all your data.
  4.     //We declare the set here.
  5.     private final Set<Encapsulation> encapsulationSet;
  6.     private final JavaPlugin javaPlugin;
  7.  
  8.     //We are setting the plugin data to the variable
  9.     public Manager(JavaPlugin javaPlugin) {
  10.         this.javaPlugin = javaPlugin;
  11.         //Initialise the set.
  12.         this.encapsulationSet = new HashSet<>();
  13.     }
  14.  
  15.     //We are going to load all the data from file and put it in the set.
  16.     public void load() {
  17.         //Remember all the keys from the configuration is made into a set.
  18.         FileConfiguration configuration = javaPlugin.getConfig();
  19.         Set<String> data = configuration.getConfigurationSection("Players").getKeys(false);
  20.         //We use lambda here, the "s" is a string variable. Kinda like the variable from the for loop
  21.         data.forEach(s -> {
  22.             String[] split = s.split(":");
  23.             encapsulationSet.add(new Encapsulation(
  24.                     UUID.fromString(split[0]),
  25.                     Integer.parseInt(split[1]),
  26.                     Integer.parseInt(split[2])
  27.             ));
  28.         });
  29.  
  30.         //Yay we have loaded all the data
  31.     }
  32.  
  33.     public void unload() {
  34.         //We iterate through each element that we have saved in the set.
  35.         FileConfiguration configuration = javaPlugin.getConfig();
  36.         //We create a new string list
  37.         List<String> data = new ArrayList<>();
  38.         //Add all the elements (player storage) from the set to the list
  39.         encapsulationSet.forEach(encapsulation -> data.add(encapsulation.toString()));
  40.         //Set the player configuration to the list that we have.
  41.         configuration.set("Players", data);
  42.     }
  43.  
  44.     public Encapsulation getEncapsulation(UUID uuid) {
  45.         for(Encapsulation encapsulation : encapsulationSet) {
  46.             if(encapsulation.getUuid().equals(uuid)) {
  47.                 return encapsulation;
  48.             }
  49.         }
  50.         return null;
  51.     }
  52. }