Advertisement
Guest User

Pvp Protection Updated

a guest
Jan 24th, 2017
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 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.l2spike;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21.  
  22. import net.sf.l2j.gameserver.ThreadPoolManager;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24.  
  25. /**
  26. * @author Anarchy
  27. *
  28. */
  29. public class PvpProtection
  30. {
  31. private Map<KillStats, Long> killStats = null;
  32. private Map<Integer, List<Integer>> protections = null;
  33.  
  34. protected PvpProtection()
  35. {
  36. killStats = new HashMap<>();
  37. protections = new HashMap<>();
  38. }
  39.  
  40. public void checkKill(L2PcInstance killer, L2PcInstance victim)
  41. {
  42. if (killer.isGM())
  43. return;
  44.  
  45. KillStats ks = null;
  46. for (KillStats k : killStats.keySet())
  47. {
  48. if (k.getKiller() == killer.getObjectId() && k.getVictim() == victim.getObjectId())
  49. {
  50. ks = k;
  51. break;
  52. }
  53. }
  54.  
  55. if (ks != null)
  56. {
  57. if (System.currentTimeMillis() - killStats.get(ks) < 10*1000)
  58. {
  59. ks.addKill();
  60. killStats.put(ks, System.currentTimeMillis());
  61. }
  62. else
  63. {
  64. ks.removeKills();
  65. ks.addKill();
  66. }
  67.  
  68. if (ks.getKills() >= 4)
  69. addNewProtection(killer, victim, ks);
  70. }
  71. else
  72. {
  73. final KillStats nfks = new KillStats(killer.getObjectId(), victim.getObjectId(), 1);
  74. killStats.put(nfks, System.currentTimeMillis());
  75. }
  76. }
  77.  
  78. public void addNewProtection(L2PcInstance killer, L2PcInstance victim, KillStats ks)
  79. {
  80. killStats.remove(ks);
  81.  
  82. if (protections.containsKey(victim))
  83. protections.get(victim).add(killer.getObjectId());
  84. else
  85. {
  86. List<Integer> temp = new ArrayList<>();
  87. temp.add(killer.getObjectId());
  88. protections.put(victim.getObjectId(), temp);
  89. }
  90.  
  91. killer.sendMessage("Killing protection enabled. You may not be rewarded for killing "+victim+" for 15 minutes.");
  92. ThreadPoolManager.getInstance().scheduleGeneral(() -> protections.get(victim.getObjectId()).remove(killer.getObjectId()), 1000*60*15);
  93. }
  94.  
  95. public boolean protectionExists(L2PcInstance killer, L2PcInstance victim)
  96. {
  97. if (!killer.isGM() && ((killer.getClan() != null && killer.getClan() == victim.getClan()) || (killer.getClan() != null && victim.getClan() != null && killer.getClan().getAllyName() != "" && killer.getClan().getAllyName() != null && killer.getClan().getAllyName().equals(victim.getClan().getAllyName())) || (killer.getClient().getConnection().getInetAddress().getHostAddress().equals(victim.getClient().getConnection().getInetAddress().getHostAddress()))))
  98. return true;
  99.  
  100. if (protections.containsKey(victim.getObjectId()))
  101. if (protections.get(victim.getObjectId()).contains(killer.getObjectId()))
  102. return true;
  103.  
  104. return false;
  105. }
  106.  
  107. public static PvpProtection getInstance()
  108. {
  109. return SingletonHolder.instance;
  110. }
  111.  
  112. private static class SingletonHolder
  113. {
  114. protected static final PvpProtection instance = new PvpProtection();
  115. }
  116.  
  117. private class KillStats
  118. {
  119. private int killer;
  120. private int victim;
  121. private int kills;
  122.  
  123. public KillStats(int killer, int victim, int kills)
  124. {
  125. this.killer = killer;
  126. this.victim = victim;
  127. this.kills = kills;
  128. }
  129.  
  130. public void removeKills()
  131. {
  132. kills = 0;
  133. }
  134.  
  135. public void addKill()
  136. {
  137. kills++;
  138. }
  139.  
  140. public int getKills()
  141. {
  142. return kills;
  143. }
  144.  
  145. public int getKiller()
  146. {
  147. return killer;
  148. }
  149.  
  150. public int getVictim()
  151. {
  152. return victim;
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement