ygimenez / Pagination-Utils

A collection of methods to make message pagination with JDA easier.
GNU Lesser General Public License v2.1
27 stars 7 forks source link

Paginator paginates first Page 2 Time #31

Closed TheHolyFire closed 2 years ago

TheHolyFire commented 3 years ago

Checklist

Make sure that you've checked all the items below.

Library info

What libraries versions are you using.

Describe the bug

The first page is shown twice and when trying to go back as the first thing it does nothing

To Reproduce

Steps to reproduce the behavior:

  1. Add 3 Pages
  2. Click on back or next
  3. See error

Expected behavior

it only should show the first page once

Screenshots

Code:

grafik

Additional context

ygimenez commented 3 years ago

What's the value of i?

TheHolyFire commented 3 years ago

i is givin by the user! Command: /check [user] [type] [i]

should i post the whole Class?

ygimenez commented 3 years ago

There should be something else causing that issue since the library itself doesn't send the first page (this is done on your side).

If possible, I'd like to see the rest of the class yes.

TheHolyFire commented 3 years ago

Edit: removed all unimportant imports

import com.github.ygimenez.method.Pages;
import com.github.ygimenez.model.Page;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;

public class CheckAPI {

    public static void CheckPlayer(String ChannelID,String userName,String type){
        ArrayList<Page> pages = new ArrayList<>();
        DataConfig config = (DataConfig) FileManager.getConfig("data.json",DataConfig.class);
        UUID uuid = getKeyByValue(config.playerList,userName);
        TextChannel channel = FreedomCoreBungee.getInstance().api.getTextChannelById(ChannelID);
        if(channel != null) {
            EmbedBuilder builder = new EmbedBuilder(),page1 = new EmbedBuilder();
            page1.setTitle("Check");
            builder.setTitle("Check");
            builder.setColor(Color.RED);
            page1.setDescription(Util.formatName(userName));
            builder.setDescription(Util.formatName(userName));
            if (uuid != null) {
                int mutes = 0,bans = 0,ipBans = 0,i;
                String ip = config.playerIPList.get(uuid);
                if (FileManager.configExists("players/" + uuid + "/data.json")) {
                    UserConfig userConfig = (UserConfig) FileManager.getConfig("players/" + uuid + "/data.json",UserConfig.class);
                    mutes = userConfig.mute;
                    bans = userConfig.ban;
                }
                if (FileManager.configExists("ips/" + ip + "/data.json")) {
                    IPConfig ipConfig = (IPConfig) FileManager.getConfig("ips/" + ip + "/data.json",IPConfig.class);
                    ipBans = ipConfig.ipban;
                }
                if (type.equalsIgnoreCase("all")) {
                    builder.addField("Mutes:", String.valueOf(mutes),false);
                    builder.addField("Bans:", String.valueOf(bans),false);
                    builder.addField("IP-Bans:", String.valueOf(ipBans),false);
                    channel.sendMessageEmbeds(builder.build()).queue();
                }
                else if (type.startsWith("ban")) {
                    String[] listType = type.split(" ");
                    try {
                        i = Integer.parseInt(listType[1]);
                        if (i > bans || i <= 0) {
                            if (bans != 1) {
                                sendError(builder,channel,"Player only has " + bans + " bans");
                            } else {
                                sendError(builder,channel,"Player only has " + bans + " ban");
                            }
                            return;
                        }

                    } catch (NumberFormatException e) {
                        sendError(builder,channel,"!check <name> <all|ban|mute|ipban> <number>");
                        return;
                    }
                    page1.setFooter("There are no earlier Interferences");
                    pages.add(new Page(page1.build()));
                    for (int f = 0;f < bans;f++){
                        pages.add(new Page(getNextInterference("ban",uuid,f+1)));
                    }
                    page1.setFooter("There are no later Interferences");
                    pages.add(new Page(page1.build()));
                    channel.sendMessageEmbeds((MessageEmbed) pages.get(i).getContent()).queue(success -> Pages.paginate(success,pages));
                }
                else if (type.startsWith("mute")) {
                    String[] listType = type.split(" ");
                    try {
                        i = Integer.parseInt(listType[1]);
                        if (i > mutes || i <= 0) {
                            if (mutes != 1) {
                                sendError(builder,channel,"Player only has " + mutes + " mutes");
                            } else {
                                sendError(builder,channel,"Player only has " + mutes + " mute");
                            }
                            return;
                        }
                    } catch (NumberFormatException e) {
                        sendError(builder,channel,"!check <name> <all|ban|mute|ipban> <number>");
                        return;
                    }
                    page1.setFooter("There are no earlier Interferences");
                    pages.add(new Page(page1.build()));
                    for (int f = 0;f < mutes;f++){
                        pages.add(new Page(getNextInterference("mute", uuid, f+1)));
                    }
                    page1.setFooter("There are no later Interferences");
                    pages.add(new Page(page1.build()));
                    channel.sendMessageEmbeds((MessageEmbed) pages.get(i).getContent()).queue(success -> Pages.paginate(success,pages));
                }
                else if (type.startsWith("ipban")) {
                    String[] listType = type.split(" ");
                    try {
                        i = Integer.parseInt(listType[1]);
                        if (i > ipBans || i <= 0) {
                            if (ipBans != 1) {
                                sendError(builder,channel,"Player only has " + ipBans + " ip-bans");
                            } else {
                                sendError(builder,channel,"Player only has " + ipBans + " ip-ban");
                            }
                            return;
                        }
                    } catch (NumberFormatException e) {
                        sendError(builder,channel,"!check <name> <all|ban|mute|ipban> <number>");
                        return;
                    }

                    page1.setFooter("There are no earlier Interferences");
                    pages.add(new Page(page1.build()));
                    for (int f = 0;f < ipBans;f++){
                        pages.add(new Page(getNextInterference("ipban",uuid,f+1)));
                    }
                    page1.setFooter("There are no later Interferences");
                    pages.add(new Page(page1.build()));
                    channel.sendMessageEmbeds((MessageEmbed) pages.get(i).getContent()).queue(success -> Pages.paginate(success,pages));
                }
            } else {
                builder.setColor(Color.RED);
                builder.addField("Error", "Player never joined the Server",false);
                channel.sendMessageEmbeds(builder.build()).queue();
            }
        } else {
            System.out.println("no channel found");
        }
    }

