1.14.4 Player Variable in Other Method

Discussion in 'Spigot Plugin Development' started by Marshmalah, Jan 15, 2020.

  1. Hey, I wish I could get the value "p" in another method how can I set the value in this other method?
    I wish I could do "Player p = onclickRecette1" but I don't know how to do this

    Code (Java):
            @EventHandler
            public void onclickRecette1(InventoryClickEvent e)
            {
                ItemStack it = e.getCurrentItem();
                Player p = (Player) e.getWhoClicked();
               
                if(it == null) return;
               
                if(it.getType() == Material.FLINT && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase("Recette : Silex à Aiguiser Brute"))
                {
                    e.setCancelled(true);
                    return;
                }
                if(it.getType() == Material.FLINT && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase("Achat - Recette : Silex à Aiguiser Brute"))
                {
                    e.setCancelled(true);
                    if(p.hasPermission("forgeron.1"))
                    {
                        if(setupEconomy())
                        {
                            double balance = economy.getBalance(p);
                            if(balance < 1000)
                            {
                                p.sendMessage(ChatColor.RED + "[Forgeron] -> Tu n'as pas assez d'argent pour apprendre cette recette");
                                p.closeInventory();
                                return;
                            }else
                            {
                                economy.withdrawPlayer(p, 1000);
                                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddp "+p.getName()+" forgeron.recette.apprenti.1");
                                p.sendMessage(ChatColor.GREEN+"[Mineur] -> Tu as débloqué une recette");                          
                            }
                        }
                    }else {
                        p.sendMessage(ChatColor.RED + "[Forgeron] -> Tu n'as pas assez de points pour apprendre cette recette");
                        p.closeInventory();
                        return;
                    }
                    return;
                }
                return;
            }
    Thanks for watching ;)
     
  2. Have a player as a parameter for the method.
     
  3. You cant.

    Variables defined in a method are local to that method.
     
  4. If it's possible i want use player in
    Code (Text):
    public void test()
    {
    //code
    }
     
  5. Then simply add a parameter to the method. The parameter being a player, then in your click event you can call the other method with the parameter "p"
     
  6. is it not possible to return a Player from a method?
     
  7. my first goal is to open the inventory of another method but my problem is that this method is carried out with an if and that the two method do not go together, if you know how to do otherwise than to call this other method that would interest me enormously and avoid adding on 17 items the same code to open this inventory
     
  8. Are you wanting to call another method from your existing click event but you need to have access to the player in the click event?
     
  9. have try that
    Code (Text):
                                economy.withdrawPlayer(p, 1000);
                                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddp "+p.getName()+" forgeron.recette.apprenti.1");
                                p.sendMessage(ChatColor.GREEN+"[Mineur] -> Tu as débloqué une recette");
                                MenuApprenti.ApprentiRecette(e);
    but the method ApprentiRecette has
    Code (Java):
            if(it.getType() == Material.DIAMOND_AXE && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "" + ChatColor.BOLD + "Apprenti Forgeron"))
            {
    for open the inventory
     
  10. You should read up on Java as a tip.

    So let's say you have an IF statement such as the one you mentioned.

    Code (Text):
    Player player = event.getPlayer();

    if(true) { // Obviously your conditions
        openInventory(player);
    }
    Code (Text):
    public void openInventory(Player player) {
        // Your code here
    }
     
  11. Just send the player to the method "ApprentiRecette(e, p)" same as the "e" you get a player
     
  12. It is.
     
    • Like Like x 1
  13. Strahan

    Benefactor

    This is why I recommend that people watch some Java tutorials before trying to make a plugin. Stuff like this is like Java 101. As mentioned prior, just pass the Player in the constructor. However, if the "e" you are passing is the event instance, then you have the Player already as it's part of that.

    In your code, some combination + negative checking would make things cleaner. Also you do not need to return when there is no further code as you fall outside the logic blocks. Like if you have
    Code (Text):
    event {
      if (something) {
        send player message

        if (something else) {
          give player item
          return;
        }
        return;
      }
      return
    }
    Every return in that example is not necessary. Now if you have:
    Code (Text):
    event {
      if (something) {
        send player message

        if (something else) {
          give player item
          return;
        }
     
        kill player
        return;
      }
      return
    }
    Then the last two returns are unnecessary but the return in the second logic block where the player is given an item is required if you do not intend to kill the player.
     
    • Winner Winner x 1