Guest User

Untitled

a guest
Apr 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. K GOML HERE IS OBJECTHANDLER.JAVA
  2.  
  3. WHERE SHALL I PUT THIS:
  4. -------------------------------------HERE IS THE CODE---------------------------------------------
  5.  
  6. ArrayList<Objects> toremove = new ArrayList<Objects>();
  7.  
  8. for (Objects s : globalObjects) {
  9. if (s.getObjectX() == o.getObjectX() && s.getObjectY() == o.getObjectY()) {
  10. toremove.add(s);
  11. }
  12. }
  13. for (Objects s : toremove) {
  14. if (globalObjects.contains(s)) {
  15. globalObjects.remove(s);
  16. }
  17. }
  18. globalObjects.add(o);
  19.  
  20. ------------------------------------HERE IS THE OBJECT HANDLER-------------------------------------------
  21.  
  22. package server.world;
  23.  
  24. import java.io.BufferedReader;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileReader;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30.  
  31. import server.Server;
  32. import server.model.objects.Objects;
  33. import server.model.players.Client;
  34. import server.model.players.Player;
  35. import server.model.players.*;
  36. import server.Config;
  37. import server.util.Misc;
  38.  
  39. /**
  40. * @author Sanity
  41. */
  42.  
  43. public class ObjectHandler {
  44.  
  45.  
  46.  
  47.  
  48. public List<Objects> globalObjects = new ArrayList<Objects>();
  49. public ObjectHandler() {
  50. loadGlobalObjects("./Data/cfg/global-objects.cfg");
  51. loadDoorConfig("./Data/cfg/doors.cfg");
  52. }
  53.  
  54.  
  55. /**
  56. * Adds object to list
  57. **/
  58. public void addObject(Objects object) {
  59. globalObjects.add(object);
  60. }
  61.  
  62. /**
  63. * Removes object from list
  64. **/
  65. public void removeObject(Objects object) {
  66. globalObjects.remove(object);
  67. }
  68.  
  69. /**
  70. * Does object exist
  71. **/
  72. public Objects objectExists(int objectX, int objectY, int objectHeight) {
  73. for(Objects o : globalObjects) {
  74. if(o.getObjectX() == objectX && o.getObjectY() == objectY && o.getObjectHeight() == objectHeight) {
  75. return o;
  76. }
  77. }
  78. return null;
  79. }
  80.  
  81. /**
  82. * Update objects when entering a new region or logging in
  83. **/
  84. public void updateObjects(Client c) {
  85. for(Objects o : globalObjects) {
  86. if(c != null) {
  87. if(c.heightLevel == o.getObjectHeight() && o.objectTicks == 0) {
  88. if (c.distanceToPoint(o.getObjectX(), o.getObjectY()) <= 60) {
  89. c.getPA().object(o.getObjectId(), o.getObjectX(), o.getObjectY(), o.getObjectFace(), o.getObjectType());
  90. }
  91. }
  92. }
  93. }
  94. if (c.distanceToPoint(2813, 3463) <= 60) {
  95. c.getFarming().updateHerbPatch();
  96. }
  97. if (c.distanceToPoint(2961, 3389) <= 60) {
  98. c.getPA().object(6552, 2961, 3389, -1, 10);
  99. }
  100. }
  101.  
  102.  
  103. /**
  104. * Creates the object for anyone who is within 60 squares of the object
  105. **/
  106. public void placeObject(Objects o) {
  107. for (Player p : Server.playerHandler.players){
  108. if(p != null) {
  109. Client person = (Client)p;
  110. if(person != null){
  111. if(person.heightLevel == o.getObjectHeight() && o.objectTicks == 0) {
  112. if (person.distanceToPoint(o.getObjectX(), o.getObjectY()) <= 60) {
  113. person.getPA().object(o.getObjectId(), o.getObjectX(), o.getObjectY(), o.getObjectFace(), o.getObjectType());
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120.  
  121. public void process() {
  122. for (int j = 0; j < globalObjects.size(); j++) {
  123. if (globalObjects.get(j) != null) {
  124. Objects o = globalObjects.get(j);
  125. if(o.objectTicks > 0) {
  126. o.objectTicks--;
  127. }
  128. if (o.objectTicks == 1) {
  129. Objects deleteObject = objectExists(o.getObjectX(), o.getObjectY(), o.getObjectHeight());
  130. removeObject(deleteObject);
  131. o.objectTicks = 0;
  132. placeObject(o);
  133. removeObject(o);
  134. if (isObelisk(o.objectId)) {
  135. int index = getObeliskIndex(o.objectId);
  136. if (activated[index]) {
  137. activated[index] = false;
  138. teleportObelisk(index);
  139. }
  140. }
  141. }
  142. }
  143.  
  144. }
  145. /*for(Objects o : globalObjects) {
  146. if(o.objectTicks > 0) {
  147. o.objectTicks--;
  148. }
  149. if(o.objectTicks == 1) {
  150. Objects deleteObject = objectExists(o.getObjectX(), o.getObjectY(), o.getObjectHeight());
  151. if(deleteObject != null) {
  152. removeObject(deleteObject);
  153. }
  154. o.objectTicks = 0;
  155. placeObject(o);
  156. removeObject(o);
  157. if (isObelisk(o.objectId)) {
  158. int index = getObeliskIndex(o.objectId);
  159. if (activated[index]) {
  160. activated[index] = false;
  161. teleportObelisk(index);
  162. }
  163. }
  164. break;
  165. }
  166. }*/
  167. }
  168.  
  169. public boolean loadGlobalObjects(String fileName) {
  170. String line = "";
  171. String token = "";
  172. String token2 = "";
  173. String token2_2 = "";
  174. String[] token3 = new String[10];
  175. boolean EndOfFile = false;
  176. int ReadMode = 0;
  177. BufferedReader objectFile = null;
  178. try {
  179. objectFile = new BufferedReader(new FileReader("./"+fileName));
  180. } catch(FileNotFoundException fileex) {
  181. Misc.println(fileName+": file not found.");
  182. return false;
  183. }
  184. try {
  185. line = objectFile.readLine();
  186. } catch(IOException ioexception) {
  187. Misc.println(fileName+": error loading file.");
  188. return false;
  189. }
  190. while(EndOfFile == false && line != null) {
  191. line = line.trim();
  192. int spot = line.indexOf("=");
  193. if (spot > -1) {
  194. token = line.substring(0, spot);
  195. token = token.trim();
  196. token2 = line.substring(spot + 1);
  197. token2 = token2.trim();
  198. token2_2 = token2.replaceAll("\t\t", "\t");
  199. token2_2 = token2_2.replaceAll("\t\t", "\t");
  200. token2_2 = token2_2.replaceAll("\t\t", "\t");
  201. token2_2 = token2_2.replaceAll("\t\t", "\t");
  202. token2_2 = token2_2.replaceAll("\t\t", "\t");
  203. token3 = token2_2.split("\t");
  204. if (token.equals("object")) {
  205. Objects object = new Objects(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 0);
  206. addObject(object);
  207. }
  208. } else {
  209. if (line.equals("[ENDOFOBJECTLIST]")) {
  210. try { objectFile.close(); } catch(IOException ioexception) { }
  211. return true;
  212. }
  213. }
  214. try {
  215. line = objectFile.readLine();
  216. } catch(IOException ioexception1) { EndOfFile = true; }
  217. }
  218. try { objectFile.close(); } catch(IOException ioexception) { }
  219. return false;
  220. }
  221.  
  222.  
  223. /**
  224. * Doors
  225. **/
  226.  
  227. public static final int MAX_DOORS = 100;
  228. public static int[][] doors = new int[MAX_DOORS][5];
  229. public static int doorFace = 0;
  230.  
  231. public void doorHandling(int doorId, int doorX, int doorY, int doorHeight) {
  232. for(int i = 0; i < doors.length; i++){
  233. if(doorX == doors[i][0] && doorY == doors[i][1] && doorHeight == doors[i][2]) {
  234. if(doors[i][4] == 0) {
  235. doorId++;
  236. } else {
  237. doorId--;
  238. }
  239. for (Player p : Server.playerHandler.players){
  240. if(p != null) {
  241. Client person = (Client)p;
  242. if(person != null){
  243. if(person.heightLevel == doorHeight) {
  244. if (person.distanceToPoint(doorX, doorY) <= 60) {
  245. person.getPA().object(-1, doors[i][0], doors[i][1], 0, 0);
  246. if(doors[i][3] == 0 && doors[i][4] == 1) {
  247. person.getPA().object(doorId, doors[i][0], doors[i][1]+1, -1, 0);
  248. } else if(doors[i][3] == -1 && doors[i][4] == 1) {
  249. person.getPA().object(doorId, doors[i][0]-1, doors[i][1], -2, 0);
  250. } else if(doors[i][3] == -2 && doors[i][4] == 1) {
  251. person.getPA().object(doorId, doors[i][0], doors[i][1]-1, -3, 0);
  252. } else if(doors[i][3] == -3 && doors[i][4] == 1) {
  253. person.getPA().object(doorId, doors[i][0]+1, doors[i][1], 0, 0);
  254. } else if(doors[i][3] == 0 && doors[i][4] == 0) {
  255. person.getPA().object(doorId, doors[i][0]-1, doors[i][1], -3, 0);
  256. } else if(doors[i][3] == -1 && doors[i][4] == 0) {
  257. person.getPA().object(doorId, doors[i][0], doors[i][1]-1, 0, 0);
  258. } else if(doors[i][3] == -2 && doors[i][4] == 0) {
  259. person.getPA().object(doorId, doors[i][0]+1, doors[i][1], -1, 0);
  260. } else if(doors[i][3] == -3 && doors[i][4] == 0) {
  261. person.getPA().object(doorId, doors[i][0], doors[i][1]+1, -2, 0);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271.  
  272. public boolean loadDoorConfig(String fileName) {
  273. String line = "";
  274. String token = "";
  275. String token2 = "";
  276. String token2_2 = "";
  277. String[] token3 = new String[10];
  278. boolean EndOfFile = false;
  279. int ReadMode = 0;
  280. BufferedReader objectFile = null;
  281. try {
  282. objectFile = new BufferedReader(new FileReader("./"+fileName));
  283. } catch(FileNotFoundException fileex) {
  284. Misc.println(fileName+": file not found.");
  285. return false;
  286. }
  287. try {
  288. line = objectFile.readLine();
  289. } catch(IOException ioexception) {
  290. Misc.println(fileName+": error loading file.");
  291. return false;
  292. }
  293. int door = 0;
  294. while(EndOfFile == false && line != null) {
  295. line = line.trim();
  296. int spot = line.indexOf("=");
  297. if (spot > -1) {
  298. token = line.substring(0, spot);
  299. token = token.trim();
  300. token2 = line.substring(spot + 1);
  301. token2 = token2.trim();
  302. token2_2 = token2.replaceAll("\t\t", "\t");
  303. token2_2 = token2_2.replaceAll("\t\t", "\t");
  304. token2_2 = token2_2.replaceAll("\t\t", "\t");
  305. token2_2 = token2_2.replaceAll("\t\t", "\t");
  306. token2_2 = token2_2.replaceAll("\t\t", "\t");
  307. token3 = token2_2.split("\t");
  308. if (token.equals("door")) {
  309. doors[door][0] = Integer.parseInt(token3[0]);
  310. doors[door][1] = Integer.parseInt(token3[1]);
  311. doors[door][2] = Integer.parseInt(token3[2]);
  312. doors[door][3] = Integer.parseInt(token3[3]);
  313. doors[door][4] = Integer.parseInt(token3[4]);
  314. door++;
  315. }
  316. } else {
  317. if (line.equals("[ENDOFDOORLIST]")) {
  318. try { objectFile.close(); } catch(IOException ioexception) { }
  319. return true;
  320. }
  321. }
  322. try {
  323. line = objectFile.readLine();
  324. } catch(IOException ioexception1) { EndOfFile = true; }
  325. }
  326. try { objectFile.close(); } catch(IOException ioexception) { }
  327. return false;
  328. }
  329.  
  330. public final int IN_USE_ID = 14825;
  331. public boolean isObelisk(int id) {
  332. for (int j = 0; j < obeliskIds.length; j++) {
  333. if (obeliskIds[j] == id)
  334. return true;
  335. }
  336. return false;
  337. }
  338. public int[] obeliskIds = {14829,14830,111235,14828,14826,14831};
  339. public int[][] obeliskCoords = {{3154,3618},{3225,3665},{3033,3730},{3104,3792},{2978,3864},{3305,3914}};
  340. public boolean[] activated = {false,false,false,false,false,false};
  341. public void startObelisk(int obeliskId) {
  342. int index = getObeliskIndex(obeliskId);
  343. if (index >= 0) {
  344. if (!activated[index]) {
  345. activated[index] = true;
  346. Objects obby1 = new Objects(14825, obeliskCoords[index][0], obeliskCoords[index][1], 0, -1, 10, 0);
  347. Objects obby2 = new Objects(14825, obeliskCoords[index][0] + 4, obeliskCoords[index][1], 0, -1, 10, 0);
  348. Objects obby3 = new Objects(14825, obeliskCoords[index][0], obeliskCoords[index][1] + 4, 0, -1, 10, 0);
  349. Objects obby4 = new Objects(14825, obeliskCoords[index][0] + 4, obeliskCoords[index][1] + 4, 0, -1, 10, 0);
  350. addObject(obby1);
  351. addObject(obby2);
  352. addObject(obby3);
  353. addObject(obby4);
  354. Server.objectHandler.placeObject(obby1);
  355. Server.objectHandler.placeObject(obby2);
  356. Server.objectHandler.placeObject(obby3);
  357. Server.objectHandler.placeObject(obby4);
  358. Objects obby5 = new Objects(obeliskIds[index], obeliskCoords[index][0], obeliskCoords[index][1], 0, -1, 10, 10);
  359. Objects obby6 = new Objects(obeliskIds[index], obeliskCoords[index][0] + 4, obeliskCoords[index][1], 0, -1, 10, 10);
  360. Objects obby7 = new Objects(obeliskIds[index], obeliskCoords[index][0], obeliskCoords[index][1] + 4, 0, -1, 10, 10);
  361. Objects obby8 = new Objects(obeliskIds[index], obeliskCoords[index][0] + 4, obeliskCoords[index][1] + 4, 0, -1, 10, 10);
  362. addObject(obby5);
  363. addObject(obby6);
  364. addObject(obby7);
  365. addObject(obby8);
  366. }
  367. }
  368. }
  369.  
  370. public int getObeliskIndex(int id) {
  371. for (int j = 0; j < obeliskIds.length; j++) {
  372. if (obeliskIds[j] == id)
  373. return j;
  374. }
  375. return -1;
  376. }
  377.  
  378. public void teleportObelisk(int port) {
  379. int random = Misc.random(5);
  380. while (random == port) {
  381. random = Misc.random(5);
  382. }
  383. for (int j = 0; j < Server.playerHandler.players.length; j++) {
  384. if (Server.playerHandler.players[j] != null) {
  385. Client c = (Client)Server.playerHandler.players[j];
  386. if (c.goodDistance(c.getX(), c.getY(), obeliskCoords[port][0] + 2, obeliskCoords[port][1] + 2, 1)) {
  387. c.getPA().startTeleport2(obeliskCoords[random][0] + 2, obeliskCoords[random][1] + 2, 0);
  388. }
  389. }
  390. }
  391. }
  392. }
Add Comment
Please, Sign In to add comment