Skip to content

Commit

Permalink
feat: Paged Country Board
Browse files Browse the repository at this point in the history
* country board for displaying available plots per country per city

* fix design patten on DecentHologramPagedDisplay to match our new built in paging api
  • Loading branch information
tintinkung committed Apr 18, 2024
1 parent 8e950c9 commit b1f7e28
Show file tree
Hide file tree
Showing 15 changed files with 432 additions and 121 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/alpsbte/plotsystem/PlotSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import com.alpsbte.alpslib.utils.AlpsUtils;
import com.alpsbte.alpslib.utils.head.AlpsHeadEventListener;
import com.alpsbte.plotsystem.commands.*;
import com.alpsbte.plotsystem.core.holograms.HologramManager;
import com.alpsbte.plotsystem.core.holograms.MessagesManager;
import com.alpsbte.plotsystem.core.holograms.connector.DecentHologramDisplay;
import com.alpsbte.plotsystem.core.holograms.LeaderboardManager;
import com.alpsbte.plotsystem.core.holograms.HologramRegister;
import com.alpsbte.plotsystem.core.system.Builder;
import com.alpsbte.plotsystem.core.system.plot.Plot;
import com.alpsbte.plotsystem.core.system.plot.utils.PlotUtils;
Expand Down Expand Up @@ -62,6 +60,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Level;
Expand Down Expand Up @@ -184,8 +183,7 @@ public void onEnable() {
});

DecentHologramDisplay.registerPlugin(this);
LeaderboardManager.init();
MessagesManager.init();
HologramRegister.init();
PlotUtils.checkPlotsForLastActivity();
PlotUtils.syncPlotSchematicFiles();
PlotUtils.syncPlotGeoCoordinate();
Expand Down Expand Up @@ -226,7 +224,7 @@ public void onDisable() {
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GRAY + "> " + ChatColor.GRAY + "GitHub: " + ChatColor.WHITE + "https://github.com/AlpsBTE/Plot-System");
Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "------------------------------------------------------");

