DevNatan / inventory-framework

Minecraft Inventory API framework
MIT License
120 stars 23 forks source link

Click handler doesn't work in onItemRender #141

Closed Azodox closed 2 years ago

Azodox commented 2 years ago

Is there an existing issue for this?

🐛 Describe the bug

I have a PaginatedView that overrides onItemRender method, and as shown in an example in the README (and also the Javadoc of onItemRender method), I retrieve the rendered item with viewItem (second parameter) and try to add a click handler with .onClick (after a couple of other methods such as .withItem) and among other stuff, I try to print debugs to see if it works but apparently it doesn't because, when I click on the item, nothing appears in the console.

Then, I tried to add a simple item in the inventory but, unlike before, in the constructor of the class. And I, of course, called the method .onClick with a simple System.out.println and it works!

Therefore, my theory is that, strangely, the method onClick doesn't when it's called from onItemRender method for some reason. Here is my code :

package fr.olten.xmas.inventory;

import fr.olten.xmas.Core;
import fr.olten.xmas.home.Home;
import fr.olten.xmas.util.HeadUtil;
import fr.olten.xmas.util.ItemBuilder;
import me.saiintbrisson.minecraft.PaginatedView;
import me.saiintbrisson.minecraft.PaginatedViewSlotContext;
import me.saiintbrisson.minecraft.ViewItem;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Random;

public class HomeListView extends PaginatedView<Home> {

    private final Core core;
    private final OfflinePlayer target;

    public HomeListView(Core core, int size, String title, List<Home> homes, OfflinePlayer target) {
        super(size, title);
        this.core = core;
        this.target = target;

        setLayout(
                "OOOOOOOOO",
                "OOOOOOOOO",
                "OOOOOOOOO",
                "XXX<X>XXX"
        );
        setSource(homes);
        setCancelOnDrag(true);
        setCancelOnDrop(true);
        setCancelOnMoveOut(true);
        setCancelOnPickup(true);
        setCancelOnClone(true);
        setCancelOnShiftClick(true);
        setCancelOnClick(true);

        setPreviousPageItem((context, item) -> item.withItem(new ItemBuilder(HeadUtil.getHead("previousPage")).setName("§c§lPage précédente").build()));
        setNextPageItem((context, item) -> item.withItem(new ItemBuilder(HeadUtil.getHead("nextPage")).setName("§c§lPage suivante").build()));

        lastSlot().withItem(new ItemBuilder(Material.ANDESITE).build()).onClick(click -> System.out.println("lastSlot")).closeOnClick();
    }

    @Override
    protected void onItemRender(@NotNull PaginatedViewSlotContext<Home> context, @NotNull ViewItem viewItem, @NotNull Home value) {
        var list = HeadUtil.getTranslator().keySet().stream().filter(s -> s.startsWith("birdhouse-")).toList();
        viewItem.withItem(new ItemBuilder(HeadUtil.getHead(list.get(new Random().nextInt(list.size()))).clone()).setName(ChatColor.GOLD + value.name()).build())
                .onClick(click -> {
            System.out.println("click");
            System.out.println("home's name : " + value.name());
            this.core.getHomeManager().getHome(target.getUniqueId(), value.name()).ifPresent(home -> {
                click.getPlayer().teleport(home.location());
                click.getPlayer().sendActionBar(Component.text(ChatColor.AQUA + "Téléportation vers " + ChatColor.BOLD + target.getName() + "/" + home.name() + ChatColor.AQUA + "..."));
            });
        }).closeOnClick();
    }
}

✔️ Expected behavior

When I click on an item using onClick method in onItemRender method it, at least, shows a message in the console.

👣 Steps to Reproduce

  1. Create a PaginatedView
  2. In the method onItemRender, try to add a click handler on an item and do some stuff
  3. Try in-game if it works
  4. If I'm right it shouldn't work

💻 Platform

⭐ Version

1.19

✍️ Additional context

I'm running Paper.

DevNatan commented 2 years ago

@Azodox Fixed in v2.5.1 please try the new version!

Azodox commented 2 years ago

Thank you @DevNatan for your prompt response and fix but I think you had a little problem with publishing? I can't retrieve the dependency and jitpack says that : https://jitpack.io/com/github/DevNatan/inventory-framework/2.5.1/build.log in the logs.

Azodox commented 2 years ago

Nevermind, just needed to wait a few minutes.