1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-10 09:22:49 +00:00

Character broadcast working

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-22 21:32:55 -03:00
parent 49a8513ec5
commit 98feb1ecce
148 changed files with 2309 additions and 597 deletions

View File

@@ -57,6 +57,10 @@ public class ServiceManager {
}
logger = LoggerFactory.getLogger(ServiceManager.class);
}
public <T extends Service> T get(Class<T> serviceClass) {
return injector.getInstance(serviceClass);
}
public <T extends Service> T start(Class<T> serviceClass)
throws ServiceStartException {

View File

@@ -54,9 +54,10 @@ public class Log4JLoggingService extends AbstractService implements
rootLogger.setLevel(Level.WARN);
rootLogger.addAppender(new ConsoleAppender(layout, "System.err"));
l2jLogger.setLevel(Level.DEBUG);
scriptLogger.setLevel(Level.DEBUG);
l2jLogger.setLevel(Level.INFO);
scriptLogger.setLevel(Level.INFO);
nettyLogger.setLevel(Level.DEBUG);
Logger.getLogger("com.l2jserver.model.id.object.allocator").setLevel(Level.WARN);
}
@Override

View File

@@ -329,7 +329,9 @@ public class MySQLDatabaseService extends AbstractService implements
final List<T> list = CollectionFactory.newList();
final ResultSet rs = st.getResultSet();
while (rs.next()) {
list.add(mapper().map(rs));
final T obj = mapper().map(rs);
if (obj != null)
list.add(obj);
}
return list;
}

View File

