Guest User

Untitled

a guest
Jun 22nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. package org.rscdaemon.server.model;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.ConcurrentModificationException;
  6.  
  7. import org.rscdaemon.server.entityhandling.EntityHandler;
  8.  
  9. import bsh.EvalError;
  10.  
  11. public class Script {
  12.  
  13. public Player player;
  14. public Npc npc;
  15.  
  16. /**
  17. * DONT TOUCH THIS. Sets the Player and Npc instances
  18. * for reach when scripting.
  19. * @param p - the player
  20. * @param n - the affected npc
  21. */
  22. public Script(Player p, Npc n) {
  23. player = p;
  24. npc = n;
  25. npc.setSprite(getDirection(player, npc));
  26. player.setSprite(getDirection(npc, player));
  27.  
  28. try {
  29. player.interpreter.getNameSpace().importObject(this);
  30. player.interpreter.source(World.getWorld().npcScripts.get(npc.getID()));
  31. player.interpreter.getNameSpace().clear();
  32. } catch (EvalError e) {
  33. error();
  34. e.printStackTrace();
  35. } catch (FileNotFoundException e) {
  36. error();
  37. e.printStackTrace();
  38. } catch (IOException e) {
  39. error();
  40. e.printStackTrace();
  41. } catch(ConcurrentModificationException cme) {
  42. System.out.println("got cme");
  43. } catch(Exception e) {
  44. error();
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49. /**
  50. * Unblock the NPC/Player if something unhandled happens.
  51. */
  52. private void error() {
  53. npc.unblock();
  54. player.setBusy(false);
  55. }
  56.  
  57. /**
  58. * Faces you and the NPC towards each other.
  59. * @param you
  60. * @param them
  61. * @return the direction number
  62. */
  63. private int getDirection(Mob you, Mob them) {
  64. if (you.getX() == them.getX() + 1 && you.getY() == them.getY() + 1) // bottom// left
  65. return 3;
  66. else if (you.getX() == them.getX() + 1 && you.getY() == them.getY() - 1) // top// left
  67. return 1;
  68. else if (you.getX() == them.getX() - 1 && you.getY() == them.getY() - 1) // right// up
  69. return 7;
  70. else if (you.getX() == them.getX() - 1 && you.getY() == them.getY() + 1) // right/down
  71. return 5;
  72. else if (you.getX() == them.getX() - 1) // face right
  73. return 6;
  74. else if (you.getX() == them.getX() + 1) // face left
  75. return 2;
  76. else if (you.getY() == them.getY() + 1) // face down
  77. return 4;
  78. else if (you.getY() == them.getY() - 1) // face up
  79. return 0;
  80. return -1;
  81. }
  82.  
  83. /**
  84. * Sends a normal Message to the player
  85. * @param msg - the message
  86. */
  87. public void SendMessage(String msg) {
  88. player.getActionSender().sendMessage(msg);
  89. }
  90.  
  91. /**
  92. * Sends a large black box Message to the player
  93. * @param msg - the message
  94. */
  95. public void SendBoxMessage(String msg) {
  96. player.getActionSender().sendAlert(msg, true);
  97. }
  98.  
  99. /**
  100. * Sends a medium or large black box message
  101. * @param msg - the message
  102. * @param big - true if large box, false if medium
  103. */
  104. public void SendBoxMessage(String msg, boolean big) {
  105. player.getActionSender().sendAlert(msg, big);
  106. }
  107.  
  108. /**
  109. * Add item(s) to the players inventory
  110. * (This is setup to handle the amount even
  111. * when the item is non stackable)
  112. * @param id - the item's ID
  113. * @param amount - the amount given
  114. */
  115. public void AddItem(int id, int amount) {
  116. InvItem item = new InvItem(id, amount);
  117. if(item.getDef().stackable) {
  118. player.getInventory().add(new InvItem(id, amount));
  119. } else {
  120. for(int i=0; i < amount; i++) {
  121. player.getInventory().add(new InvItem(id));
  122. }
  123. }
  124. player.getActionSender().sendInventory();
  125. }
  126.  
  127. /**
  128. * Says something to the NPC your talking too
  129. * @param msg - the message
  130. */
  131. public void PlayerTalk(String msg) {
  132. player.informOfChatMessage(new ChatMessage(player, msg, npc));
  133. Wait(2000);
  134.  
  135. }
  136.  
  137. /**
  138. * the Npc your interacting with says something to you
  139. * @param msg - the message
  140. */
  141. public void NpcTalk(String msg) {
  142. player.informOfNpcMessage(new ChatMessage(npc, msg, player));
  143. Wait(2000);
  144. }
  145.  
  146. /**
  147. * Sleeps for the default delay (2000)
  148. */
  149. public void Wait() {
  150. Wait(2000);
  151. }
  152.  
  153. /**
  154. * Wait's the delay until the next line is executed
  155. */
  156. public void Wait(int ms) {
  157. try {
  158. Thread.currentThread().sleep(ms);
  159. } catch (InterruptedException e) {
  160. SendMessage(e.getMessage());
  161. }
  162. }
  163.  
  164. /**
  165. * AUTO EXPANDING arrays (String...) with BeanShell do not yet work.
  166. * I know this looks dodgey, but easy-scripting support comes first.
  167. * Currently set for 7 options max.
  168. */
  169. public int PickOption(String s1, String s2, String s3, String s4, String s5, String s6, String s7) {
  170. return PickOption(new String[]{s1, s2, s3, s4, s5, s6, s7});
  171. }
  172. public int PickOption(String s1, String s2, String s3, String s4, String s5, String s6) {
  173. return PickOption(new String[]{s1, s2, s3, s4, s5, s6});
  174. }
  175. public int PickOption(String s1, String s2, String s3, String s4, String s5) {
  176. return PickOption(new String[]{s1, s2, s3, s4, s5});
  177. }
  178. public int PickOption(String s1, String s2, String s3, String s4) {
  179. return PickOption(new String[]{s1, s2, s3, s4});
  180. }
  181. public int PickOption(String s1, String s2, String s3) {
  182. return PickOption(new String[]{s1, s2, s3});
  183. }
  184. public int PickOption(String s1, String s2) {
  185. return PickOption(new String[]{s1, s2});
  186. }
  187. public int PickOption(String s1) {
  188. return PickOption(new String[]{s1});
  189. }
  190.  
  191. /**
  192. * Sends a question menu, waits for the response.
  193. * @param strs - array of options
  194. * @return the option number
  195. */
  196. public int PickOption(String[] strs) {
  197. try {
  198. long time = System.currentTimeMillis();
  199. player.setBusy(false);
  200. player.lastOption = -2;
  201. player.setMenuHandler(new MenuHandler(strs) {
  202. public void handleReply(int option, String reply) {
  203. if(option < 0 || option >= getOptions().length) {
  204. npc.unblock();
  205. player.setBusy(false);
  206. owner.lastOption = -1;
  207. return;
  208. }
  209. owner.lastOption = option;
  210. }
  211. });
  212. player.getActionSender().sendMenu(strs);
  213. while(player.lastOption == -2 && System.currentTimeMillis() - time < 20000) {
  214. Wait(25);
  215. }
  216. if(player.lastOption == -1 || player.lastOption == -2) {
  217. player.setBusy(false);
  218. npc.unblock();
  219. return -1;
  220. }
  221. player.setBusy(true);
  222. int newOpt = player.lastOption;
  223. player.lastOption = -2;
  224. PlayerTalk(strs[newOpt]);
  225. return newOpt + 1;
  226. } catch(Exception e) {
  227.  
  228. e.printStackTrace();
  229. return -1;
  230. }
  231. }
  232.  
  233. /**
  234. * Teleports you from x to y (without a bubble)
  235. * @param x - new x axis
  236. * @param y - new y axis
  237. */
  238. public void Teleport(int x, int y) {
  239. player.teleport(x, y, false);
  240. }
  241.  
  242. /**
  243. * Removes all the items from inventory that you specify
  244. * @param id - the item id to remove
  245. */
  246. public void RemoveAllItem(int id) {
  247. RemoveItem(id, player.getInventory().countId(id));
  248. }
  249.  
  250. /**
  251. * Checks inventory for items/amounts.
  252. * @param id - the items id
  253. * @param amount - the amount
  254. * @return true if it has the items.
  255. */
  256. public boolean HasItem(int id, int amount) {
  257. if(EntityHandler.getItemDef(id).stackable) {
  258. for(InvItem i : player.getInventory().getItems()) {
  259. if(i.getID() == id && i.getAmount() >= amount)
  260. return true;
  261. }
  262. } else {
  263. int count = 0;
  264. for(InvItem i : player.getInventory().getItems()) {
  265. if(i.getID() == id)
  266. count++;
  267. }
  268. if(count >= amount)
  269. return true;
  270. }
  271. return false;
  272. }
  273.  
  274. /**
  275. * Removes item(s) from your inventory
  276. * @param id - the item id
  277. * @param amount - the amount
  278. */
  279. public void RemoveItem(int id, int amount) {
  280. if(EntityHandler.getItemDef(id).stackable) {
  281. player.getInventory().remove(id, amount);
  282. } else {
  283. for(int i=0; i < amount; i++)
  284. player.getInventory().remove(id, 1);
  285. }
  286. player.getActionSender().sendInventory();
  287. }
  288.  
  289. /**
  290. * Adds experience to the selected Stat.
  291. * @param stat - the stat number
  292. * @param amount - the amount
  293. *
  294. */
  295. public void AddExp(int stat, int amount) {
  296. player.incExp(stat, amount, false, true);
  297. }
  298.  
  299. /**
  300. * Gets the free space in your inventory
  301. * @return - the free slots
  302. */
  303. public int GetInventoryFreeSpace() {
  304. return 30 - player.getInventory().size();
  305. }
  306.  
  307. /**
  308. * Gets the max level of a stat
  309. * @param stat
  310. * @return
  311. */
  312. public int GetStatMaxLevel(int stat) {
  313. return player.getMaxStat(stat);
  314. }
  315.  
  316. /**
  317. * Gets the current level of a stat
  318. * @param stat
  319. * @return
  320. */
  321. public int GetStatCurLevel(int stat) {
  322. return player.getCurStat(stat);
  323. }
  324.  
  325. /**
  326. * Adds an item to your bank
  327. * @param id - the item id
  328. * @param amount - the amount
  329. */
  330. public void AddBankItem(int id, int amount) {
  331. player.getBank().add(new InvItem(id, amount));
  332. }
  333.  
  334. public int CountItem(int id) {
  335. return player.getInventory().countId(id);
  336. }
  337.  
  338. public static final int ATTACK = 0;
  339. public static final int DEFENSE = 1;
  340. public static final int STRENGTH = 2;
  341. public static final int HITS = 3;
  342. public static final int RANGED = 4;
  343. public static final int PRAYER = 5;
  344. public static final int MAGIC = 6;
  345. public static final int COOKING = 7;
  346. public static final int WOODCUT = 8;
  347. public static final int FLETCHING = 9;
  348. public static final int FISHING = 10;
  349. public static final int FIREMAKING = 11;
  350. public static final int CRAFTING = 12;
  351. public static final int SMITHING = 13;
  352. public static final int MINING = 14;
  353. public static final int HERBLAW = 15;
  354. public static final int AGILITY = 16;
  355. public static final int THIEVING = 17;
  356. }
Add Comment
Please, Sign In to add comment