Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games.coob.portals.model;
- import games.coob.portals.PlayerCache;
- import games.coob.portals.util.HologramUtil;
- import lombok.Getter;
- import lombok.NonNull;
- import lombok.Setter;
- import org.bukkit.Location;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Player;
- import org.jetbrains.annotations.Nullable;
- import org.mineacademy.fo.Common;
- import org.mineacademy.fo.FileUtil;
- import org.mineacademy.fo.SerializeUtil;
- import org.mineacademy.fo.Valid;
- import org.mineacademy.fo.remain.CompMaterial;
- import org.mineacademy.fo.settings.ConfigItems;
- import org.mineacademy.fo.settings.YamlConfig;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import java.util.UUID;
- @Getter
- public abstract class PortalData extends YamlConfig {
- private static final String FOLDER = "portals";
- //private static final ConfigItems<PortalData> loadedFiles = ConfigItems.fromFolder(FOLDER, PortalData.class);
- private static final ConfigItems<? extends PortalData> loadedFiles = ConfigItems.fromFolder(FOLDER, fileName -> {
- final YamlConfig config = YamlConfig.fromFileFast(FileUtil.getFile(FOLDER + "/" + fileName + ".yml"));
- final PortalType type = config.get("Type", PortalType.class);
- Valid.checkNotNull(type, "Unrecognized Portal Type." + config.getObject("Type") + " in " + fileName + "! Available: " + Common.join(PortalType.values()));
- return type.getInstanceClass();
- });
- private String name;
- private Location location;
- private CompMaterial material;
- private UUID owner;
- private String type;
- private String id;
- @Setter
- private int chargePercentage;
- @Setter
- private boolean used;
- @Setter
- private boolean pauseCharge;
- protected PortalData(final String portalId, @Nullable final Block block, @Nullable final PortalType type) {
- this.id = portalId;
- if (type != null && block != null) {
- this.type = type.name().toLowerCase();
- this.location = block.getLocation();
- this.material = CompMaterial.fromMaterial(block.getType());
- }
- this.loadConfiguration(NO_DEFAULT, FOLDER + "/" + portalId + ".yml");
- }
- /*private PortalData(final String id) {
- this(id, null);
- }
- protected PortalData(final String id, @Nullable final Block block) {
- this.id = id;
- if (block != null) {
- this.material = CompMaterial.fromMaterial(block.getType());
- this.location = block.getLocation();
- }
- this.loadConfiguration(NO_DEFAULT, FOLDER + "/" + id + ".yml");
- }*/
- @Override
- protected void onLoad() {
- if (this.type == null && this.location == null && this.material == null) {
- Valid.checkBoolean(isSet("Type"), "Corrupted portal file: " + this.getFileName() + ", lacks the 'Type' key to determine the type of the portal.");
- final String hash = this.getString("Block");
- final String[] split = hash.split(" \\| ");
- final Location location = SerializeUtil.deserializeLocation(split[0]);
- final CompMaterial material = CompMaterial.valueOf(split[1]);
- this.location = location;
- this.material = material;
- this.type = this.getString("Type");
- }
- this.id = this.getString("Id", "id");
- //this.type = this.getString("Type", "type");
- this.owner = this.get("Owner", UUID.class, new UUID(1, 5));
- this.name = this.getString("Name", "unnamed");
- this.save();
- }
- @Override
- protected void onSave() {
- this.set("Name", this.name);
- this.set("Block", toHash(this.location, this.material));
- this.set("Type", this.type);
- this.set("Id", this.id);
- this.set("Owner", this.owner);
- }
- private String toHash(final Location location, final CompMaterial material) {
- return SerializeUtil.serializeLoc(location) + " | " + material;
- }
- public void increaseChargePercentage() {
- this.chargePercentage = this.chargePercentage + 1;
- }
- public void setName(final String name) {
- this.name = name;
- this.save();
- }
- public void setLocation(final Location location) {
- this.location = location;
- }
- public void setMaterial(final CompMaterial material) {
- this.material = material;
- this.save();
- }
- public void setType(final String type) {
- this.type = type;
- }
- public void setId(final String id) {
- this.id = id;
- }
- public void setOwner(final UUID owner) {
- this.owner = owner;
- }
- public void register(final Block block, final String type, final Player player, final String id) {
- final Location location = block.getLocation();
- this.setId(id);
- this.setType(type);
- this.setOwner(player.getUniqueId());
- this.setLocation(location);
- if (type.equals("targeted")) {
- final PlayerCache cache = PlayerCache.from(player);
- ((Targeted) this).setAuthorizedPlayers(cache.getAuthorizedPlayers());
- }
- HologramUtil.createHologram(location.clone().add(0.5, 4, 0.5), id);
- this.save();
- }
- public void unregister() {
- HologramUtil.removeHologram(this.id);
- removePortalByName(this.id);
- this.save();
- }
- // -----------------------------------------------------------------
- // Static
- // -----------------------------------------------------------------
- public static boolean isRegistered(final Block block) {
- for (final PortalData portalData : getPortals()) {
- if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
- return true;
- }
- return false;
- }
- public static boolean isRegistered(final String portalID) {
- for (final PortalData portalData : getPortals()) {
- if (portalData.getId().equals(portalID))
- return true;
- }
- return false;
- }
- public static List<PortalData> getOwnedPortals(final UUID ownerUUID) {
- final List<PortalData> portalList = new ArrayList<>();
- for (final PortalData portalData : getPortalsOfType("targeted"))
- if (portalData.getOwner().equals(ownerUUID))
- portalList.add(portalData);
- return portalList;
- }
- public static PortalData findByBlock(final Block block) {
- for (final PortalData portalData : getPortals())
- if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
- return portalData;
- return null;
- }
- public static List<PortalData> getPortalsOfType(final String portalType) {
- final List<PortalData> dataList = new ArrayList<>();
- for (final PortalData portalData : getPortals())
- if (portalData.getType().equals(portalType))
- dataList.add(portalData);
- return dataList;
- }
- public static List<Location> getPortalLocations() {
- final List<Location> locations = new ArrayList<>();
- for (final PortalData portalData : getPortals())
- locations.add(portalData.getLocation());
- return locations;
- }
- public static List<Location> getPortalLocationsOfType(final String type) {
- final List<Location> locations = new ArrayList<>();
- for (final PortalData portalData : getPortalsOfType(type))
- locations.add(portalData.getLocation());
- return locations;
- }
- public static boolean isPortalOfType(final Block block, final String type) {
- for (final PortalData portalData : getPortalsOfType(type))
- if (portalData.getLocation().getBlock().getLocation().equals(block.getLocation()))
- return true;
- return false;
- }
- /**
- * @return
- * @see ConfigItems#getItems()
- */
- public static List<? extends PortalData> getPortals() {
- return loadedFiles.getItems();
- }
- /**
- * @return
- * @see ConfigItems#getItemNames()
- */
- public static Set<String> getPortalIDs() {
- return loadedFiles.getItemNames();
- }
- public static PortalData createPortal(final String portalId, final Block block, final PortalType type) {
- return loadedFiles.loadOrCreateItem(portalId, () -> type.instantiate(portalId, block));
- }
- /**
- * @see ConfigItems#loadItems()
- */
- public static void loadPortals() {
- loadedFiles.loadItems();
- }
- public static void removePortalByName(final String portalId) {
- loadedFiles.removeItemByName(portalId);
- }
- /**
- * @see ConfigItems#isItemLoaded(String)
- */
- public static boolean isPortalLoaded(final String id) {
- return loadedFiles.isItemLoaded(id);
- }
- /**
- * @return
- * @see ConfigItems#findItem(String)
- */
- public static PortalData findById(@NonNull final String id) {
- return loadedFiles.findItem(id);
- }
- }
Add Comment
Please, Sign In to add comment