Classic branch.
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.gameserver.ThreadPoolManager;
|
||||
import com.l2jmobius.gameserver.model.actor.L2Character;
|
||||
import com.l2jmobius.gameserver.model.actor.L2Summon;
|
||||
import com.l2jmobius.gameserver.network.serverpackets.AutoAttackStop;
|
||||
|
||||
/**
|
||||
* Attack stance task manager.
|
||||
* @author Luca Baldi, Zoey76
|
||||
*/
|
||||
public class AttackStanceTaskManager
|
||||
{
|
||||
protected static final Logger _log = Logger.getLogger(AttackStanceTaskManager.class.getName());
|
||||
|
||||
protected static final Map<L2Character, Long> _attackStanceTasks = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new attack stance task manager.
|
||||
*/
|
||||
protected AttackStanceTaskManager()
|
||||
{
|
||||
ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the attack stance task.
|
||||
* @param actor the actor
|
||||
*/
|
||||
public void addAttackStanceTask(L2Character actor)
|
||||
{
|
||||
if (actor != null)
|
||||
{
|
||||
_attackStanceTasks.put(actor, System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the attack stance task.
|
||||
* @param actor the actor
|
||||
*/
|
||||
public void removeAttackStanceTask(L2Character actor)
|
||||
{
|
||||
if (actor != null)
|
||||
{
|
||||
if (actor.isSummon())
|
||||
{
|
||||
actor = actor.getActingPlayer();
|
||||
}
|
||||
_attackStanceTasks.remove(actor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for attack stance task.<br>
|
||||
* @param actor the actor
|
||||
* @return {@code true} if the character has an attack stance task, {@code false} otherwise
|
||||
*/
|
||||
public boolean hasAttackStanceTask(L2Character actor)
|
||||
{
|
||||
if (actor != null)
|
||||
{
|
||||
if (actor.isSummon())
|
||||
{
|
||||
actor = actor.getActingPlayer();
|
||||
}
|
||||
return _attackStanceTasks.containsKey(actor);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected class FightModeScheduler implements Runnable
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
final long current = System.currentTimeMillis();
|
||||
try
|
||||
{
|
||||
final Iterator<Entry<L2Character, Long>> iter = _attackStanceTasks.entrySet().iterator();
|
||||
Entry<L2Character, Long> e;
|
||||
L2Character actor;
|
||||
while (iter.hasNext())
|
||||
{
|
||||
e = iter.next();
|
||||
if ((current - e.getValue()) > 15000)
|
||||
{
|
||||
actor = e.getKey();
|
||||
if (actor != null)
|
||||
{
|
||||
actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
|
||||
actor.getAI().setAutoAttacking(false);
|
||||
if (actor.isPlayer() && actor.hasSummon())
|
||||
{
|
||||
final L2Summon pet = actor.getPet();
|
||||
if (pet != null)
|
||||
{
|
||||
pet.broadcastPacket(new AutoAttackStop(pet.getObjectId()));
|
||||
}
|
||||
actor.getServitors().values().forEach(s -> s.broadcastPacket(new AutoAttackStop(s.getObjectId())));
|
||||
}
|
||||
}
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Unless caught here, players remain in attack positions.
|
||||
_log.log(Level.WARNING, "Error in FightModeScheduler: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the single instance of AttackStanceTaskManager.
|
||||
* @return single instance of AttackStanceTaskManager
|
||||
*/
|
||||
public static AttackStanceTaskManager getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final AttackStanceTaskManager _instance = new AttackStanceTaskManager();
|
||||
}
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.gameserver.model.actor.L2Attackable;
|
||||
import com.l2jmobius.gameserver.model.actor.L2Character;
|
||||
import com.l2jmobius.gameserver.model.actor.templates.L2NpcTemplate;
|
||||
|
||||
/**
|
||||
* @author NosBit
|
||||
*/
|
||||
public final class DecayTaskManager
|
||||
{
|
||||
private final ScheduledExecutorService _decayExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
protected final Map<L2Character, ScheduledFuture<?>> _decayTasks = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Adds a decay task for the specified character.<br>
|
||||
* <br>
|
||||
* If the decay task already exists it cancels it and re-adds it.
|
||||
* @param character the character
|
||||
*/
|
||||
public void add(L2Character character)
|
||||
{
|
||||
if (character == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
long delay;
|
||||
if (character.getTemplate() instanceof L2NpcTemplate)
|
||||
{
|
||||
delay = ((L2NpcTemplate) character.getTemplate()).getCorpseTime();
|
||||
}
|
||||
else
|
||||
{
|
||||
delay = Config.DEFAULT_CORPSE_TIME;
|
||||
}
|
||||
|
||||
if (character.isAttackable() && (((L2Attackable) character).isSpoiled() || ((L2Attackable) character).isSeeded()))
|
||||
{
|
||||
delay += Config.SPOILED_CORPSE_EXTEND_TIME;
|
||||
}
|
||||
|
||||
add(character, delay, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a decay task for the specified character.<br>
|
||||
* <br>
|
||||
* If the decay task already exists it cancels it and re-adds it.
|
||||
* @param character the character
|
||||
* @param delay the delay
|
||||
* @param timeUnit the time unit of the delay parameter
|
||||
*/
|
||||
public void add(L2Character character, long delay, TimeUnit timeUnit)
|
||||
{
|
||||
ScheduledFuture<?> decayTask = _decayExecutor.schedule(new DecayTask(character), delay, TimeUnit.SECONDS);
|
||||
|
||||
decayTask = _decayTasks.put(character, decayTask);
|
||||
// if decay task already existed cancel it so we use the new time
|
||||
if (decayTask != null)
|
||||
{
|
||||
if (!decayTask.cancel(false))
|
||||
{
|
||||
// old decay task was completed while canceling it remove and cancel the new one
|
||||
decayTask = _decayTasks.remove(character);
|
||||
if (decayTask != null)
|
||||
{
|
||||
decayTask.cancel(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the decay task of the specified character.
|
||||
* @param character the character
|
||||
*/
|
||||
public void cancel(L2Character character)
|
||||
{
|
||||
final ScheduledFuture<?> decayTask = _decayTasks.remove(character);
|
||||
if (decayTask != null)
|
||||
{
|
||||
decayTask.cancel(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the remaining time of the specified character's decay task.
|
||||
* @param character the character
|
||||
* @return if a decay task exists the remaining time, {@code Long.MAX_VALUE} otherwise
|
||||
*/
|
||||
public long getRemainingTime(L2Character character)
|
||||
{
|
||||
final ScheduledFuture<?> decayTask = _decayTasks.get(character);
|
||||
if (decayTask != null)
|
||||
{
|
||||
return decayTask.getDelay(TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
private class DecayTask implements Runnable
|
||||
{
|
||||
private final L2Character _character;
|
||||
|
||||
protected DecayTask(L2Character character)
|
||||
{
|
||||
_character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
_decayTasks.remove(_character);
|
||||
_character.onDecay();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
final StringBuilder ret = new StringBuilder();
|
||||
ret.append("============= DecayTask Manager Report ============");
|
||||
ret.append(Config.EOL);
|
||||
ret.append("Tasks count: ");
|
||||
ret.append(_decayTasks.size());
|
||||
ret.append(Config.EOL);
|
||||
ret.append("Tasks dump:");
|
||||
ret.append(Config.EOL);
|
||||
|
||||
for (Entry<L2Character, ScheduledFuture<?>> entry : _decayTasks.entrySet())
|
||||
{
|
||||
ret.append("Class/Name: ");
|
||||
ret.append(entry.getKey().getClass().getSimpleName());
|
||||
ret.append('/');
|
||||
ret.append(entry.getKey().getName());
|
||||
ret.append(" decay timer: ");
|
||||
ret.append(entry.getValue().getDelay(TimeUnit.MILLISECONDS));
|
||||
ret.append(Config.EOL);
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static DecayTaskManager getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final DecayTaskManager _instance = new DecayTaskManager();
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public abstract class Task
|
||||
{
|
||||
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
|
||||
|
||||
public void initializate()
|
||||
{
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> launchSpecial(ExecutedTask instance)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public abstract void onTimeElapsed(ExecutedTask task);
|
||||
|
||||
public void onDestroy()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,380 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
import static com.l2jmobius.gameserver.taskmanager.TaskTypes.TYPE_NONE;
|
||||
import static com.l2jmobius.gameserver.taskmanager.TaskTypes.TYPE_SHEDULED;
|
||||
import static com.l2jmobius.gameserver.taskmanager.TaskTypes.TYPE_TIME;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.commons.database.DatabaseFactory;
|
||||
import com.l2jmobius.gameserver.ThreadPoolManager;
|
||||
import com.l2jmobius.gameserver.taskmanager.tasks.TaskBirthday;
|
||||
import com.l2jmobius.gameserver.taskmanager.tasks.TaskCleanUp;
|
||||
import com.l2jmobius.gameserver.taskmanager.tasks.TaskRestart;
|
||||
import com.l2jmobius.gameserver.taskmanager.tasks.TaskScript;
|
||||
import com.l2jmobius.gameserver.taskmanager.tasks.TaskShutdown;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public final class TaskManager
|
||||
{
|
||||
protected static final Logger LOGGER = Logger.getLogger(TaskManager.class.getName());
|
||||
|
||||
private final Map<Integer, Task> _tasks = new ConcurrentHashMap<>();
|
||||
protected final List<ExecutedTask> _currentTasks = new CopyOnWriteArrayList<>();
|
||||
|
||||
protected static final String[] SQL_STATEMENTS =
|
||||
{
|
||||
"SELECT id,task,type,last_activation,param1,param2,param3 FROM global_tasks",
|
||||
"UPDATE global_tasks SET last_activation=? WHERE id=?",
|
||||
"SELECT id FROM global_tasks WHERE task=?",
|
||||
"INSERT INTO global_tasks (task,type,last_activation,param1,param2,param3) VALUES(?,?,?,?,?,?)"
|
||||
};
|
||||
|
||||
protected TaskManager()
|
||||
{
|
||||
initializate();
|
||||
startAllTasks();
|
||||
LOGGER.info(getClass().getSimpleName() + ": Loaded: " + _tasks.size() + " Tasks.");
|
||||
}
|
||||
|
||||
public class ExecutedTask implements Runnable
|
||||
{
|
||||
int id;
|
||||
long lastActivation;
|
||||
Task task;
|
||||
TaskTypes type;
|
||||
String[] params;
|
||||
ScheduledFuture<?> scheduled;
|
||||
|
||||
public ExecutedTask(Task ptask, TaskTypes ptype, ResultSet rset) throws SQLException
|
||||
{
|
||||
task = ptask;
|
||||
type = ptype;
|
||||
id = rset.getInt("id");
|
||||
lastActivation = rset.getLong("last_activation");
|
||||
params = new String[]
|
||||
{
|
||||
rset.getString("param1"),
|
||||
rset.getString("param2"),
|
||||
rset.getString("param3")
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
task.onTimeElapsed(this);
|
||||
lastActivation = System.currentTimeMillis();
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[1]))
|
||||
{
|
||||
statement.setLong(1, lastActivation);
|
||||
statement.setInt(2, id);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
LOGGER.warning("Cannot updated the Global Task " + id + ": " + e);
|
||||
}
|
||||
|
||||
if ((type == TYPE_SHEDULED) || (type == TYPE_TIME))
|
||||
{
|
||||
stopTask();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object)
|
||||
{
|
||||
if (this == object)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(object instanceof ExecutedTask))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return id == ((ExecutedTask) object).id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public Task getTask()
|
||||
{
|
||||
return task;
|
||||
}
|
||||
|
||||
public TaskTypes getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public String[] getParams()
|
||||
{
|
||||
return params;
|
||||
}
|
||||
|
||||
public long getLastActivation()
|
||||
{
|
||||
return lastActivation;
|
||||
}
|
||||
|
||||
public void stopTask()
|
||||
{
|
||||
task.onDestroy();
|
||||
|
||||
if (scheduled != null)
|
||||
{
|
||||
scheduled.cancel(true);
|
||||
}
|
||||
|
||||
_currentTasks.remove(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void initializate()
|
||||
{
|
||||
registerTask(new TaskBirthday());
|
||||
registerTask(new TaskCleanUp());
|
||||
registerTask(new TaskRestart());
|
||||
registerTask(new TaskScript());
|
||||
registerTask(new TaskShutdown());
|
||||
}
|
||||
|
||||
public void registerTask(Task task)
|
||||
{
|
||||
final int key = task.getName().hashCode();
|
||||
_tasks.computeIfAbsent(key, k ->
|
||||
{
|
||||
task.initializate();
|
||||
return task;
|
||||
});
|
||||
}
|
||||
|
||||
private void startAllTasks()
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[0]);
|
||||
ResultSet rset = statement.executeQuery())
|
||||
{
|
||||
while (rset.next())
|
||||
{
|
||||
final Task task = _tasks.get(rset.getString("task").trim().toLowerCase().hashCode());
|
||||
if (task == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
final TaskTypes type = TaskTypes.valueOf(rset.getString("type"));
|
||||
if (type != TYPE_NONE)
|
||||
{
|
||||
final ExecutedTask current = new ExecutedTask(task, type, rset);
|
||||
if (launchTask(current))
|
||||
{
|
||||
_currentTasks.add(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.SEVERE, "Error while loading Global Task table: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean launchTask(ExecutedTask task)
|
||||
{
|
||||
final ThreadPoolManager scheduler = ThreadPoolManager.getInstance();
|
||||
final TaskTypes type = task.getType();
|
||||
long delay, interval;
|
||||
switch (type)
|
||||
{
|
||||
case TYPE_STARTUP:
|
||||
task.run();
|
||||
return false;
|
||||
case TYPE_SHEDULED:
|
||||
delay = Long.valueOf(task.getParams()[0]);
|
||||
task.scheduled = scheduler.scheduleGeneral(task, delay);
|
||||
return true;
|
||||
case TYPE_FIXED_SHEDULED:
|
||||
delay = Long.valueOf(task.getParams()[0]);
|
||||
interval = Long.valueOf(task.getParams()[1]);
|
||||
task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
|
||||
return true;
|
||||
case TYPE_TIME:
|
||||
try
|
||||
{
|
||||
final Date desired = DateFormat.getInstance().parse(task.getParams()[0]);
|
||||
final long diff = desired.getTime() - System.currentTimeMillis();
|
||||
if (diff >= 0)
|
||||
{
|
||||
task.scheduled = scheduler.scheduleGeneral(task, diff);
|
||||
return true;
|
||||
}
|
||||
LOGGER.info("Task " + task.getId() + " is obsoleted.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
break;
|
||||
case TYPE_SPECIAL:
|
||||
final ScheduledFuture<?> result = task.getTask().launchSpecial(task);
|
||||
if (result != null)
|
||||
{
|
||||
task.scheduled = result;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case TYPE_GLOBAL_TASK:
|
||||
interval = Long.valueOf(task.getParams()[0]) * 86400000L;
|
||||
final String[] hour = task.getParams()[1].split(":");
|
||||
|
||||
if (hour.length != 3)
|
||||
{
|
||||
LOGGER.warning("Task " + task.getId() + " has incorrect parameters");
|
||||
return false;
|
||||
}
|
||||
|
||||
final Calendar check = Calendar.getInstance();
|
||||
check.setTimeInMillis(task.getLastActivation() + interval);
|
||||
|
||||
final Calendar min = Calendar.getInstance();
|
||||
try
|
||||
{
|
||||
min.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour[0]));
|
||||
min.set(Calendar.MINUTE, Integer.parseInt(hour[1]));
|
||||
min.set(Calendar.SECOND, Integer.parseInt(hour[2]));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Bad parameter on task " + task.getId() + ": ", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
delay = min.getTimeInMillis() - System.currentTimeMillis();
|
||||
|
||||
if (check.after(min) || (delay < 0))
|
||||
{
|
||||
delay += interval;
|
||||
}
|
||||
task.scheduled = scheduler.scheduleGeneralAtFixedRate(task, delay, interval);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3)
|
||||
{
|
||||
return addUniqueTask(task, type, param1, param2, param3, 0);
|
||||
}
|
||||
|
||||
public static boolean addUniqueTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement ps1 = con.prepareStatement(SQL_STATEMENTS[2]))
|
||||
{
|
||||
ps1.setString(1, task);
|
||||
try (ResultSet rs = ps1.executeQuery())
|
||||
{
|
||||
if (!rs.next())
|
||||
{
|
||||
try (PreparedStatement ps2 = con.prepareStatement(SQL_STATEMENTS[3]))
|
||||
{
|
||||
ps2.setString(1, task);
|
||||
ps2.setString(2, type.toString());
|
||||
ps2.setLong(3, lastActivation);
|
||||
ps2.setString(4, param1);
|
||||
ps2.setString(5, param2);
|
||||
ps2.setString(6, param3);
|
||||
ps2.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Cannot add the unique task: ", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3)
|
||||
{
|
||||
return addTask(task, type, param1, param2, param3, 0);
|
||||
}
|
||||
|
||||
public static boolean addTask(String task, TaskTypes type, String param1, String param2, String param3, long lastActivation)
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement(SQL_STATEMENTS[3]))
|
||||
{
|
||||
statement.setString(1, task);
|
||||
statement.setString(2, type.toString());
|
||||
statement.setLong(3, lastActivation);
|
||||
statement.setString(4, param1);
|
||||
statement.setString(5, param2);
|
||||
statement.setString(6, param3);
|
||||
statement.execute();
|
||||
return true;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Cannot add the task: ", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static TaskManager getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final TaskManager _instance = new TaskManager();
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public enum TaskTypes
|
||||
{
|
||||
TYPE_NONE,
|
||||
TYPE_TIME,
|
||||
TYPE_SHEDULED,
|
||||
TYPE_FIXED_SHEDULED,
|
||||
TYPE_GLOBAL_TASK,
|
||||
TYPE_STARTUP,
|
||||
TYPE_SPECIAL
|
||||
}
|
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager.tasks;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.commons.database.DatabaseFactory;
|
||||
import com.l2jmobius.gameserver.data.sql.impl.CharNameTable;
|
||||
import com.l2jmobius.gameserver.enums.MailType;
|
||||
import com.l2jmobius.gameserver.instancemanager.MailManager;
|
||||
import com.l2jmobius.gameserver.model.entity.Message;
|
||||
import com.l2jmobius.gameserver.model.itemcontainer.Mail;
|
||||
import com.l2jmobius.gameserver.taskmanager.Task;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskTypes;
|
||||
import com.l2jmobius.gameserver.util.Util;
|
||||
|
||||
/**
|
||||
* @author Nyaran
|
||||
*/
|
||||
public class TaskBirthday extends Task
|
||||
{
|
||||
private static final String NAME = "birthday";
|
||||
private static final String QUERY = "SELECT charId, createDate FROM characters WHERE createDate LIKE ?";
|
||||
private static final Calendar _today = Calendar.getInstance();
|
||||
private int _count = 0;
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeElapsed(ExecutedTask task)
|
||||
{
|
||||
final Calendar lastExecDate = Calendar.getInstance();
|
||||
final long lastActivation = task.getLastActivation();
|
||||
|
||||
if (lastActivation > 0)
|
||||
{
|
||||
lastExecDate.setTimeInMillis(lastActivation);
|
||||
}
|
||||
|
||||
final String rangeDate = "[" + Util.getDateString(lastExecDate.getTime()) + "] - [" + Util.getDateString(_today.getTime()) + "]";
|
||||
|
||||
for (; !_today.before(lastExecDate); lastExecDate.add(Calendar.DATE, 1))
|
||||
{
|
||||
checkBirthday(lastExecDate.get(Calendar.YEAR), lastExecDate.get(Calendar.MONTH), lastExecDate.get(Calendar.DATE));
|
||||
}
|
||||
|
||||
LOGGER.info("BirthdayManager: " + _count + " gifts sent. " + rangeDate);
|
||||
}
|
||||
|
||||
private void checkBirthday(int year, int month, int day)
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement statement = con.prepareStatement(QUERY))
|
||||
{
|
||||
statement.setString(1, "%-" + getNum(month + 1) + "-" + getNum(day));
|
||||
try (ResultSet rset = statement.executeQuery())
|
||||
{
|
||||
while (rset.next())
|
||||
{
|
||||
final int playerId = rset.getInt("charId");
|
||||
final Calendar createDate = Calendar.getInstance();
|
||||
createDate.setTime(rset.getDate("createDate"));
|
||||
|
||||
final int age = year - createDate.get(Calendar.YEAR);
|
||||
if (age <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
String text = Config.ALT_BIRTHDAY_MAIL_TEXT;
|
||||
|
||||
if (text.contains("$c1"))
|
||||
{
|
||||
text = text.replace("$c1", CharNameTable.getInstance().getNameById(playerId));
|
||||
}
|
||||
if (text.contains("$s1"))
|
||||
{
|
||||
text = text.replace("$s1", String.valueOf(age));
|
||||
}
|
||||
|
||||
final Message msg = new Message(playerId, Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, MailType.BIRTHDAY);
|
||||
|
||||
final Mail attachments = msg.createAttachments();
|
||||
attachments.addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
|
||||
|
||||
MailManager.getInstance().sendMessage(msg);
|
||||
_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Error checking birthdays. ", e);
|
||||
}
|
||||
|
||||
// If character birthday is 29-Feb and year isn't leap, send gift on 28-feb
|
||||
final GregorianCalendar calendar = new GregorianCalendar();
|
||||
if ((month == Calendar.FEBRUARY) && (day == 28) && !calendar.isLeapYear(_today.get(Calendar.YEAR)))
|
||||
{
|
||||
checkBirthday(year, Calendar.FEBRUARY, 29);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param num the number to format.
|
||||
* @return the formatted number starting with a 0 if it is lower or equal than 10.
|
||||
*/
|
||||
private String getNum(int num)
|
||||
{
|
||||
return (num <= 9) ? "0" + num : String.valueOf(num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializate()
|
||||
{
|
||||
TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, "1", "06:30:00", "");
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager.tasks;
|
||||
|
||||
import com.l2jmobius.gameserver.taskmanager.Task;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
|
||||
/**
|
||||
* @author Tempy
|
||||
*/
|
||||
public final class TaskCleanUp extends Task
|
||||
{
|
||||
public static final String NAME = "clean_up";
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeElapsed(ExecutedTask task)
|
||||
{
|
||||
System.runFinalization();
|
||||
System.gc();
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager.tasks;
|
||||
|
||||
import com.l2jmobius.gameserver.Shutdown;
|
||||
import com.l2jmobius.gameserver.taskmanager.Task;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public final class TaskRestart extends Task
|
||||
{
|
||||
public static final String NAME = "restart";
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeElapsed(ExecutedTask task)
|
||||
{
|
||||
final Shutdown handler = new Shutdown(Integer.parseInt(task.getParams()[2]), true);
|
||||
handler.start();
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager.tasks;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.l2jmobius.gameserver.scripting.ScriptEngineManager;
|
||||
import com.l2jmobius.gameserver.taskmanager.Task;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
|
||||
/**
|
||||
* @author janiii
|
||||
*/
|
||||
public class TaskScript extends Task
|
||||
{
|
||||
public static final String NAME = "script";
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeElapsed(ExecutedTask task)
|
||||
{
|
||||
try
|
||||
{
|
||||
ScriptEngineManager.getInstance().executeScript(Paths.get("cron", task.getParams()[2]));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.log(Level.WARNING, "Script execution failed!", e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* 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 com.l2jmobius.gameserver.taskmanager.tasks;
|
||||
|
||||
import com.l2jmobius.gameserver.Shutdown;
|
||||
import com.l2jmobius.gameserver.taskmanager.Task;
|
||||
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
|
||||
|
||||
/**
|
||||
* @author Layane
|
||||
*/
|
||||
public class TaskShutdown extends Task
|
||||
{
|
||||
public static final String NAME = "shutdown";
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeElapsed(ExecutedTask task)
|
||||
{
|
||||
final Shutdown handler = new Shutdown(Integer.parseInt(task.getParams()[2]), false);
|
||||
handler.start();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user