Solved Loop causes NPE

Discussion in 'Plugin Development' started by xYourFreindx, Aug 6, 2014.

Thread Status:
Not open for further replies.
  1. I've been fiddling with this for days,
    but the result is always the same...
    Whenever I get hit by a mob (seems to be effected by whether or not I hit it first) my console loops a NPE. At this line.
    main.pvpTimer.put(player.getUniqueId().toString(), main.pvpTimer.get(player.getUniqueId().toString()) - 1);​

    Code:java
    1.  
    2. @EventHandler
    3. public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
    4. if (e.getDamager() instanceof Player) {
    5. final Player player = (Player) e.getDamager();
    6. if(main.flyplayers.contains(player.getUniqueId().toString())){
    7. e.setCancelled(true);
    8. player.sendMessage(ChatColor.RED + reason);
    9. }else{
    10. if (main.getConfig().getLong("CameraMode.PvpTimer") != 0){
    11. if (e.getEntity() instanceof Zombie || e.getEntity() instanceof Slime || e.getEntity() instanceof Player || e.getEntity() instanceof Skeleton || e.getEntity() instanceof Creeper || e.getEntity() instanceof Spider || e.getEntity() instanceof Witch || e.getEntity() instanceof Wolf || e.getEntity() instanceof Blaze || e.getEntity() instanceof Ghast || e.getEntity() instanceof MagmaCube || e.getEntity() instanceof Arrow || e.getEntity() instanceof CaveSpider || e.getEntity() instanceof EnderDragon | e.getEntity() instanceof PigZombie || e.getEntity() instanceof Silverfish || e.getEntity() instanceof Fireball || e.getEntity() instanceof WitherSkull || e.getEntity() instanceof Wither || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Giant){
    12. //if (!main.pvpTimer.containsKey(player.getUniqueId().toString()))
    13. main.pvpTimer.put(e.getDamager().getUniqueId().toString(), main.getConfig().getInt("CameraMode.PvpTimer") +1);
    14. ID = main.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable(){
    15. public void run() {
    16. main.pvpTimer.put(player.getUniqueId().toString(), main.pvpTimer.get(player.getUniqueId().toString()) - 1);
    17. if (main.pvpTimer.get(player.getUniqueId().toString()) == 1){
    18. Bukkit.getScheduler().cancelTask(ID);
    19. }
    20. }
    21. }, 0,20);
    22. main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    23. public void run() {
    24. main.pvpTimer.remove(player.getUniqueId().toString());
    25. }
    26. },20 * main.getConfig().getLong("CameraMode.PvpTimer"));
    27.  
    28. }
    29. }
    30. }
    31. }
    32. if (e.getEntity() instanceof Player){
    33. final Player entiti = (Player) e.getEntity();
    34. if(main.flyplayers.contains(entiti.getUniqueId().toString())){
    35. e.setCancelled(true);
    36. }else{
    37. if (main.getConfig().getLong("CameraMode.PvpTimer") != 0){
    38. if (e.getDamager() instanceof Zombie || e.getEntity() instanceof Slime || e.getEntity() instanceof Player || e.getEntity() instanceof Skeleton || e.getEntity() instanceof Creeper || e.getEntity() instanceof Spider || e.getEntity() instanceof Witch || e.getEntity() instanceof Wolf || e.getEntity() instanceof Blaze || e.getEntity() instanceof Ghast || e.getEntity() instanceof MagmaCube || e.getEntity() instanceof Arrow || e.getEntity() instanceof CaveSpider || e.getEntity() instanceof EnderDragon | e.getEntity() instanceof PigZombie || e.getEntity() instanceof Silverfish || e.getEntity() instanceof Fireball || e.getEntity() instanceof WitherSkull || e.getEntity() instanceof Wither || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Giant){
    39. //if (!main.pvpTimer.containsKey(entiti.getUniqueId().toString()))
    40. main.pvpTimer.put(e.getEntity().getUniqueId().toString(), main.getConfig().getInt("CameraMode.PvpTimer") + 1);
    41. IDS = main.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable(){
    42. public void run() {
    43. main.pvpTimer.put(entiti.getUniqueId().toString(), main.pvpTimer.get(entiti.getUniqueId().toString()) - 1);
    44. if (main.pvpTimer.get(entiti.getUniqueId().toString()) == 1){
    45. Bukkit.getScheduler().cancelTask(IDS);
    46. }
    47. }
    48. }, 0,20);
    49. main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    50. public void run() {
    51. main.pvpTimer.remove(entiti.getUniqueId().toString());
    52. }
    53. },20 * main.getConfig().getLong("CameraMode.PvpTimer"));
    54.  
    55. }
    56. }
    57. }
    58. }
    59. }


    Am I doing something obviously wrong? ​

    Btw

    Code:java
    1. Int ID;
    2. Int IDS;


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    ZodiacTheories

  3. ZodiacTheories
    Main is just equal to the main class. It is where the hashmaps and config data is retrieved from.
    If you'd like a copy, let me know.
     
  4. Offline

    ZodiacTheories

    xYourFreindx

    Can I see where you declare/initialize the variable?
     
  5. ZodiacTheories
    Code:java
    1.  
    2.  
    3. public class Events implements Listener {
    4.  
    5. CameraMode main;
    6.  
    7. int ID;
    8. int IDS;
    9. String reason = "You are in CameraMode!";
    10.  
    11. public Events(CameraMode plugin){
    12. this.main = plugin;
    13. }
    14. //...


    I added a "looper" and "loopers" arraylist, and reorganized the loops.
    You can see my changes here.
    Code:java
    1.  
    2. @EventHandler
    3. public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
    4. if (e.getDamager() instanceof Player) {
    5. final Player player = (Player) e.getDamager();
    6. if(main.flyplayers.contains(player.getUniqueId().toString())){
    7. e.setCancelled(true);
    8. player.sendMessage(ChatColor.RED + reason);
    9. }else{
    10. if (main.getConfig().getLong("CameraMode.PvpTimer") != 0){
    11. if (e.getEntity() instanceof Zombie || e.getEntity() instanceof Slime || e.getEntity() instanceof Player || e.getEntity() instanceof Skeleton || e.getEntity() instanceof Creeper || e.getEntity() instanceof Spider || e.getEntity() instanceof Witch || e.getEntity() instanceof Wolf || e.getEntity() instanceof Blaze || e.getEntity() instanceof Ghast || e.getEntity() instanceof MagmaCube || e.getEntity() instanceof Arrow || e.getEntity() instanceof CaveSpider || e.getEntity() instanceof EnderDragon | e.getEntity() instanceof PigZombie || e.getEntity() instanceof Silverfish || e.getEntity() instanceof Fireball || e.getEntity() instanceof WitherSkull || e.getEntity() instanceof Wither || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Giant){
    12. //if (!main.pvpTimer.containsKey(player.getUniqueId().toString())){
    13. if (main.looper.contains(player.getUniqueId())){
    14. Bukkit.getScheduler().cancelTask(ID);
    15. main.looper.remove(player.getUniqueId());
    16. }
    17. if (main.loopers.contains(player.getUniqueId())){
    18. Bukkit.getScheduler().cancelTask(IDS);
    19. main.loopers.remove(player.getUniqueId());
    20. }
    21. main.pvpTimer.put(e.getDamager().getUniqueId().toString(), main.getConfig().getInt("CameraMode.PvpTimer") +1);
    22. main.looper.add(player.getUniqueId());
    23. ID = main.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable(){
    24. public void run() {
    25. main.pvpTimer.put(player.getUniqueId().toString(), main.pvpTimer.get(player.getUniqueId().toString()) - 1);
    26. if (main.pvpTimer.get(player.getUniqueId().toString()) == 1){
    27. Bukkit.getScheduler().cancelTask(ID);
    28. main.pvpTimer.remove(player.getUniqueId().toString());
    29. }
    30. }
    31. }, 0,20);
    32.  
    33. }
    34. }
    35. }
    36. }
    37. if (e.getEntity() instanceof Player){
    38. final Player entiti = (Player) e.getEntity();
    39. if(main.flyplayers.contains(entiti.getUniqueId().toString())){
    40. e.setCancelled(true);
    41. }else{
    42. if (main.getConfig().getLong("CameraMode.PvpTimer") != 0){
    43. if (e.getDamager() instanceof Zombie || e.getEntity() instanceof Slime || e.getEntity() instanceof Player || e.getEntity() instanceof Skeleton || e.getEntity() instanceof Creeper || e.getEntity() instanceof Spider || e.getEntity() instanceof Witch || e.getEntity() instanceof Wolf || e.getEntity() instanceof Blaze || e.getEntity() instanceof Ghast || e.getEntity() instanceof MagmaCube || e.getEntity() instanceof Arrow || e.getEntity() instanceof CaveSpider || e.getEntity() instanceof EnderDragon | e.getEntity() instanceof PigZombie || e.getEntity() instanceof Silverfish || e.getEntity() instanceof Fireball || e.getEntity() instanceof WitherSkull || e.getEntity() instanceof Wither || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Giant){
    44. //if (!main.pvpTimer.containsKey(entiti.getUniqueId().toString())){
    45. if (main.loopers.contains(entiti.getUniqueId())){
    46. Bukkit.getScheduler().cancelTask(IDS);
    47. main.loopers.remove(entiti.getUniqueId());
    48. }
    49. if (main.looper.contains(entiti.getUniqueId())){
    50. Bukkit.getScheduler().cancelTask(ID);
    51. main.looper.remove(entiti.getUniqueId());
    52. }
    53. main.loopers.add(entiti.getUniqueId());
    54. main.pvpTimer.put(e.getEntity().getUniqueId().toString(), main.getConfig().getInt("CameraMode.PvpTimer") + 1);
    55. IDS = main.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable(){
    56. public void run() {
    57. main.pvpTimer.put(entiti.getUniqueId().toString(), main.pvpTimer.get(entiti.getUniqueId().toString()) - 1);
    58. if (main.pvpTimer.get(entiti.getUniqueId().toString()) == 1){
    59. Bukkit.getScheduler().cancelTask(IDS);
    60. main.pvpTimer.remove(entiti.getUniqueId().toString());
    61. }
    62. }
    63. }, 0,20);
    64.  
    65. }
    66. }
    67. }
    68. }
    69. }

    This system works perfectly.
    The timer restarts every time the player is hit or hits something, and there are no more errors.
    The loops no longer run on top of each other. ​

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page