LeaderboardManager.getActiveDisplays().forEach(DecentHologramDisplay::delete);
HologramRegister.getActiveDisplays().forEach(DecentHologramDisplay::delete);
} else {
// Unload plots
for (UUID player : PlotUtils.Cache.getCachedInProgressPlots().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.alpsbte.plotsystem.commands.BaseCommand;
import com.alpsbte.plotsystem.core.database.DatabaseConnection;
import com.alpsbte.plotsystem.core.holograms.HologramConfiguration;
import com.alpsbte.plotsystem.core.holograms.LeaderboardManager;
import com.alpsbte.plotsystem.core.holograms.HologramRegister;
import com.alpsbte.plotsystem.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand All @@ -50,9 +50,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
PlotSystem.getPlugin().reloadConfig();
sender.sendMessage(Utils.ChatUtils.getInfoFormat("Successfully reloaded config!"));

LeaderboardManager.getActiveDisplays().forEach(leaderboard -> leaderboard.setLocation(LeaderboardManager
HologramRegister.getActiveDisplays().forEach(leaderboard -> leaderboard.setLocation(HologramRegister
.getLocation((HologramConfiguration) leaderboard)));
LeaderboardManager.reload();
HologramRegister.reload();
sender.sendMessage(Utils.ChatUtils.getInfoFormat("Successfully reloaded leaderboards!"));

DatabaseConnection.InitializeDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.alpsbte.plotsystem.commands.BaseCommand;
import com.alpsbte.plotsystem.core.holograms.HologramConfiguration;
import com.alpsbte.plotsystem.core.holograms.LeaderboardManager;
import com.alpsbte.plotsystem.core.holograms.HologramRegister;
import com.alpsbte.plotsystem.core.holograms.connector.DecentHologramDisplay;
import com.alpsbte.plotsystem.utils.Utils;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -54,15 +54,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
if (args.length != 1) {
sendInfo(sender);
player.sendMessage("§8------- §6§lLeaderboards §8-------");
for(DecentHologramDisplay holo : LeaderboardManager.getActiveDisplays()) {
for(DecentHologramDisplay holo : HologramRegister.getActiveDisplays()) {
player.sendMessage(" §6> §f" + holo.getId());
}
player.sendMessage("§8--------------------------");
return true;
}

// Find leaderboard by name
DecentHologramDisplay leaderboard = LeaderboardManager.getActiveDisplays().stream()
DecentHologramDisplay leaderboard = HologramRegister.getActiveDisplays().stream()
.filter(holo -> holo.getId().equalsIgnoreCase(args[0]))
.findFirst()
.orElse(null);
Expand All @@ -72,7 +72,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
player.sendMessage(Utils.ChatUtils.getAlertFormat("Leaderboard could not be found!"));
return true;
}
LeaderboardManager.saveLocation(leaderboard.getId(), (HologramConfiguration) leaderboard, getPlayer(sender).getLocation());
HologramRegister.saveLocation(leaderboard.getId(), (HologramConfiguration) leaderboard, getPlayer(sender).getLocation());
player.sendMessage(Utils.ChatUtils.getInfoFormat("Successfully updated hologram location!"));
player.playSound(player.getLocation(), Utils.SoundUtils.DONE_SOUND,1,1);
return true;
Expand Down
231 changes: 231 additions & 0 deletions src/main/java/com/alpsbte/plotsystem/core/holograms/CountryBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package com.alpsbte.plotsystem.core.holograms;

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.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.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.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class CountryBoard extends DecentHologramPagedDisplay implements HologramConfiguration {
private final DecimalFormat df = new DecimalFormat("#.##");
private ArrayList<Builder.DatabaseEntry<
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---------------";
private int currentPage = 0;

protected CountryBoard() {
super( "country-board", null, false, PlotSystem.getPlugin());
setEnabled(PlotSystem.getPlugin().getConfig().getBoolean(getEnablePath()));
setLocation(HologramManager.getLocation(this));

try {
this.projectsData = Builder.getProjectsSorted();
this.plotEntries = Builder.getPlotsSorted();
}
catch (SQLException ex) {
PlotSystem.getPlugin().getLogger().log(Level.SEVERE, "An error occurred while reading country board content", ex);
}
}

@Override
public void create(Player player) {
if (!PlotSystem.getPlugin().isEnabled()) return;
if (getPages().isEmpty()) {
PlotSystem.getPlugin().getLogger().log(Level.WARNING, "Unable to initialize Score-Leaderboard - No display pages enabled! Check config for display-options.");
return;
}
super.setPageCount(projectsData != null? projectsData.size() : 1);
super.create(player);
}

@Override
public void reload(UUID playerUUID) {
super.reload(playerUUID);

Hologram holo = DHAPI.getHologram(playerUUID.toString() + "-" + "country-board");
if (holo == null | projectsData == null) return;
super.setClickListener((clickEvent) -> {
int nextViewPage = currentPage + 1;
if(nextViewPage == projectsData.size()) nextViewPage = 0;
holo.show(holo.getViewerPlayers().get(0), nextViewPage);
currentPage = nextViewPage;
Bukkit.getLogger().log(Level.INFO, "Recieved Country Board Pages Event " + clickEvent.getClick() + "\nChanging to pahe " + nextViewPage);
});
}

@Override
public ItemStack getItem() {
return new ItemStack(Material.NETHER_STAR);
}

@Override
public String getTitle(UUID playerUUID) {
return "§b§lCOUNTRY BOARD §6§l";
}

@Override
public List<DataLine<?>> getContent(UUID var1) {
return null;
}

@Override
public List<List<DataLine<?>>> getPagedHeader(UUID playerUUID) {
List<List<DataLine<?>>> header = new ArrayList<>();
if(projectsData == null) 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() + " -----")
));
}


return header;
}

@Override
public List<List<DataLine<?>>> getPagedContent(UUID playerUUID) {
List<List<DataLine<?>>> content = new ArrayList<>();
if(projectsData == null) super.getPagedHeader(playerUUID);
for (Builder.DatabaseEntry<
Builder.DatabaseEntry<Integer, String>,
Builder.DatabaseEntry<Integer, String>> cityProject
: projectsData
) {
for (Builder.DatabaseEntry<Integer,
ArrayList<Builder.DatabaseEntry<Integer, String>>> plots : plotEntries)
{
Bukkit.getLogger().log(Level.INFO, "Looking for " + cityProject.getValue().getKey() + " " + plots.getKey());
ArrayList<DataLine<?>> lines = new ArrayList<>();

for (int i = 0; i < 10; i++) {
lines.add(new HologramRegister.CountryBoardPositionLine("§8#" + 0,null, null));
}

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

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());
}

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++;
}
}
content.add(lines);
}

}


return content;
}

