Solved command is not working

Discussion in 'Plugin Development' started by piddi24, Dec 29, 2020.

  1. Offline

    piddi24

    Hi, I am creating a plugin with a command called' /giveitem <item>'
    but, it is not working
    i do /giveitem iron_ore it does not work.
    i do /giveitem minecraft:iron_ore it does not work.
    here is my main class:
    Code:
    package me.hardboom.ultimate;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import me.hardboom.ultimate.Gamerules;
    
    import org.bukkit.event.*;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nUltimate has been activated\n\n");
        Commands commands = new Commands();
       
        getCommand(commands.cmd1).setExecutor(commands);
        Gamerules gamerules = new Gamerules();
        gamerules.gamerules();
       
        }
        public void onDisable() {
        Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nUltimate has been activated\n\n");
       
        }
           
    
        }
    
    
    
    This is my Commands class
    Code:
    package me.hardboom.ultimate;
    
    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.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import net.md_5.bungee.api.ChatColor;
    
    
    public class Commands  implements Listener, CommandExecutor {
       
        public String cmd1 = "giveitem";
            @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(sender instanceof Player) {
                if(cmd.getName().equalsIgnoreCase(cmd1)) {
                    Material item = Material.getMaterial(args[0]);
                    if(item != null){
                        Inventory inv = ((Player)sender).getInventory();
                        inv.addItem(new ItemStack(item, 1));
                        return true;
                    }else {
                        sender.sendMessage(ChatColor.RED + args[0] + ChatColor.BLUE + " is not a valid item");
                    }
                   
               
            }else {
                sender.sendMessage(ChatColor.RED + "Only players can use this command");
                return true;
            }
        }
            return false;           
           
           
    }
    }
       
    
    
    
    This is my gamerules class(Probably not relevant)
    Code:
    package me.hardboom.ultimate;
    
    import me.hardboom.ultimate.Commands;
    import org.bukkit.Bukkit;
    import org.bukkit.command.*;
    public class Gamerules {
    
        public void gamerules() {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "gamerule doDaylightCycle false");
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "gamerule doWeatherCycle false");
            Commands commands = new Commands();
           
        }
       
    
    }
    
    here is my plugin.yml
    Code:
    name: Ultimate
    version: 1.0
    main: me.hardboom.ultimate.Main
    
    commands:
       giveitem:
          usage: /<command>
          description: gives you an item
           
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    piddi24

    It says that its not a valid item

    like i coded it to

    2020-12-29_11.09.59.png @timtower
     
  4. Offline

    timtower Administrator Administrator Moderator

    @piddi24 Specify the API-version in your plugin.yml, it is looking at the old names.
     
  5. Offline

    piddi24

    what is that?
    The version for the plugin is 1.16.4
    is that it?
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    piddi24

    still doesnt work
    same thing
     
  8. Offline

    timtower Administrator Administrator Moderator

    @piddi24 Then try uppercase, print all the materials.
     
  9. Offline

    piddi24

    I'm sorry, I am relatively new to java and I do not understand, if you do not have time to explain, that is completely understandable
    uppercase for the item after /giveitem?

    IT WORKS
    Thank you!
    How do I mark this as solved?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 29, 2020

Share This Page