0

I have a Java-based Bukkit plugin (fairly irrelevant to the issue at hand but noteworthy) that will, every 30 minutes, query a web API for updates to pass on to users.

Here is the existing method, which works fine to pass every user to the API:

public ApiMessenger()
{        
    TASK_ID = Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin,new Runnable()
    {
        @Override
        public void run()
        {
            if(Bukkit.getOnlinePlayers().length > 0)
            {
                ApiMessenger.fetchPlayerItems(Bukkit.getOnlinePlayers(),true);
            }
        }
    }, 20*60*30, 20*60*30);
}

However the API will accept, at most, 30 users at a time.

So any server using this plugin that has more than 30 users on at a time will not be able to submit all their users at once, because the rest will be ignored.

What I'm trying to figure out is what kind of setup would allow me to store all the users with some form of a timestamp so that the ApiMessenger method shown above can remember which users have been sent and attempt to send the ones who have been waiting the longest for an update first.

I can manage removing/adding users as they join/leave, but my main concern is first creating a sorted data structure that can be updated as the groups of 30 users are sent.

Unfortunately I don't know which data types in Java to use to create such a structure that can be iterated in an ascending fashion.

4

1 回答 1

1

您是否考虑过使用队列?你可以在这里用 Java 阅读它。当玩家登录时,只需将他们添加到要处理的玩家队列中即可。然后,只需从列表中弹出 30 个项目并处理它们。然后,您只需将这些用户推到队列的后面即可。如果玩家下线,您可以将他们从队列中移除或检查他们在处理队列时是否在线。如果我理解正确 - 这应该可以帮助您解决问题!

于 2012-12-14T21:27:29.923 回答