Solved Enchantment Glow !

Discussion in 'Plugin Development' started by Jace_oio, May 18, 2013.

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

    Jace_oio

    Hi,

    How would I put enchantment glow on a item without having to enchant it ?

    Any help is appreciated :)


    Best wishes - Jace_OiO :)
     
  2. You can use NBT to add the "ench" tag to the item, but this is not recommanded, there is also a way using ProtocolLib, but for BackPacks++ I just created a new Enchantment that does nothing...
     
  3. Offline

    Jace_oio

    CaptainBern Thanks for the quik answer :)

    But, how would you create a Enchantment that does nothing ..?

    - Best wishes Jace_OiO :)
     
  4. In BackPacks++ I did it like this:
    Code:
    try{
    try {
    Field f = Enchantment.class.getDeclaredField("acceptingNew");
    f.setAccessible(true);
    f.set(null, true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    //
    try {
    ench = new BPEnchantmentWrapper(69); //< this is your custom wrapper (a class that extends an EnchantmentWrapper with the needed stuff and returns, 69 is the id I choosed (lawl)
    EnchantmentWrapper.registerEnchantment(ench); //<this is used to register the enchantment.
    } catch (IllegalArgumentException e){
     
    }
    }catch(Exception e){
    e.printStackTrace();
    }
     
  5. Offline

    Jace_oio

    CaptainBern - thanks alot for the code :=)

    So basicly after I wrote this code I could create a itemstack and do
    item.addEnchantment(ench, 2); ??
     
  6. yes.
     
  7. Offline

    Jace_oio

    Cool, thanks ill just try it out :)

    CaptainBern Would would be needed in the BPEnchantmentWrapper class ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  8. You can change the name but it's imprtant you let it extend EnchantmentWrapper and then add the methods it says...In my case that looked like this:
    Code:
    package me.captainbern.backpack.utils;
     
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentTarget;
    import org.bukkit.enchantments.EnchantmentWrapper;
    import org.bukkit.inventory.ItemStack;
     
    public class BPEnchantmentWrapper extends EnchantmentWrapper {
     
        public BPEnchantmentWrapper(int id) {
            super(id);
        }
       
        @Override
        public boolean canEnchantItem(ItemStack item) {
            return true;
        }
     
        @Override
        public boolean conflictsWith(Enchantment other) {
            return false;
        }
     
        @Override
        public EnchantmentTarget getItemTarget() {
            return null;
        }
     
        @Override
        public int getMaxLevel() {
            return 2;
        }
     
        @Override
        public String getName() {
            return "BackPack";
        }
     
        @Override
        public int getStartLevel() {
            return 1;
        }
    }
     
  9. Offline

    Jace_oio

    Ok thanks,

    time to test !
     
  10. No problem! (Note: I think you still need to use addUnsafeEnchantment(); if you left the enchantmentTarget return null;)
     
  11. Offline

    Jace_oio

    CaptainBern
    It gives me a red line under ench in ench = new BPEnchantmentWrapper(69);
    Maybe I should do like this ?
    BPEnchantmentWrapper ench = newBPEnchantmentWrapper(69);
     
  12. Offline

    GodzOfMadness

    Jace_oio If it says it can't be resolved to a variable then yes.
     
  13. Oh yes, I did it like that because I made BPEnchantmentWrapper ench; a static that can be altered by Main.ench;
     
  14. Offline

    Jace_oio

    Okay :) well time to test XD

    CaptainBern Im not getting the glow effect or any enchantment :(

    Code:
    ItemStack kit = new ItemStack(Material.BOOK);
            ItemMeta meta7 = kit.getItemMeta();
            try{
                try {
                java.lang.reflect.Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);
                } catch (Exception e) {
                e.printStackTrace();
                }
                //
                try {
                ench = new BPEnchantmentWrapper(69); //< this is your custom wrapper (a class that extends an EnchantmentWrapper with the needed stuff and returns, 69 is the id I choosed (lawl)
                EnchantmentWrapper.registerEnchantment(ench); //<this is used to register the enchantment.
                } catch (IllegalArgumentException e){
               
                }
                }catch(Exception e){
                e.printStackTrace();
                }
            meta7.setDisplayName(ChatColor.AQUA +  ""  + ChatColor.ITALIC +  "Diamond sword and full iron armor kit");
            lore14.add(lore13);
            meta7.setLore(lore14);
            kit.addUnsafeEnchantment(plugin.ench, 1);
            kit.setItemMeta(meta7);
            shop.addItem(kit);
    and then
    static BPEnchantmentWrapper ench; in the top

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  15. Offline

    GodzOfMadness

    Jace_oio If you are just wanting to add a glow effect to the ItemStack then use this,
    Code:
        public static ItemStack addGlow(ItemStack item) {
            net.minecraft.server.v1_5_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
            NBTTagCompound tag = null;
            if (!nmsStack.hasTag()) {
                tag = new NBTTagCompound();
                nmsStack.setTag(tag);
            }
            if (tag == null)
                tag = nmsStack.getTag();
            NBTTagList ench = new NBTTagList();
            tag.set("ench", ench);
            nmsStack.setTag(tag);
            return CraftItemStack.asCraftMirror(nmsStack);
        }
     
  16. Hmm, does it print any errors?

    edit: Use the way GodOfMadness said, (I use my way to check if an item is a backpack)
     
  17. Offline

    Jace_oio

    Okay :) ill try it out

    GodzOfMadness how would I use your code if I wanted enchantment glow on a book ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  18. Offline

    GodzOfMadness

    Jace_oio The code I've given you should add the glow on the book.
    EDIT:
    The addGlow method returns an ItemStack so you would have to set the ItemStack that the addGlow method returns.
     
  19. Offline

    Jace_oio

    Ok Ill test :)

    But how can I make sure its the book I wanna add into the shop ???
    GodzOfMadness

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  20. Offline

    GodzOfMadness

    Jace_oio Which way do you want to check if it's the certain book? Through the DisplayName/Lore?
    If so then just get the ItemMeta of the ItemStack before you use the addGlow method and if it doesn't equal null(same for lore) then compare to one another. If all the checks go well then use the addGlow method.
     
  21. Offline

    TheTinySpider

    If you want to use NMS you can use this:

    PHP:
    public static ItemStack addGlow(ItemStack item) {
            
    net.minecraft.server.v1_5_R3.ItemStack nmsStack null;
            
    CraftItemStack craftStack null;
         
            if (
    item instanceof CraftItemStack)
                
    craftStack = (CraftItemStackitem;
            else
                
    craftStack CraftItemStack.asCraftCopy(item);
         
            
    nmsStack CraftItemStack.asNMSCopy(craftStack);
         
            
    NBTTagCompound tag null;
            if (!
    nmsStack.hasTag()) return item;
            
    tag nmsStack.getTag();
            
    tag.add("ench");
            
    nmsStack.setTag(tag);
         
            return 
    CraftItemStack.asCraftMirror(nmsStack);
        }
    Instead of 1.5.R3 you could use some reflection.
     
    TheGreenGamerHD likes this.
  22. Offline

    Jace_oio

    TheTinySpider thanks for the reply, what is NMS code ?? and how to use ?

    EDIT: hehe im guesing its net minecraft server ?
     
  23. Offline

    GodzOfMadness

    Jace_oio NMS = net.minecraft.server
     
  24. Offline

    Jace_oio

    hehe thanks even though I figured it out :)

    But still how do I use NMS code ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  25. Offline

    GodzOfMadness

    Jace_oio You're using it if you still have that addGlow method that I've given you. :p
     
  26. Offline

    Jace_oio

    GodzOfMadness hehe XD but how would I use it in general ?

    - every line of code is easyer to use if you know the meaning of it.
     
  27. Offline

    GodzOfMadness

    Jace_oio Here is the github for craftbukkit. You can always look through their classes and see how it works and how you can manipulate it. Like creating a custom entity. Like make a mob AI. You can say extend EntityZombie and takes it's methods and add/remove some of it's path finder goals to make it do what you want it to do.
     
  28. Offline

    ZeusAllMighty11

    You use it whenever you use an object or method that belongs to the 'net.minecraft.server' packages.

    CraftPlayer, CraftWorld, etc. are methods of Craftbukkit, not bukkit. Bukkit alone can not do this
     
  29. Offline

    Jace_oio

    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page