Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.l2j.gameserver.templates;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import net.sf.l2j.commons.concurrent.ThreadPool;
- import net.sf.l2j.gameserver.model.actor.instance.Player;
- /**
- * @author Anarchy
- *
- */
- public class PvpProtection
- {
- private Map<KillStats, Long> killStats = null;
- private Map<Integer, List<Integer>> protections = null;
- protected PvpProtection()
- {
- killStats = new HashMap<>();
- protections = new HashMap<>();
- }
- public void checkKill(Player killer, Player victim)
- {
- if (killer.isGM())
- return;
- KillStats ks = null;
- for (KillStats k : killStats.keySet())
- {
- if (k.getKiller() == killer.getObjectId() && k.getVictim() == victim.getObjectId())
- {
- ks = k;
- break;
- }
- }
- if (ks != null)
- {
- if (System.currentTimeMillis() - killStats.get(ks) < 10*1000)
- {
- ks.addKill();
- killStats.put(ks, System.currentTimeMillis());
- }
- else
- {
- ks.removeKills();
- ks.addKill();
- }
- if (ks.getKills() >= 6)
- addNewProtection(killer, victim, ks);
- }
- else
- {
- final KillStats nfks = new KillStats(killer.getObjectId(), victim.getObjectId(), 1);
- killStats.put(nfks, System.currentTimeMillis());
- }
- }
- public void addNewProtection(Player killer, Player victim, KillStats ks)
- {
- killStats.remove(ks);
- if (protections.containsKey(victim))
- protections.get(victim).add(killer.getObjectId());
- else
- {
- List<Integer> temp = new ArrayList<>();
- temp.add(killer.getObjectId());
- protections.put(victim.getObjectId(), temp);
- }
- killer.sendMessage("Killing protection enabled. You may not be rewarded for killing "+victim+" for 15 minutes.");
- ThreadPool.schedule(() -> protections.get(victim.getObjectId()).remove(killer.getObjectId()), 1000*60*15);
- }
- public boolean protectionExists(Player killer, Player victim)
- {
- 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()))))
- return true;
- if (protections.containsKey(victim.getObjectId()))
- if (protections.get(victim.getObjectId()).contains(killer.getObjectId()))
- return true;
- return false;
- }
- public static PvpProtection getInstance()
- {
- return SingletonHolder.instance;
- }
- private static class SingletonHolder
- {
- protected static final PvpProtection instance = new PvpProtection();
- }
- private class KillStats
- {
- private int killer;
- private int victim;
- private int kills;
- public KillStats(int killer, int victim, int kills)
- {
- this.killer = killer;
- this.victim = victim;
- this.kills = kills;
- }
- public void removeKills()
- {
- kills = 0;
- }
- public void addKill()
- {
- kills++;
- }
- public int getKills()
- {
- return kills;
- }
- public int getKiller()
- {
- return killer;
- }
- public int getVictim()
- {
- return victim;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement