How to return a public string in another class

Discussion in 'Plugin Development' started by nnx, Jun 24, 2020.

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

    nnx

    PS: English fom Google Translate.

    Hello .. Good night! I would like a help here, because I tried in different ways and I couldn't.

    Code:
    public String getProgressBar(int current, int max, int totalBars, char symbol, ChatColor completedColor, ChatColor notCompletedColor) {
    
         p = null;
         Rank r = Main.getAPI().getRPlayer(p.getName()).getRank();
         Rank next = Main.getRankCore().getRank(NextRank.getNext(r.getRankName()));
        
         current = (int) CoreUtils.getMoney(p.getName());
         max = (int) next.getPrice();
         totalBars = 10;
         symbol = '█';
         completedColor = ChatColor.GREEN;
         notCompletedColor = ChatColor.GRAY;
        
         float percent = (float) current / max;
         int progressBars = (int) (totalBars * percent);
         return Strings.repeat("" + completedColor + symbol, progressBars) + Strings.repeat("" + notCompletedColor + symbol, totalBars - progressBars);
        
       }
    
    I would like to return the getProgressBar method to the Main class. More specifically for this use:
    Screenshot_1.png

    Thank you very much for your attention. If you have any typos. This was translated by Google Translate
     
    Last edited by a moderator: Jun 24, 2020
  2. Offline

    KarimAKL

    @nnx You need to pass an instance of the class to whatever class you want to use the method in.
     
  3. Offline

    mAndAle

    For example you can create a Util class:

    Code:
    public class Util{
    
    public class ChatUtil(String s){
       return ChatColor.TranslateAlternativeCodes('&', s);
    }
    }
    and now in the class you want to use this:

    Code:
    private Util util = new Util();
    
    util.ChatUtil("//your message");
    now for your case:

    Code:
    //In another class
    
    private //yourclass example = new //yourclass();
    
    
    example.getProgressBar(//your code here);
    
     
  4. Offline

    nnx

    PS: English from Google Translate

    I've tried to do it this way, but the method is a string with assigned values:
    public String getProgressBar(int current, int max, int totalBars, char symbol, ChatColor completedColor, ChatColor notCompletedColor)
    When I return it to Main it comes with its variables and this happens:
    Screenshot_2.png
    If I remove the values, it accuses error:
    Screenshot_3.png
    What do I do to be able to declare the method?
     
  5. Offline

    Legendary_zotar

    You need to pass the parameters to receive the string from your method, If the method is being called somewhere else, maybe save it and have the Main grab the saved version
     
  6. Offline

    mAndAle

    Beacause you need to pass the parameters to your metod:

    Code:
      progresso.getProgessoBar(//value, //value, //value ed ecc..)
    
     
  7. Offline

    Strahan

    Also in the code you posted, it should immediately crash as soon as you call that function because:
    Code:
    p = null;
    Rank r = Main.getAPI().getRPlayer(p.getName()).getRank();
    You are setting p to null, then trying to perform a method against it. You can't getName() from null.

    Really, and I don't mean this to be rude, but you really should learn the basics of Java before trying to make a plugin. It would make for a lot less headaches.
     
Thread Status:
Not open for further replies.

Share This Page