    public static MessageEmbed getNextInterference(String type,UUID uuid,int i){
        EmbedBuilder builder = new EmbedBuilder();
        DataConfig config = (DataConfig) FileManager.getConfig("data.json",DataConfig.class);
        builder.setTitle("Check");
        builder.setColor(Color.YELLOW);
        String userName = config.playerList.get(uuid);
        String ip = "";
        if(type.equalsIgnoreCase("ipban")){
            ip = config.playerIPList.get(uuid);
        }
        builder.setDescription(Util.formatName(userName));
        if(type.equalsIgnoreCase("ban")){
            UserConfig userConfig = (UserConfig) FileManager.getConfig("players/" + uuid + "/data.json",UserConfig.class);
            PunishmentConfig punishmentConfig = userConfig.bans.get(i);
            long left = Util.getLeft(punishmentConfig.start, punishmentConfig.time);

            builder.addField("Staff",formatName(punishmentConfig.staffName),false);
            builder.addField("Time", punishmentConfig.time,false);

            if (left < 0) {
                if (punishmentConfig.current) {
                    builder.addField("Status","Unbanned",false);
                } else {
                    builder.addField("Status","Expired",false);
                }
            } else {
                String[] WholeTime = Util.milliToTime(Util.getLeft(punishmentConfig.start, punishmentConfig.time)).split(", ");
                List<String> playtime = new ArrayList<>(Arrays.asList(WholeTime));
                builder.addField("Status",playtime + " left",false);
            }
            builder.addField("Reason", punishmentConfig.reason,false);
            builder.addField("ID", String.valueOf(punishmentConfig.ID),false);
        }
        else if(type.equalsIgnoreCase("mute")) {
            UserConfig userConfig = (UserConfig) FileManager.getConfig("players/" + uuid + "/data.json", UserConfig.class);
            PunishmentConfig punishmentConfig = userConfig.mutes.get(i);
            long left = Util.getLeft(punishmentConfig.start, punishmentConfig.time);

            builder.addField("Staff", formatName(punishmentConfig.staffName), false);
            builder.addField("Time", punishmentConfig.time, false);
            if (punishmentConfig.current) {
                builder.addField("Status", "Unbanned", false);
            } else {
                if (left < 0) {
                    builder.addField("Status", "Expired", false);
                } else {
                    String[] WholeTime = Util.milliToTime(Util.getLeft(punishmentConfig.start, punishmentConfig.time)).split(", ");
                    List<String> playtime = new ArrayList<>(Arrays.asList(WholeTime));
                    builder.addField("Status", playtime + " left", false);
                }
            }
            builder.addField("Reason", punishmentConfig.reason, false);
            builder.addField("ID", String.valueOf(punishmentConfig.ID), false);
        }
        else if(type.equalsIgnoreCase("ipban")){
            IPConfig ipConfig = (IPConfig) FileManager.getConfig("ips/" + ip + "/data.json",IPConfig.class);
            PunishmentConfig punishmentConfig = ipConfig.ipbans.get(i);
            long left = Util.getLeft(punishmentConfig.start, punishmentConfig.time);

            builder.addField("Staff",formatName(punishmentConfig.staffName),false);
            builder.addField("Time", punishmentConfig.time,false);

            if (left < 0) {
                if (punishmentConfig.current) {
                    builder.addField("Status","Unbanned",false);
                } else {
                    builder.addField("Status","Expired",false);
                }
            } else {
                String[] WholeTime = Util.milliToTime(Util.getLeft(punishmentConfig.start, punishmentConfig.time)).split(", ");
                List<String> playtime = new ArrayList<>(Arrays.asList(WholeTime));
                builder.addField("Status",playtime + " left",false);
            }
            builder.addField("Reason", punishmentConfig.reason,false);
            builder.addField("ID", String.valueOf(punishmentConfig.ID),false);
        }
        return builder.build();
    }

    private static void sendError(EmbedBuilder builder,TextChannel channel,String message){
        builder.addField("Error",message,false);
        channel.sendMessageEmbeds(builder.build()).queue();
    }
}
ygimenez commented 3 years ago

Sorry for taking so long to answer, could you please try the code below?

List<Page> pages = new ArrayList<>();
EmbedBuilder eb = new EmbedBuilder();
for (int i = 0; i < 10; i++) {
    eb.setTitle("Page " + (i + 1));
    pages.add(new Page(eb.build()));
}
channel.sendMessageEmbeds((MessageEmbed) pages.get(0)).queue(s -> Pages.paginate(s, pages));
TheHolyFire commented 2 years ago

Hey yeah I will try later

ygimenez commented 2 years ago

I'm closing this due to no response, if needed please reopen.