my explosion regen code has strange behaviour

Discussion in 'Plugin Development' started by xize, Jun 1, 2014.

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

    xize

    Hello,

    so I'm trying to update a very old code I made in the past in order to have more schedulers instead of one globally called scheduler.

    however while it tries to generate blocks it gives false/positives because its just skipping lots of blocks for example when ive a normal tnt explosion the scheduler runs 3 times while it should do more than 3 because more than 3 blocks where exploded. I highly suspect the Comperator is doing something weird.

    my event so far:

    Code:java
    1.  
    2. @EventHandler
    3. public void onExplosion(EntityExplodeEvent e) {
    4. System.out.print("event has been runned.");
    5.  
    6. for(Block block : e.blockList()) {
    7. if(block.getType() == Material.CHEST || block.getType() == Material.FURNACE || block.getType() == Material.ANVIL || block.getType() == Material.BED || block.getType() == Material.BREWING_STAND || block.getType() == Material.WOOD_DOOR || block.getType() == Material.IRON_DOOR || block.getType() == Material.TORCH || block.getType() == Material.REDSTONE_WIRE || block.getType() == Material.REDSTONE_COMPARATOR || block.getType() == Material.REDSTONE_TORCH_ON || block.getType() == Material.REDSTONE_TORCH_OFF) {
    8. e.setCancelled(true);
    9. } else if(block.getType() == Material.TNT) {
    10. //leave empty ignore tnt
    11. } else {
    12. blocks.put(block.getLocation(), block.getState().getData());
    13. block.setType(Material.AIR);
    14. }
    15. }
    16.  
    17. System.out.print("regen object has been created.");
    18.  
    19. RegenObject regen = new RegenObject(blocks);
    20.  
    21. xEssentials.getRegenList().getList.add(regen);
    22. }
    23.  


    my RegenObject class where my scheduler is in:
    Code:java
    1.  
    2. public class RegenObject implements Runnable {
    3. //note ive also just removed the comperator here but still the same behaviour.
    4. private TreeMap<Location, MaterialData> blocks = new TreeMap<Location, MaterialData>(new Comparator<Location>() {
    5. @Override
    6. public int compare(Location o1, Location o2) {
    7. return Integer.valueOf(o1.getBlockY()).compareTo(o2.getBlockY());
    8. }
    9. });
    10.  
    11. private BukkitTask task;
    12. public RegenObject(TreeMap<Location, MaterialData> blocks) {
    13. this.blocks = blocks;
    14. startRegen();
    15. }
    16. /**
    17.   * @author xize
    18.   * @param starts the block regen task.
    19.   */
    20. public void startRegen() {
    21. if(!(task instanceof BukkitTask)) {
    22. this.task = Bukkit.getScheduler().runTaskTimer(xEssentials.getPlugin(), this, 0L, 1L);
    23. }
    24. }
    25.  
    26. /**
    27.   * @author xize
    28.   * @param stops the regen task.
    29.   */
    30. public void stopRegen() {
    31. if(task instanceof BukkitTask) {
    32. this.task.cancel();
    33. this.task = null;
    34. }
    35. if(xEssentials.getRegenList().getList.contains(this)) {
    36. xEssentials.getRegenList().getList.remove(this);
    37. }
    38.  
    39. }
    40. @SuppressWarnings("deprecation")
    41. @Override
    42. public void run() {
    43. System.out.print("scheduler is still running.");
    44. Iterator<Entry<Location, MaterialData>> it = blocks.entrySet().iterator();
    45. if(it.hasNext()) {
    46. Map.Entry<Location, MaterialData> entry = (Map.Entry<Location, MaterialData>) it.next();
    47. Location loc = entry.getKey();
    48. MaterialData data = entry.getValue();
    49. System.out.print("setting block!");
    50. loc.getBlock().setTypeIdAndData(data.getItemTypeId(), data.getData(), true);
    51. loc.getWorld().playEffect(loc, Effect.STEP_SOUND, data.getItemTypeId());
    52. it.remove();
    53. blocks.remove(loc);
    54. } else {
    55. System.out.print("its stopped regen");
    56. stopRegen();
    57. }
    58. }
    59. @Override
    60. public String toString() {
    61. return "RegenObject [blocks=" + blocks + ", task=" + task
    62. + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
    63. + ", toString()=" + super.toString() + "]";
    64. }
    65. @Override
    66. public int hashCode() {
    67. final int prime = 31;
    68. int result = 1;
    69. result = prime * result + ((blocks == null) ? 0 : blocks.hashCode());
    70. result = prime * result + ((task == null) ? 0 : task.hashCode());
    71. return result;
    72. }
    73. @Override
    74. public boolean equals(Object obj) {
    75. if (this == obj)
    76. return true;
    77. if (obj == null)
    78. return false;
    79. if (getClass() != obj.getClass())
    80. return false;
    81. RegenObject other = (RegenObject) obj;
    82. if (blocks == null) {
    83. if (other.blocks != null)
    84. return false;
    85. } else if (!blocks.equals(other.blocks))
    86. return false;
    87. if (task == null) {
    88. if (other.task != null)
    89. return false;
    90. } else if (!task.equals(other.task))
    91. return false;
    92. return true;
    93. }
    94. }
    95.  


    if someone could explain me what is 'corrupting' the resulting behaviour I'm very happy thanks:)
     
Thread Status:
Not open for further replies.

Share This Page