private static ArrayList<Map.Entry<Integer, Status>> plotStatusSorter(HashMap<Integer, Status> entries) {
// Create an ArrayList and insert all hashmap key-value pairs.
ArrayList<Map.Entry<Integer, Status>> sortEntries = new ArrayList<>(entries.entrySet());

// Sort the Arraylist using a custom comparator.
sortEntries.sort(new Comparator<Map.Entry<Integer, Status>>() {
@Override
public int compare(Map.Entry<Integer, Status> o1, Map.Entry<Integer, Status> o2) {
if (o1.getValue() == o2.getValue())
return o1.getKey().compareTo(o2.getKey());

return Integer.compare(o1.getValue().ordinal(), o2.getValue().ordinal());
}
});

return sortEntries;

// Create a LinkedHashMap.
// Map sortedMap = new LinkedHashMap<>();

// 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());
// }
// }
}

@Override
public List<String> getPages() {
if (ConfigUtil.getInstance() == null) return new ArrayList<>();
FileConfiguration config = PlotSystem.getPlugin().getConfig();
return Arrays.stream(ScoreLeaderboard.LeaderboardTimeframe.values())
.filter(p -> config.getBoolean(p.configPath)).map(ScoreLeaderboard.LeaderboardTimeframe::toString).collect(Collectors.toList());
}

@Override
public long getInterval() {
return PlotSystem.getPlugin().getConfig().getInt(ConfigPaths.DISPLAY_OPTIONS_INTERVAL) * 20L;
}

@Override
public String getEnablePath() {
return ConfigPaths.COUNTRY_BOARD_ENABLE;
}

@Override
public String getXPath() {
return ConfigPaths.COUNTRY_BOARD_X;
}

@Override
public String getYPath() {
return ConfigPaths.COUNTRY_BOARD_Y;
}

@Override
public String getZPath() {
return ConfigPaths.COUNTRY_BOARD_Z;
}

@Override
public boolean hasViewPermission(UUID uuid) {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;

public abstract class HologramManager {
public static List<DecentHologramDisplay> activeDisplays = new ArrayList<>();

public static void reload() {
for (DecentHologramDisplay display : activeDisplays) {
Bukkit.getLogger().log(Level.INFO, "Enabling Hologram: " + PlotSystem.getPlugin().getConfig().getBoolean(((HologramConfiguration) display).getEnablePath()));

// Register and create holograms
if (PlotSystem.getPlugin().getConfig().getBoolean(((HologramConfiguration) display).getEnablePath())) {
display.setEnabled(true);
Expand Down Expand Up @@ -52,7 +49,7 @@ public static void saveLocation(String id, HologramConfiguration configPaths, Lo
config.set(configPaths.getZPath(), newLocation.getZ());
ConfigUtil.getInstance().saveFiles();

LeaderboardManager.getActiveDisplays().stream().filter(leaderboard -> leaderboard.getId().equals(id)).findFirst()
HologramRegister.getActiveDisplays().stream().filter(leaderboard -> leaderboard.getId().equals(id)).findFirst()
.ifPresent(holo -> holo.setLocation(newLocation));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,28 @@

package com.alpsbte.plotsystem.core.holograms;

import com.alpsbte.plotsystem.PlotSystem;
import com.alpsbte.plotsystem.core.holograms.connector.DecentHologramDisplay;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Objects;
import java.util.logging.Level;
import java.sql.SQLException;

public final class LeaderboardManager extends HologramManager {
public final class HologramRegister extends HologramManager {

public static void init() {
for (DecentHologramDisplay display : activeDisplays) {
Bukkit.getLogger().log(Level.INFO, "Enabling Hologram: " + PlotSystem.getPlugin().getConfig().getBoolean(((HologramConfiguration) display).getEnablePath()));

// Register and create holograms
if (PlotSystem.getPlugin().getConfig().getBoolean(((HologramConfiguration) display).getEnablePath()))
for (Player player : Objects.requireNonNull(Bukkit.getWorld(display.getLocation().getWorld().getName())).getPlayers())
display.create(player);
else display.removeAll();
}
activeDisplays.add(new ScoreLeaderboard());
activeDisplays.add(new PlotsLeaderboard());
activeDisplays.add(new WelcomeMessage());
activeDisplays.add(new CountryBoard());
}

public static class LeaderboardPositionLine extends DecentHologramDisplay.TextLine {
public LeaderboardPositionLine(int position, String username, int score) {
super("§e#" + position + " " + (username != null ? "§a" + username : "§8No one, yet") + " §7- §b" + 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"));
}
}
}
Loading

0 comments on commit b1f7e28

Please sign in to comment.