Jump to content

[L2J Contest - Guide & Share] ♥ Task Managers + Custom Code ♥


Recommended Posts

Hello people!

 

I know that Ass4s1n has already made a guide for Task Managers, but I want to explain it even more because at his share there were complains..

So allow me to show you!

I will make a simple code and explain every step.

 

 

You can skip that Step.

We always start with the GNU License (No reason actually, but its cool xD).

/*
* 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/>.
*/

 

Now lets define the package.

As we are working on Tasks, the package has to be this:

package com.l2jserver.gameserver.taskmanager.tasks;

 

If you're using Interlude or other projects than L2JServer, you just need to fic those:

com.l2jserver

 

with

 

net.sf.l2j

 

Etcetera..

 

Now we have two options:

1) We can first add the imports that we will use, if we are sure about what we will make

2) We can start writing the code and leave the imports for later, to be added as quickfixes.

 

I personally prefer to start writing the code immediately, so in case I need to add something extra, I will not have to worry!

 

The only imports that you will need for sure are those:

import com.l2jserver.gameserver.taskmanager.Task;
import com.l2jserver.gameserver.taskmanager.TaskManager;
import com.l2jserver.gameserver.taskmanager.TaskTypes;
import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;

 

Let's continue.

 

You can skip that Step.

Now let's add the author's name (your name if you are the creator) just to be respected by the ones that will use the code, or simply to keep your credits.

So, we will make something like:

 

/**
* 
* @author YourNameHere
*/

 

In my case, for example, it will be like:

/**
* 
* @author SoFaKi
*/

 


 

OK, now we will start with the main thingy! The code.

 

Let's start as we always start:

public class TaskNameHere extends Task
{

 

It's a public class (you have to replace the TaskNameHere with you class's name) which extends the Task class.

 

Now let's talk about the Logger, since we may need it for catching exceptions below (we will :D).

 

So write something like

private static final Logger _log = Logger.getLogger(TaskNameHere.class.getName());

 

But now, you will have to import it either manually, or by the quickfix.

So the import you have to add below is that:

import java.util.logging.Logger;

 

NOTE! L2JServer is using the random java logger, however other projects like L2jFree have advanced their commons one, so it will be kinda different at its use. Don't worry, the difference is not big!!

 

OK, after this we will need to add one more code:

public static final String NAME = "your_string";

 

This string defines something like.. The use of the code.

For example, at the TaskSevenSignsUpdate.java class, that String is this:

 

public static final String NAME = "seven_signs_update";

 

I hope you can understand everything until now :)

 

Let's continue now, by setting the name.

What do I mean? Check out:

 

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#getName()
 */
@Override
public String getName()
{
	return NAME;
}

 

Here, we see the Task class (where we will register that task later) and return THIS task's name.

The NAME is the one we defined above as a string (public static final String NAME = "your_string";).

 

Add this, as the next step:

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#onTimeElapsed(com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask)
 */
@Override
public void onTimeElapsed(ExecutedTask task)
{
	try
	{	
		//your action
	}
	catch (Exception e)
	{
		_log.warning("Error X : " + e);
	}
}

 

Here is the MAIN part, imo.

At this part, we define which will be the use of the task - important, don't you agree?

 

The public void "onTimeElapsed" defines the use of the code, after the proper time (we will set it below) has passed, and it's time to use the code. And in this, we set the use.

 

The use of the task is at the "try".

Here you will set your own stuff.

And I have set it to catch an exception, in case something goes wrong..

 

Note that you can make it normally, without setting any tries, or so, but it depends on what you want to do. For example, the TaskOlympiadSave class, is setting the task's use like that:

if (Olympiad.getInstance().inCompPeriod())
	{
		Olympiad.getInstance().saveOlympiadStatus();
		_log.info("Olympiad System: Data updated.");
	}

 

I suppose you can understand me.

 

And we are almost done.

Now, we must define how much time will be needed to execute the task.

We will define it in this way:

 

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#initializate()
 */
@Override
public void initializate()
{
	super.initializate();
	TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_FIXED_SHEDULED, "60000", "60000", "");
}

 

As you can see, we use the NAME (we set it above) and the TaskType set as sheduled task.

Take care of the time though: Those numbers aren't minutes, but miliseconds!

What does that mean? For example, 1 minute is 60000 miliseconds! So be careful: Don't use too small values but also don't use too high ones! First calculate them!

 

And this is how we end the class!

Our task is now ready (considering that you have set a use for the task)!

Let's see how did the code become:

