Value only adds once

Discussion in 'Spigot Plugin Development' started by yhl, Apr 25, 2017.

  1. yhl


    Okay Ive been working on this all day and I've tried a bunch of things so everythings cluttered. Basically I made it so when a player moves it adds 0.2 to the config and when you do /traveled it returns the value but it only adds to 0.2 then stops. You may be thinking that it only triggers once (it doesnt, I've debugged) I think its just the config or something. Heres the code: (btw no errors)
  2. Okay don't do it like this!

    One of the most time consuming things you can do programming is reading/writing to the disk. And you're using an event that gets called thousands of times per second.

    Every time you move your mouse even a millimeter on playerMoveEvent gets called. So that event is writing to the disk a LOT

    There is a statistic if you're trying to track how far a player has walked, flown etc. in player.getStatistic()

    If you really need to record 0.2 every time a player moves you need to create a class variable that you can use to store the player movements.

    Like a HashMap<Player, Double>

    Write to the hash map if you must but don't constantly write to the config multiply this event by 10 players and you're going to have a ton of lag on the server.

    If you update the variable you can save it when the player logs out, on a timer every 30 minutes or onDisable()

    Sent from my iPhone using Tapatalk
    • Agree Agree x 2
  3. WAS


    onDisable() you should be storing your data to file. onEnable you should read it. Rest should be in memory. You should also use the UUID like I showed you and not store a dynamic, and much larger player object.

    UUID is great to call players and save memory. Imagine a huge network with multiple plugins storing the players of useless information as a identifier.
    #3 WAS, Apr 25, 2017
    Last edited: Apr 25, 2017
  4. yhl


    Don't I already have a hashmap like that?
  5. yhl


    Actually getStatistic is way better thanks a ton! (If anyone is looking for how I finally did it here it is: