Solved a anoying problem with a boolean

Discussion in 'Plugin Development' started by xize, Apr 24, 2014.

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

    xize

    Hello,

    so I'm trying to create a switch on my custom portals however when I pull the lever first all the portal blocks are showed, but when I turn off the switch the portal blocks goes away one for one rather than all at once, so I came to the conclusion something is wrong with the boolean since its still true while it should be changed to false, so the BlockPhysicsEvent will not be cancelled.

    my code please look at the replace boolean:

    Code:
    public class PortalActivateEvent implements Listener {
        public boolean replace = false;
        @EventHandler
        public void onPowered(BlockRedstoneEvent e) {
            List<Portal> portals = Arrays.asList(Configuration.getPortalConfig().getPortals().values().toArray(new Portal[Configuration.getPortalConfig().getPortals().size()]));
            for(Portal portal : portals) {
                if(doesMatch(e.getBlock(), portal)) {
                    for(Block block : portal.getInnerBlocks()) {
                        if(block.getType() == Material.AIR && block.getType() != Material.PORTAL) {
                            if(!replace) {replace = true;}
                            block.setType(Material.PORTAL);
                        } else if(block.getType() == Material.PORTAL && block.getType() != Material.AIR) {
                            if(replace) {replace = false;}
                            block.setType(Material.AIR);
                        }
                    }
                    
                }
            }
            //replace = false;
        }
        @EventHandler
        public void psycic(BlockPhysicsEvent e) {
            if(e.isCancelled()) {
                return;
            }
            
            if(replace) {
                if(e.getBlock().getType() == Material.PORTAL) {
                    e.setCancelled(true);
                }
            }
        }
        public boolean doesMatch(Block block, Portal portal) {
            BlockFace[] faces = {BlockFace.SELF, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
            for(BlockFace face : faces) {
                Block newblock = block.getRelative(face);
                List<Block> blocks = Arrays.asList(portal.getBlocks());
                if(blocks.contains(newblock)) {
                    return true;
                }
            }
            return false;
        }
    }
    
    I'm still clueless why the boolean isn't changed from true to false when using the lever, ive did some debugging tests and the blocks are found but the boolean is incorrectly set in that for loop.

    thanks for the help:)
     
  2. Offline

    Garris0n

    if(block.getType() == Material.AIR && block.getType() != Material.PORTAL)
    if(block.getType() == Material.PORTAL && block.getType() != Material.AIR)
    Both of these are completely redundant.
    Anyway, what are you even trying to do? You're just setting the boolean value to whatever the last block happens to be. But what is it for?
     
  3. Offline

    xize

    Garris0n

    thats true, but the idea is to replace all blocks to portal blocks if it is air, and when it is a portal block change it back to air however I got it sort of working now but need some futher testing.

    the reason I use a boolean is for the BlockPhysics because when I do setType(Material.PORTAL) I need to cancel that event.
     
  4. Offline

    Garris0n

    So what does the variable represent?
     
  5. Offline

    xize

    Garris0n

    if the variable replace is true it should cancel the BlockPsycicsEvent however I think it doesn't work as how ive implemented it:p

    i've updated my code:

    Code:
    public class PortalActivateEvent implements Listener {
        public boolean replace = false;
        @EventHandler
        public void onPowered(BlockRedstoneEvent e) {
            List<Portal> portals = Arrays.asList(Configuration.getPortalConfig().getPortals().values().toArray(new Portal[Configuration.getPortalConfig().getPortals().size()]));
            if(e.getNewCurrent() > 0 && e.getOldCurrent() == 0) {
                for(Portal portal : portals) {
                    replace = true;
                    if(doesMatch(e.getBlock(), portal)) {
                        for(Block block : portal.getInnerBlocks()) {
                            if(block.getType() == Material.AIR && block.getType() != Material.PORTAL) {
                                block.setType(Material.PORTAL);
                            }
                        }
                        //if the portal blocks are placed uncancel the physics event, however this won't work, or the event is not called yet.
                        replace = false;
                        break;
                    }
                }    
            } else if(e.getNewCurrent() == 0 && e.getOldCurrent() > 0) {
                for(Portal portal : portals) {
                    if(doesMatch(e.getBlock(), portal)) {
                        replace = false;
                        for(Block block : portal.getInnerBlocks()) {
                            if(block.getType() == Material.PORTAL && block.getType() != Material.AIR) {
                                block.setType(Material.AIR);
                            }
                        }
                        break;
                    }
                }
            }
        }
        @EventHandler
        public void psycic(BlockPhysicsEvent e) {
            if(e.isCancelled()) {
                return;
            }
            System.out.print("is replace true?: " + replace);
            if(replace) {
                if(e.getBlock().getType() == Material.PORTAL) {
                    e.setCancelled(true);
                }
            } else {
                if(e.getBlock().getType() == Material.PORTAL) {
                    e.setCancelled(false);
                }
            }
        }
        public boolean doesMatch(Block block, Portal portal) {
            BlockFace[] faces = {BlockFace.SELF, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
            for(BlockFace face : faces) {
                Block newblock = block.getRelative(face);
                List<Block> blocks = Arrays.asList(portal.getBlocks());
                if(blocks.contains(newblock)) {
                    return true;
                }
            }
            return false;
        }
    }
    
    the problem now is when I do replace = false; at the end of the loop where I'm sure all portal blocks 'shoulded' be placed the BlockPhysicsEvent didn't cancelled the portal blocks, actually this confuses me:p
     
  6. Offline

    Garris0n

    If you're looping through each block and setting the value again, the value is only going to be whatever the value of the last block was. That's why I don't understand what you're trying to make it represent.
     
    xize likes this.
  7. Offline

    xize

    Garris0n

    you are correct, I now see what you mean, it seems the boolean is working but when I cancel it, it actually goes back to a older blockstate so if the block whas suposed to be a AIR block and then got replaced by a PORTAL it always reverse back to AIR because the BlockPhysicsEvent cancels, i've got it fixed now by just doing the checks in the BlockPhysicsEvent and it works thanks:D
     
Thread Status:
Not open for further replies.

Share This Page