Solved How to set different colour for levels?

Discussion in 'Plugin Development' started by CrazyYoungBro, Jun 15, 2016.

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

    CrazyYoungBro

    Hello,
    I am making a levelup plugin which also has a chat prefix built-in. For that matter, I need to do such that for levels 0-19 the chat would show up like &7<level> <rank> <name> > <message>
    I am not able to figure out how to do so. Can someone help me? Also, do I need to create a seperate method for this or do it in the onChat method itself?
     
  2. Offline

    mine-care

    You mean their experience levels or a custom leveling system you got?

    Anyways there are plenty of ways you can go about doing this.
    You can have a ton of if and else statements (or a switch statement) but i dont recomend it, you can have a map with the level as key and the color as value which i think is not nessesary but the best solution i think is to go about doing it with an array.
    See the following Pseudocode example:

    Code:
    
    ChatColor[] array = {ChatColor1,ChatColor2 ... ChatColorn};
    
    onChat:
        int LEVEL = getPlayerLevel;
        if array.length < level then:
            ChatColor color = array [ LEVEL ];
        else 
            ChatColor othercolor = ...
        end if;
    end onChat;
    
    
     
  3. Offline

    CrazyYoungBro

    Oh Nice. But is there any simpler way to do it?
     
  4. Offline

    Minesuchtiiii

    Check the level of the player. If its value is bigger than X and less than X2 then change the format.
    Code:
    int lvl = p.getLevel(); //use your method to get his lvl
    
    //in chat listener
    
    if(lvl  > 0 && lvl < 21) {
    
    e.setFormat(some.format);
    
    }
    else if(lvl  > 20 && lvl < 41) {
    
    e.setFormat(your.format); //and so on..
    
    }
     
  5. Offline

    CrazyYoungBro

    Thank you.
     
  6. Offline

    mine-care

    @Minesuchtiiii That wouldn't be efficient at all for large levels...
    For instance if the op has to create 50 different levels, they would have to create 5o if/else if statements which would make the code hard to read, update and maintain, but also, lets compare your solution with mine for the worse case senarios:
    Lets say that the last if condition in your code is ment to serve level 50/50 (not experience level, i mean actual level. That means we have 50 49 if's before this one). That means that before finding the correct value, the VM is making 49 comparisons, that are rather simple but still take some time.
    On the other hand, with my solution and given that arrays have a recall efficiency O(1), it would take exactly as much as it would take for any other level making it more efficient. In other words it is kinda like comparing two ways of doing it where one has efficiency O(1) whereas the other has efficiency O(n). What is better? :p
     
  7. Offline

    Minesuchtiiii

    He asked for a simpler way. There he got it. No one was comparing our solutions.
     
  8. Offline

    mine-care

    @Minesuchtiiii I see, but still what I said was a correction and I just wonder how a ton of if, else if and else conditions are simpler than 10 lines of code. Also I don't see what is hard about accessing elements in an array.

    I also felt that it was a good idea to remind the op and possibly you why efficiency matters and what it is.
     
  9. Offline

    Minesuchtiiii

    Yes, good job on that. I agree with you on that point. I just wanted to show him an easy way, no one said that it is efficient or just 10 lines of code.
     
  10. Offline

    mine-care

    @Minesuchtiiii Still cant see what is hard about accessing elements in an array but ok :p
     
  11. Offline

    Minesuchtiiii

    Read what I wrote, I don't think you get it.
     
  12. Offline

    mine-care

    It is indeed possible, and apologies if that is the case. What i responded to is:

    Anyways i think we went too far off topic, we can continue this in a private conversation if you like :)
     
Thread Status:
Not open for further replies.

Share This Page