Advertisement
JackOUT

Untitled

May 19th, 2022
561
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package games.coob.smp.hologram;
  2.  
  3. import games.coob.smp.nms.HologramRegistryI;
  4. import games.coob.smp.nms.NMSHologramI;
  5. import lombok.Getter;
  6. import org.mineacademy.fo.MinecraftVersion;
  7. import org.mineacademy.fo.Valid;
  8. import org.mineacademy.fo.collection.SerializedMap;
  9. import org.mineacademy.fo.constants.FoConstants;
  10. import org.mineacademy.fo.settings.YamlConfig;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import java.util.UUID;
  16.  
  17. /**
  18.  * Represents an elegant way to permanently store and load Holograms
  19.  */
  20. public final class HologramRegistryImpl extends YamlConfig implements HologramRegistryI {
  21.  
  22.     /**
  23.      * The singleton of this class
  24.      */
  25.     @Getter
  26.     private static final HologramRegistryImpl instance = new HologramRegistryImpl();
  27.  
  28.     /**
  29.      * Represents currently loaded Holograms
  30.      */
  31.     private List<NMSHologramI> loadedHolograms = new ArrayList<>();
  32.  
  33.     /**
  34.      * Create a new registry and load
  35.      */
  36.     private HologramRegistryImpl() {
  37.         this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
  38.     }
  39.  
  40.     /**
  41.      * Automatically loads stored disk Holograms and spawns them
  42.      */
  43.     public void spawnFromDisk() {
  44.         if (!MinecraftVersion.atLeast(MinecraftVersion.V.v1_16))
  45.             return;
  46.  
  47.         // Tricky: This automatically calls the spawn method which puts the hologram to our loadedHolograms list
  48.  
  49.         // TODO save
  50.         this.loadedHolograms = loadHolograms();
  51.  
  52.         System.out.println("@Found " + this.loadedHolograms.size() + " Holograms on the disk");
  53.  
  54.         for (final NMSHologramI hologram : this.loadedHolograms)
  55.             System.out.println("\tspawned " + hologram + " at " + hologram.getLocation());
  56.     }
  57.  
  58.  
  59.     public List<NMSHologramI> loadHolograms() {
  60.         final List<NMSHologramI> loadedHologram = new ArrayList<>();
  61.  
  62.         for (final SerializedMap map : getMapList("Saved_Holograms")) {
  63.             final NMSHologramI hologram = NMSHologramProvider.deserialize(map);
  64.  
  65.             loadedHologram.add(hologram);
  66.         }
  67.  
  68.         return loadedHologram;
  69.     }
  70.  
  71.     /**
  72.      * @see YamlConfig#serialize()
  73.      */
  74.     @Override
  75.     protected SerializedMap serialize() {
  76.         return SerializedMap.of("Saved_Holograms", this.getLoadedHolograms());
  77.     }
  78.  
  79.     /**
  80.      * Registers a new hologram to our map
  81.      *
  82.      * @param hologram
  83.      */
  84.     public void register(final NMSHologramI hologram) {
  85.         Valid.checkBoolean(!this.isRegistered(hologram), hologram + " is already registered!");
  86.  
  87.         this.loadedHolograms.add(hologram);
  88.         this.save();
  89.     }
  90.    
  91.     public void unregister(final NMSHologramI hologram) {
  92.         this.loadedHolograms.remove(hologram);
  93.         this.save();
  94.     }
  95.  
  96.     /**
  97.      * Return true if the given hologram is already registered
  98.      *
  99.      * @param hologram
  100.      * @return
  101.      */
  102.     public boolean isRegistered(final NMSHologramI hologram) {
  103.         return this.isRegistered(hologram.getUniqueId());
  104.     }
  105.  
  106.     /**
  107.      * Return true if the given hologram is already registered
  108.      *
  109.      * @param entityUniqueId
  110.      * @return
  111.      */
  112.     public boolean isRegistered(final UUID entityUniqueId) {
  113.         for (final NMSHologramI hologram : this.loadedHolograms)
  114.             if (hologram != null && hologram.getUniqueId().equals(entityUniqueId))
  115.                 return true;
  116.  
  117.         return false;
  118.     }
  119.  
  120.     /**
  121.      * Get the loaded holograms
  122.      */
  123.     public List<NMSHologramI> getLoadedHolograms() {
  124.         return Collections.unmodifiableList(loadedHolograms);
  125.     }
  126. }
  127.  
Advertisement
RAW Paste Data Copied
Advertisement