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

255 lines
7.4 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;
Events (#4) * Refactor island and island player classes to interfaces Refactored the existing Island and IslandPlayer classes to interfaces and created the implementation classes, IslandImpl and IslandPlayerImpl. * Add event handling for player island actions Implemented new event classes for handling player activities related to islands. These include IslandPlayerJoinEvent, IslandPlayerLeaveEvent, IslandPlayerJoinIslandEvent, and IslandPlayerLeaveIslandEvent, facilitating better tracking and handling of player actions on islands for other plugins. * Implement Island interfaces in IslandImpl and IslandPlayerImpl Updated the IslandImpl and IslandPlayerImpl classes to implement the Island and IslandPlayer interfaces respectively. * Refactor IslandPlayer Interface Removed player() method and added islandOwner() method in the IslandPlayer interface. This change reflects way the IslandPlayerImpl currently works. * Implement event triggers for island join/leave and server join/leave events Added triggers for IslandPlayer events in the relevant island management methods. Also, modified IslandPlayerJoinEvent and IslandPlayerLeaveEvent to remove unnecessary references to Island-object. * Reverting in events * Replace interfaces with records in event parameters The commit replaces the usage of interfaces Island and IslandPlayer with record classes in event parameters. It also adds methods to convert instances to respective records in Island and IslandPlayer classes. The interfaces are then removed as they are no longer needed. * Replace interfaces with records in event parameters The commit replaces the usage of interfaces Island and IslandPlayer with record classes in event parameters. It also adds methods to convert instances to respective records in Island and IslandPlayer classes. The interfaces are then removed as they are no longer needed.
2024-02-19 02:52:17 +00:00
import com.alttd.cometskyblock.records.IslandRecord;
2024-02-10 21:12:15 +00:00
import com.alttd.cometskyblock.request.Request;
2024-01-25 14:49:04 +00:00
import lombok.Getter;
2024-02-10 21:12:15 +00:00
import lombok.Setter;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.Location;
2024-02-09 18:46:39 +00:00
import org.bukkit.World;
2024-02-04 20:44:57 +00:00
import org.bukkit.configuration.file.YamlConfiguration;
2024-02-09 18:46:39 +00:00
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
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
}
public static Island loadIslandFromFile(File file) {
return new Island(file);
}
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-10 21:12:15 +00:00
@Getter @Setter private Request request;
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();
}
private Island(File file) {
this.islandUUID = Island.NILL_UUID;
this.file = file;
reload();
}
public static Collection<Island> getIslands() {
return configs.values();
}
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) {
IslandData.updateIsland(new IslandData(islandId(), islandName, level()));
2024-02-04 20:44:57 +00:00
set("island.islandname", islandName);
save();
}
public int level() {
2024-02-11 15:14:04 +00:00
return getInt("island.level", 0);
2024-02-04 20:44:57 +00:00
}
public void level(int level) {
IslandData.updateIsland(new IslandData(islandId(), islandName(), level));
set("island.level", level);
2024-02-04 20:44:57 +00:00
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());
String islandName = islandName();
if ((islandName == null || islandName.isBlank()) && !uuid.equals(NILL_UUID)) {
islandName(Bukkit.getOfflinePlayer(uuid).getName() + "'s island");
}
2024-02-04 20:44:57 +00:00
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()));
save();
2024-02-04 20:44:57 +00:00
}
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) {
2024-02-10 20:36:22 +00:00
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(uuid);
islandPlayer.islandId(islandId());
islandPlayer.islandUUID(islandUUID());
2024-02-04 20:44:57 +00:00
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;
}
public int cobblegenLevel() {
return getInt("island.generator.level", 0);
}
2024-02-09 18:46:39 +00:00
public void cobblegenLevel(int level) {
set("island.generator.level", level);
save();
}
2024-02-09 18:46:39 +00:00
public int worldBorderLevel() {
return getInt("island.worldborder.level", 0);
}
public void worldBorderLevel(int level) {
set("island.worldborder.level", level);
save();
}
2024-02-09 18:46:39 +00:00
public void teleport(Player player) {
World islandWorld = CometSkyBlockPlugin.instance().worldGenerator().loadIslandWorld(worldName());
if (islandWorld == null) {
player.sendRichMessage("<red>Could not load islandWorld. Contact an administrator");
return;
}
teleport(player, islandWorld.getSpawnLocation());
}
2024-02-09 18:46:39 +00:00
public void teleport(Player player, Location location) {
PotionEffect potionEffectBlindness = new PotionEffect(PotionEffectType.BLINDNESS, 600, 1);
PotionEffect potionEffectNightVision = new PotionEffect(PotionEffectType.NIGHT_VISION, 600, 1);
player.addPotionEffect(potionEffectBlindness);
player.addPotionEffect(potionEffectNightVision);
World islandWorld = CometSkyBlockPlugin.instance().worldGenerator().loadIslandWorld(location.getWorld().getName());
if (islandWorld == null) {
player.sendRichMessage("<red>Could not load islandWorld. Contact an administrator");
player.removePotionEffect(PotionEffectType.BLINDNESS);
player.removePotionEffect(PotionEffectType.NIGHT_VISION);
return;
}
player.teleportAsync(location).whenComplete((b, e) -> {
player.removePotionEffect(PotionEffectType.BLINDNESS);
player.removePotionEffect(PotionEffectType.NIGHT_VISION);
});
2024-02-09 18:46:39 +00:00
}
public void broadCast(String message, TagResolver placeholder) {
Player islandOwner = Bukkit.getPlayer(owner());
if (islandOwner != null)
islandOwner.sendMiniMessage(message, placeholder);
for (UUID uuid : members()) {
Player islandMember = Bukkit.getPlayer(uuid);
if (islandMember == null)
continue;
islandMember.sendMiniMessage(message, placeholder);
}
}
Events (#4) * Refactor island and island player classes to interfaces Refactored the existing Island and IslandPlayer classes to interfaces and created the implementation classes, IslandImpl and IslandPlayerImpl. * Add event handling for player island actions Implemented new event classes for handling player activities related to islands. These include IslandPlayerJoinEvent, IslandPlayerLeaveEvent, IslandPlayerJoinIslandEvent, and IslandPlayerLeaveIslandEvent, facilitating better tracking and handling of player actions on islands for other plugins. * Implement Island interfaces in IslandImpl and IslandPlayerImpl Updated the IslandImpl and IslandPlayerImpl classes to implement the Island and IslandPlayer interfaces respectively. * Refactor IslandPlayer Interface Removed player() method and added islandOwner() method in the IslandPlayer interface. This change reflects way the IslandPlayerImpl currently works. * Implement event triggers for island join/leave and server join/leave events Added triggers for IslandPlayer events in the relevant island management methods. Also, modified IslandPlayerJoinEvent and IslandPlayerLeaveEvent to remove unnecessary references to Island-object. * Reverting in events * Replace interfaces with records in event parameters The commit replaces the usage of interfaces Island and IslandPlayer with record classes in event parameters. It also adds methods to convert instances to respective records in Island and IslandPlayer classes. The interfaces are then removed as they are no longer needed. * Replace interfaces with records in event parameters The commit replaces the usage of interfaces Island and IslandPlayer with record classes in event parameters. It also adds methods to convert instances to respective records in Island and IslandPlayer classes. The interfaces are then removed as they are no longer needed.
2024-02-19 02:52:17 +00:00
public IslandRecord toRecord() {
return new IslandRecord(islandId(), islandName(), level());
}
2024-01-18 07:58:21 +00:00
}