Skip to content

Commit

Permalink
refactor review menu for page system
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTuxn committed Jan 19, 2022
1 parent 3d5f60a commit 0ce097a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
90 changes: 52 additions & 38 deletions src/main/java/com/alpsbte/plotsystem/core/menus/ReviewMenu.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright © 2021, Alps BTE <[email protected]>
* Copyright © 2021-2022, Alps BTE <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,45 +43,39 @@
import java.util.logging.Level;
import java.util.stream.Collectors;

public class ReviewMenu extends AbstractMenu {

private final List<Plot> plots = new ArrayList<>();
private int plotDisplayCount = 0;
public class ReviewMenu extends AbstractPaginatedMenu {

public ReviewMenu(Player player) throws SQLException {
super(6, "Manage & Review Plots", player);
super(6, 5, "Manage & Review Plots", player);
}

@Override
protected void setPreviewItems() {
// Set previous page item
getMenu().getSlot(46).setItem(MenuItems.previousPageItem());
protected List<?> getSource() {
List<Plot> plots = new ArrayList<>();
try {
plots.addAll(PlotManager.getPlots(Status.unreviewed));
plots.addAll(PlotManager.getPlots(Status.unfinished));
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
}
return plots;
}

@Override
protected void setPreviewItems() {
// Set close item
getMenu().getSlot(49).setItem(MenuItems.closeMenuItem());

// Set next page item
getMenu().getSlot(52).setItem(MenuItems.nextPageItem());

super.setPreviewItems();
}

@Override
protected void setMenuItemsAsync() {
// Get all unreviewed and unfinished plots
try {
plots.addAll(PlotManager.getPlots(Status.unreviewed));
plots.addAll(PlotManager.getPlots(Status.unfinished));
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
}

protected void setPaginatedMenuItemsAsync(List<?> source) {
// Set unreviewed and unfinished plot items
plotDisplayCount = Math.min(plots.size(), 45);
for(int i = 0; i < plotDisplayCount; i++) {
List<Plot> plots = source.stream().map(p -> (Plot) p).collect(Collectors.toList());
int index = 0;
for(Plot plot : plots) {
try {
Plot plot = plots.get(i);

List<String> lines = new ArrayList<>();
lines.add("§7ID: §f" + plot.getID());
lines.add("");
Expand All @@ -98,27 +92,29 @@ protected void setMenuItemsAsync() {
lines.add("§7City: §f" + plot.getCity().getName());
lines.add("§7Difficulty: §f" + plot.getDifficulty().name().charAt(0) + plot.getDifficulty().name().substring(1).toLowerCase());

getMenu().getSlot(i).setItem(new ItemBuilder(plot.getStatus() == Status.unfinished ? Material.EMPTY_MAP : Material.MAP, 1)
getMenu().getSlot(index).setItem(new ItemBuilder(plot.getStatus() == Status.unfinished ? Material.EMPTY_MAP : Material.MAP, 1)
.setName(plot.getStatus() == Status.unfinished ? "§b§lManage Plot" : "§b§lReview Plot")
.setLore(lines)
.build());
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
getMenu().getSlot(i).setItem(MenuItems.errorItem());
getMenu().getSlot(index).setItem(MenuItems.errorItem());
}
index++;
}
}

@Override
protected void setItemClickEventsAsync() {
protected void setPaginatedItemClickEventsAsync(List<?> source) {
// Set click event for unreviewed and unfinished plot items
List<Plot> plots = source.stream().map(p -> (Plot) p).collect(Collectors.toList());
int index = 0;
for (Plot plot : plots.subList(0, plotDisplayCount)) {
for (Plot plot : plots) {
getMenu().getSlot(index).setClickHandler((player, info) -> {
try {
getMenuPlayer().closeInventory();
if(plot.getStatus() == Status.unreviewed) {
if (!plot.getPlotOwner().getUUID().toString().equals(getMenuPlayer().getUniqueId().toString())){
if (plot.getStatus() == Status.unreviewed) {
if (!plot.getPlotOwner().getUUID().toString().equals(getMenuPlayer().getUniqueId().toString())) {
plot.getWorld().teleportPlayer(getMenuPlayer());
} else {
getMenuPlayer().sendMessage(Utils.getErrorMessageFormat("You cannot review your own builds!"));
Expand All @@ -132,19 +128,37 @@ protected void setItemClickEventsAsync() {
});
index++;
}
}

@Override
protected void setMenuItemsAsync() {
// Set previous page item
if (hasPreviousPage()) getMenu().getSlot(46).setItem(MenuItems.previousPageItem());

// Set next page item
if (hasNextPage()) getMenu().getSlot(52).setItem(MenuItems.nextPageItem());
}

@Override
protected void setItemClickEventsAsync() {
// Set click event for previous page item
//getMenu().getSlot(46).setClickHandler((clickPlayer, clickInformation) -> {
// Not implemented yet
//});
getMenu().getSlot(46).setClickHandler((clickPlayer, clickInformation) -> {
if (hasPreviousPage()) {
previousPage();
clickPlayer.playSound(clickPlayer.getLocation(), Utils.INVENTORY_CLICK, 1, 1);
}
});

// Set click event for close item
getMenu().getSlot(49).setClickHandler((clickPlayer, clickInformation) -> clickPlayer.closeInventory());

// Set click event for next page item
//getMenu().getSlot(52).setClickHandler((clickPlayer, clickInformation) -> {
// Not implemented yet
//});
getMenu().getSlot(52).setClickHandler((clickPlayer, clickInformation) -> {
if (hasNextPage()) {
nextPage();
clickPlayer.playSound(clickPlayer.getLocation(), Utils.INVENTORY_CLICK, 1, 1);
}
});
}

@Override
Expand All @@ -156,7 +170,7 @@ protected Mask getMask() {
.pattern("000000000")
.pattern("000000000")
.pattern("000000000")
.pattern("101101101")
.pattern("111101111")
.build();
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/alpsbte/plotsystem/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright © 2021, Alps BTE <[email protected]>
* Copyright © 2021-2022, Alps BTE <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -63,6 +63,7 @@ public static ItemStack getPlayerHead(UUID playerUUID) {
public static Sound FinishPlotSound = Sound.ENTITY_PLAYER_LEVELUP;
public static Sound AbandonPlotSound = Sound.ENTITY_ENDERDRAGON_FIREBALL_EXPLODE;
public static Sound Done = Sound.ENTITY_EXPERIENCE_ORB_PICKUP;
public static Sound INVENTORY_CLICK = Sound.ENTITY_ITEMFRAME_ADD_ITEM;

// Spawn Location
public static Location getSpawnLocation() {
Expand Down

0 comments on commit 0ce097a

Please sign in to comment.