Jump to content
  • 0

Task Giving Items


Question

Posted

Hello how are you?. Has anyone seen or knows where I can get some sample code to take as a basis for some task that, at a certain time that the user is connected within the game, gives you items from a list of objects?

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

There's different ways you can do that. How I would do it:

 

First of all, you need to save login time for each player on EnterWorld. Just make a private long _loginTime; in Player.java then on EnterWorld player.setLoginTime(System.currentTimeMillis());.

 

Then, run a task (can be a ScheduledQuest, or check gameserver.taskmanager package classes) every 1 minute to loop through all players that are online (World.getInstance().getPlayers()) and check if the correct time has passed, then reward the player.

For example, if you want players to receive a reward 30 minutes after they login, the check should be:

if ((System.currentTimeMillis() - player.getLoginTime()) / 1000 / 60 >= 30)

 

This calculation can be changed if there are other factors affecting the time for the reward. For example you want premium players to be 15 minutes instead of 30:

if ((System.currentTimeMillis() - player.getLoginTime()) / 1000 / 60 >= (player.isPremium() ? 15 : 30))

 

If the reward is static (for example, 1 per day) you can take it a step further. You can only schedule the task to run 1 time when a player logs in that hasn't received the reward yet. Then, when the task fires check if there is another player logged in that hasn't received the reward and run it again (you can even calculated the least time to run it for the next player that is to be rewarded).

 

-

 

The method mentioned above is pretty accurate, but the player can have up to 1 minute delay for the reward (which should be fine generally, unless it's something you need done on the clock).

If you want precise time, then you could either reduce the time for the above task or you could run a task for each player on EnterWorld:

 

ThreadPool.scheduleGeneral(() -> player.getInventory().addItemByItemId("reward", id, count, null, true), 30 * 1000 * 60);

Edited by An4rchy
  • Like 1
  • 0
Posted
10 hours ago, An4rchy said:

There's different ways you can do that. How I would do it:

 

First of all, you need to save login time for each player on EnterWorld. Just make a private long _loginTime; in Player.java then on EnterWorld player.setLoginTime(System.currentTimeMillis());.

 

Then, run a task (can be a ScheduledQuest, or check gameserver.taskmanager package classes) every 1 minute to loop through all players that are online (World.getInstance().getPlayers()) and check if the correct time has passed, then reward the player.

For example, if you want players to receive a reward 30 minutes after they login, the check should be:

if ((System.currentTimeMillis() - player.getLoginTime()) / 1000 / 60 >= 30)

 

This calculation can be changed if there are other factors affecting the time for the reward. For example you want premium players to be 15 minutes instead of 30:

if ((System.currentTimeMillis() - player.getLoginTime()) / 1000 / 60 >= (player.isPremium() ? 15 : 30))

 

If the reward is static (for example, 1 per day) you can take it a step further. You can only schedule the task to run 1 time when a player logs in that hasn't received the reward yet. That way you will avoid running the task every one minute.

 

-

 

The method mentioned above is pretty accurate, but the player can have up to 1 minute delay for the reward (which should be fine generally, unless it's something you need done on the clock).

If you want precise time, then you could either reduce the time for the above task (which I do not recommend since it loops through all online players) or you could run a task for each player on EnterWorld:

 

ThreadPool.scheduleGeneral(() -> player.getInventory().addItemByItemId("reward", id, count, null, true), 30 * 1000 * 60);

 

 

 

Thanks bro! great idea.

  • Like 1
  • 0
Posted (edited)

 

okay. I have been able to solve that problem and I have received the item after some time.
I am using Sunrise, is it possible that I can create an array of ints with the ID number of the item so that depending on the time it is connected, it will go through the array and give away the items?
I speak type of the variable CONFIGS of the ini.

Edited by barao45
  • 0
Posted (edited)

did you made setter / getter ?

 

if you dont use a hwid protection for this kind of rewards it will be very easy to exploit.

 

Date dt = new Date(_loginTime);
 final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

activeChar.sendMessage("You are logged in since -> " + df.format(dt) + ". Enjoy the Game.");
                    

Edited by arm4729

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...