Advertisement
Code_ex

Random Event

Jul 27th, 2011
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.57 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P TEST_GAMESERVER
  3. Index: java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java
  4. ===================================================================
  5. --- java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java   (revision 4699)
  6. +++ java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java   (working copy)
  7. @@ -452,6 +452,11 @@
  8.  
  9.     private boolean _noble = false;
  10.     private boolean _hero = false;
  11. +   private boolean _dice = false;
  12. +  
  13. +  
  14. +  
  15. +   private int nums;
  16.  
  17.     /** The L2FolkInstance corresponding to the last Folk wich one the player talked. */
  18.     private L2FolkInstance _lastFolkNpc = null;
  19. @@ -8215,6 +8220,22 @@
  20.         return true;
  21.     }
  22.  
  23. +   public boolean isDice()
  24. +   {
  25. +       return _dice;
  26. +   }
  27. +  
  28. +   public void setDice(boolean y){
  29. +       _dice = y;
  30. +   }
  31. +  
  32. +   public int getNum(){
  33. +       return nums;
  34. +   }
  35. +  
  36. +   public void setNum(int n){
  37. +       nums = n;
  38. +   }
  39.      public boolean isNoble()
  40.      {
  41.         return _noble;
  42. Index: java/net/sf/l2j/gameserver/handler/voicedcommandhandlers/Random.java
  43. ===================================================================
  44. --- java/net/sf/l2j/gameserver/handler/voicedcommandhandlers/Random.java    (revision 0)
  45. +++ java/net/sf/l2j/gameserver/handler/voicedcommandhandlers/Random.java    (revision 0)
  46. @@ -0,0 +1,59 @@
  47. +/* This program is free software; you can redistribute it and/or modify
  48. + * it under the terms of the GNU General Public License as published by
  49. + * the Free Software Foundation; either version 2, or (at your option)
  50. + * any later version.
  51. + *
  52. + * This program is distributed in the hope that it will be useful,
  53. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  55. + * GNU General Public License for more details.
  56. + *
  57. + * You should have received a copy of the GNU General Public License
  58. + * along with this program; if not, write to the Free Software
  59. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  60. + * 02111-1307, USA.
  61. + *
  62. + * http://www.gnu.org/copyleft/gpl.html
  63. + */
  64. +package net.sf.l2j.gameserver.handler.voicedcommandhandlers;
  65. +
  66. +import java.util.StringTokenizer;
  67. +
  68. +import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
  69. +import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  70. +
  71. +/**
  72. + *
  73. + * @author  Codex
  74. + */
  75. +public class Random implements IVoicedCommandHandler
  76. +{
  77. +    private static final String[] VOICED_COMMANDS = { "dice"};
  78. +    
  79. +   public boolean useVoicedCommand(String command, L2PcInstance activeChar,String target){
  80. +       if(command.startsWith("dice")){
  81. +           String val = command.substring(4);
  82. +           StringTokenizer st = new StringTokenizer(val);
  83. +           int num = Integer.parseInt(st.nextToken());
  84. +          
  85. +           if(!activeChar.isDice())
  86. +           {
  87. +               activeChar.sendMessage("You can't press this command now,you are out of the event");
  88. +               return false;
  89. +           }
  90. +          
  91. +           if(num >= 1 && num <= 8)
  92. +           {
  93. +               activeChar.sendMessage("You successfully voted for this round!");
  94. +               activeChar.setNum(num);
  95. +           }
  96. +       }
  97. +       return true;
  98. +   }
  99. +
  100. +
  101. +   public String[] getVoicedCommandList(){
  102. +        return VOICED_COMMANDS;
  103. +   }
  104. +
  105. +}
  106. Index: java/net/sf/l2j/gameserver/model/entity/RandomEvent.java
  107. ===================================================================
  108. --- java/net/sf/l2j/gameserver/model/entity/RandomEvent.java    (revision 0)
  109. +++ java/net/sf/l2j/gameserver/model/entity/RandomEvent.java    (revision 0)
  110. @@ -0,0 +1,229 @@
  111. +/* This program is free software; you can redistribute it and/or modify
  112. + * it under the terms of the GNU General Public License as published by
  113. + * the Free Software Foundation; either version 2, or (at your option)
  114. + * any later version.
  115. + *
  116. + * This program is distributed in the hope that it will be useful,
  117. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  118. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  119. + * GNU General Public License for more details.
  120. + *
  121. + * You should have received a copy of the GNU General Public License
  122. + * along with this program; if not, write to the Free Software
  123. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  124. + * 02111-1307, USA.
  125. + *
  126. + * http://www.gnu.org/copyleft/gpl.html
  127. + */
  128. +package net.sf.l2j.gameserver.model.entity;
  129. +
  130. +import java.util.Random;
  131. +
  132. +import net.sf.l2j.gameserver.Announcements;
  133. +import net.sf.l2j.gameserver.ThreadPoolManager;
  134. +import net.sf.l2j.gameserver.model.L2World;
  135. +import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  136. +
  137. +import javolution.util.FastList;
  138. +
  139. +/**
  140. + *
  141. + * @author  Codex
  142. + */
  143. +public class RandomEvent
  144. +{
  145. +   Random dice = new Random();
  146. +   FastList <Integer> _numbers = new FastList <Integer>();
  147. +   static FastList <L2PcInstance> _players = new FastList <L2PcInstance>();
  148. +   public static boolean canRegist = false;
  149. +   int number;
  150. +   boolean running = false;
  151. +  
  152. +    class ReTask implements Runnable {
  153. +       public void run()
  154. +       {
  155. +           if(running) return;
  156. +          
  157. +           handle();
  158. +       }
  159. +      
  160. +    }
  161. +    
  162. +    void handle(){
  163. +       running = true;
  164. +       setCanRegist(true);
  165. +       globalMessage("Random Event Registering Started, you have 15 minutes go register..");
  166. +       waitMinutes(10);
  167. +       globalMessage("Random Event Registering Finishing in 5 minutes...go and register fast");
  168. +       waitMinutes(5);
  169. +       globalMessage("Registrations Finished, event starts in 30 seconds");
  170. +       setCanRegist(false);
  171. +       generateNums();
  172. +       waitSeconds(30);
  173. +       takeStatusVote();
  174. +       while(_players.size() > 1){
  175. +       globalMessage("You can now press .dice 1/2/3/4/5/6/7/8 , you have 20 seconds,Hurry up!");
  176. +       waitSeconds(20);
  177. +       checkAfkers();
  178. +       announceNumber();
  179. +       globalMessage("The Global Random Number is: "+announceNumber());
  180. +       checkNumbers();
  181. +       }
  182. +       rewarding();
  183. +       running = false;
  184. +      
  185. +    }
  186. +    
  187. +    void generateNums() {
  188. +       int counter = 1;
  189. +       while( counter < 9){
  190. +           number = 1 + dice.nextInt(8);
  191. +           _numbers.add(number);
  192. +           counter ++ ;
  193. +       }
  194. +    }
  195. +    
  196. +    void clear(){
  197. +       _numbers.clear();
  198. +       _players.clear();
  199. +       for(L2PcInstance x: L2World.getInstance().getAllPlayers())
  200. +           x.setNum(0);
  201. +    }
  202. +    public static void register(L2PcInstance p){
  203. +       setCanRegist(true);
  204. +      
  205. +       if(!canRegist){
  206. +           p.sendMessage("Event is closed now.");
  207. +           return;
  208. +       }
  209. +       if(_players.contains(p)){
  210. +           p.sendMessage("You can't register because you are already in the list");
  211. +           return;
  212. +       }
  213. +      
  214. +       if(p.isOlympiadStart() || p.isInOlympiadMode()){
  215. +           p.sendMessage("You can't register during olympiad match");
  216. +           return;
  217. +       }
  218. +      
  219. +       _players.add(p);
  220. +       p.sendMessage("You have successfully registed in the random event");
  221. +    }
  222. +    
  223. +    public static void unRegister(L2PcInstance p){
  224. +       if(!_players.contains(p)){
  225. +           p.sendMessage("You can't unregister because you are not in the list");
  226. +           return;
  227. +       }
  228. +      
  229. +       if(!canRegist){
  230. +           p.sendMessage("Event is closed now.");
  231. +           return;
  232. +       }
  233. +      
  234. +       _players.remove(p);
  235. +       p.sendMessage("You have successfully unregisted from the random event");
  236. +    }
  237. +    
  238. +    public void checkEmpty(){
  239. +       if(_players.isEmpty() || _players.size() == 1){
  240. +           globalMessage("Event Canceled during of no participations.");
  241. +       }
  242. +    }
  243. +    
  244. +    public void waitSeconds(int i){
  245. +       try{
  246. +           Thread.sleep(i * 1000);
  247. +       }
  248. +       catch (Exception e ){
  249. +           e.printStackTrace();
  250. +       }
  251. +    }
  252. +    
  253. +    public void waitMinutes(int i){
  254. +       try{
  255. +           Thread.sleep(i * 60000);
  256. +       }
  257. +       catch (Exception e ){
  258. +           e.printStackTrace();
  259. +       }
  260. +    }
  261. +    
  262. +    public void takeStatusVote(){
  263. +       for(L2PcInstance p : _players){
  264. +           p.sendMessage("You can now press .dice 1/2/3/4/5/6/7/8!");
  265. +           p.setDice(true);
  266. +       }
  267. +    }
  268. +    
  269. +    public void checkAfkers(){
  270. +       for(L2PcInstance p: _players){
  271. +           if(p.getNum() == 0){
  272. +               _players.remove(p);
  273. +           }
  274. +       }
  275. +    }
  276. +    
  277. +    public void globalMessage(String s ){
  278. +       Announcements.getInstance().announceToAll(s);
  279. +    }
  280. +    
  281. +    public int announceNumber()  {
  282. +       return _numbers.get(1+dice.nextInt(8));
  283. +    }
  284. +    
  285. +    public void checkNumbers(){
  286. +       for(L2PcInstance p : _players){
  287. +          if(p.getNum() != announceNumber()){
  288. +              p.sendMessage("We are sorry, you lost...you are out of the event now");
  289. +              _players.remove(p);
  290. +          }
  291. +          else {
  292. +              p.sendMessage("wOw!!You continue the event now...");
  293. +          }
  294. +          if(_players.size() == 0){
  295. +              globalMessage("No-one answered correctly, round redo");
  296. +             for(L2PcInstance pl : L2World.getInstance().getAllPlayers()){
  297. +                 if(pl.getNum() > 0){
  298. +                     _players.add(pl);
  299. +                 }
  300. +             }
  301. +                
  302. +          }
  303. +       }
  304. +    }
  305. +    
  306. +    public void rewarding(){
  307. +       for(L2PcInstance p: _players){
  308. +           if(_players.contains(p) && _players.size() == 1){
  309. +               p.sendMessage("You won the event!!!!!!Now you are rewarding!!");
  310. +               globalMessage(p.getName()+" won the  Random Event");
  311. +               p.getInventory().addItem("Winner", 3481, 25, p, null);
  312. +               p.broadcastUserInfo();
  313. +           }
  314. +       }
  315. +    }
  316. +    
  317. +    
  318. +    
  319. +    public static void setCanRegist(boolean x)
  320. +    {
  321. +       canRegist = x;
  322. +    }
  323. +    
  324. +    private RandomEvent()
  325. +    {
  326. +    ThreadPoolManager.getInstance().scheduleGeneral(new ReTask(), 10800000);
  327. +   }
  328. +    
  329. +    public static RandomEvent getInstance()
  330. +           {
  331. +                   return SingletonHolder._instance;
  332. +           }
  333. +          
  334. +           private static class SingletonHolder
  335. +           {
  336. +                   @SuppressWarnings("synthetic-access")
  337. +                   protected static final RandomEvent _instance = new RandomEvent();
  338. +          }
  339. +}
  340. Index: java/net/sf/l2j/gameserver/model/actor/instance/L2RandomInstance.java
  341. ===================================================================
  342. --- java/net/sf/l2j/gameserver/model/actor/instance/L2RandomInstance.java   (revision 0)
  343. +++ java/net/sf/l2j/gameserver/model/actor/instance/L2RandomInstance.java   (revision 0)
  344. @@ -0,0 +1,44 @@
  345. +/* This program is free software; you can redistribute it and/or modify
  346. + * it under the terms of the GNU General Public License as published by
  347. + * the Free Software Foundation; either version 2, or (at your option)
  348. + * any later version.
  349. + *
  350. + * This program is distributed in the hope that it will be useful,
  351. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  352. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  353. + * GNU General Public License for more details.
  354. + *
  355. + * You should have received a copy of the GNU General Public License
  356. + * along with this program; if not, write to the Free Software
  357. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  358. + * 02111-1307, USA.
  359. + *
  360. + * http://www.gnu.org/copyleft/gpl.html
  361. + */
  362. +package net.sf.l2j.gameserver.model.actor.instance;
  363. +
  364. +import net.sf.l2j.gameserver.model.entity.RandomEvent;
  365. +import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  366. +
  367. +/**
  368. + *
  369. + * @author  Codex
  370. + */
  371. +public class L2RandomInstance extends L2NpcInstance
  372. +{
  373. +
  374. +   public L2RandomInstance(int objectId, L2NpcTemplate template)
  375. +   {
  376. +       super(objectId, template);
  377. +   }
  378. +  
  379. +   public void onBypassFeedback(L2PcInstance player , String command ){
  380. +       if(command.startsWith("unregister")){
  381. +           RandomEvent.unRegister(player);
  382. +       }
  383. +       else if(command.startsWith("register")){
  384. +           RandomEvent.register(player);
  385. +       }
  386. +   }
  387. +
  388. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement