[Mapping] avoid Stack Overflowes

Discussion in 'Plugin Development' started by TheA13X, Nov 28, 2013.

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

    TheA13X

    Hello once again people.
    I have a little problem with my worldmapper plugin and it is what the title says.
    Let me describe what I did:
    1. I get the size of the loaded world.
    2. I made a method with a switch for every material I want to notice in it(I don't want Blocks like shrubs, flowers levers etc.)
    3. As default case (i.e. if the Block is from a material I don't need) I call the method again with the block under the last one. (though I knew this is a bad idea :D )
    thats how it's looks like:
    Code:java
    1.  
    2. private static String typeToString(Block b) {
    3. String tname="";
    4. if(b.getChunk().isLoaded()){
    5. switch(b.getType()){
    6. case BEDROCK:
    7. tname="BEDROCK";
    8. break;
    9. case BIRCH_WOOD_STAIRS:
    10. tname="BIRCH_STAIRS";
    11. break;
    12. case BOOKSHELF:
    13. tname="BOOKSHELF";
    14. break;
    15. case BRICK:
    16. tname="BRICK";
    17. break;
    18. case BRICK_STAIRS:
    19. tname="BRICK_STAIRS";
    20. break;
    21. case BURNING_FURNACE:
    22. tname="FURNACE";
    23. break;
    24. case CACTUS:
    25. tname="CACTUS";
    26. break;
    27. //...
    28. case WOOL:
    29. DyeColor colorwl=((Wool)b.getState().getData()).getColor();
    30. switch(colorwl){
    31. case WHITE:
    32. tname="WHITE_WOOL";
    33. break;
    34. case ORANGE:
    35. tname="ORANGE_WOOL";
    36. break;
    37. case BLACK:
    38. tname="BLACK_WOOL";
    39. break;
    40. case BLUE:
    41. tname="BLUE_WOOL";
    42. break;
    43. case BROWN:
    44. tname="BROWN_WOOL";
    45. break;
    46. case CYAN:
    47. tname="CYAN_WOOL";
    48. break;
    49. case GRAY:
    50. tname="GRAY_WOOL";
    51. break;
    52. case GREEN:
    53. tname="GREEN_WOOL";
    54. break;
    55. case LIGHT_BLUE:
    56. tname="LIGHT_BLUE_WOOL";
    57. break;
    58. case LIME:
    59. tname="LIME_WOOL";
    60. break;
    61. case MAGENTA:
    62. tname="MAGENTA_WOOL";
    63. break;
    64. case PINK:
    65. tname="PINK_WOOL";
    66. break;
    67. case PURPLE:
    68. tname="PURPLE_WOOL";
    69. break;
    70. case SILVER:
    71. tname="LIGHT_GRAY_WOOL";
    72. break;
    73. case YELLOW:
    74. tname="YELLOW_WOOL";
    75. break;
    76. case RED:
    77. tname="RED_WOOL";
    78. break;
    79. }
    80. break;
    81. case WORKBENCH:
    82. tname="WORKBENCH";
    83. break;
    84. default:
    85. tname=typeToString(b.getRelative(BlockFace.DOWN));
    86. break;
    87. }
    88. }
    89. else{
    90. tname="nothing";
    91. }
    92. return tname;
    93. }


    Is there another way which don't create a stack overflow?
     
  2. 1: Its not a stack overflow, its a stack trace
    2: use try/catch statements
    like this:
    Code:
    try {
    //stuff
    } catch (Exception e){
    //do stuff with the exception
    }
    but, it can be any exception you want!
     
  3. Offline

    Skyost

    WTF !?
    Why not ?
    Code:
    String tname = block.getType().name();
     
  4. Offline

    TheA13X

    Skyost
    because I don't want the name of the material, but my own string for it.
    @Datdenkikniet
    No, it IS a Stack Overflow:
    Code:
     Exception in thread "Mapper"
    java.lang.StackOverflowError
            at org.bukkit.craftbukkit.v1_6_R3.util.LongHashSet.remove(LongHashSet.java:132)
            at org.bukkit.craftbukkit.v1_6_R3.util.LongHashSet.remove(LongHashSet.java:128)
            at net.minecraft.server.v1_6_R3.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:88)
            at net.minecraft.server.v1_6_R3.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:84)
            at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.getChunkAt(CraftWorld.java:118)
            at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.getBlockAt(CraftWorld.java:82)
            at org.bukkit.craftbukkit.v1_6_R3.block.CraftBlock.getRelative(CraftBlock.java:161)
            at org.bukkit.craftbukkit.v1_6_R3.block.CraftBlock.getRelative(CraftBlock.java:169)
            at org.bukkit.craftbukkit.v1_6_R3.block.CraftBlock.getRelative(CraftBlock.java:165)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
            at inc.A13X.SSW.Mapper.typeToString(Mapper.java:602)
     
  5. Offline

    Minnymin3

    Well how may blocks are you doing this for?
     
  6. Offline

    TheA13X

    I do it for every goddamn Block, which is loaded.

    Edit:
    Thats not true. I'm not THAT crazy.
    It's just every goddamn surface block, which is in a loaded Chunk.

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

    Rocoty

    Your method is being called recursively for every block which you did not list in the outer switch-statement...and over and over again all the way down past the bedrock into the void until the stack overflows...simple as that.
     
  8. Offline

    TheA13X

    Rocoty that COULD be true. But I don't think so.
    1. Bedrock is in my caselist and there is ALWAYS bedrock down there.
    2. I save the string as a String array in a File and when I read this file back the array looks complete.
    Edit: I don't know how it REALLY works, but the plan was that if the block is not in my caselist it read the material from the Block under the last one.
    maybe it doesn't work because it would call the default case next time automatically, but I don't think so. It's simply unlogical.
     
  9. Offline

    Rocoty

    Okay. New idea. In the default case block, before the recursive call, log or otherwise print the y position of the block to see how far down it is before the stack overflows.
     
  10. Offline

    TheA13X

    Rocoty
    the number don't goes under 62 so it works :D.
    And it looks like the overflow error was there beacause the Notebook I was using was to slow or something like that.
    This time I used a server and it worked.
    Anyway it would be nice to find a way for this plugin to work on slower devices...
     
  11. Offline

    Rocoty

    In that case I would recommend you loop instead of doing recursive method calls TheA13X
     
  12. Offline

    LucasEmanuel

    What StackOverflow basically means is that your code is trying to schedule more tasks for the processor than it can keep in memory. Basically, you are making the stack of stuff to do overflow with all of your recursive calls. :)
     
Thread Status:
Not open for further replies.

Share This Page