Command Block Interact event issue!

Discussion in 'Plugin Development' started by uyscutix, Sep 6, 2017.

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

    uyscutix

    Hello, I'm making a system where command blocks only work on players added to my plugin ArrayList. But the following code doesn't work.

    How do I make interact events only work if a player right clicks a block already placed on the ground to do something? The player interaction event only seems to work if a player is right clicking the filtered block (command block) while holding it in their inventory. That's not what I want since I used the BlockPlaceEvent and BlockBreakEvent for that. Now I want to make it so interactions only work if a player clicks on a block placed, not a block they're holding in their inventory.

    The code so far:
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e)
        {    
            Action a = e.getAction();
           
            if (a != Action.PHYSICAL)
            {
                if (e.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                    return;
                }
               
                final Player player = e.getPlayer();
               
                switch (e.getMaterial())
                {
                    case COMMAND:
                    {
                        if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                        {
                        }
                        else
                        {
                            player.closeInventory();
                            e.setCancelled(true);
                            player.sendMessage(ChatColor.RED + "You are not authorised to utilize command blocks.");
                        }
                    }
                   
                    case COMMAND_CHAIN:
                    {
                        if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                        {
                        }
                        else
                        {
                            player.closeInventory();
                            e.setCancelled(true);
                            player.sendMessage(ChatColor.RED + "You are not authorised to utilize command blocks.");
                        }
                    }
                   
                    case COMMAND_REPEATING:
                    {
                        if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                        {
                        }
                        else
                        {
                            player.closeInventory();
                            e.setCancelled(true);
                            player.sendMessage(ChatColor.RED + "You are not authorised to utilize command blocks.");
                        }
                    }
                }
            }
        }
    
     
  2. This is why:
    Code:
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK)
    {
    return;
    }
    
    Also, I rewrote your code. I am not 100% sure all the syntax is correct, but It should be good.

    https://hastebin.com/qudumegowe.java
     
    Last edited: Sep 6, 2017
  3. Offline

    uyscutix

    Doesn't work. I opened up a command block placed on the ground by right clicking and it didn't run the code to close inventory and send unauthorized permission message.

    Here was the code so far (I had to change a bit since this plugin is for a cracked server):
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e)
        {    
            Action action = e.getAction();
           
            if (action != Action.RIGHT_CLICK_BLOCK) 
            { 
                return; 
            }
           
            Player player = e.getPlayer();
           
            if (e.getMaterial() == Material.COMMAND_REPEATING || e.getMaterial() == Material.COMMAND_CHAIN || e.getMaterial() == Material.COMMAND)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    player.closeInventory();
                    player.getInventory().remove(Material.COMMAND);
                    player.getInventory().remove(Material.COMMAND_CHAIN);
                    player.getInventory().remove(Material.COMMAND_REPEATING);
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
           
        }
    
    The code above only works if holding a command block again in hand, and if opening a command block placed on ground, it does nothing unless holding a command block in hand too.

    The part that does work however is if someone tries to place a command block or break one that the whitelisted players on the ArrayList made. The code that does work is below (not sure though if this could be affecting the interaction event):

    Code:
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e)
        {
            Player player = e.getPlayer();
           
            Block block = e.getBlock();
            Material material = block.getType();
           
            if (material == Material.COMMAND)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
           
            if (material == Material.COMMAND_CHAIN)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
           
            if (material == Material.COMMAND_REPEATING)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
        }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e)
        {
            Player player = e.getPlayer();
           
            Block block = e.getBlock();
            Material material = block.getType();
           
            if (material == Material.COMMAND)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
            else if (material == Material.COMMAND_CHAIN)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
            else if (material == Material.COMMAND_REPEATING)
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
        }
    
     
  4. Offline

    FabeGabeMC

    getClickedBlock() is what you are looking for.
    getMaterial() returns the material of the item in hand.
    To convert the block to Material, of course you must use Block#getType().
     
  5. Offline

    uyscutix

    It's still not working after testing it, I need more help. :(

    Why is it not working even when I changed the variable to e.getClickedBlock()
    the code is not even running when I click inside a command block.

    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e)
        {   
            Action action = e.getAction();
          
            if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
            {
                return;
            }
    
            Player player = e.getPlayer();
            Block block = e.getClickedBlock();
    
            if (block.equals(Material.COMMAND) || block.equals(Material.COMMAND_CHAIN) || block.equals(Material.COMMAND_REPEATING))
            {
                if (Command_sys.COMMAND_BLOCK.contains(player.getName()))
                {
                }
                else
                {
                    player.closeInventory();
                    player.getInventory().remove(Material.COMMAND);
                    player.getInventory().remove(Material.COMMAND_CHAIN);
                    player.getInventory().remove(Material.COMMAND_REPEATING);
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.RED + "You are not authorized to utilize command blocks.");
                }
            }
        }
    
     
  6. I've tried to improve your code and you've completely ignored me.
    1. You didn't listen to me when I told you if you return from the RIGHT_CLICK_BLOCK the event won't trigger.
    2. You didn't listen to @FabeGabeMC when he told you check if block.getType() == Material.COMMAND
    3. You're still using empty statements and an else statement. Purely disgusting since I told you to not do that.
    4. You're not checking if the player has the command block, the chain command block, or the repeating command block, so don't remove them until you can prove within the code that they have it.
    5. Just use the collection/arraylist variable for Command_sys.COMMAND_BLOCK or rename the variable AND THE CLASS to meet java conventions. I'd recommend renaming the class CommandSys or SysCommand (assuming it is a command under the name SYS) and the list ACCEPTABLE_PLAYERS.
    6. You're using usernames and not uuids <screams>
     
    Last edited by a moderator: Sep 7, 2017
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page