Solved Send message on chest interactment

Discussion in 'Plugin Development' started by PrayRNGesus, Apr 19, 2020.

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

    PrayRNGesus

    I want to send a message to someone when they try to open a chest, while the noDrop command is enabled.

    Context: I am making a simple plugin that enables/disabled item drops! When noDrop is enabled, I don't want people to be able to open chest so they can't store items. I accomplished that, but now I want to send them a message if they try to open the chest. My code atm is

    Code:
     @EventHandler
    
        public void onChestOpen(InventoryOpenEvent event3) {
            if (event3.getInventory().getType().equals(InventoryType.CHEST)) {
                if (noDrop == true)
                event3.setCancelled(true);
            } else {
                event3.setCancelled(false);
            }
    
        @EventHandler
    
            public void onChestInteract(PlayerInteractEvent event4) {
                if (noDrop == true) {
            //I want to send them a message
            } else {
                return;
            }
        }
    } 
    I am a bit new to coding so help would be appreciated, thanks!
     
  2. Offline

    yPedx

    @PrayRNGesus
    You shouldn't use PlayerInteractEvent to check it the way you have, because it'll cause it to send the message to any player interacting with anything if the noDrop boolean is true. (unless you check clicked block type)

    You can accomplish what you desire by using the InventoryOpenEvent, and just putting the message beneath event3.setCancelled(true);
    I don't see why you have an } else { statement in your InventoryOpenEvent if the event is going to be cancelled anyway. Below is what I'd recommend doing.

    Code:Java
    1. @EventHandler
    2. public void onChestOpen(InventoryOpenEvent event3) {
    3.  
    4. Player player = event3.getPlayer();
    5.  
    6. if (noDrop) {
    7. event3.setCancelled(true);
    8. player.sendMessage("You cannot open this chest right now!");
    9. }
    10. }
     
  3. Offline

    Strahan

    I'd also add that you said your plugin prevents drops, but it didn't say anything about picking up items. I'd personally design it to let them open the chest, just not to actually deposit items into it. That way you still allow them to retrieve stuff if they wish.
     
  4. Offline

    PrayRNGesus

    The goal is to prevent dropping items and not allowing people to open the chest. Thanks for all the help, I have fixed it :)
     
Thread Status:
Not open for further replies.

Share This Page