package listeners.items;
import java.util.Calendar;
import l2p.commons.threading.RunnableImpl;
import l2p.commons.util.Rnd;
import l2p.gameserver.ThreadPoolManager;
import l2p.gameserver.enums.LogAction;
import l2p.gameserver.enums.SummonType;
import l2p.gameserver.listener.actor.player.OnPlayerEnterListener;
import l2p.gameserver.listener.actor.player.OnPremiumStatusListener;
import l2p.gameserver.listener.actor.player.OnSummonListener;
import l2p.gameserver.model.Player;
import l2p.gameserver.model.Summon;
import l2p.gameserver.model.actor.listener.PlayerListenerList;
import l2p.gameserver.scripts.ScriptFile;
import l2p.gameserver.utils.ItemUtils;
import l2p.gameserver.utils.Log;
/**
* @author Gaikotsu
*/
public class PremiumAgathion implements ScriptFile
{
private static final int AgathionItemId = 20970;
private static final int AgathionNpcId = 1575;
private static final PremiumStatsusListener _premiumStatusListener = new PremiumStatsusListener();
private static final PlayerEnterListener _playerEnterListener = new PlayerEnterListener();
private static final SummonListener _summonListener = new SummonListener();
@Override
public void onLoad()
{
PlayerListenerList.addGlobal(_premiumStatusListener);
PlayerListenerList.addGlobal(_playerEnterListener);
PlayerListenerList.addGlobal(_summonListener);
}
@Override
public void onReload()
{
PlayerListenerList.removeGlobal(_premiumStatusListener);
PlayerListenerList.removeGlobal(_playerEnterListener);
PlayerListenerList.removeGlobal(_summonListener);
}
@Override
public void onShutdown()
{}
public static class PremiumStatsusListener implements OnPremiumStatusListener
{
@Override
public void onStarted(Player player, int hours)
{
// Добавляем премиального агатиона
if (player.getItemCount(AgathionItemId) == 0)
ItemUtils.addItem(player, AgathionItemId, 1, true);
// Добавляем премиальный куб, если премиум куплен на 30 дней и более
if (hours >= 720)
ItemUtils.addItem(player, 23813, hours / 720, true);
}
@Override
public void onEnded(Player player)
{
if (player.getItemCount(AgathionItemId) > 0)
ItemUtils.removeItem(player, AgathionItemId, 1, true);
}
}
public static class PlayerEnterListener implements OnPlayerEnterListener
{
@Override
public void onPlayerEnter(Player player)
{
if (player.getRateBonusExpire() > 0)
{
if (player.getItemCount(AgathionItemId) == 0)
ItemUtils.addItem(player, AgathionItemId, 1, true);
}
else
{
if (player.getItemCount(AgathionItemId) > 0)
ItemUtils.removeItem(player, AgathionItemId, 1, true);
}
}
}
public static class SummonListener implements OnSummonListener
{
@Override
public void onSummon(Player player, Summon summon, int id, SummonType type)
{
if (type != SummonType.AGATHION)
return;
if (id == AgathionNpcId)
player.addSpecialTask(AgathionItemId, ThreadPoolManager.getInstance().scheduleAtFixedRate(new PremiumAgathionTask(player), Rnd.get(5, 10) * 60 * 1000L, 10 * 60 * 1000L));
else
player.removeSpecialTask(AgathionItemId);
}
}
public static class PremiumAgathionTask extends RunnableImpl
{
private final Player _player;
public PremiumAgathionTask(Player player)
{
_player = player;
}
@Override
public void runImpl() throws Exception
{
if (_player == null || !_player.isConnected() || _player.isInOfflineMode() || _player.isLogoutStarted() || _player.getRateBonusExpire() <= 0 || _player.getAgathionId() != AgathionNpcId)
return;
int dayOfYear = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
if (_player.getVariables().getVar("lastPremiumEgg", -1) != dayOfYear)
{
_player.getVariables().setVar("lastPremiumEgg", dayOfYear);
ItemUtils.addItem(_player, 22649, 1, true);
Log.logItem(_player, LogAction.AddPremiumEgg, 22649, 1);
}
}
}
}