Code does oposite of what its supposed to

Discussion in 'Plugin Development' started by Williamsson, Aug 5, 2011.

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

    Williamsson

    Uhm, my first goal with my plugin is to prevent the use of TNT to permissiongroups without the permission node preventer.allow.tnt

    But, when I don't have the permisson node, I can place tnt, but all other blocks are blocked.
    With the node, I can't place anything.

    Here's the part of the code I guess you are interested in:

    Main class

    Code:
    @Override
        public void onEnable(){
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.BLOCK_PLACE, this.blockListener, Event.Priority.Normal, this);
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
        setupPermissions();
        }    
    PreventerBlockListener class:

    Code:
    public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlockPlaced();
            if (((block.getType() == Material.TNT) && (!Preventer.permissionHandler.has(player, "preventer.allow.tnt")))) {
                return;
              }
            //Checks if the player is placing TNT, and if the player has the permission node, then does nothing.
            /*else if ((player.isOp())) {
                return;
            }
            //Does nothing if the player is OP*/
            else {
                block.setTypeId(0);
                player.sendMessage(ChatColor.RED + ("You don't have permission to place TNT!"));
              }
            //Else it sets the placed block to air, and gives the player his TNT back. Or, the idea is to do so at least..
        }
    I do have a github acc, but I cant however set it up until monday, since I'm far from my ordinary computer, therefore copy-pastes or pastebin is what'll have to do until then.

    What seems to be the problem with my code?
    I can pastebin the whole if needed.
     
  2. Offline

    The_Guest

    The problem is your if statement. If the block isn't TNT, it will default down to the else statement, and remove the placed block or do whatever.

    What you want to do is have:

    if (block.getType() == Material.TNT)
    {
    ...Check permissions, blah blah
    }
    else
    {
    Do nothing, or rather, just remove this else.
    }

    So:

    Code:
    
    public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlockPlaced();
    
    if(block.getType() == Material.TNT)
    {
            if ((!Preventer.permissionHandler.has(player, "preventer.allow.tnt")))) {
                return;
              }
            //Checks if the player is placing TNT, and if the player has the permission node, then does nothing.
            /*else if ((player.isOp())) {
                return;
            }
            //Does nothing if the player is OP*/
            else {
                block.setTypeId(0);
                player.sendMessage(ChatColor.RED + ("You don't have permission to place TNT!"));
              }
            //Else it sets the placed block to air, and gives the player his TNT back. Or, the idea is to do so at least..
    }
        }
     
  3. Offline

    Williamsson

    It worked! :D
    But the node preventer.allow.tnt actually denies the use of tnt, I'll try and solve that one myself.

    Question: How do I do so that the TNT won't ignite?
    Noticed that if I place tnt near a redstone current, it will ignite and not be removed.
    Besides that it works fine.
     
  4. Don't just return, you need to cancel the event:

    event.setCancelled(true);
     
  5. Offline

    Pencil

    Bro don't set the block to air (0). Cancel the event like

    set.Cancelled(true) that way it never gets placed in the first place. Now it gets placed and then changed to air.

    gawd ur post just appeared now XD

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

    Williamsson

    That sure makes sense! :p

    But, this is how I have it atm, and it stills places the tnt:(

    Code:
        public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlockPlaced();
    
            if(block.getType() == Material.TNT)
    {
            if ((!Preventer.permissionHandler.has(player, "preventer.allow.tnt"))) {
                event.setCancelled(false);
              }
            //Checks if the player is placing TNT, and if the player has the permission node, then does nothing.
            else if ((player.isOp())) {
                event.setCancelled(false);
            }
            //Does nothing if the player is OP
            else {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + ("You don't have permission to place TNT!"));
              }
            //Else it cancells the event and gives the player his TNT back. Or, the idea is to do so at least..
    }
     
  8. Offline

    Pencil


    Give this a try:

    Code:
        public void onBlockPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            Block block = event.getBlockPlaced();
    
            if(block.getType() == Material.TNT)
    {
            if ((Preventer.permissionHandler.has(player, "preventer.allow.tnt"))) {
    
            }
    
            else {
    
                if ((player.isOp())) {
    
                }else{
    
                    event.setCancelled(true);
                    player.sendMessage(ChatColor.RED + ("You don't have permission to place TNT!"));
                }
      
                event.setCancelled(true);
            }
              }
        }
    
        }
         
     
  9. Offline

    Williamsson

    Everything works fine with that, the node "preventer.alllow.tnt" actually allows tnt now, and not the other way around.
    However TNT still ignites from redstone, before it isn't placed. Is there another code-piece for that, or should event.setCancelled(true) fix that too?
     
  10. Offline

    Pencil

    Nope, I'm not actually 100% sure of how to approach that problem the right way. Basically you could check if there are redstone pieces/torches next to the tnt and then disallow placement, but I doubt that will work as we're already cancelling the event D:

    You will have to see if you can cancel the PlayerInteract event D: And see if that works, but I'm not entirely sure.

    But just fyi, when you block TNT with WorldGuard it doesn't stop redstone activation either :) So you aren't the only one with that problem :p
     
  11. Offline

    Williamsson

    Oh :p
    I'll see if I canprevent users from even having it in their inventory, or at least in hand, that should make it impossible for them to place it :3
    But on the other hand, that would make some quests on our server pretty useless... Damn D: would be nice to have something that deactivates tnt activation.


    Many thanks to you, and others further up! :)
     
Thread Status:
Not open for further replies.

Share This Page