Public Method/Public Variable from Config? (And making &<code> color codes work)

Discussion in 'Plugin Development' started by WolfLeader116, Aug 26, 2014.

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

    WolfLeader116

    Hello! So is there any way of creating a method in the main class that can be called from any other class? Also is there a way of fetching the value of a key from the config file, and changing the value, (internally without actually editing the config file) if it were something such as &<code>hello, to ChatColor.<color> + "hello" and saving it to a global variable? Or whatever you would save it as to be fetched in a message such as:
    Code:java
    1. sender.sendMessage(variable + " this is some text!"

    And by global variable, I mean a variable that can be fetched in any class other than the main one where it was set.

    Thanks!
     
  2. Offline

    Gater12

  3. Offline

    WolfLeader116


    Thanks for that link, but how do I pass the instance to the other classes? :/ I generally have a hard time understanding things unless I see the code, and see the result which is why I never really got that... Uhh and I didn't 100% get the link either... :/ In the main class, am I supposed to set the key to a variable, then elsewhere in the main class, I put something like this?
    Code:java
    1. public static String translateAlternateColorCodes(char, variable) {
    2. //idk what this is
    3. }
     
  4. Offline

    Gater12

  5. Offline

    WolfLeader116

  6. Offline

    Gater12

    WolfLeader116
    There is an example on how to pass in parameters on the page.

    You want to pass in your main class instance.
     
  7. Offline

    WolfLeader116

    So I would do something like this?
    Code:java
    1. public static String translateAlternateColorCodes(char &, String cprefix);
    2.  
    3. private void prefix() {
    4. Object cprefix;
    5. getConfig().set("Miscellaneous.Prefix", cprefix);
    6. }


    But then Eclipse says Syntax error on token "&", invalid VariableDeclaratorId about &. Also, how can I get the "value" of this in another class inside of a string?
     
  8. Offline

    Gater12

    WolfLeader116
    That's not how Java syntax works.

    translateAlternateColorCodes is a static method belonging to the class ChatColor.

    E.g.

    Code:java
    1. String translated = ChatColor.translateAlternateColorCodes(charHere, stringToBeTranslated);


    Also be sure to save the config after setting a value to a node!
     
  9. Offline

    WolfLeader116

    Ok there were 2 things in my last message that were a fail on my part.... #1, I was tired when I made this code earlier so I accidentally tried to set a new value for the node instead of getting the value of it and #2, I am still a bit tired so I only just half realized exactly what you have been talking about this whole time... For some reason I thought it was something else... Well basiclly what I wanted to do, was get the value of the node Miscellaneous.Prefix and set it to a variable (is that possible? Something like getConfig().addDefault("Miscellaneous.Prefix", cprefix); would set the value of Miscellaneous.Prefix to the variable cprefix?) and then translate it using your method (String translated = ChatColor.translateAlternateColorCodes(&, cprefix); ) and then somehow get the value of the variable translated in another class? So in the main class, I would have something like this?
    Code:java
    1. private void prefix() {
    2. Object cprefix;
    3. getConfig().addDefault("Miscellaneous.Prefix", cprefix);
    4. String translated = ChatColor.translateAlternateColorCodes(&, cprefix);
    5. }


    Sorry if this seems really stupid, I'm just out of it today...
     
  10. Offline

    Gater12

    WolfLeader116
    cpprefix is null and why not make it a String instead of an Object?
    If it's because you saw that addDefault accepted Object instance as the second parameter, all classes are an instance of Object. Pretty much you can pass anything in there (Wouldn't neccessary mean it would serialize correctly but it wouldn't throw an error)

    Chars are in single quotes (e.g. 'a').
     
  11. Offline

    WolfLeader116

    Gater12
    Would this work?
    Code:java
    1. private void prefix() {
    2. String cprefix = null;
    3. getConfig().addDefault("Miscellaneous.Prefix", cprefix);
    4. String prefix = ChatColor.translateAlternateColorCodes('&', cprefix);
    5. }


    But then how would I get the value of the variable prefix in another class?

    Ok I've added some stuff and now everything is done, except for some reason, I still can't get this thing to work.
    Code:java
    1. private void prefix() {
    2. String cprefix = null;
    3. getConfig().addDefault("Miscellaneous.Prefix", cprefix);
    4. String prefix = ChatColor.translateAlternateColorCodes('&', cprefix) + " ";
    5. }

    Also, that would add a space to the prefix right? And what I just need to figure out, is how to get the value of the variable prefix from another class. The code above is in the main class. I need to put the variable into a message that will be sent to the sender of the command. Thanks for all your help so far! :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page