Can someone tell me what's wrong with my code..

Discussion in 'Spigot Plugin Development' started by CyberCrafter, Aug 22, 2017.

  1. I want to give a player a item with a custom displayname and lore. This dosen't work tho:
    Code (Text):

        List<String> lore = new ArrayList<String>();
    public void onEnable(){
        lore.add("Increases the Radius of sucktion of Hopper by 4!");
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(cmd.getName().equalsIgnoreCase("jumper")){
                if(args.length == 1 && args[0].equalsIgnoreCase("reload")){
                    cfile = new File(getDataFolder(), "config.yml");
                    config = YamlConfiguration.loadConfiguration(cfile);
                    sender.sendMessage(ChatColor.GOLD + "Config reloaded!");
                }else if(args.length == 3 && args[0].equalsIgnoreCase("give")){
                    if(isInt(args[2])){
                        Player p = Bukkit.getServer().getPlayer(args[1]);
                        if(p == null){
                            sender.sendMessage("Player is offline!");
                        } else {
                            boolean bool = false;
                            for(ItemStack is : p.getInventory().getContents()){
                                if(is == null){
                                    bool = true;
                                }
                            }
                            ItemStack is = new ItemStack(Material.HOPPER, Integer.parseInt(args[2]));
                            ItemMeta meta = is.getItemMeta();
                            meta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Jumper");
                            meta.setLore(lore);
                            is.setItemMeta(meta);
                            if(bool){
                                p.getInventory().addItem(is);
                                p.sendMessage(ChatColor.GOLD + "" + Integer.parseInt(args[2]) + "Jumper(s) recieved!");
                            } else {
                                p.sendMessage(ChatColor.GOLD + "No inventory space," + Integer.parseInt(args[2])  + "Jumper(s) not given!");
                                Bukkit.getLogger().info("Player " + p.getName() + " did not recieve" + Integer.parseInt(args[2])  +" Jumper(s) because his/her 's inventory was full!");
                            }
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Use /jumper give <playername> <amount>");
                    }
                } else {
                    sender.sendMessage(ChatColor.DARK_PURPLE + "Use /jumper <reload> or /jumper give <playername> <amount>");
                }
            }
            return true;
        }
     
    No errors either,
    Thanks in advance.
     
    #1 CyberCrafter, Aug 22, 2017
    Last edited: Aug 22, 2017
  2. What is happening when executing the command ?
     
  3. You're calling lore.add outside a method which isn't allowed. Move that line into a method/constructor/static block.
     
  4. While it is ok to initialize a field "directly" (i.e. at the same time the decleration happens), any other code must be inside a method (or (static/instance) initializer/constructor); you should move the list mutation inside a method.
     
  5. I get the item without lore or custom name
     
  6. Thats actually in my onEnable() method
     
  7. And one more thing, how do I drop the item at their feet if they don't have space?
     
  8. Aw come on, i told you multiple times learn Java first. Why do you keep coming with such , (pardon) stupid questions? These arent beginner mistakes, these are mistakes of someone, who didnt even looked a little bit into Java first.

    This forum isnt here to teach you how to java, its something people should already have when writing here.
     
  9. But..I know Java...

    EDIT: Unfortunately there is no unfriendly button for your post
     
  10. The thing with saying that someone knows or doesn't know Java is... How much knowledge do you need to know Java? What subjects do you need to know about?
     
  11. I'm sorry but why doesn't my code work proper?
     
  12. Don't be sorry. @TheSilentHorizon should just stop telling everyone with this kind of question that they don't know Java.

    I've not worked with item meta before so I can't really tell (which of course means that TheSilentHorizon is going to tell me that I don't know Java, isn't he?).
     
    • Winner Winner x 2
  13. Nah, he's a smart guy. He's helped me before. Idk about now,
    So will you give a proper answer @TheSilentHorizon?
     
    • Friendly Friendly x 1