diff --git a/src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java b/src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java index 3d827945..9eabf804 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java +++ b/src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java @@ -3,22 +3,19 @@ import com.alpsbte.plotsystem.PlotSystem; import com.alpsbte.plotsystem.core.holograms.connector.DecentHologramPagedDisplay; import com.alpsbte.plotsystem.core.system.Builder; -import com.alpsbte.plotsystem.core.system.tutorial.AbstractTutorial; +import com.alpsbte.plotsystem.utils.enums.PlotDifficulty; import com.alpsbte.plotsystem.utils.enums.Status; import com.alpsbte.plotsystem.utils.io.ConfigPaths; import com.alpsbte.plotsystem.utils.io.ConfigUtil; import eu.decentsoftware.holograms.api.DHAPI; import eu.decentsoftware.holograms.api.holograms.Hologram; -import it.unimi.dsi.fastutil.Hash; -import net.md_5.bungee.api.ChatMessageType; +import org.apache.commons.lang3.StringUtils; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.scheduler.BukkitRunnable; -import java.sql.Array; import java.sql.SQLException; import java.text.DecimalFormat; import java.util.*; @@ -31,8 +28,9 @@ public class CountryBoard extends DecentHologramPagedDisplay implements Hologram Builder.DatabaseEntry, Builder.DatabaseEntry>> projectsData = null; private ArrayList>>> plotEntries = null; - private static final String contentSeparator = "§7---------------"; + ArrayList>>>> plotByCities = null; + private static final String contentSeparator = "§7------------------------"; private int currentPage = 0; protected CountryBoard() { @@ -42,7 +40,7 @@ protected CountryBoard() { try { this.projectsData = Builder.getProjectsSorted(); - this.plotEntries = Builder.getPlotsSorted(); + this.plotByCities = Builder.getPlotsSorted(); } catch (SQLException ex) { PlotSystem.getPlugin().getLogger().log(Level.SEVERE, "An error occurred while reading country board content", ex); @@ -82,7 +80,7 @@ public ItemStack getItem() { @Override public String getTitle(UUID playerUUID) { - return "§b§lCOUNTRY BOARD §6§l"; + return "§7[§eID§7] - [§bStatus§7] - [§uDifficulty§7]"; } @Override @@ -93,7 +91,8 @@ public List> getContent(UUID var1) { @Override public List>> getPagedHeader(UUID playerUUID) { List>> header = new ArrayList<>(); - if(projectsData == null) super.getPagedHeader(playerUUID); + if(projectsData == null) return super.getPagedHeader(playerUUID); + for (Builder.DatabaseEntry< Builder.DatabaseEntry, Builder.DatabaseEntry> cityProject @@ -101,93 +100,146 @@ public List>> getPagedHeader(UUID playerUUID) { ) { header.add(Arrays.asList( new ItemLine(this.getItem()), - new TextLine(this.getTitle(playerUUID)), - new TextLine("----- " + cityProject.getKey().getValue() + " -----") + new TextLine("§b§lCOUNTRY BOARD §6§l"), + new TextLine(contentSeparator), + new TextLine("§6§l[" + cityProject.getKey().getValue() + "§7§l: §r§3" + cityProject.getValue().getValue() + "§6§l]"), + new TextLine(contentSeparator), + new TextLine(this.getTitle(playerUUID)) )); } - return header; } @Override - public List>> getPagedContent(UUID playerUUID) { + public List>> getPagedFooter(UUID playerUUID) { + return Collections.singletonList(Arrays.asList( + new TextLine(contentSeparator), + new TextLine("§6<<< click to swap pages >>>"), + new TextLine(contentSeparator) + )); + } + + @Override + public List>> getPagedContent(UUID playerUUID) { List>> content = new ArrayList<>(); - if(projectsData == null) super.getPagedHeader(playerUUID); + if(projectsData == null) return super.getPagedContent(playerUUID); + for (Builder.DatabaseEntry< Builder.DatabaseEntry, Builder.DatabaseEntry> cityProject : projectsData - ) { + ) { // For each registered city projects + ArrayList> lines = new ArrayList<>(); + int index = 0; + for (int i = 0; i < 10; i++) lines.add(new HologramRegister.CountryBoardPositionLine("&8#" + 0,null, null)); + + plotEntry: // For each plotEntries in the city project for (Builder.DatabaseEntry>> plots : plotEntries) + ArrayList>>> plotEntries + : plotByCities) { - Bukkit.getLogger().log(Level.INFO, "Looking for " + cityProject.getValue().getKey() + " " + plots.getKey()); - ArrayList> lines = new ArrayList<>(); + if(!cityProject.getValue().getKey().equals(plotEntries.getKey())) continue; - for (int i = 0; i < 10; i++) { - lines.add(new HologramRegister.CountryBoardPositionLine("§8#" + 0,null, null)); - } + for(Map.Entry> plot : plotStatusSorter(plotEntries.getValue())) { + if(index >= 10) break plotEntry; - int index = 0; - if(cityProject.getValue().getKey().equals(plots.getKey())) - { - HashMap sortedEntries = new HashMap<>(); + Status status = Status.valueOf(plot.getValue().getKey()); + PlotDifficulty difficulty = PlotDifficulty.valueOf(plot.getValue().getValue()); - for(Builder.DatabaseEntry entry : plots.getValue()) { - sortedEntries.put(entry.getKey(), Status.valueOf(entry.getValue())); - Bukkit.getLogger().log(Level.INFO, "Found " + entry.getKey() + " " + entry.getValue()); + String statusColor = plot.getValue().getKey(); + switch (status) { + case unclaimed: statusColor = "&7" + statusColor + " "; break; + case unfinished: statusColor = "&6" + statusColor; break; + case unreviewed: statusColor = "&3" + statusColor; break; + case completed: statusColor = "&a" + statusColor + " "; break; } - - for(Map.Entry plot : plotStatusSorter(sortedEntries)) { - String statusText = plot.getValue().toString(); - switch (plot.getValue()) { - case unclaimed: statusText = "&8" + statusText; - case unfinished: statusText = "&6" + statusText; - case unreviewed: statusText = "&3" + statusText; - case completed: statusText = "&a" + statusText; - } - lines.set(index, new HologramRegister.CountryBoardPositionLine("§e#" + plot.getKey(), - cityProject.getValue().getValue(), // city name - statusText)); // city status - index++; + String difficultyColor = ""; + switch (difficulty) { + case EASY: difficultyColor += "&a"; break; + case MEDIUM: difficultyColor += "&6"; break; + case HARD: difficultyColor += "&c"; break; } + PositionLineWithSpacing innerLine = new PositionLineWithSpacing(this.getTitle(playerUUID), + "&e#" + plot.getKey(), // id + statusColor, // city status + difficultyColor + "[" + plot.getValue().getValue() + "]"); // difficulty + + lines.set(index, new TextLine(innerLine.getLine())); // city difficulty + index++; } - content.add(lines); } - + content.add(lines); } return content; } - private static ArrayList> plotStatusSorter(HashMap entries) { + private static ArrayList>> + plotStatusSorter(ArrayList>> plotEntries) + { + // Prepare entries with Hashmap + HashMap> entriesMap = new HashMap<>(); + for(Builder.DatabaseEntry> entry : plotEntries) + entriesMap.put(entry.getKey(), entry.getValue()); + // Create an ArrayList and insert all hashmap key-value pairs. - ArrayList> sortEntries = new ArrayList<>(entries.entrySet()); + ArrayList>> sortedEntries = new ArrayList<>(entriesMap.entrySet()); // Sort the Arraylist using a custom comparator. - sortEntries.sort(new Comparator>() { + sortedEntries.sort(new Comparator>>() { @Override - public int compare(Map.Entry o1, Map.Entry o2) { + public int compare(Map.Entry> o1, + Map.Entry> o2) { + Status status1 = Status.valueOf(o1.getValue().getKey()); + Status status2 = Status.valueOf(o2.getValue().getKey()); + if (o1.getValue() == o2.getValue()) return o1.getKey().compareTo(o2.getKey()); - return Integer.compare(o1.getValue().ordinal(), o2.getValue().ordinal()); + return Integer.compare(status1.ordinal(), status2.ordinal()); } }); - return sortEntries; + return sortedEntries; + } + + private static class PositionLineWithSpacing extends HologramRegister.CountryBoardPositionLine { + private final int width; - // Create a LinkedHashMap. - // Map sortedMap = new LinkedHashMap<>(); + public PositionLineWithSpacing(String spacing, String id, String status, String difficulty) { + super(id, status, difficulty); + String split = spacing.replaceAll("[§&][a-zA-Z0-9]", ""); + Bukkit.getLogger().log(Level.INFO, "Splited: >" + split); + this.width = split.length(); + } - // Iterate over the ArrayList and insert the key-value pairs into LinkedHashMap. -// for (int i = 0; i hashmap){ -// for (Map.Entry entry : hashmap.entrySet()) { -// System.out.println("Key : " + entry.getKey() + " \t value : " + entry.getValue()); -// } -// } + public String getLine() { + Bukkit.getLogger().log(Level.INFO, "Splited: " + super.getLine()); + String[] split = super.getLine().split("-"); + + int spaceLeft = width - super.getLine().length(); + int difficultyText = split[split.length - 1].length(); + int difficultySpacing = 6 - difficultyText; + if(difficultySpacing > 0) spaceLeft = spaceLeft - difficultySpacing; + + String result = ""; + for (String text : split) { + if(text.equals(split[split.length - 2])) { + String frontSpacing = StringUtils.repeat(" ", spaceLeft); + String backSpacing = StringUtils.repeat(" ", difficultySpacing); + result = result.concat(text.concat(frontSpacing + "- " + backSpacing)); + } + else if(text.equals(split[split.length - 1])) result = result.concat(text); + else result = result.concat(text.concat(" -")); + } + Bukkit.getLogger().log(Level.INFO, "Result: " + result); + + return result; + } } @Override @@ -227,5 +279,5 @@ public String getZPath() { public boolean hasViewPermission(UUID uuid) { return true; } - } + diff --git a/src/main/java/com/alpsbte/plotsystem/core/holograms/HologramRegister.java b/src/main/java/com/alpsbte/plotsystem/core/holograms/HologramRegister.java index 55120697..0ec05d8f 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/holograms/HologramRegister.java +++ b/src/main/java/com/alpsbte/plotsystem/core/holograms/HologramRegister.java @@ -44,8 +44,8 @@ public LeaderboardPositionLine(int position, String username, int score) { } public static class CountryBoardPositionLine extends DecentHologramDisplay.TextLine { - public CountryBoardPositionLine(String id, String city, String status) { - super(id + " " + (city != null ? "§a" + city : "§8No plot left") + " §7- §b" + (status != null ? status : "§8Null")); + public CountryBoardPositionLine(String id, String status, String difficulty) { + super(id + "§7 - " + (status != null ? "§a" + status : "§8No plot left") + " §7- " + (difficulty != null ? difficulty : "§8Unknown")); } } } \ No newline at end of file diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/Builder.java b/src/main/java/com/alpsbte/plotsystem/core/system/Builder.java index f4b9ee82..e7347cd4 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/Builder.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/Builder.java @@ -311,36 +311,56 @@ public static ArrayList, DatabaseEn } } - public static ArrayList>>> getPlotsSorted() throws SQLException { + /** + * Select city_id as "city_id", plot id as "id", "difficulty_id", "difficulty" + * Ordered by each city project id, do this will be incrementing from 1 + *
+     * {@code
+     *   SELECT city_projects.id AS city_id, difficulties.name AS difficulty FROM plotsystem_city_projects AS city_projects
+     *   RIGHT JOIN plotsystem_plots AS plots ON city_projects.id = plots.city_project_id
+     *   INNER JOIN plotsystem_difficulties AS difficulties ON difficulty_id = difficulties.id
+     *   ORDER BY city_projects.id
+     * }
SQL Query statement + * @return A List of every City ID containing a List of all plot entries in the city project. + * @throws SQLException If Database connection is lost + */ + public static ArrayList>>>> getPlotsSorted() throws SQLException { String query = "SELECT city_projects.id AS city_id, " - + "city_projects.name AS city_name, plots.status, plots.id " + + "plots.status, plots.id, plots.difficulty_id AS difficulty_id, " + + "difficulties.name AS difficulty " + "FROM plotsystem_city_projects AS city_projects\n" + "RIGHT JOIN plotsystem_plots AS plots " + "ON city_projects.id = plots.city_project_id\n" + + "INNER JOIN plotsystem_difficulties AS difficulties " + + "ON difficulty_id = difficulties.id\n" + "ORDER BY city_projects.id"; try(ResultSet rs = DatabaseConnection.createStatement(query).executeQuery()) { - ArrayList>>> result = new ArrayList<>(); + ArrayList> // the plot entry containing a pair of Status and Difficulty + >>> result = new ArrayList<>(); int currentCursor = 0; - ArrayList> entries = new ArrayList<>(); + ArrayList>> entries = new ArrayList<>(); while(rs.next()) { int currentCity = rs.getInt("city_id"); - if(currentCity == currentCursor) { - entries.add(new DatabaseEntry<>(rs.getInt("id"), rs.getString("status"))); - if(rs.isLast()) result.add(new DatabaseEntry<>(currentCity, entries)); - } - else { - if(entries.isEmpty()) { - entries.add(new DatabaseEntry<>(rs.getInt("id"), rs.getString("status"))); - currentCursor++; - } - else { - result.add(new DatabaseEntry<>(currentCity, entries)); - entries.clear(); + if(currentCity != currentCursor) + { + if(!entries.isEmpty()) { + result.add(new DatabaseEntry<>(currentCursor, new ArrayList<>(entries))); + if(!rs.isLast()) entries.clear(); } + currentCursor++; } + + entries.add(new DatabaseEntry<>(rs.getInt("id"), + new DatabaseEntry<>(rs.getString("status"), + rs.getString("difficulty")))); + if(rs.isLast()) result.add(new DatabaseEntry<>(currentCity, entries)); } DatabaseConnection.closeResultSet(rs);