Sylian
StaffStaff
LEVEL 26
121 XP
Introduction
Hey everyone!
This is my first post related to L2, and I am still learning as I go along. I started 1 day ago from this post, and I just achieved my first goal, which was learning how to add a new event to the source code, so I thought I would like to share it.
Goal
1. Create a custom event that triggers when a creature drops an item. This event should contain both the player who earned the loot, and the item dropped.
2. Create a custom .java file inside "\game\data\scripts\custom" for testing our new event.
3. Have fun while doing it.
Creating the event
First, inside Eclipse, navigate to "java/org.l2jmobius.gameserver.model.events.impl.creature.npc", here we will create the code for our event, so right click on "org.l2jmobius.gameserver.model.events.impl.creature.npc", now click "New - Class".
Call it "OnNpcDropLoot", and click "Finish".
Next, open the file we just created and either copy this code or try to write it yourself (Just remember to replace the author with your own name!).
Java:
/*
* 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 org.l2jmobius.gameserver.model.events.impl.creature.npc;
import org.l2jmobius.gameserver.model.actor.Creature;
import org.l2jmobius.gameserver.model.events.EventType;
import org.l2jmobius.gameserver.model.events.impl.IBaseEvent;
import org.l2jmobius.gameserver.model.item.instance.Item;
/**
* @author {YOUR NAME}
*/
public class OnNpcDropLoot implements IBaseEvent
{
private final Creature _lootOwner;
private final Item _loot;
public OnNpcDropLoot(Creature lootOwner, Item loot)
{
_lootOwner = lootOwner;
_loot = loot;
}
public Creature getLootOwner()
{
return _lootOwner;
}
public Item getLoot()
{
return _loot;
}
@Override
public EventType getType()
{
return EventType.ON_NPC_DROP_LOOT;
}
}
Next, let's open the "EventType.java", which can be located here "java/org.l2jmobius.gameserver.model.events", next open the file called "EventType.java" and scroll down to the "NPC events".
At the bottom of the NPC events, add
Java:
ON_NPC_DROP_LOOT(OnNpcDropLoot.class, void.class),
We are now done with creating the event, let's move on to implementing our event so it gets called.
Implementing our custom event
Navigate to "java/org.l2jmobius.gameserver.model.actor", now open the file called "Npc.java", after you opened this file, you should go to the function called "dropItem", it will look like the image below.
Depending on what you want the event to be used for, you could move the next code around, but for this tutorial/guide purpose, I will add it right before the code that returns the item.
Go to the line above
Java:
return item;
Code:
// Code checks if anything is listning to our custom "ON_NPC_DROP_LOOT" event.
if (hasListener(EventType.ON_NPC_DROP_LOOT))
{
// Notify the listeners about the event being fired, and send the player (creature) and the item with it.
EventDispatcher.getInstance().notifyEvent(new OnNpcDropLoot(creature, item), this);
}
You just implemented your first custom event into the server source code, congratulations!
Testing our custom event
The fastest and easiest way to test our new event would be to create a new custom ".java" file inside the "scripts" folder, this can be done after building/compiling the server code, but it can also be done inside the server project, so to make it easy for ourselves, we will just create it inside the project.
Navigate to "/dist/game/data/scripts/", now create a new java package called "custom.TestingCustomEvent". Right-click on our new package "custom.TestingCustomEvent" and create a new class, same way as we did before when creating the custom event, and name the new file "TestingEvent"
Now, put this code inside your newly created file
Java:
/*
* Copyright (c) 2013 L2jMobius
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package custom.TestingCustomEvent;
import org.l2jmobius.gameserver.model.events.Containers;
import org.l2jmobius.gameserver.model.events.EventType;
import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcDropLoot;
import org.l2jmobius.gameserver.model.events.listeners.ConsumerEventListener;
/**
* @author {YOUR NAME}
*/
public class TestingEvent
{
// Constructor.
public TestingEvent()
{
// Makes sure we listen to the event.
Containers.Monsters().addListener(new ConsumerEventListener(Containers.Monsters(), EventType.ON_NPC_DROP_LOOT, (OnNpcDropLoot event) -> onNpcDropLoot(event), this));
}
// Function which gets called when our event is triggered.
private void onNpcDropLoot(OnNpcDropLoot event)
{
// Simple debugging line, feel free to remove it.
System.out.println("Inside the drop event.");
// Player sends a message in game chat saying "Item dropped! {ItemName}"
event.getLootOwner().asPlayer().sendMessage("Item dropped! " + event.getLoot().getItemName());
}
// Make sure to always have this part, otherwise the server wont load your custom script on start-up!
public static void main(String[] args)
{
// Creates a new instance of this class.
new TestingEvent();
}
}
Congratulations, you are now done, go ahead and test it in-game, you should see it saying "Item dropped: {Name of item that dropped.}".
The End
Now that you know how to implement custom events, try to create a new one yourself!
Liked By 1 member :