Hello, I need help to decrease the amount of exp a player has by 4 every 15 seconds.How would I go about doing this?I would prefer that the timer starts when the player logs in.If that doesn't work, then its fineThank you very much!
Use a sceduled timer. Then in there just define a variable for the player. Im not quite sure how to decrease xp but im sure its not too hard.
Do what @bartboy8 said, use a sceduled timer. Then just set the XP decresed by 4. Like (not actual code) Code: setXP(player.getXP() - 4)
Untested code and just something i pulled out of my head, i could very well have done it wrong with the methods. But it should run every 15 seconds and you only have to initiate it once. This is way better performance wise compared to your idea of creating a new schedule for each player. Code: server.getScheduler().scheduleSyncRepeatingTask(new Runnable() { public void run() { for(Player player : server.getOnlinePlayers()) { player.setTotalExperience(player.getTotalExperience() - 4); } } }, 300L);
Edit: I got it working, here is the code Code: this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { public void run() { for(Player player : Bukkit.getServer().getOnlinePlayers()) { if(player.getExp() > 0) { player.giveExp(- 4); } else { player.setLevel(player.getLevel() - 1); if(player.getLevel() < 1) { player.setHealth(0); } } } } },20L, 1200L);
depends on what unit the long represends, somethimes you can use an long to detonate ticks, seconds or miliseconds Its like saying: what is 1 in seconds, whitout knowing if 1 is days or weeks or that