Solved Not sending anything

Discussion in 'Spigot Plugin Development' started by DuckKing, Mar 20, 2020.

Thread Status:
Not open for further replies.
  1. I have been stuck on this weird bug for a while, I am messing around with configs (trying to get better at them) and for some reason whenever I try these commands (/setfood and /getfood), it just does not work, here is my code:

    Code (Java):
    package me.Duck.ConfigurationTEST;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;


    public class Commands implements CommandExecutor {

        Plugin plugin = ConfigurationMAIN.getPlugin(ConfigurationMAIN.class);

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (command.getName().equals("getfood")) {
                Player player = (Player) sender;
                Inventory inventory = player.getInventory();
                ItemStack item = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Food")));
                if(sender instanceof Player) {
                    if (args.length > 0) {
                        sender.sendMessage(ChatColor.YELLOW + "Usage: /getFood");
                    } else {
                        inventory.addItem(item);
                        sender.sendMessage(ChatColor.GREEN + "You have been given " + item + "!");
                    }
                }else {
                    System.out.println("You must be a player to execute this command!");
                }

            } else if (command.getName().equals("setfood")) {
                Player player = (Player) sender;
                if (args.length > 0) {
                    if (!(args.length > 1)) {
                        plugin.getConfig().set("Food", args[0]);
                        plugin.saveDefaultConfig();
                        sender.sendMessage(ChatColor.GREEN + "Successfully set food!");
                    } else {
                        sender.sendMessage(ChatColor.YELLOW + "Usage: /setFood <valid food>");
                    }
                } else {
                    sender.sendMessage(ChatColor.YELLOW + "Usage: /setFood <valid food>");
                }
            } else if(command.getName().equals("foodReload")) {
                Player player = (Player) sender;
                if(!(args.length > 0)) {
                    plugin.reloadConfig();
                    sender.sendMessage(ChatColor.GREEN + "Config.yml has been reloaded!");
                }else {
                    sender.sendMessage(ChatColor.YELLOW + "Usage: /foodReload");
                }
            }

            return true;
        }
    }
     

    Here is my Main Class:
    Code (Java):
    package me.Duck.ConfigurationTEST;

    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class ConfigurationMAIN extends JavaPlugin {

        public void onEnable() {
            getConfig().options().copyDefaults();
            saveDefaultConfig();
            getCommand("getFood").setExecutor(new Commands());
            getCommand("setFood").setExecutor(new Commands());
            getCommand("foodReload").setExecutor(new Commands());
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ConfigurationTEST] Enabled");
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "[ConfigurationTEST] Disabled");

        }

    }
     

    And here is my Plugin.yml:

    Code (Text):
    name: ConfigurationMAIN
    main: me.Duck.ConfigurationTEST.ConfigurationMAIN
    description: Testing Configuration Plugin
    version: 1.0-SNAPSHOT

    commands:
      getFood:
        description: Gets your food that you specified in config!
        usage: /<command>
      setFood:
        description: Sets configuration file to the food you specified and reloads it!
        usage: /<command>
      foodReload:
        description: Reloads configuration file to set food!
        usage: /<command>
    Hope someone can find a fix to this, thanks.

    Edit: By the way, there are no errors sent in the console when I execute the commands.
     
  2. Try to make commands in lower case everywhere.
     
  3. drives_a_ford

    Moderator

    Please read the stickied Beginner Programming Mistakes and Why You're Making Them thread. Encapsulation and repetitive code are what jump out at a glance.

    Make sure your member fields are private. No need for the field in you Commands class to be package private.

    Do not assume the CommandSender is a Player. Do an instanceof check before casting. In your current state you'll get the ClassCastException when using the commands from the console.
    You need to check for instance before casting.

    You don't need to create a new instance of the Commands class for each command. Just create one instance and use that.


    As for your issue, what "doesn't work"?
    What command are you using exactly?
    What kind of outcome would you expect?
    What kind of outcome are you experiencing?
     
    • Agree Agree x 1
  4. Strahan

    Benefactor

    You are grabbing input from the config and just assuming it's valid. There is a chance that either the config doesn't have a value or that the set value is not valid. If for example the user wanted to use cooked steak and put "/setfood steak", it will save that to config but you cannot create an ItemStack using that as a material. Especially since you are using getMaterial instead of matchMaterial, because in getMaterial case matters. They'd have to use the appropriate material and format it right - /setfood COOKED_BEEF

    Also you should use the logger to talk to the console, that's why it exists.
     
  5. I know, I am not actually making it a thing, I am just trying to get better at configurations since I barely use them. And I only check the player if it does not give anything if you know what I mean. Thanks for the tips though. :)

    Edit: I am not sure why it's not sending anything, I tried debugging even the first part (I deleted the code and tried it) but it still did not send what I debugged. Everything looks right in plugin.yml and in my main class...

    Another Edit: I am a huge idiot, I messed up with the caps, everything works fine now. xD Thanks :D
     
    #5 DuckKing, Mar 20, 2020
    Last edited: Mar 20, 2020
Thread Status:
Not open for further replies.