1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +00:00

Better event system

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-18 02:09:14 -03:00
parent be329a50e9
commit cd41122035
34 changed files with 660 additions and 186 deletions

View File

@@ -27,6 +27,10 @@ import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.configuration.ProxyConfigurationService;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.MySQLDatabaseService;
import com.l2jserver.service.game.CharacterService;
import com.l2jserver.service.game.CharacterServiceImpl;
import com.l2jserver.service.game.SpawnService;
import com.l2jserver.service.game.SpawnServiceImpl;
import com.l2jserver.service.game.chat.ChatService;
import com.l2jserver.service.game.chat.SimpleChatService;
import com.l2jserver.service.game.scripting.ScriptingService;
@@ -70,6 +74,10 @@ public class ServiceModule extends AbstractModule {
bind(ChatService.class).to(SimpleChatService.class)
.in(Scopes.SINGLETON);
bind(SpawnService.class).to(SpawnServiceImpl.class)
.in(Scopes.SINGLETON);
bind(CharacterService.class).to(CharacterServiceImpl.class).in(
Scopes.SINGLETON);
bind(WorldService.class).to(WorldServiceImpl.class)
.in(Scopes.SINGLETON);

View File

@@ -0,0 +1,43 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver 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.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.Service;
/**
* This service manages {@link L2Character} instances
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface CharacterService extends Service {
/**
* Perform all operations required to this character join the world
*
* @param character
* the character
*/
void enterWorld(L2Character character);
/**
* Perform all operations required to this character leave the world
*
* @param character
* the character
*/
void leaveWorld(L2Character character);
}

View File

@@ -0,0 +1,139 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver 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.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game;
import com.google.inject.Inject;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.client.CharacterChatMessagePacket.MessageDestination;
import com.l2jserver.game.net.packet.server.ActorChatMessagePacket;
import com.l2jserver.game.net.packet.server.GameGuardQueryPacket;
import com.l2jserver.game.net.packet.server.InventoryPacket;
import com.l2jserver.game.net.packet.server.UserInformationPacket;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.character.event.CharacterEnterWorldEvent;
import com.l2jserver.model.world.character.event.CharacterEvent;
import com.l2jserver.model.world.character.event.CharacterLeaveWorldEvent;
import com.l2jserver.model.world.character.event.CharacterListener;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.game.chat.ChatService;
import com.l2jserver.service.game.chat.channel.ChatChannel;
import com.l2jserver.service.game.chat.channel.ChatChannelListener;
import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.network.NetworkService;
/**
* Default implementation for {@link CharacterService}.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
@Depends({ WorldService.class, ChatService.class, NetworkService.class })
public class CharacterServiceImpl extends AbstractService implements
CharacterService {
/**
* The {@link WorldService}
*/
private final WorldService worldService;
/**
* The {@link WorldService} event dispatcher
*/
private final WorldEventDispatcher eventDispatcher;
/**
* The {@link ChatService}
*/
private final ChatService chatService;
/**
* The {@link NetworkService}
*/
private final NetworkService networkService;
/**
* The {@link SpawnService}
*/
private final SpawnService spawnService;
@Inject
public CharacterServiceImpl(WorldService worldService,
WorldEventDispatcher eventDispatcher, ChatService chatService,
NetworkService networkService, SpawnService spawnService) {
this.worldService = worldService;
this.eventDispatcher = eventDispatcher;
this.chatService = chatService;
this.networkService = networkService;
this.spawnService = spawnService;
}
@Override
public void enterWorld(final L2Character character) {
final CharacterID id = character.getID();
final Lineage2Connection conn = networkService.discover(id);
if (conn == null)
return;
if (!worldService.add(character))
// character is already in the world!
return;
// chat listener
final ChatChannelListener globalChatListener = new ChatChannelListener() {
@Override
public void onMessage(ChatChannel channel, CharacterID source,
String message) {
conn.write(new ActorChatMessagePacket(source.getObject(),
MessageDestination.ALL, message));
}
};
// leave world event
eventDispatcher.addListener(id, new CharacterListener() {
@Override
protected boolean dispatch(CharacterEvent e) {
if (!(e instanceof CharacterLeaveWorldEvent))
return true;
// remove chat listeners
chatService.getGlobalChannel().removeChatChannelListener(
globalChatListener);
return false;
}
});
// register global chat listener
chatService.getGlobalChannel().addChatChannelListener(
globalChatListener);
// send this user information
conn.write(new UserInformationPacket(character));
// TODO game guard enforcing
conn.write(new GameGuardQueryPacket());
conn.write(new InventoryPacket(character.getInventory()));
// dispatch enter world event
eventDispatcher.dispatch(new CharacterEnterWorldEvent(character));
// spawn the player -- this will also dispatch a spawn event
spawnService.spawn(character);
}
@Override
public void leaveWorld(L2Character character) {
if (!worldService.remove(character))
return;
eventDispatcher.dispatch(new CharacterLeaveWorldEvent(character));
}
}

