Solved need some help, persistance objects or sort of

Discussion in 'Plugin Development' started by xize, Jul 14, 2014.

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

    xize

    Hello,

    I'm having a little trouble understanding the logic for example in a custom object.

    the thing what ive recovered is whenever I make a custom object with the Player instance through the constructor it looks to be persistance even when the player leaves the server the Player object is still alive...

    my code:

    Code:java
    1.  
    2. public class SchematicBuilder {
    3. private final Schematic schematic;
    4. private final Location loc;
    5. private final Location playerLoc;
    6. private final Player player;
    7. private boolean warning = false;
    8. private LinkedHashMap<Block, Integer> data = new LinkedHashMap<Block, Integer>();
    9. public SchematicBuilder(Schematic schematic, Location base, Player player) {
    10. this.schematic = schematic;
    11. this.loc = base;
    12. this.playerLoc = loc.clone();
    13. this.loc.setX(loc.getX()-(schematic.getWidth()/2));
    14. this.loc.setZ(loc.getZ()-(schematic.getLength()/2));
    15. this.player = player;
    16. }
    17. /**
    18.   * @author xize
    19.   * @param returns the schematic
    20.   * @return Schematic
    21.   */
    22. public Schematic getSchematic() {
    23. return schematic;
    24. }
    25. /**
    26.   * @author xize
    27.   * @param slowly generates the schematic from bottom to highest.
    28.   */
    29. public void startGeneration(final EntityType type) {
    30. new BukkitRunnable() {
    31. private Iterator<Entry<Block, Integer>> it;
    32. private LivingEntity entity;
    33. public void setData() {
    34. for (int y = 0; y < schematic.getHeight(); y++){
    35. for(int x = 0; x < schematic.getWidth(); x++){
    36. for (int z = 0; z < schematic.getLength(); ++z){
    37. Location temp = loc.clone().add(x, y, z);
    38. //Location temp = loc.clone().add(x/2, y, z/2); <- pastes the schematic but 1/² smaller this is per accident but pretty cool.
    39. //Location loc = new Location(temp.getWorld(), temp.getBlockX()/2, temp.getBlockY(), temp.getBlockZ()/2);
    40. Block block = temp.getBlock();
    41. int index = y * schematic.getWidth() * schematic.getLength() + z * schematic.getWidth() + x;
    42. if(getMaterial(schematic.getBlocks()[index]) != Material.AIR) {
    43. data.put(block, index);
    44. }
    45. }
    46. }
    47. }
    48. }
    49. public void setupBuilderEntity() {
    50. if(this.entity instanceof LivingEntity) {
    51. return;
    52. } else {
    53. this.entity = (LivingEntity) playerLoc.getWorld().spawnEntity(playerLoc, type);
    54. this.entity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
    55. this.entity.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1));
    56. this.entity.setCustomName(ChatColor.GOLD + "[ManCo]"+ChatColor.WHITE + "builder!");
    57. this.entity.setCustomNameVisible(true);
    58. }
    59. }
    60. @SuppressWarnings("deprecation")
    61. @Override
    62. public void run() {
    63. if(data.isEmpty()) {
    64. setData();
    65. }
    66.  
    67. if(!(player instanceof Player)) {
    68. entity.remove();
    69. data.clear();
    70. cancel();
    71. return;
    72. }
    73. setupBuilderEntity();
    74. if(!(it instanceof Iterator)) {
    75. this.it = data.entrySet().iterator();
    76. }
    77. if(it instanceof Iterator) {
    78. if(it.hasNext()) {
    79. Entry<Block, Integer> entry = it.next();
    80. Block block = entry.getKey();
    81. int index = entry.getValue();
    82. Material dataValue = getMaterial(schematic.getBlocks()[index]);
    83. byte subValue = schematic.getData()[index];
    84. if(entity instanceof Enderman) {
    85. Enderman enderman = (Enderman) entity;
    86. if(subValue == 0) {
    87. enderman.setCarriedMaterial(new MaterialData(dataValue));
    88. } else {
    89. enderman.setCarriedMaterial(new MaterialData(dataValue.getId(), subValue));
    90. }
    91. }
    92. if(block.getType() == Material.AIR) {
    93. try {
    94. if(!block.getChunk().isLoaded()) {
    95. block.getChunk().load();
    96. }
    97. if(subValue == 0) {
    98. block.setType(dataValue);
    99. } else {
    100. block.setTypeIdAndData(dataValue.getId(), subValue, true);
    101. }
    102. saveRollback(block, player);
    103. block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, dataValue);
    104. entity.teleport(block.getRelative(BlockFace.UP).getRelative(BlockFace.UP).getLocation());
    105. entity.setHealth(entity.getMaxHealth());
    106. }catch(NullPointerException e) {e.printStackTrace();}
    107. }
    108. it.remove();
    109. data.remove(block);
    110. } else {
    111. entity.remove();
    112. data.clear();
    113. cancel();
    114. }
    115. }
    116. }
    117. }.runTaskTimer(ManCo.getPlugin(), 0L, 1L);
    118. }
    119. @SuppressWarnings("deprecation")
    120. public Material getMaterial(int id) {
    121. try {
    122. Material mat = Material.getMaterial(id);
    123. mat.getId();
    124. return mat;
    125. } catch(NullPointerException e) {
    126. if(!warning) {
    127. ManCo.log(LogType.SEVERE, "Warning we don't know anything about data value " + id + " at schematic " + schematic.getName() + " perhaps this schematic is made in a newer minecraft version or mod?");
    128. ManCo.log(LogType.SEVERE, "however this block will be changed to air, and the generation could have unpredictal results.");
    129. }
    130. return Material.AIR;
    131. }
    132. }
    133. private void saveRollback(Block block, Player p) {
    134. if(p instanceof Player) {
    135. System.out.print("player still active");
    136. Bukkit.getPluginManager().callEvent(new BlockPlaceEvent(block, null, block, p.getItemInHand(), p, true));
    137. }
    138. }
    139. }
    140.  


    how is it even possible that the method saveRollback still says the player is active while the player already is gone from the server?, ive tried to check if the player is still an instanceof in the scheduler to debug it but it still claims its always an instanceof, could it be that the scheduler is litterly hostaging the player object?:p

    thanks for the help though, I think this is something really usefulls to learn about.
     
  2. Offline

    Traks

    Checking if a Player object can be cast to a Player object will always return true, so p instanceof Player will always return true. To check if a Player is online, you need to use OfflinePlayer#isOnline() or Player#isOnline()
     
  3. Offline

    Rocoty

    When the player leaves the server, the connection is broken. But that's mostly as far as it goes. There is no connection between the server and the client anymore, but the Player object is still in memory. And it will continue to be in memory until the Garbage Collector has collected it. The garbage collector won't collect any object that has a strong reference. Which means the Player object will continue to exist at least until your SchematicBuilder stops referencing it.

    So, in answer to your question. Yes, the scheduler IS hostaging the Player object, and you should make sure the object is garbage collected to avoid memory leaks, by removing all references to it.
     
    xize likes this.
  4. Offline

    xize

    Traks

    well thats true, but if the player goes offline it should actually return null which for some reason doesn't happends D:
    I suspect it will not be garbage collected when it is in a constant use since I'm able to use its name to...

    edit
    solved :)
     
  5. Offline

    Rocoty

    I am afraid this is where you're wrong. The player disconnecting doesn't automatically nullify your reference. Mind you, there is a significant difference between an object and a reference. Please look into it. An object can never be null, but a variable can reference null.
     
  6. Offline

    xize

    Rocoty

    I know, my page wasn't refreshed when I typed that response, but thanks it explained alot though:)
     
  7. Offline

    ResultStatic

    xize store the players name or uuid, and get the player object by the name or uuid, thats what i do
     
Thread Status:
Not open for further replies.

Share This Page