RGEnchantments - Make your costum enchantments come true!

Discussion in 'Resources' started by Omerrg, Jun 2, 2013.

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

    Omerrg

    Hello Guys, Im Omer, well known as Omerrg. Im an Israeli developer, We're not well known in the world as developers, But i tried my best to make this class. Please dont decompile it or use it for bad uses, I made it for fun and for everybody's fun. I made it as good as i can, and it may be simple and not the best, But you know, YOLO ;)
    Well, Let's get to the topic:
    You download the LIB here:
    https://mega.co.nz/#!aY5xkLiC!KxBf1QdeHzjtn5SABXk2MwLhpaBmFS28lxEF2wIEb7Y
    you place it in your Build Path (Referenced Libraries) and add this line in your plugin.yml:
    Code:
     softdepend: [RGEnchantments]
    Now all you have to do is drop this in your plugins folder, and use the methods to make your dreamy enchantments come true!
    How does this work you ask?
    Well, I made some simple methods for this, If you have ideas for more just post in the comments:
    REMEMBER TO REPLACE RGMethods. With Methods.
    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent e){
    //Regular interact event.. Nothing special
        if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
            // Getting the action..
    if(RGMethods.hasEnchantment(e.getItem(), "Test")){
    //Now we check if the item we used has the enchantment with the name 'Test', If it does this will return true.
                e.getPlayer().sendMessage("" +RGMethods.getEnchantmenLevel(e.getItem(), "Test"));
            //If the 'Test' Enchantment exist on this item, it will send the player a message with the level of the enchantment.
          }
      }
    }
    //Now, here's a code that will give you a diamond axe with the enchantment 'Test' on it, enchantment lvl is 11.
    Player p = (Player) sender;
                ItemStack us = new ItemStack(Material.DIAMOND_AXE);
                ItemStack is = RGMethods.addEnchantment(us, "Test", 11);
     
            p.setItemInHand(is);
     
    
    Have a good day, Tell me what you think. This is my first lib so be ez on me >.<


    People asked for it, so here it is : if you dont wanna download it, You can use this class instead:
    (Tried to minimize everything to one class, Its not little but if you can help me make it better i will be glad :D )
    Code:
    package com.omerg.enchantments;
     
     
    import java.util.ArrayList;
    import java.util.List;
     
    import net.minecraft.server.v1_5_R3.NBTTagCompound;
    import net.minecraft.server.v1_5_R3.NBTTagList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
     
     
     
     
     
    public class RGMethods {
     
    public static class RN {
     
        enum Numeral {
            I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
            int weigth;
     
            Numeral(int weigth) {
                this.weigth = weigth;
            }
        };
     
        public static String roman(long n) {
     
            if( n <= 0) {
                throw new IllegalArgumentException();
            }
     
            StringBuilder buf = new StringBuilder();
     
            final Numeral[] values = Numeral.values();
            for (int i = values.length - 1; i >= 0; i--) {
                while (n >= values[i].weigth) {
                    buf.append(values[i]);
                    n -= values[i].weigth;
                }
            }
            return buf.toString();
        }
     
        public static void test(long n) {
            System.out.println(n + " = " + roman(n));
        }
     
     
        public static int romanToDecimal(java.lang.String romanNumber) {
            int decimal = 0;
            int lastNumber = 0;
            String romanNumeral = romanNumber.toUpperCase();
                /* operation to be performed on upper cases even if user enters roman values in lower case chars */
            for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
                char convertToDecimal = romanNumeral.charAt(x);
     
                switch (convertToDecimal) {
                    case 'M':
                        decimal = processDecimal(1000, lastNumber, decimal);
                        lastNumber = 1000;
                        break;
     
                    case 'D':
                        decimal = processDecimal(500, lastNumber, decimal);
                        lastNumber = 500;
                        break;
     
                    case 'C':
                        decimal = processDecimal(100, lastNumber, decimal);
                        lastNumber = 100;
                        break;
     
                    case 'L':
                        decimal = processDecimal(50, lastNumber, decimal);
                        lastNumber = 50;
                        break;
     
                    case 'X':
                        decimal = processDecimal(10, lastNumber, decimal);
                        lastNumber = 10;
                        break;
     
                    case 'V':
                        decimal = processDecimal(5, lastNumber, decimal);
                        lastNumber = 5;
                        break;
     
                    case 'I':
                        decimal = processDecimal(1, lastNumber, decimal);
                        lastNumber = 1;
                        break;
                }
            }
            return decimal;
        }
     
        public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
            if (lastNumber > decimal) {
                return lastDecimal - decimal;
            } else {
                return lastDecimal + decimal;
            }
        }
     
     
    }
        /*
        *
        *All the rights on this class goes to Omerrg
        *for making the RGEnchantments class
        *and each method that was used in this class was
        *made by him but the one above.
        *(Credit to bukkit for the API)
        *
        */
     
        private static CraftItemStack addGlow(org.bukkit.inventory.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);
          }
     
    public static ItemStack addEnchantment(ItemStack itemtoenchant,String enchantment,int level){
        ItemStack itemtoenchantt = itemtoenchant;
        ItemMeta meta = itemtoenchantt.getItemMeta();
        List<String> lore;
        if(meta.hasLore()){
        lore = meta.getLore(); 
        }else{
            lore = new ArrayList<String>();
        }
     
     
     
         
            lore.add(ChatColor.GRAY + enchantment +" " + RN.roman(level));
     
     
     
        meta.setLore(lore);
        itemtoenchantt.setItemMeta(meta);
        itemtoenchantt = addGlow(itemtoenchantt);
        return itemtoenchantt;
     
    }
    public static boolean hasEnchantment(ItemStack itemtocheck,String enchantment){
        ItemMeta meta = itemtocheck.getItemMeta();
        for(String s : meta.getLore()){
            if(s.contains(enchantment)){
                return true;
            }
        }
        return false;
    }
    public static int getEnchantmenLevel(ItemStack itemtocheck, String enchantment){
        ItemMeta meta = itemtocheck.getItemMeta();
        //List<Integer> digits = new ArrayList<Integer>();
        for(String s : meta.getLore()){
            if(s.contains(enchantment)){
                String si = s.replaceAll(enchantment, "");
                return RN.romanToDecimal(si);
            /*    char[] charlist = s.toCharArray();
                for(char c : charlist){
                    if(Character.isDigit(c)){
                        digits.add(Integer.parseInt(Character.toString(c)));
                    }
                }
                for(int i : digits){
                    String stoint = s + i;
                    return Integer.parseInt(stoint);
                }*/
            }
        }
        return 0;
    }
    }
    
     
  2. Offline

    CakeProgrammer

    Omer ! so swag! awesome tutorial maybe I'll use this sometimes!
     
  3. Offline

    Omerrg

    Its a tutorial on using the Lib, But w/e, It doesn't meant to be a tutorial but a library, But thx anyways!!! ;)
     
  4. Offline

    hawkfalcon

    It would be nice if you made a nice little class out of this to share instead, so we wouldn't have to force the user to download another plugin.
     
  5. Offline

    Omerrg

    I did it! If you can help me making it minimized and compactible as possible i would be glad. Because im so glad you commented, i did it as fast as possible! Not there's the class and the jar, Though i didn't tested the class by itself (it was seperetaed to 2 before). Good Day and thank you again! :D
    (Post again to let me know your thoughts about the class)
     
    bobacadodl and hawkfalcon like this.
  6. Offline

    stirante

    Cool, I even see my code here :D (I had class called EnchantGlow in old PrettyScaryLib and your method addGlow looks almost the same)
     
  7. Offline

    Ultimate_n00b

    Code:java
    1. package com.omerg.enchantments;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import net.minecraft.server.v1_5_R3.NBTTagCompound;
    7. import net.minecraft.server.v1_5_R3.NBTTagList;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13.  
    14. public class RGMethods {
    15.  
    16. public static class RN {
    17.  
    18. enum Numeral {
    19. I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
    20. int weigth;
    21.  
    22. Numeral(final int weigth) {
    23. this.weigth = weigth;
    24. }
    25. }
    26.  
    27. public static String roman(long n) {
    28.  
    29. if (n <= 0)
    30. final StringBuilder buf = new StringBuilder();
    31. final Numeral[] values = Numeral.values();
    32. for (int i = values.length - 1; i >= 0; i--)
    33. while (n >= values[i].weigth) {
    34. buf.append(values[i]);
    35. n -= values[i].weigth;
    36. }
    37. return buf.toString();
    38. }
    39.  
    40. public static void test(final long n) {
    41. System.out.println(n + " = " + roman(n));
    42. }
    43.  
    44. public static int romanToDecimal(final java.lang.String romanNumber) {
    45. int decimal = 0;
    46. int lastNumber = 0;
    47. final String romanNumeral = romanNumber.toUpperCase();
    48. /* operation to be performed on upper cases even if user enters Roman values in lower case chars */
    49. for (int x = romanNumeral.length() - 1; x >= 0; x--) {
    50. final char convertToDecimal = romanNumeral.charAt(x);
    51.  
    52. switch (convertToDecimal) {
    53. case 'M':
    54. decimal = processDecimal(1000, lastNumber, decimal);
    55. lastNumber = 1000;
    56. break;
    57.  
    58. case 'D':
    59. decimal = processDecimal(500, lastNumber, decimal);
    60. lastNumber = 500;
    61. break;
    62.  
    63. case 'C':
    64. decimal = processDecimal(100, lastNumber, decimal);
    65. lastNumber = 100;
    66. break;
    67.  
    68. case 'L':
    69. decimal = processDecimal(50, lastNumber, decimal);
    70. lastNumber = 50;
    71. break;
    72.  
    73. case 'X':
    74. decimal = processDecimal(10, lastNumber, decimal);
    75. lastNumber = 10;
    76. break;
    77.  
    78. case 'V':
    79. decimal = processDecimal(5, lastNumber, decimal);
    80. lastNumber = 5;
    81. break;
    82.  
    83. case 'I':
    84. decimal = processDecimal(1, lastNumber, decimal);
    85. lastNumber = 1;
    86. break;
    87. }
    88. }
    89. return decimal;
    90. }
    91.  
    92. public static int processDecimal(final int decimal, final int lastNumber, final int lastDecimal) {
    93. if (lastNumber > decimal)
    94. return lastDecimal - decimal;
    95. else
    96. return lastDecimal + decimal;
    97. }
    98. }
    99.  
    100. /*
    101.   *
    102.   *All the rights on this class goes to Omerrg
    103.   *for making the RGEnchantments class
    104.   *and each method that was used in this class was
    105.   *made by him but the one above.
    106.   *(Credit to bukkit for the API)
    107.   *
    108.   */
    109.  
    110. private static CraftItemStack addGlow(final org.bukkit.inventory.ItemStack item) {
    111. final net.minecraft.server.v1_5_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
    112. NBTTagCompound tag = null;
    113. if (!nmsStack.hasTag()) {
    114. tag = new NBTTagCompound();
    115. nmsStack.setTag(tag);
    116. }
    117. if (tag == null)
    118. tag = nmsStack.getTag();
    119. final NBTTagList ench = new NBTTagList();
    120. tag.set("ench", ench);
    121. nmsStack.setTag(tag);
    122. return CraftItemStack.asCraftMirror(nmsStack);
    123. }
    124.  
    125. public static ItemStack addEnchantment(final ItemStack itemtoenchant, final String enchantment, final int level) {
    126. ItemStack itemtoenchantt = itemtoenchant;
    127. final ItemMeta meta = itemtoenchantt.getItemMeta();
    128. List<String> lore;
    129. if (meta.hasLore())
    130. lore = meta.getLore();
    131. else
    132. lore = new ArrayList<String>();
    133. lore.add(ChatColor.GRAY + enchantment + " " + RN.roman(level));
    134. meta.setLore(lore);
    135. itemtoenchantt.setItemMeta(meta);
    136. itemtoenchantt = addGlow(itemtoenchantt);
    137. return itemtoenchantt;
    138.  
    139. }
    140.  
    141. public static boolean hasEnchantment(final ItemStack itemtocheck, final String enchantment) {
    142. final ItemMeta meta = itemtocheck.getItemMeta();
    143. for (final String s : meta.getLore())
    144. if (s.contains(enchantment))
    145. return true;
    146. return false;
    147. }
    148.  
    149. public static int getEnchantmenLevel(final ItemStack itemtocheck, final String enchantment) {
    150. final ItemMeta meta = itemtocheck.getItemMeta();
    151. //List<Integer> digits = new ArrayList<Integer>();
    152. for (final String s : meta.getLore()) {
    153. if (s.contains(enchantment)) {
    154. final String si = s.replaceAll(enchantment, "");
    155. return RN.romanToDecimal(si);
    156. /* char[] charlist = s.toCharArray();
    157.   for(char c : charlist){
    158.   if(Character.isDigit(c)){
    159.   digits.add(Integer.parseInt(Character.toString(c)));
    160.   }
    161.   }
    162.   for(int i : digits){
    163.   String stoint = s + i;
    164.   return Integer.parseInt(stoint);
    165.   }*/
    166. }
    167. }
    168. return 0;
    169. }
    170. }[/i][/i][/i]


    Mostly just formatting stuff, removed 20 lines. Didn't go too in depth, just Eclipse Formatter + Clean Up, and a quick scan of the code.
     
  8. Offline

    Omerrg

    Ultimate_n00b thank you vey much! I will change it later (I'm from my phone)
    Yaps, I used it and I didn't take credit on it
    Cause it isn't
    Mine :3 thx for visiting ;)
     
  9. Is there any difference from this and EnchantmentAPI?
     
  10. Offline

    Omerrg

    Hey, Can you show me EnchantmentAPI? Because i dont know what it is.. But anyways, thx for visiting :D
     
  11. Offline

    Omerrg

    -I didn't saw it before, but it's pretty similar . I made this by myself and this has different methods and different uses from the 'enchantmentapi', thx for visiting and good day my sir ;)
     
    InspectorFacepalm likes this.
  12. ahh kay, thanks! I'll try to your way once or twice :p!
     
  13. Offline

    Omerrg

    Forgot to say that thx to Ultimate_n00b and hawkfalcon you can add it
    To your own
    Package no
    Need for an external
    Plugin if you don't want ;). I hope you will use my way! That why I made it ;)
     
    hawkfalcon likes this.
  14. Offline

    ELCHILEN0

    I personally think it would be better to extend the Enchantment class. You can then add simple listeners to check if the enchantment is present and can use the native Bukkit API. However, good job for posting it :)
     
  15. Offline

    Omerrg

    Yaps, but this is nice and compact too . Maybe I'll try to merge it with the API later ;) I like it that way right now
     
  16. Offline

    LordManegane

    Omerrg Do you devolpe a way to check the enchantment that the items have?
     
  17. Offline

    Omerrg

    You mean the costum enchantments, or the Vanila Enchantments?...
     
  18. Offline

    bobacadodl

    Just to let you know, its "custom", not "costum" :p Nice library though :)
     
  19. Offline

    LordManegane

    Omerrg i say custom enchantments.
    Something like made a ArrayList with the enchantments that the item have
     
  20. Offline

    Omerrg

    You can get it by doing it with a method i made (getEnchantment)
     
  21. Offline

    LordManegane

    Omerrg The getEnchantment its for checking this no?
    checkitem, enchantment
     
  22. Offline

    Omerrg

    There's the hasEnchantment method, and the getEnchantmentLevel method..
     
  23. Offline

    Samthelord1

    Bookmarked, looks nice
     
Thread Status:
Not open for further replies.

Share This Page