[CLOSED] Skull item enchantment

Discussion in 'Spigot Plugin Development' started by mlgcraftnetwork, Jun 8, 2016.

  1. I'm trying to add an enchantmet to a skull item but its not being added, I'm not getting any errors. Is it possible to add enchantments to skull items? This is my current code:
    Code (Text):
            ItemStack irongolem = new ItemStack(Material.SKULL_ITEM, 1);
            irongolem.setDurability((short)3);
            SkullMeta irongolemm = (SkullMeta) irongolem.getItemMeta();
            irongolemm.setDisplayName("§eIronGolem");
            irongolemm.setLore(Arrays.asList("§7Click to select","","§7Requirements:"," §7- Find in mysterybox"," §7- Rank: §b[Diamond]"));
            irongolemm.setOwner("MHF_Golem");
            irongolem.setItemMeta(irongolemm);

    if(selected.get(p.getUniqueId()).equals("m8")){irongolem.addUnsafeEnchantment(Enchantment.DURABILITY, 3);}
    (I'm sure selected is m8)
     
  2. MiniDigger

    Supporter

    try adding the enchantment to the item meta and not the the itemstack.
    also make sure that your code gets excuted.
     
    • Agree Agree x 1
  3. I tried that using this:
    Code (Text):
    if(selected.get(p.getUniqueId()).equals("m4")){((ItemStack) cowm).addUnsafeEnchantment(Enchantment.DURABILITY, 3);}
    and I got this error:
    Code (Text):
    [16:17:49] [Server thread/ERROR]: Could not pass event InventoryClickEvent to PLGlobal v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1851) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R2.inventory.CraftMetaSkull cannot be cast to org.bukkit.inventory.ItemStack
        at mcgglobal.Minions.openMinions(Minions.java:244) ~[?:?]
        at mcgglobal.Menu.cosmeticsListener(Menu.java:556) ~[?:?]
        at mcgglobal.Listeners.invClick(Listeners.java:248) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 15 more
     
  4. MiniDigger

    Supporter

    whats not what I told you to try... :confused:
    the item meta has its own methods to add enchantments, you should try these.
     
  5. Oh sorry I misunderstood. You told me to add the enchant to the itemmeta right? "cowm" is the itemmeta (skullmeta actually) of the cow itemstack, but I had to add (itemstack) to fix an error. Or isn't think what you meant?
     
  6. MiniDigger

    Supporter

    yes, but you are trying to cast the itemmeta into an itemstack to call the addUnsafeEnchantment of itemstack when I told you to call https://hub.spigotmc.org/javadocs/s...ukkit.enchantments.Enchantment, int, boolean)
     
  7. I just thought of this, I now have this but I still don't see the enchantment effect.
    Code (Text):
    if(selected.get(p.getUniqueId()).equals("m8")){irongolem.getItemMeta().addEnchant(Enchantment.DURABILITY, 1, true);}
    I also made sure the code gets executed.
     
  8. MiniDigger

    Supporter

    you need to get the item meta, add the enchant and set the changed itemmeta on the itemstack...
    (just like you did with your other stuff)
     
  9. I also tried that but that didn't do the trick either.
    Code:
    Code (Text):
    if(selected.get(p.getUniqueId()).equals("m8")){
    ItemMeta meta = irongolem.getItemMeta();
    meta.addEnchant(Enchantment.DURABILITY, 3, true);
    irongolem.setItemMeta(meta);
    }
    UPDATE:
    I tried adding the enchantment to the itemstack without the if statement and it DID add it because it was in the lore, BUT the item didn't look enchanted.