warc222

Hero24h By AbsolutePower v2

Sep 24th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P L2JHellasD
  3. Index: data/scripts/handlers/itemhandlers/Hero24h.java
  4. ===================================================================
  5. --- data/scripts/handlers/itemhandlers/Hero24h.java (revision 0)
  6. +++ data/scripts/handlers/itemhandlers/Hero24h.java (working copy)
  7. @@ -0,0 +1,269 @@
  8. +/*
  9. + * This program is free software: you can redistribute it and/or modify it under
  10. + * the terms of the GNU General Public License as published by the Free Software
  11. + * Foundation, either version 3 of the License, or (at your option) any later
  12. + * version.
  13. + *
  14. + * This program is distributed in the hope that it will be useful, but WITHOUT
  15. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. + * details.
  18. + *
  19. + * You should have received a copy of the GNU General Public License along with
  20. + * this program. If not, see <http://www.gnu.org/licenses/>.
  21. + */
  22. +package handlers.itemhandlers;
  23. +
  24. +import java.io.File;
  25. +import java.io.FileReader;
  26. +import java.io.FileWriter;
  27. +import java.io.IOException;
  28. +import java.io.LineNumberReader;
  29. +import java.util.StringTokenizer;
  30. +import java.util.concurrent.ScheduledFuture;
  31. +
  32. +import javolution.util.FastList;
  33. +
  34. +import com.PackRoot;
  35. +import com.l2jhellas.Config;
  36. +import com.l2jhellas.gameserver.ThreadPoolManager;
  37. +import com.l2jhellas.gameserver.handler.IItemHandler;
  38. +import com.l2jhellas.gameserver.model.L2ItemInstance;
  39. +import com.l2jhellas.gameserver.model.actor.L2Playable;
  40. +import com.l2jhellas.gameserver.model.actor.instance.L2PcInstance;
  41. +import com.l2jhellas.gameserver.model.entity.Hero;
  42. +
  43. +/**
  44. + * @author AbsolutePower
  45. + */
  46. +public class Hero24h implements IItemHandler
  47. +{
  48. + private static int[] _itemIds =
  49. + {
  50. + //custom item id here too
  51. + };
  52. +
  53. + //hero item id here(dont forget to put item id here :) )
  54. + private int HeroItemId =0;
  55. + private L2PcInstance _player = null;
  56. + private boolean is24Hero = false;
  57. + Hero _hero;
  58. +
  59. + public static FastList<String> HeroIds = new FastList<>();
  60. + static File file = new File(PackRoot.DATAPACK_ROOT, "data/HeroIds.txt");
  61. +
  62. +
  63. + @Override
  64. + public void useItem(L2Playable playable, L2ItemInstance item)
  65. + {
  66. + if (!(playable instanceof L2PcInstance))
  67. + return;
  68. +
  69. + final L2PcInstance activeChar = (L2PcInstance) playable;
  70. + _player = activeChar;
  71. + final int itemId = item.getItemId();
  72. +
  73. + if(isCatched(_player))
  74. + {
  75. + _player.sendMessage("You are already 24h hero!");
  76. + return;
  77. + }
  78. +
  79. + if(_player!=null && itemId == HeroItemId && _hero!=null)
  80. + {
  81. + MkTiNe();
  82. + _hero.activate24hHero(_player);
  83. + addHeroId( _player);
  84. + is24Hero = true;
  85. + HeroiDsLoad();
  86. + StartHeroChecker();
  87. +
  88. + }
  89. + }
  90. +
  91. + private ScheduledFuture<?> _HeroChecker;
  92. +
  93. + private class HeroChecker implements Runnable
  94. + {
  95. +
  96. + @Override
  97. + public void run()
  98. + {
  99. + if(_player != null)
  100. + {
  101. + if(is24Hero && _hero != null)
  102. + {
  103. + _hero.Deactivate24hHero(_player);
  104. + removeHeroId(_player);
  105. + stopHeroChecker();
  106. + }
  107. + }
  108. + else if(isCatched(_player))
  109. + {
  110. + _hero.Deactivate24hHero(_player);
  111. + removeHeroId(_player);
  112. + stopHeroChecker();
  113. + }
  114. + HeroiDsLoad();
  115. + }
  116. +
  117. + }
  118. + private void stopHeroChecker()
  119. + {
  120. + if(_HeroChecker!=null)
  121. + _HeroChecker.cancel(true);
  122. + _HeroChecker = null;
  123. + }
  124. +
  125. + private void StartHeroChecker()
  126. + {
  127. + _HeroChecker = ThreadPoolManager.getInstance().scheduleGeneral(new HeroChecker(), 60000* 1000);
  128. + }
  129. +
  130. + // make textfile if not exist
  131. + public static void MkTiNe()
  132. + {
  133. + try
  134. + {
  135. + File file = new File(PackRoot.DATAPACK_ROOT, "data/HeroIds.txt");
  136. + boolean exist = file.createNewFile();
  137. + if (!exist)
  138. + return;
  139. + }
  140. + catch (IOException e)
  141. + {
  142. + System.err.print("could not create new file:" + file);
  143. + if (Config.DEVELOPER)
  144. + e.printStackTrace();
  145. + }
  146. + }
  147. +
  148. + public void addHeroId(L2PcInstance p)
  149. + {
  150. + MkTiNe();
  151. + final int HeroId = getId(p);
  152. + final String name = p.getName();
  153. +
  154. + if (HeroId != 0 && name != null)
  155. + HeroIds.add("Name");
  156. + HeroIds.add(name);
  157. + HeroIds.add("HeroId");
  158. + HeroIds.add(String.valueOf(HeroId));
  159. + HeroIds.add("-");
  160. +
  161. + FileWriter s = null;
  162. + try
  163. + {
  164. + s = new FileWriter(file);
  165. + for (int i = 0; i < HeroIds.size(); i++)
  166. + {
  167. + s.write(HeroIds.get(i));
  168. + s.write("\r\n");
  169. + }
  170. + s.flush();
  171. + s.close();
  172. + s = null;
  173. + }
  174. + catch (IOException e)
  175. + {
  176. + System.err.print("could not add HeroiD in:" + file);
  177. + if (Config.DEVELOPER)
  178. + e.printStackTrace();
  179. + }
  180. + HeroiDsLoad();
  181. + }
  182. +
  183. + public void removeHeroId(L2PcInstance p)
  184. + {
  185. + final int HeroId = getId(p);
  186. + final String name = p.getName();
  187. +
  188. + if (HeroId != 0 && name != null)
  189. + HeroIds.remove("Name");
  190. + HeroIds.remove(name);
  191. + HeroIds.remove("HeroId");
  192. + HeroIds.remove(String.valueOf(HeroId));
  193. + HeroIds.remove("-");
  194. +
  195. + FileWriter s = null;
  196. + try
  197. + {
  198. + s = new FileWriter(file);
  199. + for (int i = 0; i < HeroIds.size(); i++)
  200. + {
  201. + s.write(HeroIds.get(i));
  202. + s.write("\r\n");
  203. + }
  204. + s.flush();
  205. + s.close();
  206. + s = null;
  207. + }
  208. + catch (IOException e)
  209. + {
  210. + System.err.print("could not remove heroid new file:" + file);
  211. + if (Config.DEVELOPER)
  212. + e.printStackTrace();
  213. + }
  214. + HeroiDsLoad();
  215. + }
  216. +
  217. + public static void HeroiDsLoad()
  218. + {
  219. + LineNumberReader l = null;
  220. + try
  221. + {
  222. + MkTiNe();
  223. + String line = null;
  224. + l = new LineNumberReader(new FileReader(file));
  225. + while ((line = l.readLine()) != null)
  226. + {
  227. + StringTokenizer st = new StringTokenizer(line, "\n\r");
  228. + if (st.hasMoreTokens())
  229. + {
  230. + String n = st.nextToken();
  231. + HeroIds.add(n);
  232. + }
  233. + }
  234. + }
  235. + catch (Exception e)
  236. + {
  237. + System.err.print("could not load heroIds from file:" + file);
  238. + if (Config.DEVELOPER)
  239. + e.printStackTrace();
  240. + }
  241. + finally
  242. + {
  243. + try
  244. + {
  245. + if (l != null)
  246. + l.close();
  247. + }
  248. + catch (Exception e)
  249. + {
  250. + }
  251. + }
  252. + }
  253. +
  254. + // return true if Hero include
  255. + public boolean isCatched(L2PcInstance p)
  256. + {
  257. + HeroiDsLoad();
  258. +
  259. + if (HeroIds.contains(getId(p)) || HeroIds.contains(p.getName()))
  260. + return true;
  261. +
  262. + return false;
  263. + }
  264. +
  265. + public int getId(L2PcInstance p)
  266. + {
  267. + return p.getObjectId();
  268. + }
  269. +
  270. +
  271. + @Override
  272. + public int[] getItemIds()
  273. + {
  274. + return _itemIds;
  275. + }
  276. +}
  277. \ No newline at end of file
  278. #P L2JHellasC
  279. Index: java/com/l2jhellas/gameserver/model/entity/Hero.java
  280. ===================================================================
  281. --- java/com/l2jhellas/gameserver/model/entity/Hero.java (revision 500)
  282. +++ java/com/l2jhellas/gameserver/model/entity/Hero.java (working copy)
  283. @@ -989,6 +989,38 @@
  284. updateHeroes(false);
  285. }
  286.  
  287. + public void activate24hHero(L2PcInstance player)
  288. + {
  289. + StatsSet hero = _heroes.get(player.getObjectId());
  290. + hero.set(ACTIVE, 1);
  291. +
  292. + _heroes.put(player.getObjectId(), hero);
  293. +
  294. + player.setHero(true);
  295. + player.sendSkillList();
  296. + player.broadcastPacket(new SocialAction(player.getObjectId(), 16));
  297. + player.broadcastUserInfo();
  298. +
  299. + _heroMessage.put(player.getObjectId(), "");
  300. +
  301. + updateHeroes(false);
  302. + }
  303. +
  304. + public void Deactivate24hHero(L2PcInstance player)
  305. + {
  306. + StatsSet hero = _heroes.get(player.getObjectId());
  307. + hero.set(ACTIVE, 0);
  308. +
  309. + _completeHeroes.remove(player.getObjectId(),hero);
  310. + _heroes.remove(player.getObjectId(),hero);
  311. + _heroMessage.remove(player.getObjectId(), "");
  312. + player.setHero(false);
  313. + player.sendSkillList();
  314. + player.broadcastUserInfo();
  315. +
  316. + updateHeroes(false);
  317. + }
  318. +
  319. private static class SingletonHolder
  320. {
  321. protected static final Hero _instance = new Hero();
Advertisement
Add Comment
Please, Sign In to add comment