Solved Getting config data on join?

Discussion in 'Plugin Development' started by drpk, Oct 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    drpk

    I'm trying to put playerdata in a HashMap on a playerjoin, but it wont load it and it resets it.
    CODE
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent evt) {
    3. if (c.getConfig().contains(evt.getPlayer().getUniqueId().toString())) {
    4. c.block.bb.remove(evt.getPlayer().getUniqueId());
    5. c.block.bb.put(evt.getPlayer().getUniqueId(), c.getConfig().getInt(String.valueOf(evt.getPlayer().getUniqueId())));
    6. }
    7. }
    8. //START OF BLOCKBREAKHANDLER
    9.  
    10. public BlockBreakHandler(Main c) {
    11. this.c = c;
    12. }
    13.  
    14. public HashMap<UUID, Integer> bb = new HashMap<>();
    15.  
    16. @EventHandler
    17. public void BlockBreak(BlockBreakEvent evt) {
    18. Player p = evt.getPlayer();
    19. ItemStack dye = new ItemBuilder(Material.BLAZE_ROD).
    20. setLore(Arrays.asList("Use this to add enchants!"))
    21. .name("§cTokens")
    22. .build();
    23.  
    24. if (!bb.containsKey(p.getUniqueId())) {
    25. bb.put(p.getUniqueId(), 0);
    26. } else {
    27. bb.put(p.getUniqueId(), bb.get(p.getUniqueId()) + 1);
    28. }
    29. c.getConfig().set(p.getUniqueId().toString(), bb.get(p.getUniqueId()));
    30. c.saveConfig();
    31. if (bb.get(p.getUniqueId()) == 1999) {
    32. p.sendMessage(ChatColor.AQUA + "You have broken 2000 blocks!");
    33. }
    34.  
    35. if (bb.get(p.getUniqueId()) == 5) {
    36.  
    37. p.getInventory().addItem(dye);
    38. bb.remove(p.getUniqueId());
    39. }
    40. }
     
  2. Offline

    567legodude

    drpk I think it is because you are creating a new HashMap each time the event is fired. Try creating the HashMap outside of the event, where it will only fire once. A good place would be to put it before the onEnable()
     
  3. Offline

    drpk

    567legodude but the HashMap is outside of the BlockBreakEvent...
     
  4. Offline

    567legodude

    drpk I believe that a HashMap is cleared when you reload, so every player has to rejoin in order for it to store the info. You need to store the info in a config, so that it can retrieve it after a reload.
     
  5. Offline

    drpk

    567legodude I do save it in a config, but when I try to break a block after a reload the config resets and the HashMap resets.
     
  6. Offline

    567legodude

    drpk When the server reloads, the HashMap is cleared, so when you break a block, it updates the config with the blank data.
     
  7. Offline

    drpk

    567legodude I debugged and it keeps thinking the HashMap does not contain the players UUID, even while I add it in the PlayerJoin
     
  8. Offline

    OffLuffy

    Unless the HashMap is static, it will be re-initialized and blanked out on each initialization of the BlockBreakHandler, as it somewhat looks like you've put the map in that class, although the code is a bit jumbled for me to tell for sure.

    You shouldn't need to re-register the BlockBreakHandler every time a player joins though, as far as I'm aware. You may want to instead do that in the plugins onEnable() method.
     
  9. Offline

    drpk

    OffLuffy Oh whoops, I forgot to update the OP, anyways I moved the BlockBreakHandler, I tried your idea, and deleted the instantiation and using the instance from the Main class, to no avail
     
  10. Offline

    OffLuffy

    Are you registering the events? Hate to ask a basic question, but we can't tell from the code.
     
  11. Offline

    fireblast709

    OffLuffy even if static, the classes are reloaded (which is why you can change code, recompile and instantly apply the changes), thus the static variables would be reinitialised.
    drpk in the case of a reload, there are players online (which you can get with Bukkit.getOnlinePlayers()). Just do what you would do in the join event, and their data should persist (if you save the config, of course).
     
  12. Offline

    OffLuffy

    Yeah, I'm assuming you mean over /reload, but I mealt this a bit differently. I think there was a HashMap in a class that was initialized on player join when it should've been static in the class.
     
  13. Offline

    fireblast709

    OffLuffy it shouldn't have been static. It should've been a nonstatic field.
     
  14. Offline

    OffLuffy

    His BlockBreakHandler was initialized on every PlayerJoinEvent prior to him editing the code, which was blanking out his HashMap that was stored in the Handler. It doesn't need to be static now, but it sorta needed to be if he kept the handler initializer in the join event.
     
  15. Offline

    drpk

    OffLuffy yeah I registered the event, however, it keeps thinking the players UUID is not in the HashMap, even when I add it onJoin and in the onEnable.
     
  16. Offline

    drpk

    bump
     
  17. Offline

    drpk

    bump
     
  18. Offline

    fireblast709

    drpk Just for clarity, post the latest code used.
     
  19. Offline

    drpk

    fireblast709

    Code:java
    1. public class BlockBreakHandler implements Listener {
    2.  
    3. public HashMap<UUID, Integer> bb = new HashMap<>();
    4.  
    5. public Main c;
    6.  
    7. public BlockBreakHandler(Main c) {
    8. this.c = c;
    9.  
    10. }
    11.  
    12.  
    13.  
    14. @EventHandler
    15. public void BlockBreak(BlockBreakEvent evt) {
    16. Player p = evt.getPlayer();
    17. if (!bb.containsKey(p.getUniqueId())) {
    18. p.sendMessage("§cHEYYA");
    19. bb.put(p.getUniqueId(), 1);
    20. } else {
    21. p.sendMessage("§bHEYYA");
    22. bb.put(p.getUniqueId(), bb.get(p.getUniqueId()) + 1);
    23. }
    24. c.getConfig().set(p.getUniqueId().toString(), this.bb.get(p.getUniqueId()));
    25. c.saveConfig();
    26.  
    27. if (bb.get(p.getUniqueId()) == 2000) {
    28. p.sendMessage(ChatColor.AQUA + "You have broken 2000 blocks!");
    29.  
    30. }
    31.  
    32. if (bb.get(p.getUniqueId()) == 500) {
    33.  
    34. p.getInventory().addItem(c.token);
    35. bb.remove(p.getUniqueId());
    36. }
    37. if (bb.containsKey(p.getUniqueId())){
    38. p.sendMessage(bb.get(p.getUniqueId()).toString());
    39. }
    40. }
    41. @EventHandler
    42. public void onJoin(PlayerJoinEvent evt) {
    43. Player p = evt.getPlayer();
    44. if (c.getConfig().contains(p.getUniqueId().toString())) {
    45. c.block.bb.put(p.getUniqueId(), c.getConfig().getInt(evt.getPlayer().getUniqueId().toString()));
    46. evt.getPlayer().sendMessage(c.block.bb.get(p.getUniqueId()).toString());
    47. }
    48.  
    49. }
    50. @EventHandler
    51. public void onLeave(PlayerQuitEvent evt){
    52. c.block.bb.remove(evt.getPlayer().getUniqueId());
    53.  
    54. }
    55. }


    when the player first joins, it recognizes him in the hashmap, but after he breaks the block, it resets.
     
  20. Offline

    fireblast709

    drpk post your main class
     
  21. Offline

    drpk

    fireblast709
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public BlockBreakHandler block = new BlockBreakHandler(this);
    4. public Explosive exp = new Explosive(this);
    5. public ExplosiveHandler exph = new ExplosiveHandler(this);
    6. public ItemBuilder itemBuilder = new ItemBuilder(this);
    7. public OnJoin onJoin = new OnJoin(this);
    8. public ExplosionHandler explosionHandler = new ExplosionHandler(this);
    9.  
    10. @Override
    11. public void onEnable() {
    12.  
    13. PluginManager pm = Bukkit.getServer().getPluginManager();
    14. print("§a" + getDescription().getFullName() + " Has Been Disabled!");
    15. pm.registerEvents(new BlockBreakHandler(this), this);
    16. pm.registerEvents(new OnJoin(this), this);
    17. pm.registerEvents(exph, this);
    18. pm.registerEvents(explosionHandler, this);
    19. pm.registerEvents(new OnLeave(this), this);
    20. getConfig().addDefault("explosion-level", 4);
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. for (Player player : Bukkit.getOnlinePlayers()) {
    24. block.bb.remove(player.getUniqueId());
    25. block.bb.put(player.getUniqueId(), getConfig().getInt(player.getUniqueId().toString()));
    26. player.sendMessage(block.bb.get(player.getUniqueId()).toString());
    27. ItemStack is = new ItemBuilder(Material.DIAMOND_PICKAXE).addLore(Arrays.asList("§cExplosive!")).build();
    28. player.getInventory().addItem(is);
    29. }
    30.  
    31. //TODO add votifier support
    32. //TODO add configuration for amnt of tokens given in votifier
    33. }
    34.  
    35. @Override
    36. public void onDisable() {
    37. print("§c" + getDescription().getFullName() + " Has Been Disabled!");
    38. for (Player player : Bukkit.getOnlinePlayers()) {
    39. block.bb.remove(player.getUniqueId());
    40. }
    41. }
    42.  
    43. public void print(String message) {
    44. ConsoleCommandSender ccs = Bukkit.getConsoleSender();
    45. ccs.sendMessage(message);
    46. }
    47.  
    48.  
    49. public ItemStack token = new ItemBuilder(Material.BLAZE_ROD).
    50. setLore(Arrays.asList("Use this to add enchants!"))
    51. .name("§cTokens").addLore(Arrays.asList("Token!"))
    52. .build();
    53.  
    54. }
     
  22. Offline

    fireblast709

    drpk you are creating multiple instances of BlockBreakHandler and OnJoin. If you reuse the fields you created then the issue should be solved. Currently, the join event handler in BlockBreakHandler uses a different BlockBreakHandler than the rest of the code (it uses the field in Main, while the rest of them use the BlockBreakHandler you registered)
     
  23. Offline

    drpk

Thread Status:
Not open for further replies.

Share This Page