CometSkyBlock/plugin/src/main/java/com/alttd/cometskyblock/island/Island.java

154 lines
3.7 KiB
Java
Raw Normal View History

2024-01-18 07:58:21 +00:00
package com.alttd.cometskyblock.island;
2024-02-04 20:44:57 +00:00
import com.alttd.cometskyblock.CometSkyBlockPlugin;
2024-01-25 14:49:04 +00:00
import lombok.Getter;
2024-02-04 20:44:57 +00:00
import org.bukkit.configuration.file.YamlConfiguration;
2024-01-25 14:49:04 +00:00
2024-02-04 20:44:57 +00:00
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
2024-01-18 07:58:21 +00:00
2024-02-04 20:44:57 +00:00
public class Island extends YamlConfiguration {
2024-01-18 07:58:21 +00:00
2024-02-04 20:44:57 +00:00
private static final Map<UUID, Island> configs = new HashMap<>();
public static final UUID NILL_UUID = new UUID(0L, 0L);
2024-01-18 07:58:21 +00:00
2024-02-04 20:44:57 +00:00
public static Island getIsland(UUID uuid) {
synchronized (configs) {
return configs.computeIfAbsent(uuid, k -> new Island(uuid));
}
2024-01-18 07:58:21 +00:00
}
2024-02-04 20:44:57 +00:00
public static void remove(UUID uuid) {
synchronized (configs) {
configs.remove(uuid);
}
2024-01-25 14:49:04 +00:00
}
2024-02-04 20:44:57 +00:00
public static void removeAll() {
synchronized (configs) {
configs.clear();
}
2024-01-25 14:49:04 +00:00
}
2024-02-04 20:44:57 +00:00
private final File file;
private final Object saveLock = new Object();
@Getter private final UUID islandUUID;
2024-02-04 20:44:57 +00:00
private Island(UUID uuid) {
super();
this.islandUUID = uuid;
this.file = new File(CometSkyBlockPlugin.instance().getDataFolder(), "IslandData" + File.separator + uuid + ".yml");
reload();
}
2024-02-04 20:44:57 +00:00
private void reload() {
synchronized (saveLock) {
try {
load(file);
} catch (Exception ignore) {
}
}
2024-02-04 20:44:57 +00:00
}
2024-02-04 20:44:57 +00:00
private void save() {
synchronized (saveLock) {
try {
save(file);
} catch (Exception ignore) {
}
}
2024-02-04 20:44:57 +00:00
}
2024-02-04 20:44:57 +00:00
public int islandId() {
return getInt("island.id", 0);
}
2024-02-04 20:44:57 +00:00
public void islandId(int id) {
set("island.id", id);
save();
}
public String worldName() {
return getString("island.worldname");
}
public void worldName(String worldName) {
set("island.worldname", worldName);
save();
}
public String islandName() {
return getString("island.islandname");
}
public void islandName(String islandName) {
set("island.islandname", islandName);
save();
}
public int level() {
return getInt("island.id", 0);
}
public void level(int id) {
set("island.level", id);
save();
}
public UUID owner() {
String string = getString("island.owner");
if (string == null || string.isEmpty())
return NILL_UUID;
return UUID.fromString(string);
}
public void owner(UUID uuid) {
set("island.owner", uuid.toString());
save();
}
public List<UUID> members() {
List<String> members = getStringList("island.members");
return members.stream()
.map(this::stringToUUID)
.collect(Collectors.toList());
}
public void members(List<UUID> members) {
set("island.members", members.stream()
.map(uuid -> uuid == null ? "null" : uuid.toString())
.collect(Collectors.toList()));
}
private String uuidToString(UUID uuid) {
return (uuid == null ? NILL_UUID : uuid).toString();
}
private UUID stringToUUID(String s) {
return s == null || s.isEmpty() ? NILL_UUID : UUID.fromString(s);
}
public boolean canBuild(UUID uuid) {
return owner().equals(uuid) || members().contains(uuid);
}
public void addMember(UUID uuid) {
List<UUID> list = members();
list.add(uuid);
members(list);
}
public void removeMember(UUID uuid) {
List<UUID> list = members();
list.remove(uuid);
members(list);
}
// TODO - Island settings
public boolean visitNeedsRequest() {
return true;
}
2024-01-18 07:58:21 +00:00
}