Advertisement
JackOUT

Untitled

Jan 7th, 2022
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package games.coob.smp;
  2.  
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import org.bukkit.Location;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.inventory.Inventory;
  8. import org.mineacademy.fo.collection.SerializedMap;
  9. import org.mineacademy.fo.constants.FoConstants;
  10. import org.mineacademy.fo.remain.Remain;
  11. import org.mineacademy.fo.settings.YamlSectionConfig;
  12.  
  13. import javax.annotation.Nullable;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.UUID;
  17.  
  18. @Getter
  19. public final class PlayerCache extends YamlSectionConfig {
  20.  
  21.     private static final Map<UUID, PlayerCache> cacheMap = new HashMap<>();
  22.  
  23.     private final UUID uniqueId;
  24.  
  25.     private final String playerName;
  26.  
  27.     @Getter
  28.     private Location deathLocation;
  29.  
  30.     @Getter
  31.     private Location portalLocation;
  32.  
  33.     @Getter
  34.     private String trackingLocation;
  35.  
  36.     @Getter
  37.     private Player trackPlayer;
  38.  
  39.     @Getter
  40.     @Setter
  41.     private Inventory deathChestInventory;
  42.  
  43.     //
  44.     // Store any custom saveable data here
  45.     //
  46.  
  47.     /*
  48.      * Creates a new player cache (see the bottom)
  49.      */
  50.     private PlayerCache(final String name, final UUID uniqueId) {
  51.         super("Players." + uniqueId.toString());
  52.  
  53.         this.playerName = name;
  54.         this.uniqueId = uniqueId;
  55.  
  56.         this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
  57.     }
  58.  
  59.     /**
  60.      * Automatically called when loading data from disk.
  61.      *
  62.      * @see org.mineacademy.fo.settings.YamlConfig#onLoadFinish()
  63.      */
  64.     @Override
  65.     protected void onLoadFinish() {
  66.         // Load any custom fields here, example:
  67.         // this.chatColor = get("Chat_Color", CompChatColor.class);
  68.         this.deathLocation = getLocation("Death_Location");
  69.         this.portalLocation = getLocation("Portal_Location");
  70.         this.trackingLocation = getString("Tracking_Location");
  71.         this.trackPlayer = get("Track_Player", Player.class);
  72.         //this.deathDrops = get("Death_Drops", ItemStack[].class);
  73.     }
  74.  
  75.     /**
  76.      * Convert data in this class into a map that can
  77.      * be saved into file or written to the databaes.
  78.      *
  79.      * @return
  80.      */
  81.     @Override
  82.     public SerializedMap serialize() {
  83.         final SerializedMap map = new SerializedMap();
  84.  
  85.         map.putIf("Death_Location", deathLocation);
  86.         map.putIf("Portal_Location", portalLocation);
  87.         map.putIf("Tracking_Location", trackingLocation);
  88.         map.putIf("Track_Player", trackPlayer);
  89.  
  90.         return map;
  91.     }
  92.  
  93.     /**
  94.      * Return player from cache if online or null otherwise
  95.      *
  96.      * @return
  97.      */
  98.     @Nullable
  99.     public Player toPlayer() {
  100.         final Player player = Remain.getPlayerByUUID(this.uniqueId);
  101.  
  102.         return player != null && player.isOnline() ? player : null;
  103.     }
  104.  
  105.     /**
  106.      * Remove this cached data from memory if it exists
  107.      */
  108.     public void removeFromMemory() {
  109.         synchronized (cacheMap) {
  110.             cacheMap.remove(this.uniqueId);
  111.         }
  112.     }
  113.  
  114.     @Override
  115.     public String toString() {
  116.         return "PlayerCache{" + this.playerName + ", " + this.uniqueId + "}";
  117.     }
  118.  
  119.     public void setDeathLocation(final Location deathLocation) {
  120.         this.deathLocation = deathLocation;
  121.  
  122.         save();
  123.     }
  124.  
  125.     public void setPortalLocation(final Location portalLocation) {
  126.         this.portalLocation = portalLocation;
  127.  
  128.         save();
  129.     }
  130.  
  131.     public void setTrackingLocation(final String trackingLocation) {
  132.         this.trackingLocation = trackingLocation;
  133.  
  134.         save();
  135.     }
  136.  
  137.     public void setTrackPlayer(final Player trackPlayer) {
  138.         this.trackPlayer = trackPlayer;
  139.  
  140.         save();
  141.     }
  142.  
  143.     /* ------------------------------------------------------------------------------- */
  144.     /* Static access */
  145.     /* ------------------------------------------------------------------------------- */
  146.  
  147.     /**
  148.      * Return or create new player cache for the given player
  149.      *
  150.      * @param player
  151.      * @return
  152.      */
  153.     public static PlayerCache from(final Player player) {
  154.         synchronized (cacheMap) {
  155.             final UUID uniqueId = player.getUniqueId();
  156.             final String playerName = player.getName();
  157.  
  158.             PlayerCache cache = cacheMap.get(uniqueId);
  159.  
  160.             if (cache == null) {
  161.                 cache = new PlayerCache(playerName, uniqueId);
  162.  
  163.                 cacheMap.put(uniqueId, cache);
  164.             }
  165.  
  166.             return cache;
  167.         }
  168.     }
  169.  
  170.     /**
  171.      * Clear the entire cache map
  172.      */
  173.     public static void clear() {
  174.         synchronized (cacheMap) {
  175.             cacheMap.clear();
  176.         }
  177.     }
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement