its_j0hn

Class Example

Jun 17th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public class Data {
  2. //A static HashMap that stores the data for all the scripts running
  3. //in different tabs.
  4. private static final HashMap<String, Data> data = new HashMap<>();
  5. //The instance variables that pertain to each individual script running in each tab.
  6. private Point position;
  7. private int value1;
  8. private int value2;
  9.  
  10. private Data() {
  11. String playerName;
  12. while ((playerName = Player.getRSPlayer().getName()) == null) {
  13. General.sleep(5);
  14. }
  15. //Reset the old data before adding the new data.
  16. resetData();
  17. //Add the new data to the HashMap.
  18. data.put(playerName, this);
  19. }
  20.  
  21. /**
  22. * Call this at the beginning of every script you make, and the rest will be taken
  23. * cared of automatically. */
  24. public static void initData() {
  25. new Data();
  26. }
  27.  
  28. /**
  29. * Returns true if the data has been loaded, false otherwise.
  30. * <p>
  31. * @[member=Return] True if the data has been loaded, false otherwise.
  32. */
  33. private static boolean isDataLoaded() {
  34. //The ONLY reason a null check isn't necessary here is because "containsKey(...)"
  35. //allows a null parameter.
  36. //This is not always the case. If you aren't sure if a method allows for null parameters, ALWAYS do
  37. //a null check.
  38. return data.containsKey(Player.getRSPlayer().getName());
  39. }
  40.  
  41. /**
  42. * Resets the data of the currently running script. Returns true if the script data
  43. * has been removed at the end of the method, false otherwise.
  44. * <p>
  45. * @[member=Return] True if the script data has been removed at the end of the method,
  46. * false otherwise. */
  47. private static boolean resetData() {
  48. String playerName = Player.getRSPlayer().getName();
  49. if (playerName != null) {
  50. data.remove(playerName);
  51. return !data.containsKey(playerName);
  52. } else {
  53. return false;
  54. }
  55. }
  56.  
  57. /**
  58. * Returns the proper data that is to be used in your methods.
  59. * <p>
  60. * @[member=Return] The proper data that is to be used in your methods. */
  61. private static Data getData() {
  62. if (isDataLoaded()) {
  63. String playerName = Player.getRSPlayer().getName();
  64. return playerName == null ? null : data.get(playerName);
  65. } else {
  66. throw new RuntimeException("Data has not been loaded");
  67. }
  68. }
  69.  
  70. /**
  71. * Returns the data attached to the specified key, or null if the key is null.
  72. * <p>
  73. * @param key The key that is used to retrieve the Data from the HashMap.
  74. * @[member=Return] The data attached to the specified key, or null if the key is null. */
  75. private static Data getData(String key) {
  76. if (key == null) {
  77. return null;
  78. } else {
  79. return data.get(key);
  80. }
  81. }
  82.  
  83. //You can also make these methods "blocking methods". That means that they will not return a value
  84. //until a condition is met, hence blocking further execution. Instead of throwing an exception
  85. //(which might cause issues if the script logs out), you could instead wait for the name of the
  86. //player to not be null before returning a value.
  87. //Methods that you will be calling in your scripts:
  88. public static Point getPosition() {
  89. if (isDataLoaded()) {
  90. return getData().position;
  91. } else {
  92. throw new RuntimeException("Data has not been loaded");
  93. }
  94. }
  95. public static int getValue1() {
  96. if (isDataLoaded()) {
  97. return getData().value1;
  98. } else {
  99. throw new RuntimeException("Data has not been loaded");
  100. }
  101. }
  102. public static int getValue2() {
  103. if (isDataLoaded()) {
  104. return getData().value2;
  105. } else {
  106. throw new RuntimeException("Data has not been loaded");
  107. }
  108. }
  109. }
Add Comment
Please, Sign In to add comment