JackOUT

Untitled

May 21st, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. package games.coob.portals.model;
  2.  
  3. import games.coob.portals.PlayerCache;
  4. import games.coob.portals.util.HologramUtil;
  5. import lombok.Getter;
  6. import lombok.NonNull;
  7. import lombok.Setter;
  8. import org.bukkit.Location;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.entity.Player;
  11. import org.jetbrains.annotations.Nullable;
  12. import org.mineacademy.fo.Common;
  13. import org.mineacademy.fo.FileUtil;
  14. import org.mineacademy.fo.SerializeUtil;
  15. import org.mineacademy.fo.Valid;
  16. import org.mineacademy.fo.remain.CompMaterial;
  17. import org.mineacademy.fo.settings.ConfigItems;
  18. import org.mineacademy.fo.settings.YamlConfig;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.UUID;
  24.  
  25. @Getter
  26. public abstract class PortalData extends YamlConfig {
  27.  
  28.     private static final String FOLDER = "portals";
  29.  
  30.     //private static final ConfigItems<PortalData> loadedFiles = ConfigItems.fromFolder(FOLDER, PortalData.class);
  31.  
  32.     private static final ConfigItems<? extends PortalData> loadedFiles = ConfigItems.fromFolder(FOLDER, fileName -> {
  33.         final YamlConfig config = YamlConfig.fromFileFast(FileUtil.getFile(FOLDER + "/" + fileName + ".yml"));
  34.         final PortalType type = config.get("Type", PortalType.class);
  35.  
  36.         Valid.checkNotNull(type, "Unrecognized Portal Type." + config.getObject("Type") + " in " + fileName + "! Available: " + Common.join(PortalType.values()));
  37.         return type.getInstanceClass();
  38.     });
  39.  
  40.     private String name;
  41.  
  42.     private Location location;
  43.  
  44.     private CompMaterial material;
  45.  
  46.     private UUID owner;
  47.  
  48.     private String type;
  49.  
  50.     private String id;
  51.  
  52.     @Setter
  53.     private int chargePercentage;
  54.  
  55.     @Setter
  56.     private boolean used;
  57.  
  58.     @Setter
  59.     private boolean pauseCharge;
  60.  
  61.     protected PortalData(final String portalId, @Nullable final Block block, @Nullable final PortalType type) {
  62.         this.id = portalId;
  63.  
  64.         if (type != null && block != null) {
  65.             this.type = type.name().toLowerCase();
  66.             this.location = block.getLocation();
  67.             this.material = CompMaterial.fromMaterial(block.getType());
  68.         }
  69.  
  70.         this.loadConfiguration(NO_DEFAULT, FOLDER + "/" + portalId + ".yml");
  71.     }
  72.  
  73.     /*private PortalData(final String id) {
  74.         this(id, null);
  75.     }
  76.  
  77.     protected PortalData(final String id, @Nullable final Block block) {
  78.         this.id = id;
  79.  
  80.         if (block != null) {
  81.             this.material = CompMaterial.fromMaterial(block.getType());
  82.             this.location = block.getLocation();
  83.         }
  84.  
  85.         this.loadConfiguration(NO_DEFAULT, FOLDER + "/" + id + ".yml");
  86.     }*/
  87.  
  88.     @Override
  89.     protected void onLoad() {
  90.         if (this.type == null && this.location == null && this.material == null) {
  91.             Valid.checkBoolean(isSet("Type"), "Corrupted portal file: " + this.getFileName() + ", lacks the 'Type' key to determine the type of the portal.");
  92.  
  93.             final String hash = this.getString("Block");
  94.  
  95.             final String[] split = hash.split(" \\| ");
  96.             final Location location = SerializeUtil.deserializeLocation(split[0]);
  97.             final CompMaterial material = CompMaterial.valueOf(split[1]);
  98.  
  99.             this.location = location;
  100.             this.material = material;
  101.             this.type = this.getString("Type");
  102.         }
  103.  
  104.         this.id = this.getString("Id", "id");
  105.         //this.type = this.getString("Type", "type");
  106.         this.owner = this.get("Owner", UUID.class, new UUID(1, 5));
  107.         this.name = this.getString("Name", "unnamed");
  108.  
  109.         this.save();
  110.     }
  111.  
  112.     @Override
  113.     protected void onSave() {
  114.         this.set("Name", this.name);
  115.         this.set("Block", toHash(this.location, this.material));
  116.         this.set("Type", this.type);
  117.         this.set("Id", this.id);
  118.         this.set("Owner", this.owner);
  119.     }
  120.  
  121.     private String toHash(final Location location, final CompMaterial material) {
  122.         return SerializeUtil.serializeLoc(location) + " | " + material;
  123.     }
  124.  
  125.     public void increaseChargePercentage() {
  126.         this.chargePercentage = this.chargePercentage + 1;
  127.     }
  128.  
  129.     public void setName(final String name) {
  130.         this.name = name;
  131.  
  132.         this.save();
  133.     }
  134.  
  135.     public void setLocation(final Location location) {
  136.         this.location = location;
  137.     }
  138.  
  139.     public void setMaterial(final CompMaterial material) {
  140.         this.material = material;
  141.  
  142.         this.save();
  143.     }
  144.  
  145.     public void setType(final String type) {
  146.         this.type = type;
  147.     }
  148.  
  149.     public void setId(final String id) {
  150.         this.id = id;
  151.     }
  152.  
  153.     public void setOwner(final UUID owner) {
  154.         this.owner = owner;
  155.     }
  156.  
  157.     public void register(final Block block, final String type, final Player player, final String id) {
  158.         final Location location = block.getLocation();
  159.  
  160.         this.setId(id);
  161.         this.setType(type);
  162.         this.setOwner(player.getUniqueId());
  163.         this.setLocation(location);
  164.  
  165.         if (type.equals("targeted")) {
  166.             final PlayerCache cache = PlayerCache.from(player);
  167.             ((Targeted) this).setAuthorizedPlayers(cache.getAuthorizedPlayers());
  168.         }
  169.  
  170.         HologramUtil.createHologram(location.clone().add(0.5, 4, 0.5), id);
  171.  
  172.         this.save();
  173.     }
  174.  
  175.     public void unregister() {
  176.         HologramUtil.removeHologram(this.id);
  177.         removePortalByName(this.id);
  178.         this.save();
  179.     }
  180.  
  181.     // -----------------------------------------------------------------
  182.     // Static
  183.     // -----------------------------------------------------------------
  184.  
  185.     public static boolean isRegistered(final Block block) {
  186.         for (final PortalData portalData : getPortals()) {
  187.             if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
  188.                 return true;
  189.         }
  190.  
  191.         return false;
  192.     }
  193.  
  194.     public static boolean isRegistered(final String portalID) {
  195.         for (final PortalData portalData : getPortals()) {
  196.             if (portalData.getId().equals(portalID))
  197.                 return true;
  198.         }
  199.  
  200.         return false;
  201.     }
  202.  
  203.     public static List<PortalData> getOwnedPortals(final UUID ownerUUID) {
  204.         final List<PortalData> portalList = new ArrayList<>();
  205.  
  206.         for (final PortalData portalData : getPortalsOfType("targeted"))
  207.             if (portalData.getOwner().equals(ownerUUID))
  208.                 portalList.add(portalData);
  209.  
  210.         return portalList;
  211.     }
  212.  
  213.     public static PortalData findByBlock(final Block block) {
  214.         for (final PortalData portalData : getPortals())
  215.             if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
  216.                 return portalData;
  217.  
  218.         return null;
  219.     }
  220.  
  221.     public static List<PortalData> getPortalsOfType(final String portalType) {
  222.         final List<PortalData> dataList = new ArrayList<>();
  223.  
  224.         for (final PortalData portalData : getPortals())
  225.             if (portalData.getType().equals(portalType))
  226.                 dataList.add(portalData);
  227.  
  228.         return dataList;
  229.     }
  230.  
  231.     public static List<Location> getPortalLocations() {
  232.         final List<Location> locations = new ArrayList<>();
  233.  
  234.         for (final PortalData portalData : getPortals())
  235.             locations.add(portalData.getLocation());
  236.  
  237.         return locations;
  238.     }
  239.  
  240.     public static List<Location> getPortalLocationsOfType(final String type) {
  241.         final List<Location> locations = new ArrayList<>();
  242.  
  243.         for (final PortalData portalData : getPortalsOfType(type))
  244.             locations.add(portalData.getLocation());
  245.  
  246.         return locations;
  247.     }
  248.  
  249.     public static boolean isPortalOfType(final Block block, final String type) {
  250.         for (final PortalData portalData : getPortalsOfType(type))
  251.             if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
  252.                 return true;
  253.  
  254.         return false;
  255.     }
  256.  
  257.     /**
  258.      * @return
  259.      * @see ConfigItems#getItems()
  260.      */
  261.     public static List<? extends PortalData> getPortals() {
  262.         return loadedFiles.getItems();
  263.     }
  264.  
  265.  
  266.     /**
  267.      * @return
  268.      * @see ConfigItems#getItemNames()
  269.      */
  270.     public static Set<String> getPortalIDs() {
  271.         return loadedFiles.getItemNames();
  272.     }
  273.  
  274.     public static PortalData createPortal(final String portalId, final Block block, final PortalType type) {
  275.         return loadedFiles.loadOrCreateItem(portalId, () -> type.instantiate(portalId, block));
  276.     }
  277.  
  278.     /**
  279.      * @see ConfigItems#loadItems()
  280.      */
  281.     public static void loadPortals() {
  282.         loadedFiles.loadItems();
  283.     }
  284.  
  285.     public static void removePortalByName(final String portalId) {
  286.         loadedFiles.removeItemByName(portalId);
  287.     }
  288.  
  289.     /**
  290.      * @see ConfigItems#isItemLoaded(String)
  291.      */
  292.     public static boolean isPortalLoaded(final String id) {
  293.         return loadedFiles.isItemLoaded(id);
  294.     }
  295.  
  296.     /**
  297.      * @return
  298.      * @see ConfigItems#findItem(String)
  299.      */
  300.     public static PortalData findById(@NonNull final String id) {
  301.         return loadedFiles.findItem(id);
  302.     }
  303. }
  304.  
  305.  
  306.  
Add Comment
Please, Sign In to add comment