Solved ItemStack stick from one class to another

Discussion in 'Plugin Help/Development/Requests' started by Randomguy, Jul 26, 2015.

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

    Randomguy

    Hi, I have a problem I'm not sure how to solve. I am making a game plugin, and I want to have it so they have to set the perimeter of the arena... I have this line of code (the tool for setting)
    Code:
    ItemStack stick = new ItemStack(Material.STICK, 1);
    and this code in a different class:
    Code:
        @EventHandler
            public void onRightclick(PlayerInteractEvent e) {
                Player player = e.getPlayer();
                Material playerinhand = player.getInventory().getItemInHand().getType();
                if(playerinhand == stick) {
                   
                }
    well, it is in a different class, and I have no idea how to get the itemstack from 1 class to the other. Please help...
     
  2. Offline

    LeGhost

    You basically can't or else it will give you an error about accessing it in a specific scope...
    Just put the ItemStack inside the right click event.
    Here is a corrected version:
    Code:
        @̶E̶v̶e̶n̶t̶H̶a̶n̶d̶l̶e̶r̶
            p̶u̶b̶l̶i̶c̶ ̶v̶o̶i̶d̶ ̶o̶n̶R̶i̶g̶h̶t̶c̶l̶i̶c̶k̶(̶P̶l̶a̶y̶e̶r̶I̶n̶t̶e̶r̶a̶c̶t̶E̶v̶e̶n̶t̶ ̶e̶)̶ ̶{̶
                P̶l̶a̶y̶e̶r̶ ̶p̶l̶a̶y̶e̶r̶ ̶=̶ ̶e̶.̶g̶e̶t̶P̶l̶a̶y̶e̶r̶(̶)̶;̶
               M̶a̶t̶e̶r̶i̶a̶l̶ ̶p̶l̶a̶y̶e̶r̶i̶n̶h̶a̶n̶d̶ ̶=̶ ̶p̶l̶a̶y̶e̶r̶.̶g̶e̶t̶I̶n̶v̶e̶n̶t̶o̶r̶y̶(̶)̶.̶g̶e̶t̶I̶t̶e̶m̶I̶n̶H̶a̶n̶d̶(̶)̶.̶g̶e̶t̶T̶y̶p̶e̶(̶)̶;̶
               I̶t̶e̶m̶S̶t̶a̶c̶k̶ ̶s̶t̶i̶c̶k̶ ̶=̶ ̶n̶e̶w̶ ̶I̶t̶e̶m̶S̶t̶a̶c̶k̶(̶M̶a̶t̶e̶r̶i̶a̶l̶.̶S̶T̶I̶C̶K̶,̶ ̶1̶)̶
               i̶f̶(̶p̶l̶a̶y̶e̶r̶i̶n̶h̶a̶n̶d̶ ̶=̶=̶ ̶s̶t̶i̶c̶k̶)̶ ̶{̶
                    ̶/̶/̶D̶O̶ ̶S̶T̶U̶F̶F̶
               }̶
            }̶
    
    Or, an easier way would be to do this:
    Code:
        @EventHandler
            public void onRightclick(PlayerInteractEvent e) {
                Player player = e.getPlayer();
                Material playerinhand = player.getInventory().getItemInHand().getType();
                if(playerinhand == Material.STICK) {
                   //DO STUFF
                }
            }

    EDIT
    By the way, in the ItemStack, it says 1 STICK. That means if a player had multiple of those sticks then it would not work.
    The second way is the correct way.
    :)
     
    Last edited: Jul 26, 2015
  3. Offline

    Randomguy

    I thought if it said 1 stick there it meant it would give them 1 stick... Also I want it to be so when they enter a command they are given the item... and I don't want it to be a plain normal stick that could be used because I have had a server and I find it highly annoying when you right click with a stick it sets the perimeter of the arena... So I wanted it to only work with the stick from the specific command...

    please help me...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    Randomguy

    Anyone? I know how to get 1 thing from a class to another, but I usually add public before it but it won't let me, it will only let me add final... So I have no idea how to call it... Is this even possible?

    I solved it... I declared the stick and everything and i did this:

    Code:
    @EventHandler
            public void onRightclick(PlayerInteractEvent e) {
                Player player = e.getPlayer();
                Material playerinhand = player.getInventory().getItemInHand().getType();
                if(playerinhand == Material.STICK) {
                    String playerinhandname = player.getInventory().getItemInHand().getItemMeta().getDisplayName();
                    if(playerinhandname == ChatColor.YELLOW + "Wand") {
    i haven't tested it yet but it should work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page