@@ -23,6 +23,7 @@ import com.l2jserver.game.net.Lineage2Connection;
import com.l2jserver.game.net.SystemMessage;
import com.l2jserver.game.net.packet.server.ActorChatMessagePacket;
import com.l2jserver.game.net.packet.server.ActorMovementPacket;
import com.l2jserver.game.net.packet.server.CharacterInformationBroadcastPacket;
import com.l2jserver.game.net.packet.server.CharacterInformationExtraPacket;
import com.l2jserver.game.net.packet.server.CharacterInformationPacket;
import com.l2jserver.game.net.packet.server.CharacterInventoryPacket;
@@ -30,6 +31,7 @@ import com.l2jserver.game.net.packet.server.CharacterMovementTypePacket;
import com.l2jserver.game.net.packet.server.CharacterTargetSelectedPacket;
import com.l2jserver.game.net.packet.server.GameGuardQueryPacket;
import com.l2jserver.game.net.packet.server.NPCInformationPacket;
import com.l2jserver.game.net.packet.server.ObjectRemove;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.Actor;
@@ -50,6 +52,7 @@ import com.l2jserver.model.world.character.event.CharacterTargetSelectedEvent;
import com.l2jserver.model.world.character.event.CharacterWalkingEvent;
import com.l2jserver.model.world.npc.event.NPCSpawnEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportedEvent;
import com.l2jserver.model.world.player.event.PlayerTeleportingEvent;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.game.chat.ChatChannel;
@@ -65,7 +68,9 @@ import com.l2jserver.service.game.world.event.FilteredWorldListener;
import com.l2jserver.service.game.world.event.WorldEvent;
import com.l2jserver.service.game.world.event.WorldEventDispatcher;
import com.l2jserver.service.game.world.event.WorldListener;
import com.l2jserver.service.game.world.filter.impl.IDFilter;
import com.l2jserver.service.game.world.filter.impl.KnownListFilter;
import com.l2jserver.service.game.world.filter.impl.KnownListUpdateFilter;
import com.l2jserver.service.network.NetworkService;
import com.l2jserver.util.dimensional.Coordinate;
import com.l2jserver.util.dimensional.Point;
@@ -133,7 +138,7 @@ public class CharacterServiceImpl extends AbstractService implements
return;
itemDao.loadInventory(character);
character.setOnline(true);
// chat listener
@@ -158,9 +163,10 @@ public class CharacterServiceImpl extends AbstractService implements
// this listener will be filtered so that only interesting events are
// dispatched, once a event arrives will be possible to check check if
// the given event will be broadcasted or not
// TODO this should not be here, it should be i world service or a newly
// TODO this should not be here, it should be in world service or a
// newly
// created broadcast service.
final WorldListener broadcastListener = new FilteredWorldListener<PositionableObject>(
final WorldListener neighboorListener = new FilteredWorldListener<PositionableObject>(
new KnownListFilter(character)) {
@Override
protected boolean dispatch(WorldEvent e, PositionableObject object) {
@@ -168,29 +174,64 @@ public class CharacterServiceImpl extends AbstractService implements
conn.write(new NPCInformationPacket((NPC) object));
} else if (e instanceof CharacterMoveEvent) {
final CharacterMoveEvent evt = (CharacterMoveEvent) e;
if (object.equals(character))
return true;
conn.write(new ActorMovementPacket((L2Character) object,
evt.getPoint().getCoordinate()));
} else if (e instanceof PlayerTeleportedEvent
|| e instanceof CharacterEnterWorldEvent) {
// TODO this should not be here!
for (final PositionableObject o : worldService
.iterable(new KnownListFilter(character))) {
if (o instanceof NPC) {
conn.write(new NPCInformationPacket((NPC) o));
}
if (object instanceof NPC) {
conn.write(new NPCInformationPacket((NPC) object));
} else if (object instanceof L2Character) {
conn.write(new CharacterInformationBroadcastPacket(
(L2Character) object));
}
} else if (e instanceof CharacterWalkingEvent
|| e instanceof CharacterRunningEvent) {
} else if (e instanceof PlayerTeleportingEvent
|| e instanceof CharacterLeaveWorldEvent) {
// object is not out of sight
conn.write(new ObjectRemove(object));
} else if (e instanceof CharacterWalkingEvent) {
conn.write(new CharacterMovementTypePacket(
((CharacterWalkingEvent) e).getCharacter()));
} else if (e instanceof CharacterRunningEvent) {
conn.write(new CharacterMovementTypePacket(
((CharacterRunningEvent) e).getCharacter()));
}
// keep listener alive
return true;
}
};
eventDispatcher.addListener(broadcastListener);
eventDispatcher.addListener(neighboorListener);
final WorldListener sendPacketListener = new FilteredWorldListener<WorldObject>(
new IDFilter(character.getID())) {
@Override
protected boolean dispatch(WorldEvent e, WorldObject object) {
if (e instanceof CharacterMoveEvent) {
final CharacterMoveEvent evt = (CharacterMoveEvent) e;
// process update known list
for (final WorldObject o : worldService
.iterable(new KnownListUpdateFilter(character, evt
.getPoint()))) {
if (o instanceof NPC) {
conn.write(new NPCInformationPacket((NPC) o));
} else if (o instanceof L2Character) {
conn.write(new CharacterInformationBroadcastPacket(
(L2Character) o));
}
}
} else if (e instanceof PlayerTeleportedEvent
|| e instanceof CharacterEnterWorldEvent) {
broadcast(conn, character);
} else if (e instanceof CharacterWalkingEvent) {
conn.write(new CharacterMovementTypePacket(
((CharacterWalkingEvent) e).getCharacter()));
} else if (e instanceof CharacterRunningEvent) {
conn.write(new CharacterMovementTypePacket(
((CharacterRunningEvent) e).getCharacter()));
}
// keep listener alive
return true;
}
};
eventDispatcher.addListener(sendPacketListener);
// leave world event
eventDispatcher.addListener(id, new CharacterListener() {
@@ -206,7 +247,8 @@ public class CharacterServiceImpl extends AbstractService implements
tradeChatListener);
// remove broadcast listener
eventDispatcher.removeListener(broadcastListener);
eventDispatcher.removeListener(neighboorListener);
eventDispatcher.removeListener(sendPacketListener);
// we can kill this listener now
return false;
@@ -236,22 +278,29 @@ public class CharacterServiceImpl extends AbstractService implements
// we can ignore this one
}
// broadcast knownlist -- trashy implementation
// TODO should be moved to world service or a newly created broadcast
// service, whichever fits the purpose
for (final WorldObject o : worldService.iterable(new KnownListFilter(
character))) {
if (o instanceof NPC) {
conn.write(new NPCInformationPacket((NPC) o));
}
}
// dispatch enter world event
eventDispatcher.dispatch(new CharacterEnterWorldEvent(character));
broadcast(conn, character);
// spawn the player -- this will also dispatch a spawn event
// here the object is registered in the world
spawnService.spawn(character, null);
// dispatch enter world event
eventDispatcher.dispatch(new CharacterEnterWorldEvent(character));
}
// broadcast knownlist -- trashy implementation
// TODO should be moved to world service or a newly created broadcast
// service, whichever fits the purpose
private void broadcast(Lineage2Connection conn, L2Character character) {
for (final WorldObject o : worldService.iterable(new KnownListFilter(
character))) {
if (o instanceof NPC) {
conn.write(new NPCInformationPacket((NPC) o));
} else if (o instanceof L2Character) {
conn.write(new CharacterInformationBroadcastPacket(
(L2Character) o));
}
}
}
@Override
@@ -303,7 +352,8 @@ public class CharacterServiceImpl extends AbstractService implements
@Override
public void attack(L2Character character, Actor target)
throws CannotSetTargetServiceException, ActorIsNotAttackableServiceException {
throws CannotSetTargetServiceException,
ActorIsNotAttackableServiceException {
Preconditions.checkNotNull(character, "character");
Preconditions.checkNotNull(target, "target");
final CharacterID id = character.getID();
@@ -388,9 +438,10 @@ public class CharacterServiceImpl extends AbstractService implements
// ignore while teleporting, for some reason the client sends a
// validation just before teleport packet
return;
final Point old = character.getPoint();
character.setPoint(point);
character.setState(CharacterState.MOVING);
eventDispatcher.dispatch(new CharacterMoveEvent(character, point));
eventDispatcher.dispatch(new CharacterMoveEvent(character, old));
}
@Override

View File

@@ -16,11 +16,15 @@
*/
package com.l2jserver.service.game.npc;
import java.util.List;
import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.NPC;
import com.l2jserver.service.Service;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException;
/**
* This service manages {@link NPC} instances
@@ -56,6 +60,17 @@ public interface NPCService extends Service {
void action(NPC npc, L2Character character, String... args)
throws ActionServiceException;
/**
* Load from database and spawn all {@link NPC NPCs} instances
*
* @throws AlreadySpawnedServiceException
* if one of the NPCs is already spawned
* @throws SpawnPointNotFoundServiceException
* if one of the NPCs does not have an spawn point defined
*/
List<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException;
/**
* Attacks an given NPC, if possible.
*

View File

@@ -16,12 +16,19 @@
*/
package com.l2jserver.service.game.npc;
import java.util.List;
import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.l2jserver.db.dao.NPCDAO;
import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction;
import com.l2jserver.model.template.NPCTemplate;
import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.NPC;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException;
import com.l2jserver.service.game.spawn.SpawnService;
import com.l2jserver.util.exception.L2Exception;
/**
@@ -30,6 +37,22 @@ import com.l2jserver.util.exception.L2Exception;
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class NPCServiceImpl extends AbstractService implements NPCService {
/**
* The {@link SpawnService} used to spawn the {@link NPC} instances
*/
private final SpawnService spawnService;
/**
* The {@link NPCDAO}
*/
private final NPCDAO npcDao;
@Inject
public NPCServiceImpl(SpawnService spawnService, NPCDAO npcDao) {
this.spawnService = spawnService;
this.npcDao = npcDao;
}
@Override
public void action(NPC npc, L2Character character, CharacterAction action)
throws ActionServiceException {
@@ -61,6 +84,16 @@ public class NPCServiceImpl extends AbstractService implements NPCService {
}
}
@Override
public List<NPC> spawnAll() throws SpawnPointNotFoundServiceException,
AlreadySpawnedServiceException {
final List<NPC> npcs = npcDao.loadAll();
for (final NPC npc : npcs) {
spawnService.spawn(npc, null);
}
return null;
}
@Override
public void attack(NPC npc, L2Character attacker)
throws NotAttackableNPCServiceException {

View File

@@ -27,6 +27,7 @@ import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.l2jserver.db.dao.CharacterDAO;
import com.l2jserver.db.dao.ItemDAO;
import com.l2jserver.db.dao.NPCDAO;
import com.l2jserver.model.id.ID;
import com.l2jserver.model.id.ObjectID;
import com.l2jserver.model.id.object.allocator.IDAllocator;
@@ -64,6 +65,10 @@ public class CachedWorldIDService extends AbstractService implements
* The {@link ItemDAO}
*/
private final ItemDAO itemDao;
/**
* The {@link NPCDAO}
*/
private final NPCDAO npcDao;
/**
* The ID cache
@@ -77,11 +82,12 @@ public class CachedWorldIDService extends AbstractService implements
@Inject
public CachedWorldIDService(CacheService cacheService,
IDAllocator allocator, CharacterDAO characterDao, ItemDAO itemDao) {
IDAllocator allocator, CharacterDAO characterDao, ItemDAO itemDao, NPCDAO npcDao) {
this.cacheService = cacheService;
this.allocator = allocator;
this.characterDao = characterDao;
this.itemDao = itemDao;
this.npcDao = npcDao;
}
@Override
@@ -99,6 +105,7 @@ public class CachedWorldIDService extends AbstractService implements
public void load() {
load(characterDao.listIDs());
load(itemDao.listIDs());
//load(npcDao.listIDs());
loaded = true;
}

View File

@@ -39,7 +39,7 @@ public abstract class FilteredWorldListener<T extends WorldObject> implements
@SuppressWarnings("unchecked")
public boolean dispatch(WorldEvent e) {
if (!filter.accept((T) e.getObject()))
return false;
return true;
return dispatch(e, (T) e.getObject());
}

View File

@@ -21,6 +21,7 @@ import java.util.Queue;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -63,32 +64,35 @@ public class WorldEventDispatcherImpl implements WorldEventDispatcher {
/**
* The events pending dispatch
*/
private Queue<EventContainer> events = CollectionFactory
.newConcurrentQueue();
private Queue<EventContainer> events = new ArrayBlockingQueue<EventContainer>(
100 * 1000);
public void start() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
final TimerTask task = new TimerTask() {
@Override
public void run() {
final EventContainer event = events.poll();
if (event == null)
return;
try {
// set state
event.future.running = true;
event.future.complete = false;
EventContainer event;
while ((event = events.poll()) != null) {
try {
log.debug("Dispatching event {}", event.event);
// dispatch
if (doDispatch(event))
// the set will update state
event.future.set(event.event);
} catch (Throwable t) {
event.future.setException(t);
log.warn("Exception in WorldEventDispatcher thread", t);
// set state
event.future.running = true;
event.future.complete = false;
// dispatch
if (doDispatch(event))
// the set will update state
event.future.set(event.event);
} catch (Throwable t) {
event.future.setException(t);
log.warn("Exception in WorldEventDispatcher thread", t);
}
}
}
}, 0, 50);
};
timer.scheduleAtFixedRate(task, 0, 50);
}
@Override
@@ -97,6 +101,9 @@ public class WorldEventDispatcherImpl implements WorldEventDispatcher {
log.debug("Queing dispatch for event {}", event);
final WorldEventFutureImpl<E> future = new WorldEventFutureImpl<E>();
events.add(new EventContainer(event, future));
// final WorldEventFutureImpl<E> future = new WorldEventFutureImpl<E>();
// final EventContainer c = new EventContainer(event, future);
// doDispatch(c);
return future;
}
@@ -107,12 +114,24 @@ public class WorldEventDispatcherImpl implements WorldEventDispatcher {
* the event
* @return true if dispatch was not canceled
*/
public boolean doDispatch(EventContainer event) {
log.debug("Dispatching event {}", event);
public synchronized boolean doDispatch(EventContainer event) {
final ObjectID<?>[] objects = event.event.getDispatchableObjects();
for (ObjectID<?> obj : objects) {
if (obj == null)
continue;
for (final WorldListener listener : globalListeners) {
if (event.future.isCancelled())
return false;
try {
if (!listener.dispatch(event.event))
// remove listener if return value is false
globalListeners.remove(listener);
} catch (Throwable t) {
log.warn("Exception in listener", t);
// always remove any listener that throws an exception
globalListeners.remove(listener);
}
}
final Set<WorldListener> listeners = getListeners(obj);
for (final WorldListener listener : listeners) {
if (event.future.isCancelled())
@@ -200,10 +219,7 @@ public class WorldEventDispatcherImpl implements WorldEventDispatcher {
set = CollectionFactory.newSet();
listeners.put(id, set);
}
final Set<WorldListener> union = CollectionFactory.newSet();
union.addAll(set);
union.addAll(globalListeners);
return union;
return set;
}
public void stop() {

View File

@@ -60,6 +60,6 @@ public class ExcludeFilter<O extends WorldObject> implements
@Override
public boolean accept(O object) {
return objects.contains(object);
return !objects.contains(object);
}
}

View File

@@ -19,6 +19,7 @@ package com.l2jserver.service.game.world.filter.impl;
import com.l2jserver.model.world.PositionableObject;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.service.game.world.filter.AndFilter;
import com.l2jserver.service.game.world.filter.ExcludeFilter;
/**
* This filter will only accept {@link WorldObject} which are in vision of
@@ -32,6 +33,7 @@ public class KnownListFilter extends AndFilter<PositionableObject> {
@SuppressWarnings("unchecked")
public KnownListFilter(PositionableObject object) {
super(new InstanceFilter<PositionableObject>(PositionableObject.class),
new RangeFilter(object, KNOWNLIST_RANGE));
new RangeFilter(object, KNOWNLIST_RANGE),
new ExcludeFilter<PositionableObject>(object));
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.PositionableObject;
import com.l2jserver.model.world.WorldObject;
import com.l2jserver.service.game.world.filter.AndFilter;
import com.l2jserver.service.game.world.filter.NotFilter;
import com.l2jserver.util.dimensional.Point;
/**
* This filter will only accept {@link WorldObject} which are in vision of
* another object.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class KnownListUpdateFilter extends AndFilter<PositionableObject> {
public static final int KNOWNLIST_RANGE = 2000;
@SuppressWarnings("unchecked")
public KnownListUpdateFilter(PositionableObject object, Point old) {
super(new KnownListFilter(object), new NotFilter<PositionableObject>(
new RangePointFilter(old, KNOWNLIST_RANGE)));
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.google.common.base.Preconditions;
import com.l2jserver.model.world.PositionableObject;
import com.l2jserver.service.game.world.filter.WorldObjectFilter;
import com.l2jserver.util.dimensional.Point;
/**
* Filter objects that are in the <tt>range</tt> of <tt>coordinate</tt>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class RangePointFilter implements WorldObjectFilter<PositionableObject> {
/**
* The coordinate point
*/
private final Point point;
/**
* The desired maximum distance of the object
*/
private final int range;
/**
* Creates a new instance
*
* @param objcect
* the positionable object as center point for range search
* @param range
* the desired maximum distance of the object
*/
public RangePointFilter(final Point point, final int range) {
Preconditions.checkNotNull(point, "point");
Preconditions.checkState(range >= 0, "range < 0");
this.point = point;
this.range = range;
}
@Override
public boolean accept(PositionableObject other) {
if (other == null)
return false;
return other.getPosition().getDistance(point.getCoordinate()) <= range;
}
}