stack overflow?? [SOLVED]

Discussion in 'Plugin Development' started by Tster, Jan 29, 2012.

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

    Tster

    Hi, I have a problem, which I could do with a general answer to. I have not supplied code, as it probably wont be necesarry

    Code:
    java.lang.StackOverflowError
        at sun.util.calendar.ZoneInfo.getTransitionIndex(ZoneInfo.java:298)
        at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:248)
        at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:225)
        at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2024)
        at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
        at java.util.Calendar.setTimeInMillis(Calendar.java:1109)
        at java.util.Calendar.setTime(Calendar.java:1075)
        at java.text.SimpleDateFormat.format(SimpleDateFormat.java:876)
        at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869)
        at java.text.DateFormat.format(DateFormat.java:278)
        at java.text.Format.format(Format.java:140)
        at org.bukkit.craftbukkit.util.ShortConsoleLogFormatter.format(ShortConsoleLogFormatter.java:45)
        at java.util.logging.StreamHandler.publish(StreamHandler.java:179)
        at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:88)
        at java.util.logging.Logger.log(Logger.java:481)
        at java.util.logging.Logger.doLog(Logger.java:503)
        at java.util.logging.Logger.log(Logger.java:592)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:473)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Tster
    A stack overflow occurs when you run out of memory. Sometimes this happens when the data set is very large. However, it can also occur as a bug, when you are calling methods recursively and it fails to ever reach the terminal condition, or you inadvertently call a method within it self.
     
  3. Offline

    Tster

    Code:
    package me.tster.OreGen;
     
    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
     
    public class OreGen extends JavaPlugin {
     
        private final PluginBlockListener blockListener = new PluginBlockListener(this);
        Logger log = Logger.getLogger("Minecraft");
        public static Configuration cfg;
        public static Integer coal;
        public static Integer iron;
        public static Integer lapis;
        public static Integer redstone;
        public static Integer gold;
        public static Integer diamond;
        public static Integer stone;
     
        public void onDisable() {
            log.info("OreGen has been disabled!");
        }
     
        public void onEnable() {
            cfg = getConfiguration();
            cfg.load();
            stone = cfg.getInt("stone", 70);
            coal = cfg.getInt("coal", 11);
            iron = cfg.getInt("iron", 8);
            lapis = cfg.getInt("lapis", 2);
            redstone = cfg.getInt("redstone", 5);
            gold = cfg.getInt("gold", 3);
            diamond = cfg.getInt("diamond", 1);
            cfg.save();
           
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_PHYSICS, blockListener, Priority.Normal, this);
           
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled: Plugin By Tster");
        }
       
    }
     
  4. Offline

    hatstand

    We need your block physics listener, that's where the error comes from.
     
  5. Offline

    bergerkiller

    Correction: A stack overflow occurs when the 'calling stack', which is a simple array of calls made in the current thread, reached max capacity and can't add more entries to the stack. This happens when you use recursion in such a manner that it never ends, for example:
    Code:
    public void foo() {
        foo();
    }
    This can happen in many ways, this is also possible:
    Code:
    public void a() {
        b();
    }
    public void b() {
        c();
    }
    public void c() {
        a();
    }
    Usually it is a bit harder to find out what is going wrong. Best is to look at your functions and the conditions you put at exiting the recursive loop.

    EDIT

    If you expect a function to 'run in itself' for more than 50 times, it is best to convert your recursion into a while loop, or for loop. This way you can avoid the stack trace exception.

    However, be careful, as this can cause an endless loop which freezes the server.
     
  6. Offline

    masteroftime

    It looks like you set the type of a block in the onBlockPhysics event which causes the onBlockPhysics event to be fired again. Then the type is again changed and the event is fired again and this then runs in an infinite loop until the maximum stack size is reached.

    You somehow have ot move the setType out of the event, or enclose it with a condition to stop the endless loop
     
  7. Offline

    Sagacious_Zed Bukkit Docs

  8. Offline

    bergerkiller

    masteroftime yup he is right. Best is to check if the type needs changing (getType() != ...) and only set the type then. You can also try to prevent a new physics event from happening by changing the block type directly. (I believe you set the last boolean of setTypeIdAndData to false, or you need to use the native world.setRawTypeId).
     
  9. Offline

    Tster

    Okay, legends ;D

    Code:
        public void onBlockPhysics(final BlockPhysicsEvent event){
            //Set the event as cancelled so we can handle it ourselves without having delays
            event.setCancelled(true);
            event.getBlock().setType(Material.STONE);
            oremap.put(OreGen.coal, Material.COAL_ORE);
            oremap.put(OreGen.iron, Material.IRON_ORE);
            oremap.put(OreGen.lapis, Material.LAPIS_ORE);
            oremap.put(OreGen.redstone, Material.REDSTONE_ORE);
            oremap.put(OreGen.gold, Material.GOLD_ORE);
            oremap.put(OreGen.diamond, Material.DIAMOND_ORE);   
            oremap.put(OreGen.stone, Material.STONE);
            final TreeMap sortedoremap = new TreeMap(oremap);
           
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable () {
                public void run (){
                    //Checks if the formed block is cobblestone
                    if (event.getBlock().getType() == Material.COBBLESTONE){
                        //If so then it generates a random number between 0 and 100,
                        //this is easiest as the user can configure the chance as a percentage
                        randomGen = new Random();
                        percentOre = randomGen.nextInt(100);
                        //Add one so we are dealing with 1 to 100 (easier concept to understand)
                        percentOre = percentOre + 1;
                        //Find the number that the percentage must be greater than for each ore
                        if (percentOre >= (Integer) sortedoremap.keySet().toArray()[0] && percentOre < (Integer) sortedoremap.keySet().toArray()[1]){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else if (percentOre >= (Integer) sortedoremap.keySet().toArray()[1] && percentOre < (Integer) sortedoremap.keySet().toArray()[2]){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else if (percentOre >= (Integer) sortedoremap.keySet().toArray()[2] && percentOre < (Integer) sortedoremap.keySet().toArray()[3]){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else if (percentOre >= (Integer) sortedoremap.keySet().toArray()[3] && percentOre < (Integer) sortedoremap.keySet().toArray()[4]){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else if (percentOre >= (Integer) sortedoremap.keySet().toArray()[4] && percentOre < (Integer) sortedoremap.keySet().toArray()[5]){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else if (percentOre >= (Integer) sortedoremap.keySet().toArray()[5] && percentOre < (100 - (Integer) sortedoremap.keySet().toArray()[6])){
                            event.getBlock().setType(Material.DIAMOND_BLOCK);
                        } else {
                            event.getBlock().setType(Material.STONE);
                        }
                    } else {
                        event.setCancelled(false);
                        event.getBlock().setType(Material.OBSIDIAN);
                    }
                }}, 20L);
     
     
        }
    }
    
    I assume I need an IF statement somewhere in there?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  10. Offline

    Tster

    bumpt', looking for some help, + another dev possibly
     
  11. Offline

    Tster

    bump ppp

    world.setRawTypeId(event.getBlock, Material.DIAMOND_BLOCK) or something of the sort?

    BTW, if anyone has knowledge of the new listeners and config, please tell me!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  12. Offline

    Tster

    Native?

    Error:
    Code:
    2012-02-18 11:59:49 [SEVERE] Could not pass event org.bukkit.event.block.BlockPhysicsEvent to OreGen
    java.lang.StackOverflowError
        at java.security.AccessController.doPrivileged(Native Method)
        at java.io.PrintWriter.<init>(PrintWriter.java:78)
        at java.io.PrintWriter.<init>(PrintWriter.java:62)
        at org.bukkit.craftbukkit.util.ShortConsoleLogFormatter.format(ShortConsoleLogFormatter.java:54)
        at java.util.logging.StreamHandler.publish(StreamHandler.java:179)
        at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:88)
        at java.util.logging.Logger.log(Logger.java:481)
        at java.util.logging.Logger.doLog(Logger.java:503)
        at java.util.logging.Logger.log(Logger.java:592)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:473)
        at net.minecraft.server.World.k(World.java:518)
        at net.minecraft.server.World.applyPhysics(World.java:502)
        at net.minecraft.server.World.update(World.java:467)
        at net.minecraft.server.World.setTypeId(World.java:439)
        at org.bukkit.craftbukkit.block.CraftBlock.setTypeId(CraftBlock.java:88)
        at org.bukkit.craftbukkit.block.CraftBlock.setType(CraftBlock.java:84)
        at me.tster.OreGen.PluginBlockListener.onBlockPhysics(PluginBlockListener.java:65)
        at org.bukkit.plugin.java.JavaPluginLoader$32.execute(JavaPluginLoader.java:488)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
    
    Traced the error to here:
    event.getBlock().setType(Material.OBSIDIAN);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  13. Offline

    bergerkiller

    Tster
    Code:
        public void onBlockPhysics(final BlockPhysicsEvent event){
            //Set the event as cancelled so we can handle it ourselves without having delays
            event.setCancelled(true);
            event.getBlock().setType(Material.STONE);
    block.setType() triggers the same onBlockPhysics event again. You need to check if this needs to be done at all (if event.getBlock().getType() != Material.STONE) and only do that then. Actually, you should never set block types in the physics event directly, as it can cause a physics loop.
     
Thread Status:
Not open for further replies.

Share This Page