DevNatan / inventory-framework

Minecraft Inventory API framework
MIT License
128 stars 22 forks source link

Doubts about using ScheduleUpdate #616

Closed kaiquyricardo closed 10 months ago

kaiquyricardo commented 10 months ago

How do I update the items in my pagination, because I want the counter to update automatically in the menu

`

private final RewardsPlugin plugin;

private final State<UserCache> userCacheState = initialState("userCache");

private final State<Pagination> paginationState = lazyPaginationState(
        (context) -> rewardList(),
        (context, item, index, reward) -> {
            final UserCache userCache = userCacheState.get(context);
            final User user = userCache.getByUser(context.getPlayer().getName());

            item.withItem(new ItemBuilder(Material.SKULL_ITEM)
                    .name(reward.getName())
                    .texture(reward.getTexture())
                    .lore(reward.getLore())
                    .addLoreLine("")
                    .addLoreLine(!user.canCollect(reward.getId()) ? "§cAguarde: " +
                            TimeFormatter.format(user.getRest(reward.getId())) :
                            "§aClique para coletar")
                    .build()
            ).onClick(click -> {
                final Player player = context.getPlayer();

                if (!player.hasPermission(reward.getPermission())) {
                    MessageProvider.ERROR.sendMessage(
                            player,
                            "§c§lEPA! §cVocê tem permissão para coletar esta recompensa."
                    );
                    return;
                }

                if (!user.canCollect(reward.getId())) {
                    MessageProvider.ERROR.sendMessage(
                            player,
                            "§c§lEPA! §cVocê precisa aguardar " +
                                    TimeFormatter.format(user.getRest(reward.getId())) +
                                    " §cpara coletar está recompensa"
                    );
                    return;
                }

                for (String command : reward.getCommands()) {
                    Bukkit.dispatchCommand(
                            Bukkit.getConsoleSender(),
                            command.replace("<player>", player.getName())
                    );
                }

                user.addReward(reward);
            });
        }
);

@Override
public void onInit(@NotNull ViewConfigBuilder config) {
    config.title("Recompensas")
            .size(5)
            .layout(
                    "         ",
                    "         ",
                    " OOOOOOO ",
                    " OOOOOOO ",
                    "         "
            )
            .scheduleUpdate(20L)
            .cancelOnPickup()
            .cancelOnDrag()
            .cancelOnClick()
            .cancelOnDrop()
            .build();
}

public List<Reward> rewardList() {
    return plugin.getRewardsCache().getCachedElements();
}

} `

ch4ika commented 10 months ago

You have to use onRender or renderWith instead of withItem.

withItem is static and does not listen to the update.

kaiquyricardo commented 10 months ago

You have to use onRender or renderWith instead of withItem.

withItem is static and does not listen to the update.

thank you very much