Advertisement
JackOUT

Untitled

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