Advertisement
Guest User

PvP Protection

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