View File

@@ -16,8 +16,12 @@
*/
package com.l2jserver.service.game;
import com.l2jserver.model.world.Player;
import com.l2jserver.model.world.capability.Spawnable;
import com.l2jserver.model.world.event.SpawnEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportEvent;
import com.l2jserver.service.Service;
import com.l2jserver.util.dimensional.Coordinate;
/**
* This service is responsible for spawning monsters, npcs and players.
@@ -27,12 +31,28 @@ import com.l2jserver.service.Service;
public interface SpawnService extends Service {
/**
* Spawns an object in the world
* <p>
* An {@link SpawnEvent} will be dispatched and the object will be
* registered in the world (if it isn't already)
*
* @param spawnable
* the spawnable object
*/
void spawn(Spawnable spawnable);
/**
* Teleports the object to the given <tt>point</tt>.
* <p>
* An {@link PlayerTeleportEvent} will be dispatched and the new position
* will be broadcast to all clients.
*
* @param player
* the player object
* @param coordinate
* the teleportation coordinate
*/
void teleport(Player player, Coordinate coordinate);
/**
* Schedules an {@link Spawnable} object to be respawn in a certain time.
*

View File

@@ -16,19 +16,62 @@
*/
package com.l2jserver.service.game;
import com.google.inject.Inject;
import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.packet.server.CharacterTeleportPacket;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.Player;
import com.l2jserver.model.world.capability.Spawnable;
import com.l2jserver.model.world.player.event.PlayerTeleportEvent;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.game.world.WorldService;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.network.NetworkService;
import com.l2jserver.util.dimensional.Coordinate;
/**
* Default implementation for {@link SpawnService}
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
@Depends({ WorldService.class })
public class SpawnServiceImpl extends AbstractService implements SpawnService {
/**
* The {@link WorldService} event dispatcher
*/
private final WorldEventDispatcher eventDispatcher;
/**
* The {@link NetworkService}
*/
private final NetworkService networkService;
@Inject
public SpawnServiceImpl(WorldEventDispatcher eventDispatcher,
NetworkService networkService) {
this.eventDispatcher = eventDispatcher;
this.networkService = networkService;
}
@Override
public void spawn(Spawnable spawnable) {
// TODO Auto-generated method stub
}
@Override
public void teleport(Player player, Coordinate coordinate) {
player.setPosition(coordinate);
if (player instanceof L2Character) {
final Lineage2Connection conn = networkService
.discover((CharacterID) player.getID());
if (conn == null)
return;
conn.write(new CharacterTeleportPacket(conn.getCharacter()));
}
// dispatch events
eventDispatcher.dispatch(new PlayerTeleportEvent(player, coordinate));
// TODO broadcast this player new position
}
@Override

View File

