Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Data {
- //A static HashMap that stores the data for all the scripts running
- //in different tabs.
- private static final HashMap<String, Data> data = new HashMap<>();
- //The instance variables that pertain to each individual script running in each tab.
- private Point position;
- private int value1;
- private int value2;
- private Data() {
- String playerName;
- while ((playerName = Player.getRSPlayer().getName()) == null) {
- General.sleep(5);
- }
- //Reset the old data before adding the new data.
- resetData();
- //Add the new data to the HashMap.
- data.put(playerName, this);
- }
- /**
- * Call this at the beginning of every script you make, and the rest will be taken
- * cared of automatically. */
- public static void initData() {
- new Data();
- }
- /**
- * Returns true if the data has been loaded, false otherwise.
- * <p>
- * @[member=Return] True if the data has been loaded, false otherwise.
- */
- private static boolean isDataLoaded() {
- //The ONLY reason a null check isn't necessary here is because "containsKey(...)"
- //allows a null parameter.
- //This is not always the case. If you aren't sure if a method allows for null parameters, ALWAYS do
- //a null check.
- return data.containsKey(Player.getRSPlayer().getName());
- }
- /**
- * Resets the data of the currently running script. Returns true if the script data
- * has been removed at the end of the method, false otherwise.
- * <p>
- * @[member=Return] True if the script data has been removed at the end of the method,
- * false otherwise. */
- private static boolean resetData() {
- String playerName = Player.getRSPlayer().getName();
- if (playerName != null) {
- data.remove(playerName);
- return !data.containsKey(playerName);
- } else {
- return false;
- }
- }
- /**
- * Returns the proper data that is to be used in your methods.
- * <p>
- * @[member=Return] The proper data that is to be used in your methods. */
- private static Data getData() {
- if (isDataLoaded()) {
- String playerName = Player.getRSPlayer().getName();
- return playerName == null ? null : data.get(playerName);
- } else {
- throw new RuntimeException("Data has not been loaded");
- }
- }
- /**
- * Returns the data attached to the specified key, or null if the key is null.
- * <p>
- * @param key The key that is used to retrieve the Data from the HashMap.
- * @[member=Return] The data attached to the specified key, or null if the key is null. */
- private static Data getData(String key) {
- if (key == null) {
- return null;
- } else {
- return data.get(key);
- }
- }
- //You can also make these methods "blocking methods". That means that they will not return a value
- //until a condition is met, hence blocking further execution. Instead of throwing an exception
- //(which might cause issues if the script logs out), you could instead wait for the name of the
- //player to not be null before returning a value.
- //Methods that you will be calling in your scripts:
- public static Point getPosition() {
- if (isDataLoaded()) {
- return getData().position;
- } else {
- throw new RuntimeException("Data has not been loaded");
- }
- }
- public static int getValue1() {
- if (isDataLoaded()) {
- return getData().value1;
- } else {
- throw new RuntimeException("Data has not been loaded");
- }
- }
- public static int getValue2() {
- if (isDataLoaded()) {
- return getData().value2;
- } else {
- throw new RuntimeException("Data has not been loaded");
- }
- }
- }
Add Comment
Please, Sign In to add comment