Commands dont work when server is started

Discussion in 'Spigot Plugin Development' started by asenthd, Jun 5, 2017.

Thread Status:
Not open for further replies.
  1. Hey guys, I'm having an issue with a plugin I've been working on. Every time I use /tfly it does nothing. Heres my code

    Code (Text):

    TFlight.java

    package commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import trollfaceryt.timedflygui.Core;

    public class TFlight implements CommandExecutor, Listener {

       Core config;

       private static Economy econ = null;

       public TFlight(Core plugin) {
         plugin.getServer().getPluginManager().registerEvents(this, plugin);
         config = plugin;
       }

       @Override
       public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         if (label.equalsIgnoreCase("tfly"))
           if (args.length == 0 && sender instanceof Player) {
             @SuppressWarnings("unused")
             Player p = (Player) sender;
         
         @SuppressWarnings("deprecation")
         int balance = (int) econ.getBalance(sender.getName());
         if (balance < config.getConfig().getInt("price")) {
           Bukkit.getPlayer(sender.getName()).sendMessage(ChatColor.RED + "Insufficient funds!");
         }else{
         }
         @SuppressWarnings({ "deprecation", "unused" })
         EconomyResponse r = econ.withdrawPlayer(sender.getName(), config.getConfig().getInt("price"));
         if (args.length == 0 && sender instanceof Player) {
           Player p1 = (Player) sender;
           p1.setAllowFlight(true);
           sender.sendMessage(ChatColor.GREEN + "Flight has been purchased.");
         }
       }
         return true;
    }
    }
     
    Code (Text):


    Core.java
    package trollfaceryt.timedflygui;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.milkbowl.vault.economy.Economy;


    @SuppressWarnings("unused")
    public class Core extends JavaPlugin {
       public static Economy econ = null;
     
       @Override
       public void onEnable() {
         File file = new File("config.yml");
         if(!file.exists()){ //I get an error here that says i need to add a cast to 'file' but i dont know what that means
         saveDefaultConfig();}
       }

       private boolean setupEconomy() {
         if (getServer().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;
       }

       private void registerConfig() {
       }

       public void onDisable() {
         reloadConfig();

       }
    }
     
    Code (Text):

    Plugin.yml:

    main: trollfaceryt.timedflygui.Core
    version: 1.0
    name: TimedFly

    commands:
      tfly:
      description: Allows you to fly for an hour for a little bit of dough.
     
     
    #1 asenthd, Jun 5, 2017
    Last edited: Jun 5, 2017
  2. You never registered the command since it is in a different class other than the Main one. In your onEnable put something like this.getCommand("tfly").setExecutor(new TFlight(this));

    Also make sure you plugin.yml is properly configured.
     
  3. I tried what you said and this is the output when the server is started. (The command works now though)

    Code (Text):

     null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tfly' in plugin TimedFly v1.0
      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
      at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
      at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
      at commands.TFlight.onCommand(TFlight.java:33) ~[?:?]
      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
      ... 15 more
     
     
  4. line 33 in TFlight class is returning a null value
     
  5. Code (Text):
            @SuppressWarnings("deprecation")
            int balance = (int) econ.getBalance(sender.getName());
            if (balance < config.getConfig().getInt("price")) {
                Bukkit.getPlayer(sender.getName()).sendMessage(ChatColor.RED + "Insufficient funds!");
            }else{
            }
            @SuppressWarnings({ "deprecation", "unused" })
            EconomyResponse r = econ.withdrawPlayer(sender.getName(), config.getConfig().getInt("price"));
            if (args.length == 0 && sender instanceof Player) {
                Player p1 = (Player) sender;
                p1.setAllowFlight(true);
                sender.sendMessage(ChatColor.GREEN + "Flight has been purchased for" + config.getConfig().getInt(" price"));
            }
        }
            return true;
    }
    }
    Thats line 32-48 I dont see why it would return null
     
  6. at your plugin yml try adding author and change
    this :
    Code (Java):
    commands:
      tfly:
      description: Allows you to fly for an hour for a little bit of dough.
    to
    Code (Java):
    commands:
      tfly:
          description: Allows you to fly for an hour for a little bit of dough.
     
  7. I tried this, and got the exact same error as last time
     
  8. econ.getBalance() might return to null mind checking it by debugging?

    Code (Java):
    int balance = (int) balance;
    System.out.print("player balance is not null");
     
  9. How exactly would I debug it? I am newer to java
     
  10. You never defined econ, you just create the variable and set it to null, you need to define it first.
     
  11. Would I do that like this?
    Code (Text):
    Economy econ1 = (Economy) econ;
     
  12. you never call your setupEconomy method...
     
  13. No, thats just making a copy of the null variable, still null. Look up how to make the Economy variable with Essentials, I forget how, but put the code in public TFlight(Core plugin) {
     
  14. Alright, I'm a little confused. I tried googling how to make the economy varible with essentials and got virtually nothing. And still being a noob with java how would I call my setupEconomy method?
     
  15. Any more ideas? I'm at a loss here
     
    • Funny Funny x 1
  16. Call the setupEconomy method somewhere like you onEnable, and you need to use the same econ variable in both classes if you only initialize one.
     
  17. Brand new files still don't work. Code down here
    Code (Text):
    Core

    package trollfaceryt.timedflygui;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import commands.TFlight;
    import net.milkbowl.vault.economy.Economy;


    @SuppressWarnings("unused")
    public class Core extends JavaPlugin {
        public static Economy econ = null;
       
        @Override
        public void onEnable() {
            File file = new File("config.yml");
            if(!file.exists()){
            saveDefaultConfig();}
            this.getCommand("tfly").setExecutor(new TFlight(this));
            setupEconomy();
            }

        private boolean setupEconomy() {
            if (getServer().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;
        }

        private void registerConfig() {
        }

        public void onDisable() {
            reloadConfig();

        }
    }
    Code (Text):
    TFlight

    package commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import trollfaceryt.timedflygui.Core;

    public class TFlight implements CommandExecutor, Listener {

        Core config;

        private static Economy econ = null;

        public TFlight(Core plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            config = plugin;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("tfly"))
                if (args.length == 0 && sender instanceof Player) {
                    @SuppressWarnings("unused")
                    Player p = (Player) sender;
               
            @SuppressWarnings("deprecation")
            int balance = (int) econ.getBalance(sender.getName());
            if (balance < config.getConfig().getInt("price")) {
                Bukkit.getPlayer(sender.getName()).sendMessage(ChatColor.RED + "Insufficient funds!");
            }else{
            }
            @SuppressWarnings({ "deprecation", "unused" })
            EconomyResponse r = econ.withdrawPlayer(sender.getName(), config.getConfig().getInt("price"));
            if (args.length == 0 && sender instanceof Player) {
                Player p1 = (Player) sender;
                p1.setAllowFlight(true);
                sender.sendMessage(ChatColor.GREEN + "Flight has been purchased for" + config.getConfig().getInt(" price"));
            }
        }
            return true;
    }
    }

     
     
Thread Status:
Not open for further replies.