@@ -36,7 +36,7 @@ public interface WorldService extends Service, Iterable<WorldObject> {
* @param object
* the object
*/
public void add(WorldObject object);
boolean add(WorldObject object);
/**
* Removes an object of the world
@@ -44,7 +44,7 @@ public interface WorldService extends Service, Iterable<WorldObject> {
* @param object
* the object
*/
public void remove(WorldObject object);
boolean remove(WorldObject object);
/**
* Check if this object is in the world.
@@ -53,14 +53,14 @@ public interface WorldService extends Service, Iterable<WorldObject> {
* the object
* @return true if object exists
*/
public boolean contains(WorldObject object);
boolean contains(WorldObject object);
/**
* Get the event dispatcher
*
* @return the event dispatcher
*/
public WorldEventDispatcher getEventDispatcher();
WorldEventDispatcher getEventDispatcher();
/**
* Creates a list of all objects matching <tt>filter</tt>
@@ -71,7 +71,7 @@ public interface WorldService extends Service, Iterable<WorldObject> {
* the filter
* @return the list of objects
*/
public <T extends WorldObject> List<T> list(WorldObjectFilter<T> filter);
<T extends WorldObject> List<T> list(WorldObjectFilter<T> filter);
/**
* Creates a list of all objects of type <tt>type</tt>

View File

@@ -74,15 +74,15 @@ public class WorldServiceImpl extends AbstractService implements WorldService {
}
@Override
public void add(WorldObject object) {
public boolean add(WorldObject object) {
log.debug("Adding object {} to world", object);
objects.add(object);
return objects.add(object);
}
@Override
public void remove(WorldObject object) {
public boolean remove(WorldObject object) {
log.debug("Removing object {} from world", object);
objects.remove(object);
return objects.remove(object);
}
@Override

View File

@@ -48,8 +48,8 @@ public interface WorldEventDispatcher {
* @param listener
* the listener
*/
<E extends WorldEvent, L extends WorldListener<E>> void addListener(
Listenable<L, E> object, WorldListener<E> listener);
<E extends WorldEvent, L extends WorldListener> void addListener(
Listenable<L, E> object, WorldListener listener);
/**
* Adds a new <tt>listener</tt> to object with id <tt>id</tt>
@@ -63,8 +63,8 @@ public interface WorldEventDispatcher {
* @param listener
* the listener
*/
<E extends WorldEvent, L extends WorldListener<E>> void addListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener<E> listener);
<E extends WorldEvent, L extends WorldListener> void addListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener listener);
/**
* Removes an existing <tt>listener</tt> from <tt>object</tt>
@@ -78,8 +78,8 @@ public interface WorldEventDispatcher {
* @param listener
* the listener
*/
<E extends WorldEvent, L extends WorldListener<E>> void removeListener(
Listenable<L, E> object, WorldListener<E> listener);
<E extends WorldEvent, L extends WorldListener> void removeListener(
Listenable<L, E> object, WorldListener listener);
/**
* Removes an existing <tt>listener</tt> from the object with id <tt>id</tt>
@@ -93,6 +93,6 @@ public interface WorldEventDispatcher {
* @param listener
* the listener
*/
<E extends WorldEvent, L extends WorldListener<E>> void removeListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener<E> listener);
<E extends WorldEvent, L extends WorldListener> void removeListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener listener);
}

View File