/*
* 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.l2jserver.gameserver.taskmanager.tasks;

import java.util.logging.Logger;
import com.l2jserver.gameserver.taskmanager.Task;
import com.l2jserver.gameserver.taskmanager.TaskManager;
import com.l2jserver.gameserver.taskmanager.TaskTypes;
import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;

/**
* 
* @author YourNameHere
*/

public class TaskNameHere extends Task
{
private static final Logger _log = Logger.getLogger(TaskNameHere.class.getName());
public static final String NAME = "your_string";

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#getName()
 */
@Override
public String getName()
{
	return NAME;
}

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#onTimeElapsed(com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask)
 */
@Override
public void onTimeElapsed(ExecutedTask task)
{
	try
	{	
		//your action
	}
	catch (Exception e)
	{
		_log.warning("Error X : " + e);
	}
}

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#initializate()
 */
@Override
public void initializate()
{
	super.initializate();
	TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_FIXED_SHEDULED, "60000", "60000", "");
}
}

 

Yes, this is the code :)

Now, let's finish by registering the task.

If you have previously worked with Handlers, you may understand what we are going to talk about.

 

Let's go to the file TaskManager.java to set the task which will be initializated.

Let me explain just a few things about the TaskManager class, before continuing.

In this class, the tasks are getting imported into the global_tasks sql file, to be executed from there properly. It is auto, that's why you don't have to touch anything. This is the main use and that's why it's called "Manager".

 

Anyway, let's now find that line:

private void initializate()

 

Below, you just have to set the task to be initializated.

So, under that:

registerTask(new TaskShutdown());

 

Add that line:

registerTask(new TaskNameHere());

 

Don't forget to import your task's class!!

And ofc don't forget to replace the "TaskNameHere" with your class's name.

 

 


 

 

That's all with the guide!!

I explained it as well as I good, so that newbies can understand it!

I know that I didn't give a professional explanation, since I am not a professional, but I hope that people can understand it :)

 

Let me give you the class of a Task that I made, just to give a nice example.

/*
* 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.l2jserver.gameserver.taskmanager.tasks;

import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.taskmanager.Task;
import com.l2jserver.gameserver.taskmanager.TaskManager;
import com.l2jserver.gameserver.taskmanager.TaskTypes;
import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;

/**
* 
* @author Sofia
*/

public class TaskFactionOnline extends Task
{
private static final Logger _log = Logger.getLogger(TaskFactionOnline.class.getName());
public static final String NAME = "your_string";

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#getName()
 */
@Override
public String getName()
{
	return NAME;
}

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#onTimeElapsed(com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask)
 */
@Override
public void onTimeElapsed(ExecutedTask task)
{
	try
	{	
		Announcements.getInstance().announceToAll(Config.FACTION_TEAM1_NAME + " " + L2World.getInstance().getAllTeam1PlayersCount() + " || " + Config.FACTION_TEAM2_NAME + " " + L2World.getInstance().getAllTeam2PlayersCount() + " || " + Config.FACTION_TEAM3_NAME + " " + L2World.getInstance().getAllTeam3PlayersCount());
	}
	catch (Exception e)
	{
		_log.warning("Faction Engine: There was an error while trying to execute the TaskFactionOnline Task. Please fix it! " + e);
	}
}

/**
 * 
 * @see com.l2jserver.gameserver.taskmanager.Task#initializate()
 */
@Override
public void initializate()
{
	super.initializate();
	TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_FIXED_SHEDULED, "60000", "60000", "");
}
}

 

What we see here?

This is a code related to this Faction Engine!!

What happens? Well, an announcement appears every time the task is executed, which shows the online ammount of Neutral, Devs and Cheaters Factions!

And ofc it needs a registeration, as every task.

 

Note that here we used extra imports, because of the different use!

To be more exact:

import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.model.L2World;

 

 


 

 

That's all ppl!

Thanks for reading my guide!!

See you soon!!

 

Credits: SoFaKi aka Kokkinoula

Thanks to Ass4s1n for the inspiration!!

Link to comment
Share on other sites

WoW Sofi!

You are sharing awesome things last days!

Sonthis is why i will give you a little reward!

+1karma from me...for all your good works!

Keep it like this please!

 

You are not allowed to give a karma reward, for a competition topic >_>

She will get rewarded in the end, if she deserves it!

Link to comment
Share on other sites

WoW Sofi!

You are sharing awesome things last days!

Sonthis is why i will give you a little reward!

+1karma from me...for all your good works!

Keep it like this please!

You are not allowed to get karma for your share. You will be rewarded in the end, if you win. (NOTE: If somebody rewards you with karma, please report it in order for us to fix it.)

 

Since it's for competision i have to smite her

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...