Solved Craft Meta Item cannot be cast to

Discussion in 'Plugin Help/Development/Requests' started by daandegroot123, Jun 7, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    daandegroot123

    I'm making me custom kits plugin but i've got a problem with my kit archer
    Crash report:
    Code:
    17:56:22 [ERROR] null
    17:56:22 org.bukkit.command.CommandException: Unhandled exception executing command 'kitarcher' in plugin DaanKitSystem v1.0
    17:56:22 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
    17:56:22 at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
    17:56:22 at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:656) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:609) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:517) [craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    17:56:22 Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.inventory.CraftMetaItem cannot be cast to org.bukkit.inventory.ItemStack
    17:56:22 at itemstacks.Archer.giveArcher(Archer.java:25) ~[?:?]
    17:56:22 at daandegroot123.DaanKitSystem.Main.onCommand(Main.java:38) ~[?:?]
    17:56:22 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8-R0.1-SNAPSHOT-latest.jar:git-Bukkit-33d5de3]
    17:56:22 ... 14 more
    
    My plugin.yml
    Code:
    name: DaanKitSystem
    author: daandegroot123
    version: 1.0
    description: Een kit kies systeem
    main: daandegroot123.DaanKitSystem.Main
    commands:
      kittank:
       description: Gebruik het voor de Tank kit!
      kitarcher: 
       description: Gebruik het voor de Archer kit!
    
    My Main
    Code:
    package daandegroot123.DaanKitSystem;
    
    import itemstacks.Archer;
    import itemstacks.Tank;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        @Override
        public void onEnable(){
            getServer().getLogger().info("DaanKitSystem staat nu aan!");
            getCommand("kittank").setExecutor(this);
            getCommand("kitarcher").setExecutor(this);
        }
       
        @Override
        public void onDisable(){
            getServer().getLogger().info("DaanKitSystem staat nu uit!");
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("kittank")){
                if (p.hasPermission("daan.kit.tank")){
                    Tank.giveTank(p);
                    return true;
                }
            }
           
            if (cmd.getName().equalsIgnoreCase("kitarcher")){
                if (p.hasPermission("daan.kit.archer")){
                    Archer.giveArcher(p);
                    return true;
                }
            }
            return false;
        }    
    }
    
    And last my Archer
    Code:
    package itemstacks;
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Archer {
       
    
        public static void giveArcher(Player p){
            ItemStack bow = new ItemStack(Material.BOW);
            bow.addEnchantment(Enchantment.ARROW_DAMAGE, 2);
            bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
            bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 1);
            ItemMeta bowMeta = bow.getItemMeta();
            bowMeta.setDisplayName(ChatColor.RED + "Archer Bow");
            ((ItemStack) bowMeta).setItemMeta(bowMeta);
           
            ItemStack chainb = new ItemStack(Material.CHAINMAIL_BOOTS);
            chainb.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
            ItemMeta chainbMeta = chainb.getItemMeta();
            chainbMeta.setDisplayName(ChatColor.RED + "Archer Boots");
            ((ItemStack) chainbMeta).setItemMeta(chainbMeta);
           
            ItemStack chainl = new ItemStack(Material.CHAINMAIL_LEGGINGS);
            chainl.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
            ItemMeta chainlMeta = chainl.getItemMeta();
            chainlMeta.setDisplayName(ChatColor.RED + "Archer Leggings");
            ((ItemStack) chainlMeta).setItemMeta(chainlMeta);
           
            ItemStack leatherc = new ItemStack(Material.LEATHER_CHESTPLATE);
            LeatherArmorMeta chestplatel = (LeatherArmorMeta)leatherc.getItemMeta();
            int red = 0;
            chestplatel.setColor(Color.fromRGB(red, red, red));
            leatherc.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
            ItemMeta leathercMeta = leatherc.getItemMeta();
            leathercMeta.setDisplayName(ChatColor.RED + "Archer Chestplate");
            ((ItemStack) leathercMeta).setItemMeta(leathercMeta);
           
            ItemStack leatherh = new ItemStack(Material.LEATHER_HELMET);
            LeatherArmorMeta helmetl = (LeatherArmorMeta)leatherh.getItemMeta();
            int green = 0;
            helmetl.setColor(Color.fromRGB(green, green, green));
            leatherh.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2);
            ItemMeta leatherhMeta = leatherh.getItemMeta();
            leatherhMeta.setDisplayName(ChatColor.RED + "Archer Helmet");
            ((ItemStack) leatherhMeta).setItemMeta(leatherhMeta);
           
            ItemStack arrow = new ItemStack(Material.ARROW);
           
            p.getInventory().clear();
            p.getInventory().setArmorContents(null);
           
            p.getInventory().addItem(arrow);
            p.getInventory().addItem(bow);
            p.getInventory().setHelmet(leatherh);
            p.getInventory().setChestplate(leatherc);
            p.getInventory().setLeggings(chainl);
            p.getInventory().setBoots(chainb);
           
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 1 ));
           
           
           
           
        }
    
    }
    
    idk whats wrong ! I'm already trying to fix this for a few hours ! btw that crash came if i do the command /kitarcher
     
  2. Offline

    mrCookieSlime Retired Staff

    Moved to Alternatives Section.
     
  3. Offline

    FisheyLP

    Thats your problem. You tried to cast ItemMeta to an ItemStack and get an exception. Why do you cast it?? There is already an ItemStack. Correct way:
    Code:
    ItemStack item = ...;
    ItemMeta meta = item.getItemMeta();
    meta.doStuff();
    item.setItemMeta(meta);
     
  4. Offline

    daandegroot123

    Oh sorry :p
    Thanks it works now! But what i'm doing wrong because i get no color
     
  5. Offline

    FisheyLP

    Show the updated code in the class Archer
     
Thread Status:
Not open for further replies.

Share This Page