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.customs;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ScheduledFuture;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.model.L2World;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- /**
- * @author Anarchy
- *
- */
- public class PvpProtection
- {
- private Map<KillStats, ScheduledFuture<?>> _killStats;
- protected Map<String, List<String>> _protections;
- public static PvpProtection getInstance()
- {
- return SingletonHolder._instance;
- }
- protected PvpProtection()
- {
- _killStats = new HashMap<>();
- _protections = new HashMap<>();
- System.out.println("PvP protection loaded.");
- }
- public void checkKill(String killer, String victim)
- {
- KillStats ks = null;
- for (KillStats k : _killStats.keySet())
- {
- if (k.getKiller().equals(killer) && k.getVictim().equals(victim))
- {
- ks = k;
- break;
- }
- }
- if (ks != null)
- {
- ks.addKill();
- if (ks.getKills() >= 4)
- {
- addNewProtection(killer, victim, ks);
- }
- else
- {
- _killStats.get(ks).cancel(true);
- _killStats.remove(ks);
- final KillStats fks = ks;
- _killStats.put(ks, ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
- {
- @Override
- public void run()
- {
- fks.removeKills();
- }
- }, 25*1000));
- }
- }
- else
- {
- final KillStats nks = new KillStats(killer, victim, 1);
- _killStats.put(nks, ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
- {
- @Override
- public void run()
- {
- nks.removeKills();
- }
- }, 25*1000));
- }
- }
- public void addNewProtection(final String killer, final String victim, KillStats ks)
- {
- _killStats.remove(ks);
- if (_protections.containsKey(victim))
- {
- _protections.get(victim).add(killer);
- }
- else
- {
- List<String> temp = new ArrayList<>();
- temp.add(killer);
- _protections.put(victim, temp);
- }
- L2PcInstance p = L2World.getInstance().getPlayer(killer);
- if (p != null && p.isOnline())
- {
- p.sendMessage("Killing protection enabled. You may not be rewarded for killing "+victim+" for 30 minutes.");
- }
- ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
- {
- @Override
- public void run()
- {
- _protections.get(victim).remove(killer);
- }
- }, 1000*60*30);
- }
- public boolean protectionExists(String killer, String victim)
- {
- if (_protections.containsKey(victim))
- {
- if (_protections.get(victim).contains(killer))
- {
- return true;
- }
- }
- return false;
- }
- public class KillStats
- {
- private String killer,
- victim;
- private int kills;
- public KillStats(String killer, String 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 String getKiller()
- {
- return killer;
- }
- public String getVictim()
- {
- return victim;
- }
- }
- private static class SingletonHolder
- {
- protected static final PvpProtection _instance = new PvpProtection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment