SpigotLib - VariableAPI

Jun 7, 2016
SpigotLib - VariableAPI
  • [​IMG]


    Variable format:

    <variablename:arguments>
    Arguments are useful, for giving some formatting or other info to the variable handlers.

    Adding variables:


    If you write any kind of plugin, please use this way to add your variables easily to my lib, to allow other plugins, their easy access.

    PHP:
    // First you need to have a class, which implements VariableAPI.VariableHandler
    //
    // This is an example for making an IP address variable
    public class IpVariable implements VariableAPI.VariableHandler {

            // This method will be called on every variable getting request
            // As you can see, each handler receives:
            //   1. an object, whereat you need to execute some methods to get the value of your variable
            //   2. the variable requester player, for making an easy hook to language API
            //   3. the arguments of the variable
            public String getValue(Object obj, Player plr, String args) {
                // Because you aren't sure on your variable usage, it's not a bad idea, to put the returning to a
                // try-catch block, otherwise, the variableAPI will report your error to the console :-)
                try {
                    return ((Player)obj).getAddress().getAddress().toString().substring(1);
                }
                catch (Throwable e){
                    e.printStackTrace();
                    return "?";
                }
            }
        }

    //Then you can put it to the variables
    VariableAPI.handlers.put("ip",new IpVariable());
    Getting variables:

    PHP:
    // Filling all the variables in a message, recommended usage
    message=VariableAPI.fillVariables(String message,Player player,Object object);

    // Getting one variable, not recommended way
    // This way is only usefull for some testing
    VariableHandler handler=VariableAPI.handlers.get("variablename");
    String message="Failed handling";
    try{
        message=handler.getValue(Object object,Player player,String arguments);
    }
    catch (Throwable e){
        e.printStackTrace();
    }




    [​IMG]
  • Loading...
  • Loading...