Skip to content

Commit

Permalink
fix: Country Board Bugs
Browse files Browse the repository at this point in the history
* fix country board not displaying stuff properly

* rewrite for loop ordering in CountryBoard and Builder.getPlotsSorted

* more formatting works on country board
  • Loading branch information
tintinkung committed Apr 19, 2024
1 parent b1f7e28 commit 86dd3e5
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 77 deletions.
170 changes: 111 additions & 59 deletions src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -31,8 +28,9 @@ public class CountryBoard extends DecentHologramPagedDisplay implements Hologram
Builder.DatabaseEntry<Integer, String>,
Builder.DatabaseEntry<Integer, String>>> projectsData = null;
private ArrayList<Builder.DatabaseEntry<Integer,
ArrayList<Builder.DatabaseEntry<Integer, String>>>> plotEntries = null;
private static final String contentSeparator = "§7---------------";
ArrayList<Builder.DatabaseEntry<Integer,
Builder.DatabaseEntry<String, String>>>>> plotByCities = null;
private static final String contentSeparator = "§7------------------------";
private int currentPage = 0;

protected CountryBoard() {
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -93,101 +91,155 @@ public List<DataLine<?>> getContent(UUID var1) {
@Override
public List<List<DataLine<?>>> getPagedHeader(UUID playerUUID) {
List<List<DataLine<?>>> header = new ArrayList<>();
if(projectsData == null) super.getPagedHeader(playerUUID);
if(projectsData == null) return super.getPagedHeader(playerUUID);

for (Builder.DatabaseEntry<
Builder.DatabaseEntry<Integer, String>,
Builder.DatabaseEntry<Integer, String>> cityProject
: projectsData
) {
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<List<DataLine<?>>> getPagedContent(UUID playerUUID) {
public List<List<DataLine<?>>> getPagedFooter(UUID playerUUID) {
return Collections.singletonList(Arrays.asList(
new TextLine(contentSeparator),
new TextLine("§6<<< click to swap pages >>>"),
new TextLine(contentSeparator)
));
}

@Override
public List<List<DataLine<?>>> getPagedContent(UUID playerUUID) {
List<List<DataLine<?>>> content = new ArrayList<>();
if(projectsData == null) super.getPagedHeader(playerUUID);
if(projectsData == null) return super.getPagedContent(playerUUID);

for (Builder.DatabaseEntry<
Builder.DatabaseEntry<Integer, String>,
Builder.DatabaseEntry<Integer, String>> cityProject
: projectsData
) {
) { // For each registered city projects
ArrayList<DataLine<?>> 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<Integer,
ArrayList<Builder.DatabaseEntry<Integer, String>>> plots : plotEntries)
ArrayList<Builder.DatabaseEntry<Integer,
Builder.DatabaseEntry<String, String>>>> plotEntries
: plotByCities)
{
Bukkit.getLogger().log(Level.INFO, "Looking for " + cityProject.getValue().getKey() + " " + plots.getKey());
ArrayList<DataLine<?>> 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<Integer, Builder.DatabaseEntry<String, String>> plot : plotStatusSorter(plotEntries.getValue())) {
if(index >= 10) break plotEntry;

int index = 0;
if(cityProject.getValue().getKey().equals(plots.getKey()))
{
HashMap<Integer, Status> sortedEntries = new HashMap<>();
Status status = Status.valueOf(plot.getValue().getKey());
PlotDifficulty difficulty = PlotDifficulty.valueOf(plot.getValue().getValue());

for(Builder.DatabaseEntry<Integer, String> 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<Integer, Status> 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<Map.Entry<Integer, Status>> plotStatusSorter(HashMap<Integer, Status> entries) {
private static ArrayList<Map.Entry<Integer, Builder.DatabaseEntry<String, String>>>
plotStatusSorter(ArrayList<Builder.DatabaseEntry<Integer,
Builder.DatabaseEntry<String, String>>> plotEntries)
{
// Prepare entries with Hashmap
HashMap<Integer, Builder.DatabaseEntry<String, String>> entriesMap = new HashMap<>();
for(Builder.DatabaseEntry<Integer, Builder.DatabaseEntry<String, String>> entry : plotEntries)
entriesMap.put(entry.getKey(), entry.getValue());

// Create an ArrayList and insert all hashmap key-value pairs.
ArrayList<Map.Entry<Integer, Status>> sortEntries = new ArrayList<>(entries.entrySet());
ArrayList<Map.Entry<Integer, Builder.DatabaseEntry<String, String>>> sortedEntries = new ArrayList<>(entriesMap.entrySet());

// Sort the Arraylist using a custom comparator.
sortEntries.sort(new Comparator<Map.Entry<Integer, Status>>() {
sortedEntries.sort(new Comparator<Map.Entry<Integer, Builder.DatabaseEntry<String, String>>>() {
@Override
public int compare(Map.Entry<Integer, Status> o1, Map.Entry<Integer, Status> o2) {
public int compare(Map.Entry<Integer, Builder.DatabaseEntry<String, String>> o1,
Map.Entry<Integer, Builder.DatabaseEntry<String, String>> 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
Expand Down Expand Up @@ -227,5 +279,5 @@ public String getZPath() {
public boolean hasViewPermission(UUID uuid) {
return true;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}
52 changes: 36 additions & 16 deletions src/main/java/com/alpsbte/plotsystem/core/system/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,36 +311,56 @@ public static ArrayList<DatabaseEntry<DatabaseEntry<Integer, String>, DatabaseEn
}
}

public static ArrayList<DatabaseEntry<Integer, ArrayList<DatabaseEntry<Integer, String>>>> 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
* <pre>
* {@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
* }</pre> 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<DatabaseEntry<Integer,
ArrayList<DatabaseEntry<Integer,
DatabaseEntry<String, String>>>>> 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<DatabaseEntry<Integer, ArrayList<DatabaseEntry<Integer, String>>>> result = new ArrayList<>();
ArrayList<DatabaseEntry<Integer, // City ID parent
ArrayList<DatabaseEntry<Integer,// List of all plots in the parent City ID
DatabaseEntry<String, String>> // the plot entry containing a pair of Status and Difficulty
>>> result = new ArrayList<>();

int currentCursor = 0;
ArrayList<DatabaseEntry<Integer, String>> entries = new ArrayList<>();
ArrayList<DatabaseEntry<Integer, DatabaseEntry<String, String>>> 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);
Expand Down

0 comments on commit 86dd3e5

Please sign in to comment.