Guest User

Untitled

a guest
Feb 3rd, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.customs;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.concurrent.ScheduledFuture;
  22.  
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.model.L2World;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26.  
  27. /**
  28. * @author Anarchy
  29. *
  30. */
  31. public class PvpProtection
  32. {
  33. private Map<KillStats, ScheduledFuture<?>> _killStats;
  34. protected Map<String, List<String>> _protections;
  35.  
  36. public static PvpProtection getInstance()
  37. {
  38. return SingletonHolder._instance;
  39. }
  40.  
  41. protected PvpProtection()
  42. {
  43. _killStats = new HashMap<>();
  44. _protections = new HashMap<>();
  45.  
  46. System.out.println("PvP protection loaded.");
  47. }
  48.  
  49. public void checkKill(String killer, String victim)
  50. {
  51. KillStats ks = null;
  52.  
  53. for (KillStats k : _killStats.keySet())
  54. {
  55. if (k.getKiller().equals(killer) && k.getVictim().equals(victim))
  56. {
  57. ks = k;
  58. break;
  59. }
  60. }
  61.  
  62. if (ks != null)
  63. {
  64. ks.addKill();
  65.  
  66. if (ks.getKills() >= 4)
  67. {
  68. addNewProtection(killer, victim, ks);
  69. }
  70. else
  71. {
  72. _killStats.get(ks).cancel(true);
  73. _killStats.remove(ks);
  74. final KillStats fks = ks;
  75. _killStats.put(ks, ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  76. {
  77. @Override
  78. public void run()
  79. {
  80. fks.removeKills();
  81. }
  82. }, 25*1000));
  83. }
  84. }
  85. else
  86. {
  87. final KillStats nks = new KillStats(killer, victim, 1);
  88.  
  89. _killStats.put(nks, ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  90. {
  91. @Override
  92. public void run()
  93. {
  94. nks.removeKills();
  95. }
  96. }, 25*1000));
  97. }
  98. }
  99.  
  100. public void addNewProtection(final String killer, final String victim, KillStats ks)
  101. {
  102. _killStats.remove(ks);
  103.  
  104. if (_protections.containsKey(victim))
  105. {
  106. _protections.get(victim).add(killer);
  107. }
  108. else
  109. {
  110. List<String> temp = new ArrayList<>();
  111. temp.add(killer);
  112. _protections.put(victim, temp);
  113. }
  114.  
  115. L2PcInstance p = L2World.getInstance().getPlayer(killer);
  116. if (p != null && p.isOnline())
  117. {
  118. p.sendMessage("Killing protection enabled. You may not be rewarded for killing "+victim+" for 30 minutes.");
  119. }
  120.  
  121. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  122. {
  123. @Override
  124. public void run()
  125. {
  126. _protections.get(victim).remove(killer);
  127. }
  128. }, 1000*60*30);
  129. }
  130.  
  131. public boolean protectionExists(String killer, String victim)
  132. {
  133. if (_protections.containsKey(victim))
  134. {
  135. if (_protections.get(victim).contains(killer))
  136. {
  137. return true;
  138. }
  139. }
  140.  
  141. return false;
  142. }
  143.  
  144. public class KillStats
  145. {
  146. private String killer,
  147. victim;
  148. private int kills;
  149.  
  150. public KillStats(String killer, String victim, int kills)
  151. {
  152. this.killer = killer;
  153. this.victim = victim;
  154. this.kills = kills;
  155. }
  156.  
  157. public void removeKills()
  158. {
  159. kills = 0;
  160. }
  161.  
  162. public void addKill()
  163. {
  164. kills++;
  165. }
  166.  
  167. public int getKills()
  168. {
  169. return kills;
  170. }
  171.  
  172. public String getKiller()
  173. {
  174. return killer;
  175. }
  176.  
  177. public String getVictim()
  178. {
  179. return victim;
  180. }
  181. }
  182.  
  183. private static class SingletonHolder
  184. {
  185. protected static final PvpProtection _instance = new PvpProtection();
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment