Created
February 22, 2016 15:40
-
-
Save Mumfrey/b8dacc146700b569dcbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.eq2online.macros.scripting.actions.lang; | |
import java.util.Random; | |
import net.eq2online.macros.scripting.api.IMacro; | |
import net.eq2online.macros.scripting.api.IMacroAction; | |
import net.eq2online.macros.scripting.api.IReturnValue; | |
import net.eq2online.macros.scripting.api.IScriptActionProvider; | |
import net.eq2online.macros.scripting.api.ReturnValue; | |
import net.eq2online.macros.scripting.parser.ScriptAction; | |
import net.eq2online.macros.scripting.parser.ScriptContext; | |
import net.eq2online.macros.scripting.parser.ScriptCore; | |
public class ScriptActionRandom extends ScriptAction | |
{ | |
private static Random rand = new Random(); | |
public ScriptActionRandom(ScriptContext context) | |
{ | |
super(context, "random"); | |
} | |
/* (non-Javadoc) | |
* @see net.eq2online.macros.scripting.ScriptAction#Execute(net.eq2online.macros.scripting.api.IScriptActionProvider, net.eq2online.macros.scripting.api.IMacro, net.eq2online.macros.scripting.api.IMacroAction, java.lang.String, java.lang.String[]) | |
*/ | |
@Override | |
public IReturnValue execute(IScriptActionProvider provider, IMacro macro, IMacroAction instance, String rawParams, String[] params) | |
{ | |
ReturnValue retVal = new ReturnValue(0); | |
if (params.length > 0) | |
{ | |
int min = 0; | |
int max = 100; | |
if (params.length > 1) | |
{ | |
max = ScriptCore.tryParseInt(ScriptCore.parseVars(provider, macro, params[1], false), 100); | |
} | |
if (params.length > 2) | |
{ | |
min = ScriptCore.tryParseInt(ScriptCore.parseVars(provider, macro, params[2], false), 0); | |
} | |
if (max < min) | |
{ | |
int swap = min; | |
min = max; | |
max = swap; | |
} | |
int randomNumber = rand.nextInt(max - min + 1) + min; | |
retVal.setInt(randomNumber); | |
ScriptCore.setVariable(provider, macro, params[0], randomNumber); | |
} | |
else | |
{ | |
int randomNumber = rand.nextInt(101); | |
retVal.setInt(randomNumber); | |
} | |
return retVal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment