package hdvtdev.blockAndSeek; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; public class BlockAndSeekMap implements ConfigurationSerializable { private List spawn; private List lobby; private int duration; private int minPlayers; private int maxPlayers; private List blocks; public BlockAndSeekMap() { } public BlockAndSeekMap(List spawn, List lobby, int duration, int minPlayers, int maxPlayers, List blocks) { this.spawn = spawn; this.lobby = lobby; this.duration = duration; this.minPlayers = minPlayers; this.maxPlayers = maxPlayers; this.blocks = blocks; } public List getSpawn() { return spawn; } public Location getSpawnLocation(@Nullable World world) { return new Location(world, spawn.getFirst(), spawn.get(1), spawn.get(2)); } public void setSpawn(List spawn) { this.spawn = spawn; } public List getLobby() { return lobby; } public Location getLobbyLocation(@Nullable World world) { return new Location(world, lobby.getFirst(), lobby.get(1), lobby.get(2)); } public void setLobby(List lobby) { this.lobby = lobby; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public int getMinPlayers() { return minPlayers; } public void setMinPlayers(int minPlayers) { this.minPlayers = minPlayers; } public int getMaxPlayers() { return maxPlayers; } public void setMaxPlayers(int maxPlayers) { this.maxPlayers = maxPlayers; } public List getBlocks() { return blocks; } public void setBlocks(List blocks) { this.blocks = blocks; } public boolean isReady() { return !spawn.isEmpty() && !lobby.isEmpty() && duration > 0 && !blocks.isEmpty() && minPlayers > 0 && maxPlayers > 0; } public static BlockAndSeekMap defaultMap() { return new BlockAndSeekMap(List.of(), List.of(), 0, 0, 0, List.of()); } public Set check() { Set status = new HashSet<>(); if (spawn.isEmpty()) status.add(MapStatus.SPAWN_REQUIRED); if (lobby.isEmpty()) status.add(MapStatus.LOBBY_REQUIRED); if (duration <= 0) status.add(MapStatus.DURATION_REQUIRED); if (minPlayers <= 0) status.add(MapStatus.MIN_PLAYERS_REQUIRED); if (maxPlayers <= 0) status.add(MapStatus.MAX_PLAYERS_REQUIRED); if (blocks.isEmpty()) status.add(MapStatus.BLOCKS_REQUIRED); return status; } @Override public @NotNull Map serialize() { Map map = new HashMap<>(); map.put("spawn", spawn); map.put("lobby", lobby); map.put("duration", duration); map.put("min-players", minPlayers); map.put("max-players", maxPlayers); List> serBlocks = new ArrayList<>(); for (Block block : blocks) { serBlocks.add(block.toMap()); } map.put("blocks", serBlocks); return map; } @NotNull @SuppressWarnings("unchecked") public static BlockAndSeekMap deserialize(@NotNull Map args) { BlockAndSeekMap map = new BlockAndSeekMap(); map.setSpawn((List) args.get("spawn")); map.setLobby((List) args.get("lobby")); map.setDuration((int) args.get("duration")); map.setMinPlayers((int) args.get("min-players")); map.setMaxPlayers((int) args.get("max-players")); map.setBlocks(((List>) args.get("blocks")).stream().map(Block::fromMap).toList()); return map; } @Override public String toString() { return String.format("BlockAndSeekMap[spawn=%s, lobby=%s, duration=%s, minPlayers=%s, maxPlayers=%s, blocks=%s]", spawn, lobby, duration, minPlayers, maxPlayers, blocks); } public enum MapStatus { SPAWN_REQUIRED, LOBBY_REQUIRED, DURATION_REQUIRED, BLOCKS_REQUIRED, MIN_PLAYERS_REQUIRED, MAX_PLAYERS_REQUIRED } public record Block(@NotNull ItemStack block, int chance) { public Map toMap() { Map map = new HashMap<>(); map.put("block", block.getType().name()); map.put("chance", chance); return map; } public static Block fromMap(Map map) { return new Block(new ItemStack(Material.valueOf(((String) map.get("block")).toUpperCase())), (int) map.get("chance")); } } }