Skip to content

Commit

Permalink
update pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Oct 12, 2024
1 parent e05d543 commit 117ae40
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 129 deletions.
38 changes: 1 addition & 37 deletions paper/src/main/java/com/badbones69/crazyauctions/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.events.AuctionExpireEvent;
import com.badbones69.crazyauctions.api.events.AuctionWinBidEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.*;
Expand Down Expand Up @@ -150,32 +148,7 @@ public static boolean hasPermission(CommandSender sender, String perm) {
return true;
}

public static List<ItemStack> getPage(List<ItemStack> list, Integer page) {
List<ItemStack> items = new ArrayList<>();

if (page <= 0) page = 1;

int max = 45;
int index = page * max - max;
int endIndex = index >= list.size() ? list.size() - 1 : index + max;

for (; index < endIndex; index++) {
if (index < list.size()) items.add(list.get(index));
}

for (; items.size() == 0; page--) {
if (page <= 0) break;
index = page * max - max;
endIndex = index >= list.size() ? list.size() - 1 : index + max;
for (; index < endIndex; index++) {
if (index < list.size()) items.add(list.get(index));
}
}

return items;
}

public static List<Integer> getPageInts(List<Integer> list, Integer page) {
public static List<Integer> getPageItems(List<Integer> list, int page) {
List<Integer> items = new ArrayList<>();

if (page <= 0) page = 1;
Expand All @@ -202,15 +175,6 @@ public static List<Integer> getPageInts(List<Integer> list, Integer page) {
return items;
}

public static int getMaxPage(List<ItemStack> list) {
int maxPage = 1;
int amount = list.size();

for (; amount > 45; amount -= 45, maxPage++) ;

return maxPage;
}

public static String convertToTime(long time) {
Calendar C = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class Holder implements InventoryHolder, Listener {

protected final CrazyAuctions plugin = CrazyAuctions.getPlugin();
Expand Down Expand Up @@ -64,6 +69,63 @@ public Holder() {}

public abstract void run(InventoryClickEvent event);

public void setSize(final int size) {
this.size = size;
}

public final int getSize() {
return this.size - 9;
}

public void setPage(final int page) {
this.page = page;
}

public void nextPage() {
setPage(getPage() + 1);
}

public void backPage() {
setPage(getPage() - 1);
}

public final int getPage() {
return this.page;
}

public final List<ItemStack> getPageItems(final List<ItemStack> list, int page, final int size) {
List<ItemStack> items = new ArrayList<>();

if (page <= 0) page = 1;

int index = page * size - size;
int endIndex = index >= list.size() ? list.size() - 1 : index + size;

for (;index < endIndex; index++) {
if (index < list.size()) items.add(list.get(index));
}

for (;items.isEmpty(); page--) {
if (page <= 0) break;

index = page * size - size;

endIndex = index >= list.size() ? list.size() - 1 : index + size;

for (; index < endIndex; index++) {
if (index < list.size()) items.add(list.get(index));
}
}

return items;
}

public final int getMaxPage(final List<ItemStack> list) {
final int size = list.size();

return (int) Math.ceil((double) size / getSize());
}

@EventHandler
public void onPlayerClick(InventoryClickEvent event) {
run(event);
Expand All @@ -74,16 +136,16 @@ public void onPlayerClick(InventoryClickEvent event) {
return this.inventory;
}

public void click() {
public void click(final Player player) {
final FileConfiguration config = Files.config.getConfiguration();

if (config.getBoolean("Settings.Sounds.Toggle", false)) {
final String sound = config.getString("Settings.Sounds.Sound", "UI_BUTTON_CLICK");

try {
this.player.playSound(this.player.getLocation(), Sound.valueOf(sound), 1, 1);
player.playSound(player.getLocation(), Sound.valueOf(sound), 1, 1);
} catch (Exception e) {
this.player.playSound(this.player.getLocation(), Sound.UI_BUTTON_CLICK, SoundCategory.PLAYERS, 1F, 1F);
player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, SoundCategory.PLAYERS, 1F, 1F);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ public static void removePage(final Player player, final int page) {
pages.put(uuid, list);
}

public static void removePage(final Player player) {
pages.remove(player.getUniqueId());
}

public static boolean containsPage(final Player player) {
return pages.containsKey(player.getUniqueId());
return !pages.containsKey(player.getUniqueId());
}

public static List<Integer> getPages(final Player player) {
Expand Down
Loading

0 comments on commit 117ae40

Please sign in to comment.