Solved Player event with an specific item

Discussion in 'Plugin Development' started by RJTech, Nov 5, 2019.

  1. Offline

    RJTech

    Hey yall, so I'm trying to make a script that when a player right-clicks a specific button that is givin to them, it does something. Iv made the command to give a player a button, but I'm not sure how to add an the "if" part when they right-click that specific button that I gave them after its placed and not just a normal stone button. Iv checked google but all I could find was how t0 use player interaction events with generall items in player inventories.
     
  2. Online

    timtower Moderator Moderator

    @RJTech Save the location when they place it. Remove the location on block break.
     
  3. Offline

    RJTech

    @timtower, I'm trying to get the event when they right-click the button not break it, but that is very helpful for other projects :)
     
  4. Online

    timtower Moderator Moderator

    @RJTech PlayerInteractEvent, get the block, check the locations that are stored when placing.
    Unless you are talking about holding it, then you use the same event and get the item, then the displayname (after a check to see if it is there)
     
  5. Offline

    krypek

    Try this:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Action action = e.getAction();
    ItemStack item = e.getItem();

    if (action == Action.PHYSICAL || item == null || item.getType == Material.AIR) { return; }

    if (iten.getType() == Material./*your item' in your situation stone button*/) {
    //things that will happend when you click a button
    player.sendMessege("You right clicked the button!");
    }
    }
     
  6. Offline

    RJTech

    Yes, I'm now realizing what your where saying :)
    @krypek ill try that

    @krypek I tried to type in the script you have but I keep getting red errors, did I type it in right?
    [​IMG]
    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 5, 2019
  7. Online

    timtower Moderator Moderator

    @RJTech Needs to be outside of other functions.
    And why did you import javax.swing?
     
  8. Offline

    RJTech

    @timtower, im not sure, though I think I fixed it, hows this?
    [​IMG]
    I keep having issues when I try and add commands where the //mycommand is, but other than that I think its good
     
  9. Online

    timtower Moderator Moderator

    @RJTech Really depends on how you add the commands.
    And never said that it had to moved to a new class.
     
  10. Offline

    RJTech

    @timtower, Im trying to add this command, but it keeps saying "'class' or 'interface' expected", I'm pretty new to plugin development so I'm not sure how to fix this
    [​IMG]
     
  11. Online

    timtower Moderator Moderator

    @RJTech You need to specify the color.
    ChatColor is an enum.
     
  12. Offline

    RJTech

    @timtower, Iv changed the chat color, but its an error with the line that the issue. no matter what command I try it still pops up
     
  13. Online

    timtower Moderator Moderator

    @RJTech Hit ctrl+shift+f, autoformats.
    Your line is probably outside the function.
    Never put multiple brackets on the same line like that.
     
  14. Offline

    RJTech

    @timtower, I got it now thanks, for the future though ctrl+shift+f on IntelliJ opens the find in path window, for auto-formatting it's ctrl + alt + l, I'm not sure why I didn't do that in the beginning :) There are no errors now but the event is not working, sorry if I'm making noob mistakes I'm pretty new to coding plugins here's the event
    [​IMG]
    Heres the main class where I registered the event
    [​IMG]
    Update: this is the error im getting from the console https://pastebin.com/PzW6GbDL
     
    Last edited: Nov 5, 2019
  15. Online

    timtower Moderator Moderator

    @RJTech Used to eclipse so that is why I had that key.
     
  16. Offline

    RJTech

    @timtower that makes sense, do you have any ideas on what could be going wrong with my code?
     
  17. Offline

    Strahan

    I think you really should get a basic knowledge of Java before working on a plugin. Not trying to be rude, just saying it will save you a lot of frustration. You have a problem with thinking through your logic for example:

    Is the action physical, the item null or the item air?
    Yes: OK, is the item a stone button?
    Yes: Broadcast message

    See the problem? In regards to the item, the outer logic says to only run your item is null or air, then if it matches it will broadcast but only if the item is a stone button. It can never broadcast. getItem() is nullable, so when it returns a null item it passes the action check, passes the null check, then when it tries to check the type it crashes because you can't check the type of a null object.
     
  18. Online

    timtower Moderator Moderator

    Strahan likes this.

Share This Page