package events.PvP.DestroyTheEnemyBase;
import java.util.List;
import events.base.PvPEventTasks;
import events.base.PvPEventTasks.ActionTaskType;
import events.base.instances.PvPEventInstance;
import events.base.templates.EventMember;
import l2p.gameserver.ThreadPoolManager;
import l2p.gameserver.data.holder.NpcHolder;
import l2p.gameserver.enums.TeamType;
import l2p.gameserver.holders.npcs.NpcTemplate;
import l2p.gameserver.idfactory.IdFactory;
import l2p.gameserver.model.Creature;
import l2p.gameserver.model.Location;
import l2p.gameserver.model.Player;
import l2p.gameserver.model.entity.Reflection;
import l2p.gameserver.utils.Strings;
import npc.model.events.TeamBaseInstance;
/**
* @author Gaikotsu
*/
public class DestroyTheEnemyBase extends PvPEventInstance
{
private static TeamBaseInstance _redBase = null;
private static TeamBaseInstance _blueBase = null;
public DestroyTheEnemyBase()
{
super("Destroy the Enemy Base", "Уничтожение базы врага", Strings.EMPTY);
}
public static DestroyTheEnemyBase getInstance()
{
return SingletonHolder._instance;
}
@Override
public void onLoad()
{
getInstance().onLoad(true);
}
@Override
public boolean registerPlayer()
{
return getInstance().registerPlayer(getSelf());
}
@Override
public void onPlayerExit(Player player)
{
if (player == null || !isInTeam(player, false))
return;
unregisterPlayer(player);
if (getStatus() == EventStatus.PREPARE || getStatus() == EventStatus.BATTLE || getStatus() == EventStatus.ENDING)
{
teleportToSavedCoords(player);
if (getStatus() == EventStatus.BATTLE && isAnyTeamIsEmpty(false))
endBattle();
}
}
@Override
public void onTeleport(Player player, int x, int y, int z, Reflection reflection)
{
if (getStatus() != EventStatus.BATTLE || player == null || !isInTeam(player, false))
return;
if (!getLocation().getBattleZone().checkIfInZone(x, y, z, reflection))
{
unregisterPlayer(player);
if (isAnyTeamIsEmpty(false))
endBattle();
}
}
@Override
public void onKill(Creature actor, Creature victim)
{
if (actor == null || victim == null || getStatus() != EventStatus.BATTLE || actor.getPlayer() == null || !victim.isPlayer())
return;
EventMember killer = getMember(actor.getPlayer());
EventMember killed = getMember(victim.getPlayer());
if (killer == null || killed == null || killer == killed || killer.getPlayer() == null || killed.getPlayer() == null || killer.getTeam() == killed.getTeam())
return;
addRewards(killer.getPlayer(), ActionType.KILL);
ThreadPoolManager.getInstance().schedule(new PvPEventTasks.ActionTask(this, killed.getPlayer(), ActionTaskType.REVIVE_AND_BUFF), getConfig().getTimeToRevive() * 1000L);
showMessage(killed.getPlayer(), "Вас убили. Через несколько секунд вы будете возрождены.");
}
@Override
public void onRegistration()
{}
@Override
public void onCanceled()
{}
@Override
public void onPrepare(List<Player> players)
{}
@Override
public void onStartBattle(List<Player> players)
{
if (_blueBase != null)
_blueBase.deleteMe();
if (_redBase != null)
_redBase.deleteMe();
_blueBase = spawnBase(getConfig().getBlueNpc(), getLocation().getBlueNpcLoc());
_redBase = spawnBase(getConfig().getRedNpc(), getLocation().getRedNpcLoc());
}
@Override
public void onEndBattle(List<Player> players)
{
TeamType winner = TeamType.NONE;
if (_blueBase != null && _blueBase.isDead())
winner = TeamType.RED;
if (_redBase != null && _redBase.isDead())
winner = TeamType.BLUE;
if (winner == TeamType.NONE)
{
int redTeamScore = getScore(TeamType.RED);
int blueTeamScore = getScore(TeamType.BLUE);
if (redTeamScore != blueTeamScore)
winner = redTeamScore > blueTeamScore ? TeamType.RED : TeamType.BLUE;
}
if (winner != TeamType.NONE)
{
if (getMembersCount(winner, false) < 1)
winner = winner.revert();
showMessage(players, "Победила " + (winner == TeamType.RED ? "красная" : "синяя") + " команда.");
addRewards(getPlayers(winner, false), ActionType.WIN);
}
else
{
showMessage(players, "Ничья. Оба отряда получают часть награды.");
addRewards(players, ActionType.TIE);
}
if (_blueBase != null)
{
_blueBase.deleteMe();
_blueBase = null;
}
if (_redBase != null)
{
_redBase.deleteMe();
_redBase = null;
}
}
private TeamBaseInstance spawnBase(int baseId, Location loc)
{
try
{
NpcTemplate template = NpcHolder.getInstance().getTemplate(baseId);
TeamBaseInstance base = new TeamBaseInstance(IdFactory.getInstance().getNextId(), template);
base.setReflection(getLocation().getRef());
base.spawnMe(loc);
return base;
}
catch (Exception e)
{
_log.warn(getName(true) + ": Error while spawn base " + baseId, e);
}
return null;
}
private static class SingletonHolder
{
private static final DestroyTheEnemyBase _instance = new DestroyTheEnemyBase();
}
}