Help on Custom Enchantment Levels?

Discussion in 'Spigot Plugin Development' started by Uzi, May 18, 2017.

  1. Uzi

    Uzi

    Thanks to and shout out to ChefJava
    I cannot get enchantment levels to change to I to 1 or from V to 5 so roman numerals to numbers <

    Code not working <

    Here is what I typed out

    Code (Text):
    package me.Diversive;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
        public void onEnable() {
            ConsoleCommandSender console = getServer().getConsoleSender();
            console.sendMessage(ChatColor.AQUA + " (!) BuffedX420 Enabled (!) ");
            }
            public void onDisable () {
            ConsoleCommandSender console = getServer().getConsoleSender();
            console.sendMessage(ChatColor.RED + " (!) BuffedX420 Disabled (!) ");
        }
        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
            return false;
           
        }
            public int romanToInteger(String s) {
                if(s == null) {
                    return 0;
                }

                int length = s.length();
                int sum = 0;
                int pre = 0;

                for(int i = length - 1; i >= 0; i--){
                    int cur = romanTable(s.charAt(i));

                    if(i == length - 1){
                        sum = sum + cur;
                    }else{
                        if(cur < pre){
                            sum = sum - cur;
                        }else{
                            sum = sum + cur;
                        }
                    }
                    pre = cur;
                }
                return sum;
            }
            private int romanTable(char c){
                int num;
                switch(c){
                    case 'I':
                        num = 1;
                        break;
                    case 'V':
                        num = 5;
                        break;
                    case 'X':
                        num = 10;
                        break;
                    case 'L':
                        num = 50;
                        break;
                    case 'C':
                        num = 100;
                        break;
                    case 'D':
                        num = 500;
                        break;
                    case 'M':
                        num = 1000;
                        break;
                    default:
                        num = 0;
                        break;
                }
                return num;
            }
    }
     
     
  2. You got no idea of what you're doing, have you?

    Furthermore seems like you don't understand the basics of programming.

    You added 2 methods (#romanTable & #romanToInteger) to your main class. So? Noone ever is calling them! Of course nothing happens.
    If you want to apply those methods, you need to use them. Adding code to your classes does nothing except consuming RAM and disk space.

    Now some constructive help:
    • You need to apply those methods for ALL items you want to "convert" from roman to numeric
      • You need to add the org.bukkit.inventory.ItemFlag#HIDE_ENCHANTS on the item, so the roman text disappears
      • Then you need to add a lore with the same content, only numeric instead of roman
      • You don't even need your conversion methods. Those roman numbers only were clientside. Just iterate throuth all enchantments on the item, then you'll receive all levels as integer values directly.
     

  3. you have to call the method, the program wont magically call any method in your project at the appropriate time :p. wouldn't that be awesome.

    also, as said, i wasn't entirely sure what you wanted, but the method(s) themselves will convert a string roman numeral, into an integer. roman numerals are NOT integers.

    Code (Java):

    String roman = "IV";

    //assuming you have the methods

    System.out.println(romanToInteger(roman)); //will print 4.
     
  4. Uzi

    Uzi

    I am just wanting to fix the Enchantment.Level.10 where it will say Efficiency 10 only with nothing else,

    Another example is Efficiency I and make it to Efficiency 1 thanks everyone! <3 :D:cool:
     
  5. I was motivated to set something up and lead you on the right direction:
    Code (Java):
    public ItemStack convEnchText(ItemStack item) {
        if (item.hasItemMeta()) {
            ItemMeta meta = item.getItemMeta();
            if (meta.hasEnchants()) {
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                List<String> lore;
                if (meta.hasLore()) lore = meta.getLore();
                else lore = new ArrayList<>();
                for (Enchantment ench : meta.getEnchants().keySet()) {
                    /* > You may modify this string to make it more vanilla-like < */
                    lore.add(0, ench.getName() + " " + Integer.toString(meta.getEnchants().get(ench)));
                }
                meta.setLore(lore);
                item.setItemMeta(meta);
                return item;
            } else return item;
        } else return item;
    }
    Just apply this method on any item you want to convert.
     
  6. Uzi

    Uzi

    Thank you but sadly I am still not motivated still have not got this working :(
     
  7. because you don't call any methods anywhere.

    and you have an //empty// command method.