Solved Inventory Page Selector with List<Player>

Discussion in 'Spigot Plugin Development' started by Ohwnead, Jul 12, 2018.

  1. Hi, I'm stuck for my code,

    I try to make a Inventory,
    Slot 1 & last slot is an Arrow,
    The rest of the Inventory will display PlayerHead

    All of this is done,

    but I try for 2 days, to filter the list by Alphanumeric order, and make the page selector,
    I explain : the arrow up and down an Int, wich is Page.

    When clicked it will up and down, and get player in the array at a specific start (like at offset 0, and then 18, 36, 54...)
    Page will * 18 to get the player start offset.

    Here is the last code try (Some code miss, still trying different way) :
    Code (Text):
    public void setPlayerInInv(Inventory inv, List<Player> p, int page)
    {

        for (int i = 0 * page; i < p.size(); i++)
        {
            Player pl = p.get(i);

            ItemStack pHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                SkullMeta pSkull = (SkullMeta) pHead.getItemMeta();
                pSkull.setDisplayName(pl.getDisplayName());
                ArrayList<String> lore = new ArrayList<String>();
                lore.add(pl.getUniqueId().toString());
                pSkull.setLore(lore);
                pSkull.setOwner(pl.getName());
                pHead.setItemMeta(pSkull);
            inv.addItem(pHead);
        }

    }
    This code can be out of bound, and at some time make crash the server with no log, (RAM get to max of JVM attribute and my computer don't like it)
    Thanks for help
     
  2. Escad

    Escad Previously Rabbitual

    Wouldn't int i always be 0, since 0 * page will always be 0 no matter what page is?
     
  3. If you do 0*PAGE, the result is always 0 :p
     
  4. Yes i know that, it was my pseudo code, here is my actual code
    page at 0 by default
    Code (Text):
     public void setPlayerInInv(Inventory inv, List<Player> p, int page)
        {

            for (int i = 0 + ((inv.getSize() - 2) * page); i < p.size(); i++)
            {
                Player pl = p.get(i);

                ItemStack pHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                SkullMeta pSkull = (SkullMeta) pHead.getItemMeta();
                pSkull.setDisplayName(pl.getDisplayName() + "  :  " + i);
                ArrayList<String> lore = new ArrayList<String>();
                lore.add(pl.getUniqueId().toString());
                pSkull.setLore(lore);
                pSkull.setOwner(pl.getName());
                pHead.setItemMeta(pSkull);
                inv.addItem(pHead);

            }

        }
    I try to make fake player, but it say this

    Code (Text):
    List<Player> players = (List<Player>) Bukkit.getOnlinePlayers();
            for (Player p : Bukkit.getOnlinePlayers())
            {
                players.add(p);
            }
    For boucle make bug
    Code (Text):

    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1877) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_131]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Caused by: java.lang.UnsupportedOperationException
            at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) ~[?:1.8.0_131]
            at fr.SpigotServer.plugin.events.inventory_menu_action._4island_add.PlayerClickBaseInventory(_4island_add.java:35) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
            ... 15 more
     
    but why is it an unmodifiable, IDK :s
     
  5. Obviously that list would be immutable.

    Look up how to create a fake player it's somewhat complex.
     
  6. Thanks, it's done, now, I'm stuck at my arrow part, when i Click the arrow, it change nothing :s

    No logs, nothing

    Code (Text):
    if (e.getCurrentItem().getType() == Material.ARROW)
                {
                    Inventory inv = Bukkit.createInventory(null, 54, "Selectionez un §6joueur à Ajouter");
    //                page++;
                    page--;
                    setPlayerInInv(inv, players, page);
                    e.setCancelled(true);
                }
    Code (Text):
    public void setPlayerInInv(Inventory inv, List<Player> p, int page)
        {
            if (inv.getSize() == 54)
            {
                createDisplay(Material.ARROW, inv, 0, "§6Page précédente", "");
                createDisplay(Material.ARROW, inv, inv.getSize() - 1, "§6Page suivante", "");
            }
            for (int i = 0 + ((inv.getSize() - 2) * page); i < p.size(); i++)
            {
                if (p.get(i) != null)
                {
                    Player pl = p.get(i);

                    ItemStack pHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                    SkullMeta pSkull = (SkullMeta) pHead.getItemMeta();
                    pSkull.setDisplayName(pl.getDisplayName() + "  :  " + i);
                    ArrayList<String> lore = new ArrayList<String>();
                    lore.add(pl.getUniqueId().toString());
                    pSkull.setLore(lore);
                    pSkull.setOwner(pl.getName());
                    pHead.setItemMeta(pSkull);
                    inv.addItem(pHead);
                }

            }

        }
    SetPlayer in inv work well, if i put i at 1 by default it can show well player 51 to 100, but arrow seems not to work ;s

    I have say nothing, just forgot to open inventory again
     

Share This Page