@@ -87,46 +87,38 @@ public class WorldEventDispatcherImpl implements WorldEventDispatcher {
}
@Override
@SuppressWarnings("unchecked")
public <E extends WorldEvent, L extends WorldListener<E>> void addListener(
Listenable<L, E> object, WorldListener<E> listener) {
public <E extends WorldEvent, L extends WorldListener> void addListener(
Listenable<L, E> object, WorldListener listener) {
log.debug("Adding new listener {} to {}", listener, object.getID());
listeners.add(new ListenerIDPair(object.getID(),
(WorldListener<WorldEvent>) listener));
listeners.add(new ListenerIDPair(object.getID(), listener));
}
@Override
@SuppressWarnings("unchecked")
public <E extends WorldEvent, L extends WorldListener<E>> void addListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener<E> listener) {
public <E extends WorldEvent, L extends WorldListener> void addListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener listener) {
log.debug("Adding new listener {} to {}", listener, id);
listeners.add(new ListenerIDPair(id,
(WorldListener<WorldEvent>) listener));
listeners.add(new ListenerIDPair(id, listener));
}
@Override
@SuppressWarnings("unchecked")
public <E extends WorldEvent, L extends WorldListener<E>> void removeListener(
Listenable<L, E> object, WorldListener<E> listener) {
public <E extends WorldEvent, L extends WorldListener> void removeListener(
Listenable<L, E> object, WorldListener listener) {
log.debug("Removing new listener {} from {}", listener, object.getID());
listeners.remove(new ListenerIDPair(object.getID(),
(WorldListener<WorldEvent>) listener));
listeners.remove(new ListenerIDPair(object.getID(), listener));
}
@Override
@SuppressWarnings("unchecked")
public <E extends WorldEvent, L extends WorldListener<E>> void removeListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener<E> listener) {
public <E extends WorldEvent, L extends WorldListener> void removeListener(
ObjectID<? extends Listenable<L, E>> id, WorldListener listener) {
log.debug("Removing new listener {} from {}", listener, id);
listeners.remove(new ListenerIDPair(id,
(WorldListener<WorldEvent>) listener));
listeners.remove(new ListenerIDPair(id, listener));
}
private class ListenerIDPair {
private ObjectID<?> ID;
private WorldListener<WorldEvent> listener;
private WorldListener listener;
public ListenerIDPair(ObjectID<?> ID, WorldListener<WorldEvent> listener) {
public ListenerIDPair(ObjectID<?> ID, WorldListener listener) {
super();
this.ID = ID;
this.listener = listener;

View File

@@ -24,7 +24,7 @@ package com.l2jserver.service.game.world.event;
* @param <E>
* the received event type
*/
public interface WorldListener<E extends WorldEvent> {
public interface WorldListener {
/**
* Once the event call is dispatched the listener <b>WILL</b> be removed if
* false is returned. If you wish to keep this listener, you must return
@@ -34,5 +34,5 @@ public interface WorldListener<E extends WorldEvent> {
* the event
* @return true to keep listener alive
*/
boolean dispatch(E e);
boolean dispatch(WorldEvent e);
}

View File

@@ -0,0 +1,32 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver 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.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.service.game.world.filter.AndFilter;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CharacterBroadcastFilter extends AndFilter<L2Character> {
@SuppressWarnings({ "unchecked", "rawtypes" })
public CharacterBroadcastFilter(L2Character character) {
super(new InstanceFilter<L2Character>(L2Character.class),
(WorldObjectFilter) new KnownListFilter(character));
}
}

View File

@@ -0,0 +1,35 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver 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.
*
* l2jserver 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 l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.capability.Positionable;
import com.l2jserver.service.game.world.filter.AndFilter;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class KnownListFilter extends AndFilter<Positionable> {
public static final int KNOWNLIST_RANGE = 200;
@SuppressWarnings("unchecked")
public KnownListFilter(L2Character character) {
super(new InstanceFilter<Positionable>(Positionable.class),
new RangeFilter(character.getPosition(), KNOWNLIST_RANGE));
}
}

View File

@@ -49,6 +49,11 @@ import com.l2jserver.util.factory.CollectionFactory;
WorldService.class })
public class NettyNetworkService extends AbstractService implements
NetworkService {
/**
* The {@link WorldService} instance
*/
private final WorldService worldService;
/**
* The network configuration object
*/
@@ -74,9 +79,10 @@ public class NettyNetworkService extends AbstractService implements
@Inject
public NettyNetworkService(ConfigurationService configService,
Injector injector) {
Injector injector, WorldService worldService) {
this.config = configService.get(NetworkConfiguration.class);
this.injector = injector;
this.worldService = worldService;
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
}
@@ -85,7 +91,8 @@ public class NettyNetworkService extends AbstractService implements
server = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
server.setPipelineFactory(new Lineage2PipelineFactory(injector, this));
server.setPipelineFactory(new Lineage2PipelineFactory(injector, this,
worldService));
channel = (ServerChannel) server.bind(config.getListenAddress());
}