Refactor to make use of the new user bean.
This commit is contained in:
@@ -1,186 +0,0 @@
|
||||
package com.alttd.essentia.configuration;
|
||||
|
||||
import com.alttd.essentia.EssentiaPlugin;
|
||||
import com.alttd.essentia.request.Request;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlayerConfig extends YamlConfiguration {
|
||||
|
||||
private static final Map<Player, PlayerConfig> configs = new HashMap<>();
|
||||
|
||||
public static PlayerConfig getConfig(Player player) {
|
||||
synchronized (configs) {
|
||||
return configs.computeIfAbsent(player, k -> new PlayerConfig(player));
|
||||
}
|
||||
}
|
||||
|
||||
public static void remove(Player player) {
|
||||
synchronized (configs) {
|
||||
configs.remove(player);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll() {
|
||||
synchronized (configs) {
|
||||
configs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private final File file;
|
||||
private final Object saveLock = new Object();
|
||||
private final OfflinePlayer player;
|
||||
@Getter @Setter private Request request;
|
||||
|
||||
private PlayerConfig(Player player) {
|
||||
super();
|
||||
this.player = player;
|
||||
this.file = new File(EssentiaPlugin.instance().getDataFolder(), "PlayerData" + File.separator + player.getUniqueId() + ".yml");
|
||||
reload();
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
load(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void save() {
|
||||
synchronized (saveLock) {
|
||||
try {
|
||||
save(file);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Location getStoredLocation(String path) {
|
||||
if (get(path) == null) {
|
||||
return null;
|
||||
}
|
||||
World world = Bukkit.getWorld(getString(path + ".world", ""));
|
||||
if (world == null) {
|
||||
return null;
|
||||
}
|
||||
double x = getDouble(path + ".x");
|
||||
double y = getDouble(path + ".y");
|
||||
double z = getDouble(path + ".z");
|
||||
float pitch = (float) getDouble(path + ".pitch");
|
||||
float yaw = (float) getDouble(path + ".yaw");
|
||||
return new Location(world, x, y, z, yaw, pitch);
|
||||
}
|
||||
|
||||
void setStoredLocation(String path, Location location) {
|
||||
if (location == null) {
|
||||
set(path, null);
|
||||
save();
|
||||
return;
|
||||
}
|
||||
set(path + ".world", location.getWorld().getName());
|
||||
set(path + ".x", location.getX());
|
||||
set(path + ".y", location.getY());
|
||||
set(path + ".z", location.getZ());
|
||||
set(path + ".pitch", location.getPitch());
|
||||
set(path + ".yaw", location.getYaw());
|
||||
save();
|
||||
}
|
||||
|
||||
public Location getBackLocation(boolean death) {
|
||||
return getStoredLocation(death ? "teleports.death" : "teleports.back");
|
||||
}
|
||||
|
||||
public void setBackLocation(boolean death, Location location) {
|
||||
setStoredLocation(death ? "teleports.death" : "teleports.back", location);
|
||||
}
|
||||
|
||||
public boolean hasHome(String name) {
|
||||
ConfigurationSection section = getConfigurationSection("home." + name);
|
||||
return section != null;
|
||||
}
|
||||
|
||||
public Location getHome(String name) {
|
||||
return getStoredLocation("home." + name);
|
||||
}
|
||||
|
||||
|
||||
public void setHome(String name, Location location) {
|
||||
setStoredLocation("home." + name, location);
|
||||
}
|
||||
|
||||
public int getHomeCount() {
|
||||
ConfigurationSection section = getConfigurationSection("home");
|
||||
if (section == null) {
|
||||
return 0;
|
||||
}
|
||||
return section.getKeys(false).size();
|
||||
}
|
||||
|
||||
public List<String> getMatchingHomeNames(String name) {
|
||||
ConfigurationSection section = getConfigurationSection("home");
|
||||
if (section == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> list = section.getValues(false).keySet().stream()
|
||||
.filter(home -> home.toLowerCase().startsWith(name.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (player.getBedSpawnLocation() != null && "bed".startsWith(name.toLowerCase()))
|
||||
list.add("bed");
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public Map<String, Location> getHomeData() {
|
||||
ConfigurationSection section = getConfigurationSection("home");
|
||||
if (section == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Location> map = new HashMap<>();
|
||||
for (String key : section.getValues(false).keySet()) {
|
||||
map.put(key, getHome(key));
|
||||
}
|
||||
if (player.getBedSpawnLocation() != null)
|
||||
map.put("bed", player.getBedSpawnLocation());
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public List<String> getHomeList() {
|
||||
ConfigurationSection section = getConfigurationSection("home");
|
||||
if (section == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> list = new ArrayList<>(section.getValues(false).keySet());
|
||||
if (player.getBedSpawnLocation() != null)
|
||||
list.add("bed");
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public boolean allowTeleports() {
|
||||
return getBoolean("allow-teleports", true);
|
||||
}
|
||||
|
||||
public void setAllowTeleports(boolean allowTeleports) {
|
||||
set("allow-teleports", allowTeleports);
|
||||
save();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user