2019-08-25 14:07:06 +00:00
|
|
|
package me.mrCookieSlime.Slimefun.api;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2019-08-25 15:35:19 +00:00
|
|
|
import java.util.Optional;
|
2019-08-25 14:07:06 +00:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
2019-08-25 15:35:19 +00:00
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-08-25 14:07:06 +00:00
|
|
|
|
|
|
|
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
|
|
|
|
import me.mrCookieSlime.Slimefun.Objects.Research;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that can store a Player's Research Profile for caching
|
|
|
|
*
|
|
|
|
* @author TheBusyBiscuit
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class PlayerProfile {
|
|
|
|
|
|
|
|
public static Map<UUID, PlayerProfile> profiles = new HashMap<>();
|
|
|
|
|
|
|
|
private UUID uuid;
|
|
|
|
private Config cfg;
|
|
|
|
private boolean dirty = false;
|
|
|
|
private boolean markedForDeletion = false;
|
|
|
|
|
|
|
|
private Set<Research> researches = new HashSet<>();
|
2019-08-25 15:35:19 +00:00
|
|
|
private Map<Integer, BackpackInventory> backpacks = new HashMap<>();
|
2019-08-25 14:07:06 +00:00
|
|
|
|
|
|
|
private PlayerProfile(UUID uuid) {
|
|
|
|
this.uuid = uuid;
|
|
|
|
cfg = new Config(new File("data-storage/Slimefun/Players/" + uuid.toString() + ".yml"));
|
|
|
|
|
|
|
|
for (Research research: Research.list()) {
|
|
|
|
if (cfg.contains("researches." + research.getID())) researches.add(research);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:35:19 +00:00
|
|
|
protected Config getConfig() {
|
|
|
|
return cfg;
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:07:06 +00:00
|
|
|
public UUID getUUID() {
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:21:06 +00:00
|
|
|
/**
|
|
|
|
* This method returns whether the Player has logged off.
|
|
|
|
* If this is true, then the Profile can be removed from RAM.
|
|
|
|
*
|
|
|
|
* @return Whether the Profile is marked for deletion
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public boolean isMarkedForDeletion() {
|
|
|
|
return markedForDeletion;
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:21:06 +00:00
|
|
|
/**
|
|
|
|
* This method returns whether the Profile has unsaved changes
|
|
|
|
*
|
|
|
|
* @return Whether there are unsaved changes
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public boolean isDirty() {
|
|
|
|
return dirty;
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:21:06 +00:00
|
|
|
/**
|
|
|
|
* This method will save the Player's Researches and Backpacks to the hard drive
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public void save() {
|
2019-08-25 15:35:19 +00:00
|
|
|
for (BackpackInventory backpack: backpacks.values()) {
|
|
|
|
backpack.save();
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:07:06 +00:00
|
|
|
cfg.save();
|
|
|
|
dirty = false;
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:21:06 +00:00
|
|
|
/**
|
|
|
|
* This method sets the Player's "researched" status for this Research.
|
|
|
|
* Use the boolean to unlock or lock the Research
|
|
|
|
*
|
|
|
|
* @param research The Research that should be unlocked or locked
|
|
|
|
* @param unlock Whether the Research should be unlocked or locked
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public void setResearched(Research research, boolean unlock) {
|
|
|
|
dirty = true;
|
|
|
|
|
|
|
|
if (unlock) {
|
|
|
|
cfg.setValue("researches." + research.getID(), true);
|
|
|
|
researches.add(research);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cfg.setValue("researches." + research.getID(), null);
|
|
|
|
researches.remove(research);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:21:06 +00:00
|
|
|
/**
|
|
|
|
* This method returns whether the Player has unlocked the given Research
|
|
|
|
*
|
|
|
|
* @param research The Research that is being queried
|
|
|
|
* @return Whether this Research has been unlocked
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public boolean hasUnlocked(Research research) {
|
|
|
|
return !research.isEnabled() || researches.contains(research);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-25 15:35:19 +00:00
|
|
|
* This Method will return all Researches that this Player has unlocked
|
2019-08-25 14:07:06 +00:00
|
|
|
*
|
|
|
|
* @return A Hashset<Research> of all Researches this Player has unlocked
|
|
|
|
*/
|
|
|
|
public Set<Research> getResearches() {
|
|
|
|
return researches;
|
|
|
|
}
|
2019-08-25 15:35:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this method if the Player has left.
|
|
|
|
* The profile can then be removed from RAM.
|
|
|
|
*/
|
2019-08-25 14:07:06 +00:00
|
|
|
public void markForDeletion() {
|
|
|
|
this.markedForDeletion = true;
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:35:19 +00:00
|
|
|
/**
|
|
|
|
* Call this method if this Profile has unsaved changes.
|
|
|
|
*/
|
|
|
|
public void markDirty() {
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BackpackInventory createBackpack(int size) {
|
|
|
|
IntStream stream = IntStream.iterate(0, i -> i + 1).filter(i -> !cfg.contains("backpacks." + i + ".size"));
|
|
|
|
int id = stream.findFirst().getAsInt();
|
|
|
|
|
|
|
|
BackpackInventory backpack = new BackpackInventory(this, id, size);
|
|
|
|
backpacks.put(id, backpack);
|
|
|
|
|
|
|
|
return backpack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BackpackInventory getBackpack(int id) {
|
|
|
|
BackpackInventory backpack = backpacks.get(id);
|
|
|
|
|
|
|
|
if (backpack != null) return backpack;
|
|
|
|
else {
|
|
|
|
backpack = new BackpackInventory(this, id);
|
|
|
|
backpacks.put(id, backpack);
|
|
|
|
return backpack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 14:07:06 +00:00
|
|
|
public static PlayerProfile fromUUID(UUID uuid) {
|
|
|
|
PlayerProfile profile = profiles.get(uuid);
|
|
|
|
|
|
|
|
if (profile == null) {
|
|
|
|
profile = new PlayerProfile(uuid);
|
|
|
|
profiles.put(uuid, profile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
profile.markedForDeletion = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isLoaded(UUID uuid) {
|
|
|
|
return profiles.containsKey(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Iterator<PlayerProfile> iterator() {
|
|
|
|
return profiles.values().iterator();
|
|
|
|
}
|
2019-08-25 15:35:19 +00:00
|
|
|
|
|
|
|
public static BackpackInventory getBackpack(ItemStack item) {
|
|
|
|
if (item == null || !item.hasItemMeta() || !item.getItemMeta().hasLore()) return null;
|
|
|
|
|
|
|
|
Optional<Integer> id = Optional.empty();
|
|
|
|
String uuid = "";
|
|
|
|
|
|
|
|
for (String line: item.getItemMeta().getLore()) {
|
|
|
|
if (line.startsWith(ChatColor.translateAlternateColorCodes('&', "&7ID: ")) && line.contains("#")) {
|
|
|
|
try {
|
|
|
|
id = Optional.of(Integer.parseInt(line.split("#")[1]));
|
|
|
|
uuid = line.split("#")[0].replace(ChatColor.translateAlternateColorCodes('&', "&7ID: "), "");
|
|
|
|
} catch(NumberFormatException x) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id.isPresent()) {
|
|
|
|
return PlayerProfile.fromUUID(UUID.fromString(uuid)).getBackpack(id.get());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 14:07:06 +00:00
|
|
|
|
|
|
|
}
|