Solved String To Int

Discussion in 'Plugin Development' started by Albkad, Mar 6, 2017.

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

    Albkad

    So I Am Testing Out Ways To Make The Plugin I Am Making Have Unlimited Levels. So I Convert args[0] to int so i can use my method but it gives me an error when args[0] isnt an int. How do i check if args[0] isnt an int?
    Console: https://gyazo.com/f3941e43ad6aa2980a04435fdbb5760b
    Code:
    package me.albkad.test;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    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 {
        SettingsManager settings = SettingsManager.getInstance();
      
        public void onEnable() {
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          
            if (!(sender instanceof Player)) return true;
            Player p = (Player) sender;
          
            if (cmd.getName().equalsIgnoreCase("poison")) {
              
                if (args.length == 0) {
                    return true;
                }
              
                Integer x = Integer.valueOf(args[0]);
                if (p.getItemInHand().getType() == Material.DIAMOND_SWORD) {
                    p.setItemInHand(settings.Poison(Material.DIAMOND_SWORD, getint(x)));
                    return true;
                }
            }
            if (cmd.getName().equalsIgnoreCase("poison1")) {
                p.setItemInHand(settings.Poison1(p.getItemInHand().getType()));
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("poison2")) {
                p.setItemInHand(settings.Poison2(Material.DIAMOND_SWORD));
                return true;
            }
          
            return true;
        }
      
        public static String getint(int args) {
            if (args == 1) return "I";
            if (args == 2) return "II";
            if (args == 3) return "III";
            if (args == 4) return "IV";
            if (args == 5) return "V";
            if (args == 6) return "VI";
            if (args == 7) return "VII";
            if (args == 8) return "VIII";
            if (args == 9) return "IX";
            if (args == 10) return "X";
            if (args == 11) return "XI";
            if (args == 12) return "XII";
            return args+"";
            }
      
      
      
    
    }
    
     
  2. Offline

    Albkad

    @AlvinB So I Tried This But I Got To Return An Int And If I Say 0 Then It The Lore Changes Into Poison 0. I Need It To Just Stop The Code.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) return true;
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("poison")) {
               
                if (args.length == 0) {
                    return true;
                }
                if (p.getItemInHand().getType() == Material.DIAMOND_SWORD) {
                    p.setItemInHand(settings.Poison(Material.DIAMOND_SWORD, getint(changeInt(args[0]))));
                    }
                    return true;
                }
            if (cmd.getName().equalsIgnoreCase("poison1")) {
                p.setItemInHand(settings.Poison1(p.getItemInHand().getType()));
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("poison2")) {
                p.setItemInHand(settings.Poison2(Material.DIAMOND_SWORD));
                return true;
            }
           
            return true;
        }
       
        public static String getint(int args) {
            if (args == 1) return "I";
            if (args == 2) return "II";
            if (args == 3) return "III";
            if (args == 4) return "IV";
            if (args == 5) return "V";
            if (args == 6) return "VI";
            if (args == 7) return "VII";
            if (args == 8) return "VIII";
            if (args == 9) return "IX";
            if (args == 10) return "X";
            if (args == 11) return "XI";
            if (args == 12) return "XII";
            return args+"";
            }
        public int changeInt(String arg) {
            try {
    
                return Integer.parseInt(arg);
    
            } catch (NumberFormatException e) {
    
             return 0;
    
            }
        }
    
    
       
       
    
    }
     
    Last edited: Mar 6, 2017
  3. Offline

    Edvio

    if(Ints.tryParse(args[0]) == null)
     
  4. Offline

    Albkad

Thread Status:
Not open for further replies.

Share This Page