armark1ng

Untitled

Jun 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. package com.rs.game.map.bossInstance;
  2.  
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import com.rs.Settings;
  8. import com.rs.game.World;
  9. import com.rs.game.WorldTile;
  10. import com.rs.game.map.bossInstance.impl.VoragoInstance;
  11. import com.rs.game.player.Player;
  12. import com.rs.utils.Logger;
  13. import com.rs.utils.Utils;
  14.  
  15. public class BossInstanceHandler {
  16.  
  17. // to make sure no issues
  18. public static final Object LOCK = new Object();
  19.  
  20. public static enum Boss {
  21. Vorago(VoragoInstance.class, 1000000,
  22. 50, false, true, new WorldTile(2972, 3431, 0), new WorldTile(3043, 6100, 0), new WorldTile(3072, 6176,
  23. 0), "VoragoInstanceController", 1155)
  24.  
  25. ;
  26. private final Map<String, BossInstance> cachedInstances = Collections
  27. .synchronizedMap(new HashMap<String, BossInstance>());
  28. private Class<? extends BossInstance> instance;
  29. private int initialCost, maxPlayers, musicId;
  30. private boolean hasHM, publicVersion;
  31. private WorldTile insideTile, outsideTile, graveStoneTile;
  32. private String controllerName;
  33.  
  34. private Boss(Class<? extends BossInstance> instance, int initialCost, int maxPlayers, boolean hasHM,
  35. boolean publicVersion, WorldTile outsideTile, WorldTile insideTile, WorldTile graveStoneTile,
  36. String controllerName, int musicId) {
  37. this.instance = instance;
  38. this.initialCost = initialCost;
  39. this.maxPlayers = maxPlayers;
  40. this.hasHM = hasHM;
  41. this.publicVersion = publicVersion;
  42. this.insideTile = insideTile;
  43. this.outsideTile = outsideTile;
  44. this.graveStoneTile = graveStoneTile;
  45. this.controllerName = controllerName;
  46. this.musicId = musicId;
  47. }
  48.  
  49. public Map<String, BossInstance> getCachedInstances() {
  50. return cachedInstances;
  51. }
  52.  
  53. public String getControllerName() {
  54. return controllerName;
  55. }
  56.  
  57. public WorldTile getInsideTile() {
  58. return insideTile;
  59. }
  60.  
  61. public WorldTile getOutsideTile() {
  62. return outsideTile;
  63. }
  64.  
  65. public WorldTile getGraveStoneTile() {
  66. return graveStoneTile;
  67. }
  68.  
  69. public boolean isHasHM() {
  70. return hasHM;
  71. }
  72.  
  73. public int getMaxPlayers() {
  74. return maxPlayers;
  75. }
  76.  
  77. public int getInitialCost() {
  78. return initialCost;
  79. }
  80.  
  81. public int getMusicId() {
  82. return musicId;
  83. }
  84.  
  85. public boolean hasPublicVersion() {
  86. return publicVersion;
  87. }
  88.  
  89. }
  90.  
  91. public static void enterInstance(Player player, Boss boss) {
  92. player.getDialogueManager().startDialogue("BossInstanceD", boss);
  93. }
  94.  
  95. private static void createInstance(Player player, Boss boss, int maxPlayers, int minCombat, int spawnSpeed,
  96. int protection, boolean practiseMode, boolean hardMode) {
  97. createInstance(player, new InstanceSettings(boss, maxPlayers, minCombat, spawnSpeed, protection, practiseMode,
  98. hardMode));
  99. }
  100.  
  101. public static void createInstance(Player player, InstanceSettings settings) {
  102. synchronized (LOCK) {
  103.  
  104. try {
  105. String key = player == null ? "" : player.getUsername();
  106. BossInstance instance = findInstance(settings.getBoss(), key);
  107. if (instance == null) {
  108. if (player == null && !settings.getBoss().publicVersion) {
  109. if (Settings.DEBUG)
  110. Logger.log(BossInstanceHandler.class, "Not a public instance. Can't create it.");
  111. return;
  112. }
  113. instance = settings.getBoss().instance.getDeclaredConstructor(Player.class, InstanceSettings.class)
  114. .newInstance(player, settings);
  115. settings.getBoss().cachedInstances.put(key, instance);
  116. } else {// recreating the instance but not gonna replace
  117. // settings since already exists(instead, increase time)
  118. settings.setCreationTime(Utils.currentTimeMillis());
  119. joinInstance(player, settings.getBoss(), key, false); // enter
  120. // the
  121. // instance
  122. // normally
  123. }
  124. } catch (Throwable e) {
  125. Logger.handle(e);
  126. }
  127.  
  128. }
  129. }
  130.  
  131. /*
  132. * login means reloging in a public instance(u cant login into private, but
  133. * lets keep this in case rs lets u in future)
  134. */
  135. public static BossInstance joinInstance(Player player, Boss boss, String key, boolean login) {
  136. synchronized (LOCK) {
  137.  
  138. BossInstance instance = findInstance(boss, key);
  139. if (instance == null) { // not username
  140. Player owner = World.getPlayerByDisplayName(key);
  141. if (owner != null) {
  142. key = owner.getUsername();
  143. instance = findInstance(boss, key);
  144. }
  145. }
  146.  
  147. if (instance == null) {
  148. if (key.equals("")) { // supposed to be public instance
  149. player.getPackets().sendGameMessage("This boss has no public instance.");
  150. return null;
  151. }
  152. player.getPackets().sendGameMessage("That player is offline, or has privacy mode enabled.");
  153. return null;
  154. }
  155. // loading
  156. if (!instance.isInstanceReady())
  157. return null;
  158. if (!key.equals("") && !player.getUsername().equals(key)) {
  159. if (instance.getSettings().getMinCombat() > player.getSkills().getCombatLevelWithSummoning()) {
  160. player.getPackets().sendGameMessage("Your combat level is too low to enter this session.");
  161. return null;
  162. }
  163. if (instance.getSettings().getProtection() == BossInstance.FRIENDS_ONLY) {
  164. Player owner = World.getPlayer(key);
  165. if (owner == null || !owner.getFriendsIgnores().isFriend(player.getDisplayName())) {
  166. player.getPackets().sendGameMessage("That player is offline, or has privacy mode enabled.");
  167. return null;
  168. }
  169. }
  170. if (instance.getSettings().getMaxPlayers() - 1 <= instance.getPlayersCount()) {
  171. player.getPackets().sendGameMessage("This instance is full.");
  172. return null;
  173. }
  174. }
  175. instance.enterInstance(player, login);
  176. return instance;
  177. }
  178. }
  179.  
  180. public static BossInstance findInstance(Boss boss, String key) {
  181. synchronized (LOCK) {
  182. return boss.cachedInstances.get(key);
  183. }
  184. }
  185.  
  186. public static final void init() {
  187. for (Boss boss : Boss.values()) {
  188. if (!boss.publicVersion)
  189. continue;
  190. try {
  191. createInstance(null, boss, boss.maxPlayers, 1, BossInstance.STANDARD, BossInstance.FFA, false, false);
  192. } catch (Throwable e) {
  193. Logger.handle(e);
  194. }
  195. }
  196. }
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment