I failing understand this error

Discussion in 'Plugin Development' started by xize, Nov 15, 2013.

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

    xize

    Hello,

    I'm having a really hard problem finding out why my plugin spits out this error!.
    whenever I interact with a [gate] sign it just works as usual without errors.

    but when I try my redstone event it also works but it spams alot of errors where I don't get what is going wrong either.

    my error (looped twice because its powered by 2 redstone wires I guess):
    http://pastebin.com/AmYKJJ32 pasted in pastebin because it whas very long aswell.

    my redstone event it self:
    Code:
    @EventHandler
    public void doRedStone(BlockPhysicsEvent e) {
      if(e.getBlock().getType() == Material.WOOD) {
      if(e.getBlock().isBlockPowered()) {
        Block blockBehindSign = e.getBlock().getRelative(BlockFace.UP);
        Sign sign = getSignAttached(blockBehindSign);
        if(sign instanceof Sign) {
        //the sign is a instance of the Sign type so this couldn't be null here.
        if(ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase("[Gate]")) {
          if(GateShape.getGateShape(blockBehindSign)) {
          Block[] blocks = GateShape.getSelection(blockBehindSign);
          Block highestBlock = blocks[0];
          Block lowestBlock = blocks[1];
          toggleGate.ToggleGate(lowestBlock, highestBlock);
          }
        }
        }
      }
      }
    }
     
    public Sign getSignAttached(Block block) {
      for(BlockFace face : BlockFace.values()) {
      if(face != BlockFace.UP || face != BlockFace.DOWN || face != BlockFace.SELF) {
        if(block.getRelative(face).getType() == Material.WALL_SIGN) {
        return (Sign) block.getRelative(face).getState();
        }
      }
      }
      return null;
    }
    
    and my toggle for gates:

    Code:
    public static boolean toggle;
    public static ArrayList<Block> protectedFences = new ArrayList<Block>();
     
    public static boolean ToggleGate(Block lowestBlock, Block highestBlock) {
      toggle = false;
      if(lowestBlock.getX() == highestBlock.getX()) {
      //here we use the z axis
      if(highestBlock.getZ() < lowestBlock.getZ()) {
        for(int y = highestBlock.getY(); y >= lowestBlock.getY(); y--) {
        for(int z = highestBlock.getZ(); z < lowestBlock.getZ(); z++) {
          Block block = highestBlock.getWorld().getBlockAt(highestBlock.getX(), y, z);
          if(block.getType() == Material.AIR) {
          block.setType(Material.FENCE);
          protectedFences.add(block);
          toggle = true;
          } else if(block.getType() == Material.FENCE) {
          protectedFences.remove(block);
          block.setType(Material.AIR);
          toggle = false;
          }
        }
        }
      } else if(highestBlock.getZ() > lowestBlock.getZ()) {
        for(int y = highestBlock.getY(); y >= lowestBlock.getY(); y--) {
        for(int z = highestBlock.getZ(); z > lowestBlock.getZ(); z--) {
          Block block = highestBlock.getWorld().getBlockAt(highestBlock.getX(), y, z);
          if(block.getType() == Material.AIR) {
          block.setType(Material.FENCE);
          toggle = true;
          protectedFences.add(block);
          } else if(block.getType() == Material.FENCE) {
          protectedFences.remove(block);
          block.setType(Material.AIR);
          toggle = false;
          }
        }
        }
      }
      } else if(lowestBlock.getZ() == highestBlock.getZ()) {
      //here we use the x axis
      if(highestBlock.getX() < lowestBlock.getX()) {
        for(int y = highestBlock.getY(); y >= lowestBlock.getY(); y--) {
        for(int x = highestBlock.getX(); x < lowestBlock.getX(); x++) {
          Block block = highestBlock.getWorld().getBlockAt(x, y, highestBlock.getZ());
          if(block.getType() == Material.AIR) {
          block.setType(Material.FENCE);
          toggle = true;
          protectedFences.add(block);
          } else if(block.getType() == Material.FENCE) {
          protectedFences.remove(block);
          block.setType(Material.AIR);
          toggle = false;
          }
        }
        }
      } else if(highestBlock.getX() > lowestBlock.getX()) {
        for(int y = highestBlock.getY(); y >= lowestBlock.getY(); y--) {
        for(int x = highestBlock.getX(); x > lowestBlock.getX(); x--) {
          Block block = highestBlock.getWorld().getBlockAt(x, y, highestBlock.getZ());
          if(block.getType() == Material.AIR) {
          block.setType(Material.FENCE);
          toggle = true;
          protectedFences.add(block);
          } else if(block.getType() == Material.FENCE) {
          protectedFences.remove(block);
          block.setType(Material.AIR);
          toggle = false;
          }
        }
        }
      }
      }
      return toggle;
    }
     
    public static void saveGateStatesOnShutDown() {
      try {
      File f = new File(minebook.getPlugin().getDataFolder() + File.separator + "gates.db");
      if(f.exists()) {
        FileConfiguration con = YamlConfiguration.loadConfiguration(f);
        if(con.isSet("database")) {
        con.set("database", null);
        con.save(f);
        con.set("database", Serialize(protectedFences));
        con.save(f);
        protectedFences.clear();
        } else {
        con.set("database", Serialize(protectedFences));
        con.save(f);
        protectedFences.clear();
        }
      } else {
        FileConfiguration con = YamlConfiguration.loadConfiguration(f);
        con.set("database", Serialize(protectedFences));
        con.save(f);
        protectedFences.clear();
      }
      } catch(Exception e) {
      e.printStackTrace();
      }
    }
     
    public static ArrayList<String> Serialize(ArrayList<Block> block) {
      ArrayList<String> serialized = new ArrayList<String>();
      for(Block ablock : block) {
      serialized.add(ablock.getType().name()+","+ablock.getX()+","+ablock.getY()+","+ablock.getZ()+","+ablock.getWorld().getName());
      }
      return serialized;
    }
     
    public static ArrayList<Block> Deserialize(FileConfiguration con) {
      ArrayList<Block> blocks = new ArrayList<Block>();
      for(String serializedBlock : con.getStringList("database")) {
      String[] splitedName = serializedBlock.split(",");
      String MaterialName = splitedName[0];
      Integer x = Integer.parseInt(splitedName[1]);
      Integer y = Integer.parseInt(splitedName[2]);
      Integer z = Integer.parseInt(splitedName[3]);
      String world = splitedName[4];
      Location loc = new Location(Bukkit.getWorld(world), x, y, z);
      Block block = loc.getBlock();
      if(block.getType() == Material.valueOf(MaterialName)) {
        blocks.add(block);
      }
      }
      return blocks;
    }
     
    public static void loadGates() {
      try {
      File f = new File(minebook.getPlugin().getDataFolder() + File.separator + "gates.db");
      if(f.exists()) {
        FileConfiguration con = YamlConfiguration.loadConfiguration(f);
        ArrayList<Block> blocks = Deserialize(con);
        for(Block block : blocks) {
        protectedFences.add(block);
        }
      }
      } catch(Exception e) {
      e.printStackTrace();
      }
    }
    
    my normal sign interaction gate event for compairing:
    Code:
     @EventHandler
     public void onSignPlace(SignChangeEvent e) {
      if(e.getBlock().getType() == Material.WALL_SIGN) {
       if(e.getLine(0).equalsIgnoreCase("[gate]")) {
        if(e.getPlayer().hasPermission("minebook.canCreateGates")) {
         Sign sign = (Sign) e.getBlock().getState().getData();
         Block block = e.getBlock().getRelative(sign.getAttachedFace());
         if(GateShape.getGateShape(block)) {
          e.setLine(0, ChatColor.translateAlternateColorCodes('&', "&1[Gate]"));
          e.getPlayer().sendMessage(ChatColor.GREEN + "successfully created a Gate!");
         } else {
          e.getPlayer().sendMessage(ChatColor.RED + "this gate shape is invalid!");
          e.getBlock().breakNaturally();
          e.setCancelled(true);
         }
        } else {
         e.getBlock().breakNaturally();
         e.getPlayer().sendMessage(ChatColor.RED + "you are not allowed to make Gate signs!");
         e.setCancelled(true);
        }
       }
      }
     }
     
     @EventHandler
     public void onBreakFence(BlockBreakEvent e) {
      if(toggleGate.protectedFences.contains(e.getBlock())) {
       e.getPlayer().sendMessage(ChatColor.RED + "you are not allowed to mine these fences!");
       e.setCancelled(true);
      }
     }
     
     @EventHandler
     public void onSignGate(PlayerInteractEvent e) {
      if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
       if(e.getClickedBlock().getType() == Material.WALL_SIGN) {
        org.bukkit.block.Sign sign = (org.bukkit.block.Sign) e.getClickedBlock().getState();
        Sign s = (Sign) e.getClickedBlock().getState().getData();
        if(sign.getLine(0).contains("[Gate]")) {
         if(e.getPlayer().hasPermission("minebook.canUseGates")) {
          if(GateShape.getGateShape(e.getClickedBlock().getRelative(s.getAttachedFace()))) {
           Block[] blocks = GateShape.getSelection(e.getClickedBlock().getRelative(s.getAttachedFace()));
           Block highestBlock = blocks[0];
           Block lowestBlock = blocks[1];
           if(toggleGate.ToggleGate(lowestBlock, highestBlock)) {
            e.getPlayer().sendMessage(ChatColor.GREEN + "Gate toggled!");
           } else {
            e.getPlayer().sendMessage(ChatColor.GREEN + "Gate untoggled!");
           }
          } else {
           e.getPlayer().sendMessage(ChatColor.RED + "this gate does not longer work!");
          }
         } else {
          e.getPlayer().sendMessage(ChatColor.RED + "you don't have permission to do this!");
         }
        }
       }
      }
     }
     
     @EventHandler
     public void explosion(EntityExplodeEvent e) {
      for(Block block : e.blockList()) {
       if(toggleGate.protectedFences.contains(block)) {
        e.setCancelled(true);
       }
      }
     }
     
     @EventHandler
     public void piston(BlockPistonExtendEvent e) {
      for(Block block : e.getBlocks()) {
       if(toggleGate.protectedFences.contains(block)) {
        e.setCancelled(true);
       }
      }
     }
     @EventHandler
     public void piston(BlockPistonRetractEvent e) {
      if(toggleGate.protectedFences.contains(e.getRetractLocation().getBlock())) {
       e.setCancelled(true);
      }
     }
    
    actually I'm completely clueless why my sign interaction just works fine without these kind of errors and my redstone event gets fully nuts:p

    thanks for the help:)

    edited
    added my normal gate interaction event class for compairing.
     
  2. Offline

    Garris0n

    First of all, naming conventions(take note of method names), and use tabs for your indentation, not spaces.

    Second, your error is a stack overflow. Can you post your full code with one of the syntaxes that has line numbers or on pastebin? You have an infinite loop somewhere in that ToggleGate(which SHOULD be called toggleGate) method, but it's a bit hard to read with the indentation. Also, could you explain what you intend this code to do?
     
  3. Offline

    xize

    Garris0n
    basicly what the method does is check if there are fences inside a gate if its not it places fences within the gate shape else it changes the fences to air.

    the problem is I use the same toggleGate method in my PlayerInteractEvent and when I toggle it, it works fine on all coordinates and BlockFaces.

    but when I use toggleGate in the redstone event it is throwing these errors so I probally use the wrong block somewhere.

    at tv.mineinthebox.Minebook.GateUtil.toggleGate.ToggleGate(toggleGate.java:29)
    at tv.mineinthebox.Minebook.events.redstoneGate.doRedStone(redstoneGate.java:30)

    toggleGate:29 is at see commentairy tag:
    Code:java
    1.  
    2. public static boolean ToggleGate(Block lowestBlock, Block highestBlock) {
    3. toggle = false;
    4. if(lowestBlock.getX() == highestBlock.getX()) {
    5. //here we use the z axis
    6. if(highestBlock.getZ() < lowestBlock.getZ()) {
    7. for(int y = highestBlock.getY(); y >= lowestBlock.getY(); y--) {
    8. for(int z = highestBlock.getZ(); z < lowestBlock.getZ(); z++) {
    9. Block block = highestBlock.getWorld().getBlockAt(highestBlock.getX(), y, z);
    10. if(block.getType() == Material.AIR) {
    11. block.setType(Material.FENCE); //<--------------------- here is line 29
    12. protectedFences.add(block);
    13. toggle = true;
    14. } else if(block.getType() == Material.FENCE) {
    15. protectedFences.remove(block);
    16. block.setType(Material.AIR);
    17. toggle = false;
    18. }
    19. }
    20. }
    21.  


    redstoneGate:30 at:
    Code:
    toggleGate.ToggleGate(lowestBlock, highestBlock);
    
    I hope this helps

    edit
    I will show my fresh uploaded github files all the classes related with the redstone event.

    the redstone event it self.
    https://github.com/xize/Minebook/blob/HEAD/src/tv/mineinthebox/Minebook/events/redstoneGate.java

    the gate shape checker and of course where the method is which returns the cuboid coordinates which returns lowestBlock and highestBlock as a Block[] array which later will be used in the toggleGate method to form the gate.
    https://github.com/xize/Minebook/blob/HEAD/src/tv/mineinthebox/Minebook/GateUtil/GateShape.java

    the toggleGate method:
    https://github.com/xize/Minebook/blob/HEAD/src/tv/mineinthebox/Minebook/GateUtil/toggleGate.java

    I didn't change lines though in the hope this error can be found ive never had this before as the interact event works correct and the other gets terible wrong.
     
  4. Offline

    1Rogue Retired Staff

    Try printing your values for The .getY() and .getZ() on both the highest and lowest blocks When you have a crash.
     
  5. Offline

    xize

    1Rogue
    I tried setting the material types for highest to glowstone and lowest to cobblestone.

    first on the working PlayerInteractEvent without errors to compair the visual with the not working redstone event and it seems that works correct as it should be, then I did the same with the redstone event and I got exact the same behaveiour as how the working PlayerInteractEvent does it.

    frankly I think I use the wrong event as one of the errors in my pastebin also throws something with update() along the lines but not directly from my plugin but more from the event it self could it be that this event is designed before getState() get used?
     
Thread Status:
Not open for further replies.

Share This Page