Solved Cannot make a static reference to the non-static method

Discussion in 'Spigot Plugin Development' started by Lutheron, May 25, 2018.

  1. Hello, and im making a plugin for my friends prison server. Im making a rankup sign.
    And im having problems withdrawing money from the player with vault. This is what i got so far.
    Im new to java and plugin coding.

    My problem is what the title says: Cannot make a static reference to the non-static method

    The code which gives the error is at the bottom of both.

    Code (Text):
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;

    public class SignRankup extends JavaPlugin {

         public static Economy econ = null;
     
         public void onEnable() {
       
             Bukkit.getServer().getPluginManager().registerEvents(new SignListener(), this);
             if (!setupEconomy()) {
                 this.getLogger().severe("Disabled due to no Vault dependency found!");
                 Bukkit.getPluginManager().disablePlugin(this);
                 return;        
             };
         }
     
         public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
             if (!(sender instanceof Player)) {
                     sender.sendMessage(ChatColor.RED + "The console cannot use Rankup Sign!");
                     return true;
             }
             return true;
         }
     
         private boolean setupEconomy() {
                if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
                    return false;
                }
                RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
                if (rsp == null) {
                    return false;
                }
                econ = rsp.getProvider();
                return econ != null;
            }
            public static Economy getEconomy() {
                return econ;
         }
     
       
       
         public void WithdrawMoneyFromPlayer(int MoneyToWithdraw, String PlayerName) {
             Economy.withdrawPlayer(PlayerName, MoneyToWithdraw);
             return;
         }
    }
    Code (Text):
    import org.bukkit.ChatColor;
    import org.bukkit.block.Sign;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class SignListener implements Listener {
            @EventHandler
            public void onSignChange(SignChangeEvent e) {
                    if (e.getLine(0).equalsIgnoreCase("Rankup C")) {
                            e.setLine(0, "§3[Rankup C]");
                            e.setLine(2, "10000$");
                    } else if (e.getLine(0).equalsIgnoreCase("Rankup B")) {
                            e.setLine(0, "§3[Rankup B]");
                            e.setLine(2, "50000$");
                    } else if (e.getLine(0).equalsIgnoreCase("Rankup A")) {
                            e.setLine(0, "§3[Rankup A]");
                            e.setLine(2, "100000$");
                    }
            }
     
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                    if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                    if (e.getClickedBlock().getState() instanceof Sign) {
                            Sign s = (Sign) e.getClickedBlock().getState();
                            Double balance = SignRankup.econ.getBalance(e.getPlayer());
                            if (s.getLine(0).equalsIgnoreCase("§3[Rankup C]")) {                          
                                int CSignCost = 10000;
                                if(balance >= CSignCost) {
                                    //SignRankup.WithdrawMoneyFromPlayer(CSignCost, e.getPlayer().getName());
                                } else {
                                    e.getPlayer().sendMessage(ChatColor.RED + "You don't have enough money, you need: " + (CSignCost - balance));
                                }
                            }
                    }
            }
    }
     
    #1 Lutheron, May 25, 2018
    Last edited: May 25, 2018
  2. NathanWolf

    Supporter

    For your core issue, you should pass a reference to your plugin to a SignListener constructor. Then have the SignListener class store that reference in a member property, and use that to call WithdrawMoneyFromPlayer.

    Alternately, make WithdrawMoneyFromPlayer static - but this is not a great habit to get into.

    A few other notes:
    • I think WithdrawMoneyFromPlayer needs to call econ.withdrawPlayer- that's a non-static method, too, isn't it?
    • Java conventions suggest you name that method like withdrawMoneyFromPlayer. Capitals are reserved for classes, generally.
    • You're importing the wrong ChatColor in your main plugin class
    • You don't need to have an explicit return; statement at the end of a method that returns void
    Good luck!
     
  3. Well, it just means that you cannot use a non static in a static. So if for example you'll use this code:
    Code (Text):
       File file = new File("AFile.yml");
        public static YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
    , your ide is gonna highlight in red file as you cannot use it in a static method. How to fix this:
    Code (Text):
    File file = new File("File.yml");
    YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
     
  4. i forgot to say that im coding to 1.8.8 spigot server, does the withdraw command exist in that version?
     
  5. NathanWolf

    Supporter

    It won't depend on spigot server version, that's a Vault method. I think it's fine, you just need to call it non-statically (use the econ object, not the Economy class)
     
  6. Thank you guys for the help, one last question.

    Now i changed it to econ, but what does deprecated mean?
     
  7. what ever it works.
    Thank you guys so much for the help:)
     
  8. NathanWolf

    Supporter

    Deprecated means you shouldn't use it anymore, and it may be removed in a future version.

    Probably there's an alternate withdraw method that takes a UUID instead.