重复的消费者循环 [英] Repetitive Consumer For Loop

查看:74
本文介绍了重复的消费者循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有方法的库,在该方法中,我调用一个采用事件类的方法,并在事件触发时调用使用者.此方法返回一个空值.我需要在使用者中再次调用该方法,然后在该使用者中再次调用它,等等(很多次).我如何将其放入for循环中,以免输入大量这种方法? 代码:

I am using a library with a method where I call a method that takes an event class and a consumer to call when the event fires. This method returns a void. I need to call the method again in the consumer then in that consumer call it again etc. (a lot of times). How can I put this in a for loop to avoid typing out this method loads of times? Code:

ShurikenBot.getInstance().getEventWaiter().waitForEvent(MessageReactionAddEvent.class,
                    t -> t.getMessageId().equals(m.getId()) && event.getAuthor().getId().equals(t.getMember().getUser().getId())
                            && t.getReaction().getReactionEmote().getName().equals("\u25B6"),
                    t ->
                            t.getChannel().getMessageById(t.getMessageId()).queue(msg -> {
                                System.out.println("yes");
                                // i want to call waitForEvent() again here
                                msg.editMessage(builder.setTitle("Shuriken Tutorial - Step 2").setImage("https://i.615283.net/u/47794c.jpg").build()).queue();
                                t.getReaction().removeReaction(t.getUser()).queue();
                            }));

推荐答案

您可以使用通用事件侦听器来创建State-Machine,而不是使用EventWaiter.

Instead of using the EventWaiter you could create a State-Machine using a generic event listener.

public class StateMachine extends ListenerAdapter {
    private final long messageId;
    private final long userId;
    private final String emoji;

    private int state = 0;

    public StateMachine(...) {...}

    @Override
    public void onMessageReactionAdd(MessageReactionAddEvent event) {
        if (event.getMessageIdLong() != messageId) return;
        if (event.getUser().getIdLong() != userId) return;
        if (!event.getReactionEmote().getName().equals(emoji)) return;
        switch (state) {
        case 0:
            System.out.println("yes");
            event.getChannel().editMessageById(messageId, ...).queue();
            event.getReaction().removeReaction(event.getUser()).queue();
            state = 1;
            break;
        case 1:
            System.out.println("This is the next awaited event");
            // do something here...
            break;
        }
    }
}

然后,您可以定义最终状态,在该状态下您将再次删除事件侦听器.

Then you can define a final state in which you remove the event listener again.

这篇关于重复的消费者循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