Help!

Discussion in 'Plugin Development' started by DoggyCode™, Jul 4, 2015.

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

    DoggyCode™

    Hello!

    So I'm just going to describe what I want this to do. So I'm using the PlayerInteractEvent for this. Ok, so I want to do so like when I look at a player and right-click a specific item, I want some code to be executed (in this case I want to display this player's inventory). I'm kind of stuck.. and I know this is possible- I just haven't found any method yet. If someone could basicially give the example code, then that would be great!

    Much appreciated.
    Marius
     
  2. Offline

    Creeperzombi3

    @DoggyCode™
    First, "Help!" isn't a good title.
    Second, you would get the item in hand, and if it equals.. blah blah then make another if statement checking if they right click a player, then insert code
     
  3. Offline

    srspore

    The PlayerInteractAtEntityEvent should run when an entity is right clicked so then just check to see if the entity is a player and then check to see which item the event.getPlayer() has and then if it's the item you want run the code you want
     
  4. Offline

    DoggyCode™

    Thank you so much! @srspore
     
  5. @DoggyCode™ If you want to do something when a player right clicks an item, you could do something like this:
    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (event.getItem() != null || event.getItem() != Material.AIR) {
            if (event.getItem().equals(/*Item goes here*/)) {
                if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    // Your code here
                }
            }
        }
    }
    *phew* Typing that wasn't easy. Anyway, hope this helped. :)
     
    ShadowWizardMC likes this.
  6. Offline

    ShadowWizardMC

  7. Offline

    DoggyCode™

    Haha :D I'm not usually as stuck as this. But I haven't worked much with inventories, but thanks to you guys I'm almost through! I asked another question to you, @ShadowWizardMC , if you take a look at my most recent thread.


    Yeh, I figured that out. I'm not that blank, I just needed to know about that PlayerInteractEvent event, that I somehow didn't know about xD I tried to use another event, where I didn't know how to find the player. Thanks anyways!

    And I used this:
    Code:
      @EventHandler
       public void onClick(PlayerInteractEvent event) {
         Player p = (Player)event.getPlayer();
         Action a = (Action)event.getAction();
         if (a==Action.RIGHT_CLICK_BLOCK || a==Action.RIGHT_CLICK_AIR){
           ItemStack hand = p.getItemInHand();
           if (p.hasPermission(Permissions.togglestaffmode)) {
             if (hand!=null&&hand.getType()==Material.IRON_FENCE && hand.getItemMeta().getDisplayName().equals("§6Reported Players")) {
               p.openInventory(Main.reports);
               return;
             }
           } else {
             p.sendMessage(RED + "You're not allowed to do that");
             Bukkit.broadcast(RED + "Staff Tools: " + GOLD + p.getName() + RED + " tried to use a §6Staff Tool §cthat they somehow got, I'd suggest you get rid of it immediately!", "staffmode.use");
             return;
           }
         }
       }
    @CodePlaysMinecraft
     
    Last edited by a moderator: Jul 4, 2015
  8. @DoggyCode™ Well, I see only a couple of things wrong but not something big enough to affect the outcome.
    1. Why are you casting p and a to a Player and an Action when they're already a Player and an Action? That's like saying:
    Code:
    int i = (int) 27;
    It's pointless.
    1. (Should be 2) Is Permissions a class? If so, it togglestaffmode a String? (Just wondering)
    2. (Should be 3) Try debugging. Debugging will help you 90% of the time.
     
  9. Offline

    srspore

    You're welcome :3
     
  10. Offline

    DoggyCode™

    @CodePlaysMinecraft ,

    1. Yeh, I know the casting thing, Bukkit does require me to do it sometimes, it has just become a habbit xD.

    2. Yes, it's a permission, not a string. Here, I'll show you:
    Code:
    package me.pvpdog.staffmodealexay.permissions;
    
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Permissions extends JavaPlugin {
       
       public static Permission togglestaffmode = new Permission("staffmode.use");
    
    }
    3. I've never done debugging before, don't know how to do it -.-.
     
  11. @DoggyCode™ Debugging is basically a way of sending a message every line to see which line isn't being executed. The message doesn't have to be detailed, I usually just go with 1, 2, 3, 4, etc. Here's an example if needed:
    Code:
    public void changeLore(ItemStack item, String lore) {
        if (/*put something here*/) {
            System.out.println("1");
            if (/*something*/) {
                System.out.println("2");
            }
        }
    }
    If 1 doesn't print out to the console, then there is something wrong with
    Code:
    if (/*put something here*/) {}
    If 1 prints out, but not 2, then there's something wrong with
    Code:
    if (/*something*/) {}
    Anyway, I hope this helped. :)
     
  12. Offline

    DoggyCode™

    Thank you, @CodePlaysMinecraft !
     
Thread Status:
Not open for further replies.

Share This Page