Sending messages on InventoryClickEvent

Discussion in 'Plugin Development' started by Ricecutter0, Dec 24, 2016.

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

    Ricecutter0

    Hello,
    I have been working on this plugin for a while and I am stuck on sending a message when the player has placed or picked up an item in this custom inventory. I figured out how to send it when they just place it or pick it up physically, but I am stuck when it comes to the InventoryAction SWAP_WITH_CURSOR and shift-clicking an item in or out of the inventory. Below is the class I've been working on.

    Code:
    public class EditInventory implements Listener {
    
    Backpacks plugin; public EditInventory(Backpacks plugin){
    this.plugin = plugin;}
    
    @EventHandlerpublic void editInventory(InventoryClickEvent e){
    if(!plugin.openedBackpack.containsKey(e.getWhoClicked().getName())){
    return;}
    final String player;TreeMap<String, Integer> things = plugin.openedBackpack.get(e.getWhoClicked().getName());Set<String> thing = things.keySet();player = thing.toString(); if(player.equals(e.getWhoClicked().getName()))
    return; if(!plugin.backpacks.containsKey(player)){
    e.getWhoClicked().sendMessage("1"); return;}
    if(!e.getWhoClicked().hasPermission("backpacks.edit")){
    e.setCancelled(true); return;}
    Player p = (Player) e.getWhoClicked(); if(!p.hasPermission("backpacks.edit.notify")) {
    return;}
    Boolean pickedup;String action;String from_or_to;ItemStack item; if(!e.getSlotType().equals(InventoryType.SlotType.CONTAINER)){
    return;}
    switch (e.getAction()){
    case COLLECT_TO_CURSOR: if(inBackpack(e)){return;}else{pickedup = true;}break; 
    case PICKUP_ALL: if(inBackpack(e)){return;}else{pickedup = true;}break; 
    case PICKUP_ONE: if(inBackpack(e)){return;}else{pickedup = true;}break;
     case PICKUP_SOME: if(inBackpack(e)){return;}else{pickedup = true;}break;
     case PICKUP_HALF: if(inBackpack(e)){return;}else{pickedup = true;}break; 
    case PLACE_ALL: if(!inBackpack(e)){pickedup = false;;}else{return;}break;
     case PLACE_ONE: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case PLACE_SOME: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case DROP_ALL_SLOT: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case DROP_ALL_CURSOR: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case DROP_ONE_CURSOR: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case DROP_ONE_SLOT: if(!inBackpack(e)){pickedup = false;;}else{return;}break; 
    case SWAP_WITH_CURSOR: if(inBackpack(e)){return;}else{swap(e,player,p);}pickedup=true;break; case HOTBAR_SWAP: if(inBackpack(e)){return;}else{swap(e,player,p);}pickedup=true;break;
     case HOTBAR_MOVE_AND_READD: return;
     case MOVE_TO_OTHER_INVENTORY: if(!inBackpack(e)){pickedup = true;}else{pickedup= false;}break; 
    case NOTHING: return; default: pickedup = null;}
    if(pickedup == null){
    plugin.getLogger().severe("UNKNOWN ACTION WAS COMPLETED IN EDITINVENTORY."); return;}
    if(pickedup == true){
    action = " removed ";from_or_to = " from ";item = e.getCurrentItem();}
    else{
    action = " added ";from_or_to = " to ";item = e.getCurrentItem();}
    String name = item.getType().name().toLowerCase().replaceAll("_", " ");Bukkit.getServer().broadcast(p.getDisplayName() + " has" + action + item.getAmount() + " "+
    StringUtils.capitalize(name) +from_or_to+ player + "'s Backpack.", "backpacks.edit.receive");}
    
    private void swap(final InventoryClickEvent e, final String player, final Player p){
    new BukkitRunnable(){
    public void run() {
    String action;String from_or_to;ItemStack item;action = " added ";from_or_to = " to ";item = e.getCurrentItem(); if(item.getType() == Material.AIR){
    return;}
    String name = item.getType().name().toLowerCase().replaceAll("_", " ");Bukkit.getServer().broadcast(p.getDisplayName() + " has" + action + item.getAmount() + " "+
    StringUtils.capitalize(name) + from_or_to + player + "'s Backpack.", "backpacks.edit.receive");}
    }.runTask(plugin);}
    
    private boolean inBackpack(InventoryClickEvent e){
    if(e.getRawSlot() < e.getInventory().getSize())
    return false; return true;}


    Also if anyone believes I could do something in a "cleaner" way, please let me know! Thank you for reading and I appreciate any help.

    EDIT: I have found one REALLY REALLY messy way of doing it, would anyone be willing to help me find another way of doing it? I updated the code.
     
    Last edited: Dec 24, 2016
  2. Offline

    Zombie_Striker

    Instead of returning, encapsulate properly.

    Only create this variable when you need to initialize it.

    This is useless. Just use
    Code:
    Set<String> thing = things.keySet();
    Since Lists and Sets are "basically" the same thing, meaning they can be used the same way.

    Instead of returing for a all these cases, just encapsulate properly.

    Main problem: [Edited again] What is your issue? Is it that the message does not get sent when they shiftclick, press the 0-9 buttons? If so, post or highlight the bit of code that deals with the message.
     
  3. Offline

    Ricecutter0

    I'm sorry what do you mean by that?

    And the issue is that when you shift click an item into the backpack (from your own inventory) it's not sending a message. Below is that part of the code that sends the message.
    Code:
    if(pickedup == null){
                plugin.getLogger().severe("UNKNOWN ACTION WAS COMPLETED IN EDITINVENTORY.");
                return;
            }
            if(pickedup == true){
                action = " removed ";
                from_or_to = " from ";
                item = e.getCurrentItem();
            }
            else{
                action = " added ";
                from_or_to = " to ";
                item = e.getCurrentItem();
            }
            String name = item.getType().name().toLowerCase().replaceAll("_", " ");
            Bukkit.getServer().broadcast(p.getDisplayName() + " has" + action + item.getAmount() + " "+
                    StringUtils.capitalize(name) +from_or_to+ player + "'s Backpack.", "backpacks.edit.receive");
    Also I edited the code in the original post to include the entire class.
     
  4. Offline

    Zombie_Striker

    @Ricecutter0
    So instead of
    Code:
    if(  !  ...){
       return;
    }
      //now do stuff
    you would do
    Code:
    if(....){
      //do stuff
    }
    As for the message, are you sure the event is being triggered? Have you debugged? If so, at what line does it stop working/reading?
     
  5. Offline

    Ricecutter0

    @Zombie_Striker
    So when I Shift-Click an item into the backpack, it stops at this line
    Code:
    if(item.getType() == Material.AIR){
                return;
            }
    Also, It's not sending a message when I place an item one at a time by right clicking an item stack with more than 1 of the item.
     
  6. Offline

    Zombie_Striker

    @Ricecutter0
    For a quick-fix right now, you can cancel the event if "event#isShiftClick()" is true (which prevents players from shiftclicking. Use this if only if you cannot figure out what is the problem).

    In order for that to be the case, the item is null/air. What that most likely means is that the actual swap may happen before the event gets triggered. This may not work, but does the cursor pick up the item? See what event#getCursor() returns.
     
  7. Offline

    Ricecutter0

    I've tried to do that before and it works but then then when I place an item, it returns it as air. And I still don't know how to resolve the issue I mentioned above:
     
  8. Offline

    Zombie_Striker

    @Ricecutter0
    Well, for then only use the cursor if the player is shift clicking.
     
  9. Offline

    Ricecutter0

    @Zombie_Striker
    How would I do that? I tried doing something like if(e.isShiftClick){e.setCancelled(true)} but that didn't work
     
  10. Offline

    Zombie_Striker

    @Ricecutter0
    All setCanceled does it stop the action from happening. It does not stop the rest of the method from being read. What you want to do is replace setCanceled with "return;"
     
  11. Offline

    Ricecutter0

    @Zombie_Striker
    I understand that, but the problem is when I do even if(e.isShiftClick).... It doesn't register when they shift click from outside of the custom inventory (the backpack)
     
Thread Status:
Not open for further replies.

Share This Page