From 73f51e53c009789f18a8189b06f443126a70b86d Mon Sep 17 00:00:00 2001 From: Rogiel Date: Fri, 27 May 2011 02:49:15 -0300 Subject: [PATCH] Base AI, new Cache system, DAO changes and better geometry handling Signed-off-by: Rogiel --- .classpath | 2 - data/.gitignore | 1 + data/script/ai/script/AIInterest.java | 63 --- data/script/ai/script/AIScriptFactory.java | 52 --- data/script/ai/script/ai/CharacterAI.java | 128 ------ .../dao => com/l2jserver/db}/H2DAOModule.java | 7 +- .../l2jserver/db}/MySQL5DAOModule.java | 7 +- .../l2jserver/db/dao/h2/H2CharacterDAO.java | 41 ++ .../db/dao/h2/H2CharacterFriendDAO.java} | 32 +- .../com/l2jserver/db/dao/h2/H2ClanDAO.java | 36 ++ .../com/l2jserver/db/dao/h2/H2ItemDAO.java | 37 ++ src/dao/com/l2jserver/db/dao/h2/H2NPCDAO.java | 36 ++ .../db/dao/jdbc/AbstractJDBCDAO.java} | 8 +- .../db/dao/jdbc/JDBCCharacterDAO.java} | 14 +- .../db/dao/jdbc/JDBCCharacterFriendDAO.java} | 14 +- .../l2jserver/db/dao/jdbc/JDBCClanDAO.java} | 8 +- .../l2jserver/db/dao/jdbc/JDBCItemDAO.java} | 12 +- .../l2jserver/db/dao/jdbc/JDBCNPCDAO.java} | 12 +- .../db/dao/mysql5/MySQL5CharacterDAO.java | 41 ++ .../dao/mysql5/MySQL5CharacterFriendDAO.java | 36 ++ .../db/dao/mysql5/MySQL5ClanDAO.java | 36 ++ .../db/dao/mysql5/MySQL5ItemDAO.java | 37 ++ .../l2jserver/db/dao/mysql5/MySQL5NPCDAO.java | 36 ++ .../db/dao/h2/H2CharacterFriendDAO.java | 221 --------- .../h2/com/l2jserver/db/dao/h2/H2ClanDAO.java | 170 ------- .../h2/com/l2jserver/db/dao/h2/H2NPCDAO.java | 265 ----------- .../db/dao/mysql5/MySQL5CharacterDAO.java | 420 ------------------ .../db/dao/mysql5/MySQL5ItemDAO.java | 223 ---------- .../java/com/l2jserver/GameServerModule.java | 2 +- src/main/java/com/l2jserver/Installer.java | 40 ++ .../com/l2jserver/game/ProtocolVersion.java | 12 +- src/main/java/com/l2jserver/game/ai/AI.java | 39 ++ .../game/ai/desires/AbstractDesire.java | 74 +++ .../game/ai/desires/AttackDesire.java | 76 ++++ .../com/l2jserver/game/ai/desires/Desire.java | 93 ++++ .../game/ai/desires/DesireIteratorFilter.java | 45 ++ .../ai/desires/DesireIteratorHandler.java | 55 +++ .../game/ai/desires/DesireQueue.java | 212 +++++++++ .../l2jserver/game/ai/desires/MoveDesire.java | 83 ++++ .../net/packet/client/AdminCommandPacket.java | 2 +- .../packet/client/CharacterActionPacket.java | 2 +- .../client/CharacterAttackRequestPacket.java | 2 +- .../client/CharacterRequestMovePacket.java | 2 +- .../CharacterValidatePositionPacket.java | 6 +- .../packet/server/ActorMovementPacket.java | 2 +- .../server/CharacterTeleportPacket.java | 6 +- .../model/template/TeleportationTemplate.java | 2 +- .../l2jserver/model/world/L2Character.java | 8 +- .../model/world/PositionableObject.java | 12 +- .../world/actor/event/ActorSpawnEvent.java | 8 +- .../character/event/CharacterMoveEvent.java | 8 +- .../event/CharacterStopMoveEvent.java | 8 +- .../model/world/event/SpawnEvent.java | 4 +- .../model/world/npc/event/NPCSpawnEvent.java | 4 +- .../world/player/event/PlayerSpawnEvent.java | 4 +- .../player/event/PlayerTeleportedEvent.java | 4 +- .../player/event/PlayerTeleportingEvent.java | 8 +- .../service/cache/AbstractReferenceCache.java | 113 +++++ .../com/l2jserver/service/cache/Cache.java | 64 +++ .../l2jserver/service/cache/CacheService.java | 54 ++- .../service/cache/EhCacheService.java | 162 ++++--- .../l2jserver/service/cache/EternalCache.java | 87 ++++ .../service/cache/MethodInvocation.java | 69 +++ .../service/cache/SoftCacheService.java | 164 +++++++ .../service/cache/WeakCacheService.java | 164 +++++++ .../service/database/JDBCDatabaseService.java | 28 +- .../l2jserver/service/game/ai/AIService.java | 2 +- .../service/game/ai/AIServiceImpl.java | 2 +- .../game/ai/script/WalkingAIScript.java | 2 +- .../game/character/CharacterService.java | 8 +- .../game/character/CharacterServiceImpl.java | 10 +- .../game/pathing/MapperPathingService.java | 6 +- .../service/game/spawn/SpawnService.java | 6 +- .../service/game/spawn/SpawnServiceImpl.java | 6 +- .../game/template/ScriptTemplateService.java | 5 - .../game/template/TemplateService.java | 5 - .../game/template/XMLTemplateService.java | 27 +- .../game/world/CachedWorldIDService.java | 28 +- .../filter/impl/KnownListUpdateFilter.java | 4 +- .../world/filter/impl/RangePointFilter.java | 6 +- .../java/com/l2jserver/util/MathUtil.java | 211 +++++++++ .../util/factory/CollectionFactory.java | 10 + .../l2jserver/util/geometry/AbstractArea.java | 125 ++++++ .../com/l2jserver/util/geometry/Area.java | 196 ++++++++ .../{dimensional => geometry}/Coordinate.java | 6 +- .../l2jserver/util/geometry/CylinderArea.java | 122 +++++ .../com/l2jserver/util/geometry/Point.java | 31 +- .../Point.java => geometry/Point3D.java} | 42 +- .../l2jserver/util/geometry/PolygonArea.java | 155 +++++++ .../util/geometry/RectangleArea.java | 177 ++++++++ .../util/jaxb/CoordinateAdapter.java | 2 +- .../model/id/factory/IDFactoryTest.java | 2 +- .../template/StaticTemplateServiceTest.java | 2 +- .../model/template/NPCTemplateConverter.java | 2 +- test.xsd | 40 -- 95 files changed, 3079 insertions(+), 1884 deletions(-) delete mode 100644 data/script/ai/script/AIInterest.java delete mode 100644 data/script/ai/script/AIScriptFactory.java delete mode 100644 data/script/ai/script/ai/CharacterAI.java rename src/dao/{h2/com/l2jserver/db/dao => com/l2jserver/db}/H2DAOModule.java (87%) rename src/dao/{mysql5/com/l2jserver/db/dao => com/l2jserver/db}/MySQL5DAOModule.java (88%) create mode 100644 src/dao/com/l2jserver/db/dao/h2/H2CharacterDAO.java rename src/dao/{h2/com/l2jserver/db/dao/h2/AbstractH2DAO.java => com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java} (55%) create mode 100644 src/dao/com/l2jserver/db/dao/h2/H2ClanDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/h2/H2ItemDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/h2/H2NPCDAO.java rename src/dao/{mysql5/com/l2jserver/db/dao/mysql5/AbstractMySQL5DAO.java => com/l2jserver/db/dao/jdbc/AbstractJDBCDAO.java} (85%) rename src/dao/{h2/com/l2jserver/db/dao/h2/H2CharacterDAO.java => com/l2jserver/db/dao/jdbc/JDBCCharacterDAO.java} (97%) rename src/dao/{mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java => com/l2jserver/db/dao/jdbc/JDBCCharacterFriendDAO.java} (93%) rename src/dao/{mysql5/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java => com/l2jserver/db/dao/jdbc/JDBCClanDAO.java} (95%) rename src/dao/{h2/com/l2jserver/db/dao/h2/H2ItemDAO.java => com/l2jserver/db/dao/jdbc/JDBCItemDAO.java} (95%) rename src/dao/{mysql5/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java => com/l2jserver/db/dao/jdbc/JDBCNPCDAO.java} (95%) create mode 100644 src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java create mode 100644 src/dao/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java delete mode 100644 src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java delete mode 100644 src/dao/h2/com/l2jserver/db/dao/h2/H2ClanDAO.java delete mode 100644 src/dao/h2/com/l2jserver/db/dao/h2/H2NPCDAO.java delete mode 100644 src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java delete mode 100644 src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java create mode 100644 src/main/java/com/l2jserver/Installer.java create mode 100644 src/main/java/com/l2jserver/game/ai/AI.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/AbstractDesire.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/AttackDesire.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/Desire.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/DesireIteratorFilter.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/DesireIteratorHandler.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/DesireQueue.java create mode 100644 src/main/java/com/l2jserver/game/ai/desires/MoveDesire.java create mode 100644 src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java create mode 100644 src/main/java/com/l2jserver/service/cache/Cache.java create mode 100644 src/main/java/com/l2jserver/service/cache/EternalCache.java create mode 100644 src/main/java/com/l2jserver/service/cache/MethodInvocation.java create mode 100644 src/main/java/com/l2jserver/service/cache/SoftCacheService.java create mode 100644 src/main/java/com/l2jserver/service/cache/WeakCacheService.java create mode 100644 src/main/java/com/l2jserver/util/MathUtil.java create mode 100644 src/main/java/com/l2jserver/util/geometry/AbstractArea.java create mode 100644 src/main/java/com/l2jserver/util/geometry/Area.java rename src/main/java/com/l2jserver/util/{dimensional => geometry}/Coordinate.java (95%) create mode 100644 src/main/java/com/l2jserver/util/geometry/CylinderArea.java rename data/script/ai/script/ai/scanner/AIScanner.java => src/main/java/com/l2jserver/util/geometry/Point.java (62%) rename src/main/java/com/l2jserver/util/{dimensional/Point.java => geometry/Point3D.java} (70%) create mode 100644 src/main/java/com/l2jserver/util/geometry/PolygonArea.java create mode 100644 src/main/java/com/l2jserver/util/geometry/RectangleArea.java delete mode 100644 test.xsd diff --git a/.classpath b/.classpath index ca7327a91..0ff9af658 100644 --- a/.classpath +++ b/.classpath @@ -3,8 +3,6 @@ - - diff --git a/data/.gitignore b/data/.gitignore index 0c17b5b9d..3303592d0 100644 --- a/data/.gitignore +++ b/data/.gitignore @@ -1,2 +1,3 @@ /cache /pathing.db +/database.h2.db diff --git a/data/script/ai/script/AIInterest.java b/data/script/ai/script/AIInterest.java deleted file mode 100644 index ba987a13d..000000000 --- a/data/script/ai/script/AIInterest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package script; - -/** - * The {@link AIInterest} defines what the AI is interested in doing. - * - * @author Rogiel - */ -public enum AIInterest { - /** - * Idle - */ - INTEREST_IDLE, - /** - * Will scan for attackable targets, if mob is aggressive or if it is - * aggrided. - */ - INTEREST_ACTIVE, - /** - * Rest (sit until attacked) - */ - INTEREST_REST, - /** - * Attack target (cast combat magic, go to target, combat), may be ignored, - * if target is locked on another character or a peacefull zone and so on - */ - INTEREST_ATTACK, - /** - * Cast a spell, depending on the spell - may start or stop attacking - */ - INTEREST_CAST, - /** - * Just move to another location - */ - INTEREST_MOVE_TO, - /** - * Like move, but check target's movement and follow it - */ - INTEREST_FOLLOW, - /** - * PickUp and item, (got to item, pickup it, become idle - */ - INTEREST_PICK_UP, - /** - * Move to target, then interact - */ - INTEREST_INTERACT; -} \ No newline at end of file diff --git a/data/script/ai/script/AIScriptFactory.java b/data/script/ai/script/AIScriptFactory.java deleted file mode 100644 index a5fb860ee..000000000 --- a/data/script/ai/script/AIScriptFactory.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package script; - -import script.ai.CharacterAI; - -import com.google.inject.Inject; -import com.l2jserver.game.net.Lineage2Connection; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.model.world.WorldObject; -import com.l2jserver.service.game.ai.AIScript; -import com.l2jserver.service.game.world.event.WorldEventDispatcher; -import com.l2jserver.service.network.NetworkService; - -/** - * @author Rogiel - */ -public class AIScriptFactory { - private final WorldEventDispatcher eventDispatcher; - private final NetworkService networkService; - - @Inject - public AIScriptFactory(WorldEventDispatcher eventDispatcher, - NetworkService networkService) { - this.eventDispatcher = eventDispatcher; - this.networkService = networkService; - } - - public AIScript create(WorldObject object) { - if (object instanceof L2Character) { - final Lineage2Connection conn = networkService - .discover((CharacterID) object.getID()); - return new CharacterAI((L2Character) object, conn, eventDispatcher); - } - return null; - } -} diff --git a/data/script/ai/script/ai/CharacterAI.java b/data/script/ai/script/ai/CharacterAI.java deleted file mode 100644 index 30e8526c7..000000000 --- a/data/script/ai/script/ai/CharacterAI.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package script.ai; - -import script.AIInterest; - -import com.l2jserver.game.net.Lineage2Connection; -import com.l2jserver.game.net.packet.server.ActorMovementPacket; -import com.l2jserver.model.world.Actor; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.model.world.PositionableObject; -import com.l2jserver.model.world.character.event.CharacterMoveEvent; -import com.l2jserver.service.game.ai.AIScript; -import com.l2jserver.service.game.ai.script.AttackAIScript; -import com.l2jserver.service.game.ai.script.WalkingAIScript; -import com.l2jserver.service.game.world.WorldService; -import com.l2jserver.service.game.world.event.WorldEventDispatcher; -import com.l2jserver.util.dimensional.Coordinate; - -/** - * This {@link AIScript} is for {@link L2Character} object instances - * - * @author Rogiel - */ -public class CharacterAI implements AIScript, WalkingAIScript, AttackAIScript { - /** - * The {@link L2Character} being controlled by this AI - */ - private final L2Character character; - /** - * The {@link Lineage2Connection} instance for this character - */ - private final Lineage2Connection conn; - /** - * The {@link WorldService} event dispatcher - */ - private final WorldEventDispatcher eventDispatcher; - /** - * The AI interest - */ - private AIInterest interest; - - // walking - /** - * Walking destination coordinate - */ - private Coordinate coordinate; - - public CharacterAI(L2Character character, Lineage2Connection conn, - WorldEventDispatcher eventDispatcher) { - this.character = character; - this.conn = conn; - this.eventDispatcher = eventDispatcher; - } - - @Override - public void start() { - // TODO implement listener - } - - @Override - public void run(double time) { - if (interest == AIInterest.INTEREST_IDLE) - return; - - switch (interest) { - case INTEREST_MOVE_TO: - final Coordinate source = character.getPosition(); - character.setPosition(coordinate); - conn.write(new ActorMovementPacket(character, source)); - eventDispatcher.dispatch(new CharacterMoveEvent(character, - coordinate.toPoint())); - // double speed = character.getAttributes().getMoveSpeed(); - // double move = time * speed; - // // Calculate movement angles needed - // final double distance = coordinate.getDistance(character - // .getPosition()); - // final int dy = coordinate.getY() - character.getPoint().getY(); - // final int dx = coordinate.getX() - character.getPoint().getX(); - // - // double sin = dy / distance; - // double cos = dx / distance; - // - // double angleTarget = Math.toDegrees(Math.atan2(sin, cos)); - // if (angleTarget < 0) - // angleTarget = 360 + angleTarget; - // final int angle = (int) (angleTarget * 182.044444444); - this.interest = AIInterest.INTEREST_IDLE; - break; - default: - break; - } - } - - @Override - public void stop() { - // TODO Auto-generated method stub - - } - - @Override - public void walk(Coordinate coordinate) { - this.interest = AIInterest.INTEREST_MOVE_TO; - this.coordinate = coordinate; - } - - @Override - public void follow(PositionableObject positionable) { - } - - @Override - public void attack(Actor target) { - } -} diff --git a/src/dao/h2/com/l2jserver/db/dao/H2DAOModule.java b/src/dao/com/l2jserver/db/H2DAOModule.java similarity index 87% rename from src/dao/h2/com/l2jserver/db/dao/H2DAOModule.java rename to src/dao/com/l2jserver/db/H2DAOModule.java index 0da1f22f8..afd550470 100644 --- a/src/dao/h2/com/l2jserver/db/dao/H2DAOModule.java +++ b/src/dao/com/l2jserver/db/H2DAOModule.java @@ -14,11 +14,16 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao; +package com.l2jserver.db; import com.google.inject.AbstractModule; import com.google.inject.Module; import com.google.inject.Scopes; +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.CharacterFriendDAO; +import com.l2jserver.db.dao.ClanDAO; +import com.l2jserver.db.dao.ItemDAO; +import com.l2jserver.db.dao.NPCDAO; import com.l2jserver.db.dao.h2.H2CharacterDAO; import com.l2jserver.db.dao.h2.H2CharacterFriendDAO; import com.l2jserver.db.dao.h2.H2ClanDAO; diff --git a/src/dao/mysql5/com/l2jserver/db/dao/MySQL5DAOModule.java b/src/dao/com/l2jserver/db/MySQL5DAOModule.java similarity index 88% rename from src/dao/mysql5/com/l2jserver/db/dao/MySQL5DAOModule.java rename to src/dao/com/l2jserver/db/MySQL5DAOModule.java index 448a74673..5ea734c43 100644 --- a/src/dao/mysql5/com/l2jserver/db/dao/MySQL5DAOModule.java +++ b/src/dao/com/l2jserver/db/MySQL5DAOModule.java @@ -14,11 +14,16 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao; +package com.l2jserver.db; import com.google.inject.AbstractModule; import com.google.inject.Module; import com.google.inject.Scopes; +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.CharacterFriendDAO; +import com.l2jserver.db.dao.ClanDAO; +import com.l2jserver.db.dao.ItemDAO; +import com.l2jserver.db.dao.NPCDAO; import com.l2jserver.db.dao.mysql5.MySQL5CharacterDAO; import com.l2jserver.db.dao.mysql5.MySQL5CharacterFriendDAO; import com.l2jserver.db.dao.mysql5.MySQL5ClanDAO; diff --git a/src/dao/com/l2jserver/db/dao/h2/H2CharacterDAO.java b/src/dao/com/l2jserver/db/dao/h2/H2CharacterDAO.java new file mode 100644 index 000000000..1d8f08317 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/h2/H2CharacterDAO.java @@ -0,0 +1,41 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.h2; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.jdbc.JDBCCharacterDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ClanIDProvider; +import com.l2jserver.model.id.provider.AccountIDProvider; +import com.l2jserver.model.id.template.provider.CharacterTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class H2CharacterDAO extends JDBCCharacterDAO implements + CharacterDAO { + public H2CharacterDAO(DatabaseService database, + CharacterIDProvider idFactory, + CharacterTemplateIDProvider templateIdFactory, + AccountIDProvider accountIdFactory, ClanIDProvider clanIdFactory) { + super(database, idFactory, templateIdFactory, accountIdFactory, + clanIdFactory); + } +} diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/AbstractH2DAO.java b/src/dao/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java similarity index 55% rename from src/dao/h2/com/l2jserver/db/dao/h2/AbstractH2DAO.java rename to src/dao/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java index 28f89f952..1db2638ab 100644 --- a/src/dao/h2/com/l2jserver/db/dao/h2/AbstractH2DAO.java +++ b/src/dao/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java @@ -16,33 +16,21 @@ */ package com.l2jserver.db.dao.h2; -import com.google.inject.Inject; -import com.l2jserver.model.Model; -import com.l2jserver.model.id.ID; -import com.l2jserver.service.database.AbstractDAO; +import com.l2jserver.db.dao.CharacterFriendDAO; +import com.l2jserver.db.dao.jdbc.JDBCCharacterFriendDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.provider.FriendIDProvider; import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService; /** - * {@link AbstractDAO} for H2 DAO implementation + * {@link CharacterFriendDAO} implementation for MySQL5 * * @author Rogiel - * - * @param - * the object for the DAO - * @param - * the object ID type */ -public abstract class AbstractH2DAO, I extends ID> - extends AbstractDAO { - /** - * The H2 Database Service - */ - protected final JDBCDatabaseService database; - - @Inject - protected AbstractH2DAO(DatabaseService database) { - super(database); - this.database = (JDBCDatabaseService) database; +public class H2CharacterFriendDAO extends JDBCCharacterFriendDAO implements + CharacterFriendDAO { + public H2CharacterFriendDAO(DatabaseService database, + FriendIDProvider idProvider, CharacterIDProvider charIdProvider) { + super(database, idProvider, charIdProvider); } } diff --git a/src/dao/com/l2jserver/db/dao/h2/H2ClanDAO.java b/src/dao/com/l2jserver/db/dao/h2/H2ClanDAO.java new file mode 100644 index 000000000..c65a25b67 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/h2/H2ClanDAO.java @@ -0,0 +1,36 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.h2; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.ClanDAO; +import com.l2jserver.db.dao.jdbc.JDBCClanDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ClanIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class H2ClanDAO extends JDBCClanDAO implements ClanDAO { + public H2ClanDAO(DatabaseService database, + ClanIDProvider clanIdFactory, CharacterIDProvider idFactory) { + super(database, clanIdFactory, idFactory); + } +} diff --git a/src/dao/com/l2jserver/db/dao/h2/H2ItemDAO.java b/src/dao/com/l2jserver/db/dao/h2/H2ItemDAO.java new file mode 100644 index 000000000..7d0bbca49 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/h2/H2ItemDAO.java @@ -0,0 +1,37 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.h2; + +import com.l2jserver.db.dao.ItemDAO; +import com.l2jserver.db.dao.jdbc.JDBCItemDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ItemIDProvider; +import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link ItemDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class H2ItemDAO extends JDBCItemDAO implements ItemDAO { + public H2ItemDAO(DatabaseService database, ItemIDProvider idFactory, + ItemTemplateIDProvider templateIdFactory, + CharacterIDProvider charIdFactory) { + super(database, idFactory, templateIdFactory, charIdFactory); + } +} diff --git a/src/dao/com/l2jserver/db/dao/h2/H2NPCDAO.java b/src/dao/com/l2jserver/db/dao/h2/H2NPCDAO.java new file mode 100644 index 000000000..6c63ffe31 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/h2/H2NPCDAO.java @@ -0,0 +1,36 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.h2; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.NPCDAO; +import com.l2jserver.db.dao.jdbc.JDBCNPCDAO; +import com.l2jserver.model.id.object.provider.NPCIDProvider; +import com.l2jserver.model.id.template.provider.NPCTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class H2NPCDAO extends JDBCNPCDAO implements NPCDAO { + public H2NPCDAO(DatabaseService database, NPCIDProvider idProvider, + NPCTemplateIDProvider templateIdProvider) { + super(database, idProvider, templateIdProvider); + } +} diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/AbstractMySQL5DAO.java b/src/dao/com/l2jserver/db/dao/jdbc/AbstractJDBCDAO.java similarity index 85% rename from src/dao/mysql5/com/l2jserver/db/dao/mysql5/AbstractMySQL5DAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/AbstractJDBCDAO.java index 465492d99..cb3bc7484 100644 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/AbstractMySQL5DAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/AbstractJDBCDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.mysql5; +package com.l2jserver.db.dao.jdbc; import com.google.inject.Inject; import com.l2jserver.model.Model; @@ -24,7 +24,7 @@ import com.l2jserver.service.database.DatabaseService; import com.l2jserver.service.database.JDBCDatabaseService; /** - * {@link AbstractDAO} for MySQL DAO implementation + * {@link AbstractDAO} for JDBC DAO implementation * * @author Rogiel * @@ -33,7 +33,7 @@ import com.l2jserver.service.database.JDBCDatabaseService; * @param * the object ID type */ -public abstract class AbstractMySQL5DAO, I extends ID> +public abstract class AbstractJDBCDAO, I extends ID> extends AbstractDAO { /** * The MySQL Database Service @@ -41,7 +41,7 @@ public abstract class AbstractMySQL5DAO, I extends ID> protected final JDBCDatabaseService database; @Inject - protected AbstractMySQL5DAO(DatabaseService database) { + protected AbstractJDBCDAO(DatabaseService database) { super(database); this.database = (JDBCDatabaseService) database; } diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterDAO.java b/src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterDAO.java similarity index 97% rename from src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterDAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterDAO.java index ecbef6c60..f54203ae9 100644 --- a/src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterDAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.h2; +package com.l2jserver.db.dao.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -48,15 +48,15 @@ import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; import com.l2jserver.service.database.JDBCDatabaseService.Mapper; import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** - * {@link CharacterDAO} implementation for H2 + * {@link CharacterDAO} implementation for JDBC * * @author Rogiel */ -public class H2CharacterDAO extends - AbstractH2DAO implements CharacterDAO { +public class JDBCCharacterDAO extends + AbstractJDBCDAO implements CharacterDAO { /** * The {@link CharacterID} factory */ @@ -106,7 +106,7 @@ public class H2CharacterDAO extends public static final String APPEARANCE_FACE = "apperance_face"; @Inject - public H2CharacterDAO(DatabaseService database, + public JDBCCharacterDAO(DatabaseService database, final CharacterIDProvider idFactory, CharacterTemplateIDProvider templateIdFactory, AccountIDProvider accountIdFactory, ClanIDProvider clanIdFactory) { @@ -164,7 +164,7 @@ public class H2CharacterDAO extends character.setMP(rs.getDouble(MP)); character.setCP(rs.getDouble(CP)); - character.setPoint(Point.fromXYZA(rs.getInt(POINT_X), + character.setPoint(Point3D.fromXYZA(rs.getInt(POINT_X), rs.getInt(POINT_Y), rs.getInt(POINT_Z), rs.getDouble(POINT_ANGLE))); diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java b/src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterFriendDAO.java similarity index 93% rename from src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterFriendDAO.java index df9d3885c..3a7cfd9c5 100644 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/JDBCCharacterFriendDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.mysql5; +package com.l2jserver.db.dao.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -38,12 +38,12 @@ import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; /** - * {@link CharacterFriendDAO} implementation for MySQL5 + * {@link CharacterFriendDAO} implementation for JDBC * * @author Rogiel */ -public class MySQL5CharacterFriendDAO extends - AbstractMySQL5DAO implements +public class JDBCCharacterFriendDAO extends + AbstractJDBCDAO implements CharacterFriendDAO { /** * The {@link FriendID} provider @@ -59,12 +59,12 @@ public class MySQL5CharacterFriendDAO extends */ public static final String TABLE = "character_friend"; // FIELDS - public static final String CHAR_ID = MySQL5CharacterDAO.CHAR_ID; - public static final String CHAR_ID_FRIEND = MySQL5CharacterDAO.CHAR_ID + public static final String CHAR_ID = JDBCCharacterDAO.CHAR_ID; + public static final String CHAR_ID_FRIEND = JDBCCharacterDAO.CHAR_ID + "_friend"; @Inject - public MySQL5CharacterFriendDAO(DatabaseService database, + public JDBCCharacterFriendDAO(DatabaseService database, final FriendIDProvider idProvider, CharacterIDProvider charIdProvider) { super(database); diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java b/src/dao/com/l2jserver/db/dao/jdbc/JDBCClanDAO.java similarity index 95% rename from src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/JDBCClanDAO.java index a8cbfb224..89f048399 100644 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/JDBCClanDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.mysql5; +package com.l2jserver.db.dao.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -37,11 +37,11 @@ import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; /** - * {@link CharacterDAO} implementation for MySQL5 + * {@link CharacterDAO} implementation for JDBC * * @author Rogiel */ -public class MySQL5ClanDAO extends AbstractMySQL5DAO implements +public class JDBCClanDAO extends AbstractJDBCDAO implements ClanDAO { /** * The {@link ClanID} factory @@ -62,7 +62,7 @@ public class MySQL5ClanDAO extends AbstractMySQL5DAO implements public static final String CHAR_ID_LEADER = "character_id_leader"; @Inject - public MySQL5ClanDAO(DatabaseService database, + public JDBCClanDAO(DatabaseService database, ClanIDProvider clanIdFactory, final CharacterIDProvider idFactory) { super(database); this.idFactory = clanIdFactory; diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/H2ItemDAO.java b/src/dao/com/l2jserver/db/dao/jdbc/JDBCItemDAO.java similarity index 95% rename from src/dao/h2/com/l2jserver/db/dao/h2/H2ItemDAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/JDBCItemDAO.java index 829a8cda1..57fd455fa 100644 --- a/src/dao/h2/com/l2jserver/db/dao/h2/H2ItemDAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/JDBCItemDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.h2; +package com.l2jserver.db.dao.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -41,14 +41,14 @@ import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; import com.l2jserver.service.database.JDBCDatabaseService.Mapper; import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** - * {@link ItemDAO} implementation for H2f + * {@link ItemDAO} implementation for JDBC * * @author Rogiel */ -public class H2ItemDAO extends AbstractH2DAO implements +public class JDBCItemDAO extends AbstractJDBCDAO implements ItemDAO { /** * The {@link ItemID} factory @@ -70,7 +70,7 @@ public class H2ItemDAO extends AbstractH2DAO implements // FIELDS public static final String ITEM_ID = "item_id"; public static final String TEMPLATE_ID = "template_id"; - public static final String CHAR_ID = H2CharacterDAO.CHAR_ID; + public static final String CHAR_ID = JDBCCharacterDAO.CHAR_ID; public static final String LOCATION = "location"; public static final String PAPERDOLL = "paperdoll"; @@ -80,7 +80,7 @@ public class H2ItemDAO extends AbstractH2DAO implements public static final String COORD_Z = "coord_z"; @Inject - public H2ItemDAO(DatabaseService database, + public JDBCItemDAO(DatabaseService database, final ItemIDProvider idFactory, ItemTemplateIDProvider templateIdFactory, CharacterIDProvider charIdFactory) { diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java b/src/dao/com/l2jserver/db/dao/jdbc/JDBCNPCDAO.java similarity index 95% rename from src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java rename to src/dao/com/l2jserver/db/dao/jdbc/JDBCNPCDAO.java index 23fffe37e..f2d8ef771 100644 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java +++ b/src/dao/com/l2jserver/db/dao/jdbc/JDBCNPCDAO.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.db.dao.mysql5; +package com.l2jserver.db.dao.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -39,14 +39,14 @@ import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; import com.l2jserver.service.database.JDBCDatabaseService.Mapper; import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** - * {@link CharacterDAO} implementation for MySQL5 + * {@link CharacterDAO} implementation for JDBC * * @author Rogiel */ -public class MySQL5NPCDAO extends AbstractMySQL5DAO implements +public class JDBCNPCDAO extends AbstractJDBCDAO implements NPCDAO { private final Logger log = LoggerFactory.getLogger(this.getClass()); @@ -73,7 +73,7 @@ public class MySQL5NPCDAO extends AbstractMySQL5DAO implements public static final String POINT_ANGLE = "point_angle"; @Inject - public MySQL5NPCDAO(DatabaseService database, + public JDBCNPCDAO(DatabaseService database, final NPCIDProvider idProvider, NPCTemplateIDProvider templateIdProvider) { super(database); @@ -113,7 +113,7 @@ public class MySQL5NPCDAO extends AbstractMySQL5DAO implements final NPC npc = template.create(); npc.setID(id); - npc.setPoint(Point.fromXYZA(rs.getInt(POINT_X), rs.getInt(POINT_Y), + npc.setPoint(Point3D.fromXYZA(rs.getInt(POINT_X), rs.getInt(POINT_Y), rs.getInt(POINT_Z), rs.getDouble(POINT_ANGLE))); return npc; diff --git a/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java new file mode 100644 index 000000000..01b7cf33c --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java @@ -0,0 +1,41 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.mysql5; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.jdbc.JDBCCharacterDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ClanIDProvider; +import com.l2jserver.model.id.provider.AccountIDProvider; +import com.l2jserver.model.id.template.provider.CharacterTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class MySQL5CharacterDAO extends JDBCCharacterDAO implements + CharacterDAO { + public MySQL5CharacterDAO(DatabaseService database, + CharacterIDProvider idFactory, + CharacterTemplateIDProvider templateIdFactory, + AccountIDProvider accountIdFactory, ClanIDProvider clanIdFactory) { + super(database, idFactory, templateIdFactory, accountIdFactory, + clanIdFactory); + } +} diff --git a/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java new file mode 100644 index 000000000..6e4568204 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5CharacterFriendDAO.java @@ -0,0 +1,36 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.mysql5; + +import com.l2jserver.db.dao.CharacterFriendDAO; +import com.l2jserver.db.dao.jdbc.JDBCCharacterFriendDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.provider.FriendIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterFriendDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class MySQL5CharacterFriendDAO extends JDBCCharacterFriendDAO implements + CharacterFriendDAO { + public MySQL5CharacterFriendDAO(DatabaseService database, + FriendIDProvider idProvider, CharacterIDProvider charIdProvider) { + super(database, idProvider, charIdProvider); + } +} diff --git a/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java new file mode 100644 index 000000000..c777f9ce2 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ClanDAO.java @@ -0,0 +1,36 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.mysql5; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.ClanDAO; +import com.l2jserver.db.dao.jdbc.JDBCClanDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ClanIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class MySQL5ClanDAO extends JDBCClanDAO implements ClanDAO { + public MySQL5ClanDAO(DatabaseService database, + ClanIDProvider clanIdFactory, CharacterIDProvider idFactory) { + super(database, clanIdFactory, idFactory); + } +} diff --git a/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java new file mode 100644 index 000000000..96551d509 --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java @@ -0,0 +1,37 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.mysql5; + +import com.l2jserver.db.dao.ItemDAO; +import com.l2jserver.db.dao.jdbc.JDBCItemDAO; +import com.l2jserver.model.id.object.provider.CharacterIDProvider; +import com.l2jserver.model.id.object.provider.ItemIDProvider; +import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link ItemDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class MySQL5ItemDAO extends JDBCItemDAO implements ItemDAO { + public MySQL5ItemDAO(DatabaseService database, ItemIDProvider idFactory, + ItemTemplateIDProvider templateIdFactory, + CharacterIDProvider charIdFactory) { + super(database, idFactory, templateIdFactory, charIdFactory); + } +} diff --git a/src/dao/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java new file mode 100644 index 000000000..160e93a7d --- /dev/null +++ b/src/dao/com/l2jserver/db/dao/mysql5/MySQL5NPCDAO.java @@ -0,0 +1,36 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.db.dao.mysql5; + +import com.l2jserver.db.dao.CharacterDAO; +import com.l2jserver.db.dao.NPCDAO; +import com.l2jserver.db.dao.jdbc.JDBCNPCDAO; +import com.l2jserver.model.id.object.provider.NPCIDProvider; +import com.l2jserver.model.id.template.provider.NPCTemplateIDProvider; +import com.l2jserver.service.database.DatabaseService; + +/** + * {@link CharacterDAO} implementation for MySQL5 + * + * @author Rogiel + */ +public class MySQL5NPCDAO extends JDBCNPCDAO implements NPCDAO { + public MySQL5NPCDAO(DatabaseService database, NPCIDProvider idProvider, + NPCTemplateIDProvider templateIdProvider) { + super(database, idProvider, templateIdProvider); + } +} diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java b/src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java deleted file mode 100644 index f78b27538..000000000 --- a/src/dao/h2/com/l2jserver/db/dao/h2/H2CharacterFriendDAO.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package com.l2jserver.db.dao.h2; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -import com.google.inject.Inject; -import com.l2jserver.db.dao.CharacterFriendDAO; -import com.l2jserver.model.game.CharacterFriend; -import com.l2jserver.model.id.FriendID; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.id.object.provider.CharacterIDProvider; -import com.l2jserver.model.id.provider.FriendIDProvider; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.model.world.character.CharacterFriendList; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService.CachedMapper; -import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; -import com.l2jserver.service.database.JDBCDatabaseService.Mapper; -import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; -import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; - -/** - * {@link CharacterFriendDAO} implementation for H2 - * - * @author Rogiel - */ -public class H2CharacterFriendDAO extends - AbstractH2DAO implements CharacterFriendDAO { - /** - * The {@link FriendID} provider - */ - private final FriendIDProvider idProvider; - /** - * The {@link CharacterID} provider - */ - private final CharacterIDProvider charIdProvider; - - /** - * Character table name - */ - public static final String TABLE = "character_friend"; - // FIELDS - public static final String CHAR_ID = H2CharacterDAO.CHAR_ID; - public static final String CHAR_ID_FRIEND = H2CharacterDAO.CHAR_ID - + "_friend"; - - @Inject - public H2CharacterFriendDAO(DatabaseService database, - final FriendIDProvider idProvider, - CharacterIDProvider charIdProvider) { - super(database); - this.idProvider = idProvider; - this.charIdProvider = charIdProvider; - } - - /** - * The {@link Mapper} for {@link FriendID} - */ - private final Mapper idMapper = new Mapper() { - @Override - public FriendID map(ResultSet rs) throws SQLException { - final CharacterID characterId = charIdProvider.createID(rs - .getInt(CHAR_ID)); - final CharacterID friendId = charIdProvider.createID(rs - .getInt(CHAR_ID_FRIEND)); - return idProvider.createID(characterId, friendId); - } - }; - - /** - * The {@link Mapper} for {@link CharacterFriend} - */ - private final Mapper mapper = new CachedMapper( - database, idMapper) { - @Override - protected CharacterFriend map(FriendID id, ResultSet rs) - throws SQLException { - return new CharacterFriend(id); - } - }; - - @Override - public CharacterFriend select(final FriendID id) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + CHAR_ID - + "` = ? AND `" + CHAR_ID_FRIEND + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, id.getID1().getID()); - st.setInt(2, id.getID2().getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public void load(final L2Character character) { - final List list = database - .query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" - + CHAR_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) - throws SQLException { - st.setInt(1, character.getID().getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - character.getFriendList().load(list); - } - - @Override - public List selectIDs() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return idMapper; - } - }); - } - - @Override - public boolean insert(CharacterFriend friend) { - return database.query(new InsertUpdateQuery(friend) { - @Override - protected String query() { - return "INSERT INTO `" + TABLE + "` (`" + CHAR_ID + "`,`" - + CHAR_ID_FRIEND + "`) VALUES(?,?)"; - } - - @Override - protected void parametize(PreparedStatement st, - CharacterFriend friend) throws SQLException { - st.setInt(1, friend.getCharacterID().getID()); - st.setInt(2, friend.getFriendID().getID()); - } - }) > 0; - } - - @Override - public boolean update(CharacterFriend friend) { - // it is not possible update friend objects, because they are only a ID - // pair and IDs are immutable - return false; - } - - @Override - public boolean delete(CharacterFriend friend) { - return database.query(new InsertUpdateQuery(friend) { - @Override - protected String query() { - return "DELETE FROM `" + TABLE + "` WHERE `" + CHAR_ID - + "` = ?,`" + CHAR_ID_FRIEND + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, - CharacterFriend friend) throws SQLException { - st.setInt(1, friend.getCharacterID().getID()); - st.setInt(2, friend.getFriendID().getID()); - } - }) > 0; - } - - @Override - public boolean save(final CharacterFriendList friends) { - for (final CharacterFriend friend : friends) { - if (!save(friend)) - return false; - } - return true; - } - - @Override - public boolean delete(final CharacterFriendList friends) { - for (final CharacterFriend friend : friends) { - if (!delete(friend)) - return false; - } - return true; - } -} diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/H2ClanDAO.java b/src/dao/h2/com/l2jserver/db/dao/h2/H2ClanDAO.java deleted file mode 100644 index 5b051c3d4..000000000 --- a/src/dao/h2/com/l2jserver/db/dao/h2/H2ClanDAO.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package com.l2jserver.db.dao.h2; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -import com.google.inject.Inject; -import com.l2jserver.db.dao.CharacterDAO; -import com.l2jserver.db.dao.ClanDAO; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.id.object.ClanID; -import com.l2jserver.model.id.object.provider.CharacterIDProvider; -import com.l2jserver.model.id.object.provider.ClanIDProvider; -import com.l2jserver.model.world.Clan; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService.CachedMapper; -import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; -import com.l2jserver.service.database.JDBCDatabaseService.Mapper; -import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; -import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; - -/** - * {@link CharacterDAO} implementation for H2 - * - * @author Rogiel - */ -public class H2ClanDAO extends AbstractH2DAO implements ClanDAO { - /** - * The {@link ClanID} factory - */ - private final ClanIDProvider idFactory; - /** - * The {@link CharacterID} factory - */ - @SuppressWarnings("unused") - private final CharacterIDProvider charIdFactory; - - /** - * Character table name - */ - public static final String TABLE = "clan"; - // FIELDS - public static final String CLAN_ID = "clan_id"; - public static final String CHAR_ID_LEADER = "character_id_leader"; - - @Inject - public H2ClanDAO(DatabaseService database, ClanIDProvider clanIdFactory, - final CharacterIDProvider idFactory) { - super(database); - this.idFactory = clanIdFactory; - this.charIdFactory = idFactory; - } - - /** - * The {@link Mapper} for {@link ClanID} - */ - private final Mapper idMapper = new Mapper() { - @Override - public ClanID map(ResultSet rs) throws SQLException { - return idFactory.createID(rs.getInt(CLAN_ID)); - } - }; - - /** - * The {@link Mapper} for {@link Clan} - */ - private final Mapper mapper = new CachedMapper( - database, idMapper) { - @Override - protected Clan map(ClanID id, ResultSet rs) throws SQLException { - final Clan clan = new Clan(); - clan.setID(id); - return clan; - } - }; - - @Override - public Clan select(final ClanID id) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + CLAN_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, id.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List selectIDs() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return idMapper; - } - }); - } - - @Override - public boolean insert(Clan clan) { - return database.query(new InsertUpdateQuery(clan) { - @Override - protected String query() { - return "INSERT INTO `" + TABLE + "` (`" + CLAN_ID - + "`) VALUES(?)"; - } - - @Override - protected void parametize(PreparedStatement st, Clan clan) - throws SQLException { - int i = 1; - st.setInt(i++, clan.getID().getID()); - } - }) > 0; - } - - @Override - public boolean update(Clan clan) { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean delete(Clan clan) { - return database.query(new InsertUpdateQuery(clan) { - @Override - protected String query() { - return "DELETE FROM `" + TABLE + "` WHERE `" + CLAN_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, Clan clan) - throws SQLException { - st.setInt(1, clan.getID().getID()); - } - }) > 0; - } -} diff --git a/src/dao/h2/com/l2jserver/db/dao/h2/H2NPCDAO.java b/src/dao/h2/com/l2jserver/db/dao/h2/H2NPCDAO.java deleted file mode 100644 index 02971e1ec..000000000 --- a/src/dao/h2/com/l2jserver/db/dao/h2/H2NPCDAO.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package com.l2jserver.db.dao.h2; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.inject.Inject; -import com.l2jserver.db.dao.CharacterDAO; -import com.l2jserver.db.dao.NPCDAO; -import com.l2jserver.model.id.object.NPCID; -import com.l2jserver.model.id.object.provider.NPCIDProvider; -import com.l2jserver.model.id.template.NPCTemplateID; -import com.l2jserver.model.id.template.provider.NPCTemplateIDProvider; -import com.l2jserver.model.template.NPCTemplate; -import com.l2jserver.model.world.NPC; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService.CachedMapper; -import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; -import com.l2jserver.service.database.JDBCDatabaseService.Mapper; -import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; -import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Point; - -/** - * {@link CharacterDAO} implementation for H2 - * - * @author Rogiel - */ -public class H2NPCDAO extends AbstractH2DAO implements NPCDAO { - private final Logger log = LoggerFactory.getLogger(this.getClass()); - - /** - * The {@link NPCID} provider - */ - private final NPCIDProvider idProvider; - /** - * The {@link NPCTemplateID} provider - */ - private final NPCTemplateIDProvider templateIdProvider; - - /** - * Character table name - */ - public static final String TABLE = "npc"; - // FIELDS - public static final String NPC_ID = "npc_id"; - public static final String NPC_TEMPLATE_ID = "npc_template_id"; - - public static final String POINT_X = "point_x"; - public static final String POINT_Y = "point_y"; - public static final String POINT_Z = "point_z"; - public static final String POINT_ANGLE = "point_angle"; - - @Inject - public H2NPCDAO(DatabaseService database, final NPCIDProvider idProvider, - NPCTemplateIDProvider templateIdProvider) { - super(database); - this.idProvider = idProvider; - this.templateIdProvider = templateIdProvider; - } - - /** - * The {@link Mapper} for {@link NPCID} - */ - private final Mapper idMapper = new Mapper() { - @Override - public NPCID map(ResultSet rs) throws SQLException { - if (rs.getString(NPC_ID) == null) - return null; - return idProvider.createID(rs.getInt(NPC_ID)); - } - }; - - /** - * The {@link Mapper} for {@link NPC} - */ - private final Mapper mapper = new CachedMapper(database, - idMapper) { - @Override - protected NPC map(NPCID id, ResultSet rs) throws SQLException { - NPCTemplateID templateId = templateIdProvider.createID(rs - .getInt(NPC_TEMPLATE_ID)); - NPCTemplate template = templateId.getTemplate(); - if (template == null) { - // set default npc instance, for now! - // RoxxyGatekeeperTemplate - 30006 - log.warn("No template found for {}", templateId); - return null; - } - - final NPC npc = template.create(); - - npc.setID(id); - npc.setPoint(Point.fromXYZA(rs.getInt(POINT_X), rs.getInt(POINT_Y), - rs.getInt(POINT_Z), rs.getDouble(POINT_ANGLE))); - - return npc; - } - }; - - @Override - public NPC select(final NPCID id) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + NPC_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, id.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List loadAll() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List selectByTemplate(final NPCTemplateID template) { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" - + NPC_TEMPLATE_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, template.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List selectIDs() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT `" + NPC_ID + "` FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return idMapper; - } - }); - } - - @Override - public boolean insert(NPC npc) { - return database.query(new InsertUpdateQuery(npc) { - @Override - protected String query() { - return "INSERT INTO `" + TABLE + "` (`" + NPC_ID + "`,`" - + NPC_TEMPLATE_ID + "`,`" + POINT_X + "`,`" + POINT_Y - + "`,`" + POINT_Z + "`,`" + POINT_ANGLE - + "`) VALUES(?,?,?,?,?,?)"; - } - - @Override - protected void parametize(PreparedStatement st, NPC npc) - throws SQLException { - int i = 1; - - st.setInt(i++, npc.getID().getID()); - st.setInt(i++, npc.getTemplateID().getID()); - - st.setInt(i++, npc.getPoint().getX()); - st.setInt(i++, npc.getPoint().getY()); - st.setInt(i++, npc.getPoint().getZ()); - st.setDouble(i++, npc.getPoint().getAngle()); - } - }) > 0; - } - - @Override - public boolean update(NPC npc) { - return database.query(new InsertUpdateQuery(npc) { - @Override - protected String query() { - return "UPDATE `" + TABLE + "` SET " + NPC_TEMPLATE_ID - + "` = ?,`" + POINT_X + "` = ?,`" + POINT_Y + "` = ?,`" - + POINT_Z + "` = ?,`" + POINT_ANGLE + "` = ? WHERE `" - + NPC_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, NPC npc) - throws SQLException { - int i = 1; - - // SET - st.setInt(i++, npc.getTemplateID().getID()); - - st.setInt(i++, npc.getPoint().getX()); - st.setInt(i++, npc.getPoint().getY()); - st.setInt(i++, npc.getPoint().getZ()); - st.setDouble(i++, npc.getPoint().getAngle()); - - // WHERE - st.setInt(i++, npc.getID().getID()); - } - }) > 0; - } - - @Override - public boolean delete(NPC npc) { - return database.query(new InsertUpdateQuery(npc) { - @Override - protected String query() { - return "DELETE FROM `" + TABLE + "` WHERE `" + NPC_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, NPC npc) - throws SQLException { - st.setInt(1, npc.getID().getID()); - } - }) > 0; - } -} diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java b/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java deleted file mode 100644 index 6b1747442..000000000 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5CharacterDAO.java +++ /dev/null @@ -1,420 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package com.l2jserver.db.dao.mysql5; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; -import java.util.List; - -import com.google.inject.Inject; -import com.l2jserver.db.dao.CharacterDAO; -import com.l2jserver.model.id.AccountID; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.id.object.ClanID; -import com.l2jserver.model.id.object.provider.CharacterIDProvider; -import com.l2jserver.model.id.object.provider.ClanIDProvider; -import com.l2jserver.model.id.provider.AccountIDProvider; -import com.l2jserver.model.id.template.CharacterTemplateID; -import com.l2jserver.model.id.template.provider.CharacterTemplateIDProvider; -import com.l2jserver.model.template.CharacterTemplate; -import com.l2jserver.model.world.Actor.ActorRace; -import com.l2jserver.model.world.Actor.ActorSex; -import com.l2jserver.model.world.Clan; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.model.world.character.CharacterAppearance; -import com.l2jserver.model.world.character.CharacterAppearance.CharacterFace; -import com.l2jserver.model.world.character.CharacterAppearance.CharacterHairColor; -import com.l2jserver.model.world.character.CharacterAppearance.CharacterHairStyle; -import com.l2jserver.model.world.character.CharacterClass; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService.CachedMapper; -import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; -import com.l2jserver.service.database.JDBCDatabaseService.Mapper; -import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; -import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Point; - -/** - * {@link CharacterDAO} implementation for MySQL5 - * - * @author Rogiel - */ -public class MySQL5CharacterDAO extends - AbstractMySQL5DAO implements CharacterDAO { - /** - * The {@link CharacterID} factory - */ - private final CharacterIDProvider idFactory; - /** - * The {@link CharacterTemplateID} factory - */ - private final CharacterTemplateIDProvider templateIdFactory; - /** - * The {@link AccountID} factory - */ - private final AccountIDProvider accountIdFactory; - /** - * The {@link ClanID} factory - */ - private final ClanIDProvider clanIdFactory; - - /** - * Character table name - */ - public static final String TABLE = "character"; - // FIELDS - public static final String CHAR_ID = "character_id"; - public static final String ACCOUNT_ID = "account_id"; - public static final String CLAN_ID = "clan_id"; - public static final String NAME = "name"; - - public static final String RACE = "race"; - public static final String CLASS = "class"; - public static final String SEX = "sex"; - - public static final String LEVEL = "level"; - public static final String EXPERIENCE = "experience"; - public static final String SP = "sp"; - - public static final String HP = "hp"; - public static final String MP = "mp"; - public static final String CP = "cp"; - - public static final String POINT_X = "point_x"; - public static final String POINT_Y = "point_y"; - public static final String POINT_Z = "point_z"; - public static final String POINT_ANGLE = "point_angle"; - - public static final String APPEARANCE_HAIR_STYLE = "appearance_hair_style"; - public static final String APPEARANCE_HAIR_COLOR = "appearance_hair_color"; - public static final String APPEARANCE_FACE = "apperance_face"; - - @Inject - public MySQL5CharacterDAO(DatabaseService database, - final CharacterIDProvider idFactory, - CharacterTemplateIDProvider templateIdFactory, - AccountIDProvider accountIdFactory, ClanIDProvider clanIdFactory) { - super(database); - this.idFactory = idFactory; - this.templateIdFactory = templateIdFactory; - this.accountIdFactory = accountIdFactory; - this.clanIdFactory = clanIdFactory; - } - - /** - * The mapper for {@link CharacterID} - */ - private final Mapper idMapper = new Mapper() { - @Override - public CharacterID map(ResultSet rs) throws SQLException { - return idFactory.createID(rs.getInt(CHAR_ID)); - } - }; - - /** - * The {@link Mapper} for {@link L2Character} - */ - private final Mapper mapper = new CachedMapper( - database, idMapper) { - @Override - protected L2Character map(CharacterID id, ResultSet rs) - throws SQLException { - final CharacterClass charClass = CharacterClass.valueOf(rs - .getString(CLASS)); - final CharacterTemplateID templateId = templateIdFactory - .createID(charClass.id); - final CharacterTemplate template = templateId.getTemplate(); - - final L2Character character = template.create(); - - character.setID(id); - character.setAccountID(accountIdFactory.createID(rs - .getString(ACCOUNT_ID))); - if (rs.getString(CLAN_ID) != null) - character.setClanID(clanIdFactory.createID(rs.getInt(CLAN_ID))); - - character.setName(rs.getString(NAME)); - - character.setRace(ActorRace.valueOf(rs.getString(RACE))); - character.setCharacterClass(CharacterClass.valueOf(rs - .getString(CLASS))); - character.setSex(ActorSex.valueOf(rs.getString(SEX))); - - character.setLevel(rs.getInt(LEVEL)); - character.setExperience(rs.getLong(EXPERIENCE)); - character.setSP(rs.getInt(SP)); - - character.setHP(rs.getDouble(HP)); - character.setMP(rs.getDouble(MP)); - character.setCP(rs.getDouble(CP)); - - character.setPoint(Point.fromXYZA(rs.getInt(POINT_X), - rs.getInt(POINT_Y), rs.getInt(POINT_Z), - rs.getDouble(POINT_ANGLE))); - - // appearance - character.getAppearance().setHairStyle( - CharacterHairStyle.valueOf(rs - .getString(APPEARANCE_HAIR_STYLE))); - character.getAppearance().setHairColor( - CharacterHairColor.valueOf(rs - .getString(APPEARANCE_HAIR_COLOR))); - character.getAppearance().setFace( - CharacterFace.valueOf(rs.getString(APPEARANCE_FACE))); - - return character; - } - }; - - @Override - public L2Character select(final CharacterID id) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + CHAR_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, id.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public void load(final Clan clan) { - clan.getMembers().load( - database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT `" + CHAR_ID + "` FROM `" + TABLE - + "` WHERE `" + CLAN_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) - throws SQLException { - st.setInt(1, clan.getID().getID()); - } - - @Override - protected Mapper mapper() { - return idMapper; - } - })); - } - - @Override - public L2Character selectByName(final String name) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + NAME + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setString(1, name); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List selectByAccount(final AccountID account) { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + ACCOUNT_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setString(1, account.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public List selectIDs() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT `" + CHAR_ID + "` FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return idMapper; - } - }); - } - - @Override - public boolean insert(L2Character character) { - return database.query(new InsertUpdateQuery(character) { - @Override - protected String query() { - return "INSERT INTO `" + TABLE + "` (`" + CHAR_ID + "`,`" - + ACCOUNT_ID + "`,`" + CLAN_ID + "`,`" + NAME + "`,`" - + RACE + "`,`" + CLASS + "`,`" + SEX + "`,`" + LEVEL - + "`,`" + EXPERIENCE + "`,`" + SP + "`,`" + HP + "`,`" - + MP + "`,`" + CP + "`,`" + POINT_X + "`,`" + POINT_Y - + "`,`" + POINT_Z + "`,`" + POINT_ANGLE + "`,`" - + APPEARANCE_HAIR_STYLE + "`,`" + APPEARANCE_HAIR_COLOR - + "`,`" + APPEARANCE_FACE - + "`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; - } - - @Override - protected void parametize(PreparedStatement st, - L2Character character) throws SQLException { - final CharacterAppearance appearance = character - .getAppearance(); - int i = 1; - - st.setInt(i++, character.getID().getID()); - st.setString(i++, character.getAccountID().getID()); - if (character.getClanID() != null) - st.setInt(i++, character.getClanID().getID()); - else - st.setNull(i++, Types.INTEGER); - - st.setString(i++, character.getName()); - - st.setString(i++, character.getRace().name()); - st.setString(i++, character.getCharacterClass().name()); - st.setString(i++, character.getSex().name()); - - st.setInt(i++, character.getLevel()); - st.setLong(i++, character.getExperience()); - st.setInt(i++, character.getSP()); - - st.setDouble(i++, character.getHP()); - st.setDouble(i++, character.getMP()); - st.setDouble(i++, character.getCP()); - - st.setInt(i++, character.getPoint().getX()); - st.setInt(i++, character.getPoint().getY()); - st.setInt(i++, character.getPoint().getZ()); - st.setDouble(i++, character.getPoint().getAngle()); - - // appearance - st.setString(i++, appearance.getHairStyle().name()); - st.setString(i++, appearance.getHairColor().name()); - st.setString(i++, appearance.getFace().name()); - } - }) > 0; - } - - @Override - public boolean update(L2Character character) { - return database.query(new InsertUpdateQuery(character) { - @Override - protected String query() { - return "UPDATE `" + TABLE + "` SET " + ACCOUNT_ID + "` = ?,`" - + CLAN_ID + "` = ?,`" + NAME + "` = ?,`" + RACE - + "` = ?,`" + CLASS + "` = ?,`" + SEX + "` = ?,`" - + LEVEL + "` = ?,`" + EXPERIENCE + "` = ?,`" + SP - + "` = ?,`" + HP + "` = ?,`" + MP + "` = ?,`" + CP - + "` = ?,`" + POINT_X + "` = ?,`" + POINT_Y + "` = ?,`" - + POINT_Z + "` = ?,`" + POINT_ANGLE + "` = ?,`" - + APPEARANCE_HAIR_STYLE + "` = ?,`" - + APPEARANCE_HAIR_COLOR + "` = ?,`" + APPEARANCE_FACE - + "` = ? WHERE `" + CHAR_ID + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, - L2Character character) throws SQLException { - final CharacterAppearance appearance = character - .getAppearance(); - int i = 1; - - // SET - st.setString(i++, character.getAccountID().getID()); - if (character.getClanID() != null) - st.setInt(i++, character.getClanID().getID()); - else - st.setNull(i++, Types.INTEGER); - - st.setString(i++, character.getName()); - - st.setString(i++, character.getRace().name()); - st.setString(i++, character.getCharacterClass().name()); - st.setString(i++, character.getSex().name()); - - st.setInt(i++, character.getLevel()); - st.setLong(i++, character.getExperience()); - st.setInt(i++, character.getSP()); - - st.setDouble(i++, character.getHP()); - st.setDouble(i++, character.getMP()); - st.setDouble(i++, character.getCP()); - - // position - st.setInt(i++, character.getPoint().getX()); - st.setInt(i++, character.getPoint().getY()); - st.setInt(i++, character.getPoint().getZ()); - st.setDouble(i++, character.getPoint().getAngle()); - - // appearance - st.setString(i++, appearance.getHairStyle().name()); - st.setString(i++, appearance.getHairColor().name()); - st.setString(i++, appearance.getFace().name()); - - // WHERE - st.setInt(i++, character.getID().getID()); - } - }) > 0; - } - - @Override - public boolean delete(L2Character character) { - return database.query(new InsertUpdateQuery(character) { - @Override - protected String query() { - return "DELETE FROM `" + TABLE + "` WHERE `" + CHAR_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, - L2Character character) throws SQLException { - st.setInt(1, character.getID().getID()); - } - }) > 0; - } -} diff --git a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java b/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java deleted file mode 100644 index 457f0f900..000000000 --- a/src/dao/mysql5/com/l2jserver/db/dao/mysql5/MySQL5ItemDAO.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * This file is part of l2jserver . - * - * 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 . - */ -package com.l2jserver.db.dao.mysql5; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -import com.google.inject.Inject; -import com.l2jserver.db.dao.ItemDAO; -import com.l2jserver.model.id.object.CharacterID; -import com.l2jserver.model.id.object.ItemID; -import com.l2jserver.model.id.object.provider.CharacterIDProvider; -import com.l2jserver.model.id.object.provider.ItemIDProvider; -import com.l2jserver.model.id.template.ItemTemplateID; -import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; -import com.l2jserver.model.template.ItemTemplate; -import com.l2jserver.model.world.Item; -import com.l2jserver.model.world.L2Character; -import com.l2jserver.model.world.character.CharacterInventory; -import com.l2jserver.model.world.character.CharacterInventory.InventoryLocation; -import com.l2jserver.model.world.character.CharacterInventory.InventoryPaperdoll; -import com.l2jserver.service.database.DatabaseService; -import com.l2jserver.service.database.JDBCDatabaseService.CachedMapper; -import com.l2jserver.service.database.JDBCDatabaseService.InsertUpdateQuery; -import com.l2jserver.service.database.JDBCDatabaseService.Mapper; -import com.l2jserver.service.database.JDBCDatabaseService.SelectListQuery; -import com.l2jserver.service.database.JDBCDatabaseService.SelectSingleQuery; -import com.l2jserver.util.dimensional.Coordinate; - -/** - * {@link ItemDAO} implementation for MySQL5 - * - * @author Rogiel - */ -public class MySQL5ItemDAO extends AbstractMySQL5DAO implements - ItemDAO { - /** - * The {@link ItemID} factory - */ - private final ItemIDProvider idFactory; - /** - * The {@link ItemTemplateID} factory - */ - private final ItemTemplateIDProvider templateIdFactory; - /** - * The {@link CharacterID} factory - */ - private final CharacterIDProvider charIdFactory; - - /** - * Character table name - */ - public static final String TABLE = "item"; - // FIELDS - public static final String ITEM_ID = "item_id"; - public static final String TEMPLATE_ID = "template_id"; - public static final String CHAR_ID = MySQL5CharacterDAO.CHAR_ID; - - public static final String LOCATION = "location"; - public static final String PAPERDOLL = "paperdoll"; - public static final String COUNT = "count"; - public static final String COORD_X = "coord_x"; - public static final String COORD_Y = "coord_y"; - public static final String COORD_Z = "coord_z"; - - @Inject - public MySQL5ItemDAO(DatabaseService database, - final ItemIDProvider idFactory, - ItemTemplateIDProvider templateIdFactory, - CharacterIDProvider charIdFactory) { - super(database); - this.idFactory = idFactory; - this.templateIdFactory = templateIdFactory; - this.charIdFactory = charIdFactory; - } - - /** - * The {@link Mapper} for {@link ItemID} - */ - private final Mapper idMapper = new Mapper() { - @Override - public ItemID map(ResultSet rs) throws SQLException { - return idFactory.createID(rs.getInt(ITEM_ID)); - } - }; - - /** - * The {@link Mapper} instance - */ - private final Mapper mapper = new CachedMapper( - database, idMapper) { - @Override - public Item map(ItemID id, ResultSet rs) throws SQLException { - final ItemTemplateID templateId = templateIdFactory.createID(rs - .getInt(TEMPLATE_ID)); - final ItemTemplate template = templateId.getTemplate(); - final Item item = template.create(); - - item.setID(id); - if (rs.getObject(CHAR_ID) != null) - item.setOwnerID(charIdFactory.createID(rs.getInt(CHAR_ID))); - if (rs.getObject(LOCATION) != null) - item.setLocation(InventoryLocation.valueOf(rs - .getString(LOCATION))); - if (rs.getObject(PAPERDOLL) != null) - item.setPaperdoll(InventoryPaperdoll.valueOf(rs - .getString(PAPERDOLL))); - - item.setCount(rs.getInt(COUNT)); - - if (rs.getObject(COORD_X) != null && rs.getObject(COORD_Y) != null - && rs.getObject(COORD_Z) != null) - item.setPosition(Coordinate.fromXYZ(rs.getInt(COORD_X), - rs.getInt(COORD_Y), rs.getInt(COORD_Z))); - - return item; - } - }; - - @Override - public Item select(final ItemID id) { - return database.query(new SelectSingleQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + ITEM_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, id.getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - } - - @Override - public int loadInventory(final L2Character character) { - final CharacterInventory inventory = character.getInventory(); - final List items = database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT * FROM `" + TABLE + "` WHERE `" + CHAR_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st) throws SQLException { - st.setInt(1, character.getID().getID()); - } - - @Override - protected Mapper mapper() { - return mapper; - } - }); - inventory.load(items); - return items.size(); - } - - @Override - public List selectIDs() { - return database.query(new SelectListQuery() { - @Override - protected String query() { - return "SELECT `" + ITEM_ID + "` FROM `" + TABLE + "`"; - } - - @Override - protected Mapper mapper() { - return idMapper; - } - }); - } - - @Override - public boolean insert(Item item) { - throw new UnsupportedOperationException( - "Saving items is not yet implemented!"); - } - - @Override - public boolean update(Item item) { - return false; - } - - @Override - public boolean delete(Item item) { - return database.query(new InsertUpdateQuery(item) { - @Override - protected String query() { - return "DELETE FROM `" + TABLE + "` WHERE `" + ITEM_ID - + "` = ?"; - } - - @Override - protected void parametize(PreparedStatement st, Item item) - throws SQLException { - st.setInt(1, item.getID().getID()); - } - }) > 0; - } -} diff --git a/src/main/java/com/l2jserver/GameServerModule.java b/src/main/java/com/l2jserver/GameServerModule.java index 918eb44ca..6d4c26eaa 100644 --- a/src/main/java/com/l2jserver/GameServerModule.java +++ b/src/main/java/com/l2jserver/GameServerModule.java @@ -18,7 +18,7 @@ package com.l2jserver; import com.google.inject.AbstractModule; import com.google.inject.Module; -import com.l2jserver.db.dao.H2DAOModule; +import com.l2jserver.db.H2DAOModule; import com.l2jserver.model.id.provider.IDProviderModule; import com.l2jserver.service.ServiceModule; diff --git a/src/main/java/com/l2jserver/Installer.java b/src/main/java/com/l2jserver/Installer.java new file mode 100644 index 000000000..866caf42e --- /dev/null +++ b/src/main/java/com/l2jserver/Installer.java @@ -0,0 +1,40 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver; + +import com.l2jserver.service.ServiceManager; +import com.l2jserver.service.database.DatabaseService; + +/** + * @author Rogiel + * + */ +public class Installer { + public static void main(String[] args) { + final L2JGameServer server = new L2JGameServer(); + try { + final ServiceManager serviceManager = server.getInjector() + .getInstance(ServiceManager.class); + + serviceManager.start(DatabaseService.class).install(); + } catch (Exception e) { + System.out.println("GameServer could not be installed!"); + e.printStackTrace(); + System.exit(0); + } + } +} diff --git a/src/main/java/com/l2jserver/game/ProtocolVersion.java b/src/main/java/com/l2jserver/game/ProtocolVersion.java index 1f4141522..5cad785c6 100644 --- a/src/main/java/com/l2jserver/game/ProtocolVersion.java +++ b/src/main/java/com/l2jserver/game/ProtocolVersion.java @@ -23,16 +23,20 @@ package com.l2jserver.game; */ public enum ProtocolVersion { /** - * Release version + * The Release version, this is acctually a pseudo version. It never really + * existed. */ RELEASE(0), - /** - * Freya(216) + * The Interlude(200) version + */ + INTERLUDE(200, RELEASE), + /** + * The Freya(216) version */ FREYA(216, RELEASE), /** - * High5(268) + * The High5(268) version */ HIGH5(268, FREYA); diff --git a/src/main/java/com/l2jserver/game/ai/AI.java b/src/main/java/com/l2jserver/game/ai/AI.java new file mode 100644 index 000000000..05c9bc3e8 --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/AI.java @@ -0,0 +1,39 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai; + +import com.l2jserver.game.ai.desires.Desire; +import com.l2jserver.game.ai.desires.DesireQueue; +import com.l2jserver.model.world.Actor; + +/** + * @author Rogiel + * @param + * the {@link Actor} type for this {@link AI} + */ +public abstract class AI { + protected DesireQueue desireQueue = new DesireQueue(); + protected final T creature; + + protected AI(T creature) { + this.creature = creature; + } + + protected void handleDesire(Desire desire) { + desire.handleDesire(this); + } +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/AbstractDesire.java b/src/main/java/com/l2jserver/game/ai/desires/AbstractDesire.java new file mode 100644 index 000000000..58674cf38 --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/AbstractDesire.java @@ -0,0 +1,74 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +/** + * This class implements basic functionality common for each desire + * + * @author Rogiel + * @see com.l2jserver.game.ai.desires.Desire + * @see com.l2jserver.game.ai.desires.DesireQueue + * @see com.l2jserver.game.ai.AI + * @see com.l2jserver.game.ai.AI#handleDesire(Desire) + */ +public abstract class AbstractDesire implements Desire { + + /** + * Desire power. It's used to calculate what npc whants to do most of all. + */ + protected int desirePower; + + /** + * Creates new desire. By design any desire should have desire power. So + * constructor accepts basic amout. + * + * @param desirePower + * basic amount of desirePower + */ + protected AbstractDesire(int desirePower) { + this.desirePower = desirePower; + } + + /** + * Compares this desire with another, used by + * {@link com.l2jserver.game.ai.desires.DesireQueue} to keep track of desire + * priorities. + * + * @param o + * desire to compare with + * @return result of desire comparation + */ + @Override + public int compareTo(Desire o) { + return o.getDesirePower() - getDesirePower(); + } + + @Override + public int getDesirePower() { + return desirePower; + } + + @Override + public synchronized void increaseDesirePower(int desirePower) { + this.desirePower = this.desirePower + desirePower; + } + + @Override + public synchronized void reduceDesirePower(int desirePower) { + this.desirePower = this.desirePower - desirePower; + } +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/AttackDesire.java b/src/main/java/com/l2jserver/game/ai/desires/AttackDesire.java new file mode 100644 index 000000000..181a732f6 --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/AttackDesire.java @@ -0,0 +1,76 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +import com.l2jserver.game.ai.AI; +import com.l2jserver.model.world.Actor; + +/** + * This class indicates that character wants to attack somebody + * + * @author Rogiel + */ +public final class AttackDesire extends AbstractDesire { + /** + * Target of this desire + */ + protected final Actor target; + + /** + * Creates new attack desire, target can't be changed + * + * @param target + * whom to attack + * @param desirePower + * initial attack power + */ + protected AttackDesire(Actor target, int desirePower) { + super(desirePower); + this.target = target; + } + + @Override + public void handleDesire(AI ai) { + // TODO: Implement + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof AttackDesire)) + return false; + + AttackDesire that = (AttackDesire) o; + + return target.equals(that.target); + } + + @Override + public int hashCode() { + return target.hashCode(); + } + + /** + * Returns target of this desire + * + * @return target of this desire + */ + public Actor getTarget() { + return target; + } +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/Desire.java b/src/main/java/com/l2jserver/game/ai/desires/Desire.java new file mode 100644 index 000000000..1f35cc309 --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/Desire.java @@ -0,0 +1,93 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +import com.l2jserver.game.ai.AI; + +/** + * This interface represents basic desire functions.
+ * Each desire should implement {@link #handleDesire(com.l2jserver.game.ai.AI)} + * method with default behavior.
+ * AI can override {@link com.l2jserver.game.ai.AI#handleDesire(Desire)} to + * implement custom behavior of desire.
+ * + * @author Rogiel + * @see com.l2jserver.game.ai.AI + * @see com.l2jserver.game.ai.AI#handleDesire(Desire) + * @see com.l2jserver.game.ai.desires.AbstractDesire + */ +public interface Desire extends Comparable { + /** + * Invokes default desire action. AI can override invocation of this method + * to handle desire in it's own way + * + * @param ai + * actor that is doing this desire + */ + void handleDesire(AI ai); + + /** + * Returns hashcode for this object, must be overrided by child + * + * @return hashcode for this object + */ + int hashCode(); + + /** + * Compares this Desire with another object, must overriden by child + * + * @param obj + * another object to compare with + * @return result of object comparation + */ + boolean equals(Object obj); + + /** + * Returns desire power of this object + * + * @return desire power of the object + */ + int getDesirePower(); + + /** + * Adds desire power to this desire, this call is synchronized.
+ *
+ * WARNING!!! Changing desire power after adding it to queue will not + * affect it's position, you have to call + * {@link com.l2jserver.game.ai.desires.DesireQueue#addDesire(Desire)} + * passing this instance as argument + * + * @param desirePower + * amount of desirePower to add + * @see DesireQueue#addDesire(Desire) + */ + void increaseDesirePower(int desirePower); + + /** + * Reduces desire power by give amount.
+ *
+ * WARNING!!! Changing desire power after adding it to queue will not + * affect it's position, you have to call + * {@link com.l2jserver.game.ai.desires.DesireQueue#addDesire(Desire)} + * passing this instance as argument + * + * @param desirePower + * amount of desirePower to substract + * @see DesireQueue#addDesire(Desire) + */ + void reduceDesirePower(int desirePower); +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorFilter.java b/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorFilter.java new file mode 100644 index 000000000..14451345e --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorFilter.java @@ -0,0 +1,45 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +/** + * This class represents simple filter for desire iterations. + * + * @author Rogiel + */ +public interface DesireIteratorFilter { + /** + * This method is called each time for every desire that is in the queue.
+ *
{@link java.util.ConcurrentModificationException} will be thrown by + * {@link com.l2jserver.game.ai.desires.DesireQueue#iterateDesires(DesireIteratorHandler, DesireIteratorFilter[])} + * if any of the following methods will be called from here: + *
    + *
  • {@link com.l2jserver.game.ai.desires.DesireQueue#addDesire(Desire)}
  • + *
  • {@link com.l2jserver.game.ai.desires.DesireQueue#poll()}
  • + *
  • + * {@link com.l2jserver.game.ai.desires.DesireQueue#removeDesire(Desire)}
  • + *
+ *

+ * However {@link com.l2jserver.game.ai.desires.DesireQueue#clear()} can be + * called. + * + * @param desire + * current element of iteration that is beeing filtered + * @return true if this filter accepted desire, false otherwise + */ + public boolean isOk(Desire desire); +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorHandler.java b/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorHandler.java new file mode 100644 index 000000000..4ca87f648 --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/DesireIteratorHandler.java @@ -0,0 +1,55 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +import java.util.Iterator; + +/** + * This class is designed to be a helper class for desires.
+ * Direct access to desire list is not allowed, however this interface can be + * used for iteration.
+ *
+ * + * @author Rogiel + * @see com.l2jserver.game.ai.desires.DesireIteratorFilter + * @see com.l2jserver.game.ai.desires.DesireQueue#iterateDesires(DesireIteratorHandler, + * DesireIteratorFilter[]) + */ +public interface DesireIteratorHandler { + /** + * This method is called each time for every desire that is in the queue.
+ * Remove of desire must be handeled by iterator.remove();
+ *
{@link java.util.ConcurrentModificationException} will be thrown by + * {@link com.l2jserver.game.ai.desires.DesireQueue#iterateDesires(DesireIteratorHandler, DesireIteratorFilter[])} + * if any of the following methods will be called from here: + *

    + *
  • {@link com.l2jserver.game.ai.desires.DesireQueue#addDesire(Desire)}
  • + *
  • {@link com.l2jserver.game.ai.desires.DesireQueue#poll()}
  • + *
  • + * {@link com.l2jserver.game.ai.desires.DesireQueue#removeDesire(Desire)}
  • + *
+ *

+ * However {@link com.l2jserver.game.ai.desires.DesireQueue#clear()} can be + * called. + * + * @param desire + * current element of iteration + * @param iterator + * iterator object + */ + public void next(Desire desire, Iterator iterator); +} diff --git a/src/main/java/com/l2jserver/game/ai/desires/DesireQueue.java b/src/main/java/com/l2jserver/game/ai/desires/DesireQueue.java new file mode 100644 index 000000000..786b20dad --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/DesireQueue.java @@ -0,0 +1,212 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.PriorityQueue; + +/** + * This class represents desire queue, it's thread-safe. Desires can be added + * and removed. If desire is added - previous desires will be checked, if same + * desire found then desire previous one will be removed from the queue + * + * @author Rogiel + * @see com.l2jserver.game.ai.desires.Desire + * @see com.l2jserver.game.ai.desires.AbstractDesire + */ +public class DesireQueue { + /** + * Prioritized Queue of desires, lazy initialization. + */ + protected PriorityQueue queue; + + /** + * Returns first element of this queue not removing it. Returns null if + * there is no elements. + * + * @return first element or null + */ + public synchronized Desire peek() { + return queue != null ? queue.peek() : null; + } + + /** + * Removes first element from the desires list and removes it. Returns null + * if there are no elements. + * + * @return first element from the desires list or null. + */ + public synchronized Desire poll() { + if (queue != null) { + return queue.poll(); + } + + return null; + } + + /** + * Adds desire to the queue. + *

+ *

+ * When adding object this method checks first for the same object by + * {@link AbstractDesire#equals(Object)}, if such object found, next actions + * will be done:
+ *
+ * 1). Remove old desire instance from the list.
+ * 2). Check if those desires are same instances by "==".
+ * 3). If they are not the same instances, add desire power from old + * instance to new instance, if they are - do nothing.
+ *
+ * After all add new desire instance to the list. + * + * @param desire + * desire instance to add + */ + public synchronized void addDesire(Desire desire) { + // Lazy initialization of desire queue + if (queue == null) { + queue = new PriorityQueue(); + } + + // Iterate over the list to find similar desires + Iterator iterator = queue.iterator(); + while (iterator.hasNext()) { + Desire iterated = iterator.next(); + + // Find similar desires by #equals method, they can be different + // instances. + if (desire.equals(iterated)) { + // Remove the old desire from the list + iterator.remove(); + + // If current desire instance was not at the list - increase + // it's power by the value of another instance power + // and after that add it to the list + if (desire != iterated) { + desire.increaseDesirePower(iterated.getDesirePower()); + } + + // Break iteration, desire list can't contain two same desires + break; + } + } + // finally add desire to the list + queue.add(desire); + } + + /** + * Removes desire from this desireQueue. If desire was removed successfully + * this method return true, false otherwise + * + * @param desire + * what desire to remove + * @return result of desire removal + */ + public synchronized boolean removeDesire(Desire desire) { + return queue != null && queue.remove(desire); + } + + /** + * Iterates over desires, you have to provide iteration handler and + * optionally filters.
+ *
+ * Handlers and filters can't call following methods: + *

    + *
  • {@link #addDesire(Desire)}
  • + *
  • {@link #poll()}
  • + *
  • {@link #removeDesire(Desire)}
  • + *
+ *

+ * However, method {@link #clear() can be called}. + * + * @param handler + * DesireIterationhandler that will be called on the iteration + * @param filters + * optional filters that will prevent passing unneeded desires to + * the handler + * @throws java.util.ConcurrentModificationException + * only if called handler or filter modified this queue + * @see com.l2jserver.game.ai.desires.DesireIteratorFilter + * @see com.l2jserver.game.ai.desires.DesireIteratorFilter#isOk(Desire) + * @see com.l2jserver.game.ai.desires.DesireIteratorHandler + * @see com.l2jserver.game.ai.desires.DesireIteratorHandler#next(Desire , + * java.util.Iterator) + */ + public synchronized void iterateDesires(DesireIteratorHandler handler, + DesireIteratorFilter... filters) + throws ConcurrentModificationException { + + if (queue == null) { + return; + } + + Iterator iterator = queue.iterator(); + outer: while (iterator.hasNext()) { + Desire desire = iterator.next(); + + if (filters != null && filters.length > 0) { + for (DesireIteratorFilter filter : filters) { + if (!filter.isOk(desire)) { + continue outer; + } + } + } + + handler.next(desire, iterator); + } + } + + /** + * Returns true if this desire list contains same desire. Desires are + * compared by {@link AbstractDesire#equals(Object)} method. + * + * @param desire + * what desire to search + * @return true if there is equal desire, false in other case. + */ + public synchronized boolean contains(Desire desire) { + return queue.contains(desire); + } + + /** + * Returns true if this NPC has no any desires added + * + * @return true if this NPC has no any desires added + */ + public synchronized boolean isEmpty() { + return queue == null || queue.isEmpty(); + } + + /** + * Clears all desires + */ + public synchronized void clear() { + if (queue != null) { + queue.clear(); + } + } + + /** + * Returns size of the desire list + * + * @return size of remaining desires + */ + public synchronized int size() { + return queue == null ? 0 : queue.size(); + } +} \ No newline at end of file diff --git a/src/main/java/com/l2jserver/game/ai/desires/MoveDesire.java b/src/main/java/com/l2jserver/game/ai/desires/MoveDesire.java new file mode 100644 index 000000000..47af94c8d --- /dev/null +++ b/src/main/java/com/l2jserver/game/ai/desires/MoveDesire.java @@ -0,0 +1,83 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.game.ai.desires; + +import com.l2jserver.game.ai.AI; +import com.l2jserver.model.world.Actor; +import com.l2jserver.util.geometry.Point3D; + +/** + * This class indicates that {@link Actor} wants to move somewhere + * + * @author Rogiel + */ +public final class MoveDesire extends AbstractDesire { + /** + * Target of this desire + */ + protected final Point3D point; + + /** + * Creates new move desire. Target can't be changed + * + * @param point + * where to move + * @param desirePower + * initial attack power + */ + protected MoveDesire(Point3D point, int desirePower) { + super(desirePower); + this.point = point; + } + + @Override + public void handleDesire(AI ai) { + // TODO: Implement + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof MoveDesire)) + return false; + + MoveDesire that = (MoveDesire) o; + + return point.equals(that.point); + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + return point.hashCode(); + } + + /** + * Returns target of this desire + * + * @return target of this desire + */ + public Point3D getTarget() { + return point; + } +} diff --git a/src/main/java/com/l2jserver/game/net/packet/client/AdminCommandPacket.java b/src/main/java/com/l2jserver/game/net/packet/client/AdminCommandPacket.java index 2edeb6e5e..a6c94dce1 100644 --- a/src/main/java/com/l2jserver/game/net/packet/client/AdminCommandPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/client/AdminCommandPacket.java @@ -27,7 +27,7 @@ import com.l2jserver.service.game.spawn.CharacterAlreadyTeleportingServiceExcept import com.l2jserver.service.game.spawn.NotSpawnedServiceException; import com.l2jserver.service.game.spawn.SpawnService; import com.l2jserver.util.BufferUtils; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * Executes an administrator action diff --git a/src/main/java/com/l2jserver/game/net/packet/client/CharacterActionPacket.java b/src/main/java/com/l2jserver/game/net/packet/client/CharacterActionPacket.java index e0cc9325c..85e25437b 100644 --- a/src/main/java/com/l2jserver/game/net/packet/client/CharacterActionPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/client/CharacterActionPacket.java @@ -27,7 +27,7 @@ import com.l2jserver.model.id.object.provider.ObjectIDResolver; import com.l2jserver.model.world.NPC; import com.l2jserver.service.game.npc.ActionServiceException; import com.l2jserver.service.game.npc.NPCService; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * Executes an action from an character to an NPC diff --git a/src/main/java/com/l2jserver/game/net/packet/client/CharacterAttackRequestPacket.java b/src/main/java/com/l2jserver/game/net/packet/client/CharacterAttackRequestPacket.java index 031d3db79..27637f8ef 100644 --- a/src/main/java/com/l2jserver/game/net/packet/client/CharacterAttackRequestPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/client/CharacterAttackRequestPacket.java @@ -33,7 +33,7 @@ import com.l2jserver.service.game.character.ActorIsNotAttackableServiceException import com.l2jserver.service.game.character.CannotSetTargetServiceException; import com.l2jserver.service.game.character.CharacterService; import com.l2jserver.service.game.npc.NotAttackableNPCServiceException; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * Completes the creation of an character. Creates the object, inserts into the diff --git a/src/main/java/com/l2jserver/game/net/packet/client/CharacterRequestMovePacket.java b/src/main/java/com/l2jserver/game/net/packet/client/CharacterRequestMovePacket.java index d27a1e3ea..1478bb418 100644 --- a/src/main/java/com/l2jserver/game/net/packet/client/CharacterRequestMovePacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/client/CharacterRequestMovePacket.java @@ -26,7 +26,7 @@ import com.l2jserver.game.net.packet.AbstractClientPacket; import com.l2jserver.game.net.packet.server.CharacterStopMovePacket; import com.l2jserver.model.world.L2Character; import com.l2jserver.service.game.character.CharacterService; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * This packet notifies the server which character the player has chosen to use. diff --git a/src/main/java/com/l2jserver/game/net/packet/client/CharacterValidatePositionPacket.java b/src/main/java/com/l2jserver/game/net/packet/client/CharacterValidatePositionPacket.java index e237c6629..e11fbcceb 100644 --- a/src/main/java/com/l2jserver/game/net/packet/client/CharacterValidatePositionPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/client/CharacterValidatePositionPacket.java @@ -22,7 +22,7 @@ import com.google.inject.Inject; import com.l2jserver.game.net.Lineage2Connection; import com.l2jserver.game.net.packet.AbstractClientPacket; import com.l2jserver.service.game.character.CharacterService; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * This packet notifies the server which character the player has chosen to use. @@ -40,7 +40,7 @@ public class CharacterValidatePositionPacket extends AbstractClientPacket { */ private final CharacterService charService; - private Point point; + private Point3D point; @SuppressWarnings("unused") private int extra; // vehicle id @@ -51,7 +51,7 @@ public class CharacterValidatePositionPacket extends AbstractClientPacket { @Override public void read(Lineage2Connection conn, ChannelBuffer buffer) { - point = Point.fromXYZA(buffer.readInt(), buffer.readInt(), + point = Point3D.fromXYZA(buffer.readInt(), buffer.readInt(), buffer.readInt(), buffer.readInt()); extra = buffer.readInt(); } diff --git a/src/main/java/com/l2jserver/game/net/packet/server/ActorMovementPacket.java b/src/main/java/com/l2jserver/game/net/packet/server/ActorMovementPacket.java index 45419b948..cdc63ee81 100644 --- a/src/main/java/com/l2jserver/game/net/packet/server/ActorMovementPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/server/ActorMovementPacket.java @@ -21,7 +21,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import com.l2jserver.game.net.Lineage2Connection; import com.l2jserver.game.net.packet.AbstractServerPacket; import com.l2jserver.model.world.Actor; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * This packet notifies the client that the character is moving to an certain diff --git a/src/main/java/com/l2jserver/game/net/packet/server/CharacterTeleportPacket.java b/src/main/java/com/l2jserver/game/net/packet/server/CharacterTeleportPacket.java index 9955ee2a2..3ea075c7b 100644 --- a/src/main/java/com/l2jserver/game/net/packet/server/CharacterTeleportPacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/server/CharacterTeleportPacket.java @@ -22,7 +22,7 @@ import com.l2jserver.game.net.Lineage2Connection; import com.l2jserver.game.net.packet.AbstractServerPacket; import com.l2jserver.game.net.packet.server.CharacterCreateFailPacket.Reason; import com.l2jserver.model.world.L2Character; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * This packet notifies the client that the chosen character has been @@ -44,9 +44,9 @@ public class CharacterTeleportPacket extends AbstractServerPacket { /** * The teleportation point */ - private final Point point; + private final Point3D point; - public CharacterTeleportPacket(L2Character character, Point point) { + public CharacterTeleportPacket(L2Character character, Point3D point) { super(OPCODE); this.character = character; this.point = point; diff --git a/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java b/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java index 8ce8430ca..b23694578 100644 --- a/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java +++ b/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java @@ -28,7 +28,7 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.l2jserver.model.id.template.ItemTemplateID; import com.l2jserver.model.id.template.TeleportationTemplateID; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; import com.l2jserver.util.jaxb.CoordinateAdapter; import com.l2jserver.util.jaxb.ItemTemplateIDAdapter; import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; diff --git a/src/main/java/com/l2jserver/model/world/L2Character.java b/src/main/java/com/l2jserver/model/world/L2Character.java index 9c028e5b3..aa6763a89 100644 --- a/src/main/java/com/l2jserver/model/world/L2Character.java +++ b/src/main/java/com/l2jserver/model/world/L2Character.java @@ -31,7 +31,7 @@ import com.l2jserver.model.world.character.CharacterFriendList; import com.l2jserver.model.world.character.CharacterInventory; import com.l2jserver.model.world.character.CharacterShortcutContainer; import com.l2jserver.model.world.character.CharacterStats; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * This class represents a playable character in Lineage II world. @@ -179,7 +179,7 @@ public class L2Character extends Player { /** * The point the player is moving, teleporting etc... */ - private transient Point targetLocation; + private transient Point3D targetLocation; /** * Creates a new instance @@ -502,7 +502,7 @@ public class L2Character extends Player { /** * @return the targetLocation */ - public Point getTargetLocation() { + public Point3D getTargetLocation() { return targetLocation; } @@ -510,7 +510,7 @@ public class L2Character extends Player { * @param targetLocation * the targetLocation to set */ - public void setTargetLocation(Point targetLocation) { + public void setTargetLocation(Point3D targetLocation) { this.targetLocation = targetLocation; } diff --git a/src/main/java/com/l2jserver/model/world/PositionableObject.java b/src/main/java/com/l2jserver/model/world/PositionableObject.java index 8a0fa4226..c75d9839c 100644 --- a/src/main/java/com/l2jserver/model/world/PositionableObject.java +++ b/src/main/java/com/l2jserver/model/world/PositionableObject.java @@ -16,8 +16,8 @@ */ package com.l2jserver.model.world; -import com.l2jserver.util.dimensional.Coordinate; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Coordinate; +import com.l2jserver.util.geometry.Point3D; /** * This is an abstract object that objects that can be placed in world should @@ -29,12 +29,12 @@ public abstract class PositionableObject extends AbstractObject { /** * The point this object is currently in */ - private Point point; + private Point3D point; /** * @return the coordinate point */ - public Point getPoint() { + public Point3D getPoint() { return point; } @@ -42,7 +42,7 @@ public abstract class PositionableObject extends AbstractObject { * @param point * the coordinate point to set */ - public void setPoint(Point point) { + public void setPoint(Point3D point) { this.point = point; } @@ -51,6 +51,6 @@ public abstract class PositionableObject extends AbstractObject { } public void setPosition(Coordinate coord) { - this.point = new Point(coord, (point != null ? point.getAngle() : 0)); + this.point = new Point3D(coord, (point != null ? point.getAngle() : 0)); } } diff --git a/src/main/java/com/l2jserver/model/world/actor/event/ActorSpawnEvent.java b/src/main/java/com/l2jserver/model/world/actor/event/ActorSpawnEvent.java index 8b77650a2..646cde1c4 100644 --- a/src/main/java/com/l2jserver/model/world/actor/event/ActorSpawnEvent.java +++ b/src/main/java/com/l2jserver/model/world/actor/event/ActorSpawnEvent.java @@ -20,7 +20,7 @@ import com.l2jserver.model.id.ObjectID; import com.l2jserver.model.world.Actor; import com.l2jserver.model.world.WorldObject; import com.l2jserver.model.world.event.SpawnEvent; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event dispatcher once an actor has spawned in the world @@ -35,7 +35,7 @@ public class ActorSpawnEvent implements ActorEvent, SpawnEvent { /** * The spawning point */ - private final Point point; + private final Point3D point; /** * Creates a new instance @@ -45,7 +45,7 @@ public class ActorSpawnEvent implements ActorEvent, SpawnEvent { * @param point * the spawn point */ - public ActorSpawnEvent(Actor actor, Point point) { + public ActorSpawnEvent(Actor actor, Point3D point) { this.actor = actor; this.point = point; } @@ -61,7 +61,7 @@ public class ActorSpawnEvent implements ActorEvent, SpawnEvent { } @Override - public Point getPoint() { + public Point3D getPoint() { return point; } diff --git a/src/main/java/com/l2jserver/model/world/character/event/CharacterMoveEvent.java b/src/main/java/com/l2jserver/model/world/character/event/CharacterMoveEvent.java index 32046f041..dfa073446 100644 --- a/src/main/java/com/l2jserver/model/world/character/event/CharacterMoveEvent.java +++ b/src/main/java/com/l2jserver/model/world/character/event/CharacterMoveEvent.java @@ -21,7 +21,7 @@ import com.l2jserver.model.world.Actor; import com.l2jserver.model.world.L2Character; import com.l2jserver.model.world.Player; import com.l2jserver.model.world.WorldObject; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event triggered once a character moves @@ -36,7 +36,7 @@ public class CharacterMoveEvent implements CharacterEvent { /** * The old point of the character */ - private final Point point; + private final Point3D point; /** * Creates a new instance @@ -46,7 +46,7 @@ public class CharacterMoveEvent implements CharacterEvent { * @param point * the character point before moving */ - public CharacterMoveEvent(L2Character character, Point point) { + public CharacterMoveEvent(L2Character character, Point3D point) { this.character = character; this.point = point; } @@ -54,7 +54,7 @@ public class CharacterMoveEvent implements CharacterEvent { /** * @return the old point */ - public Point getPoint() { + public Point3D getPoint() { return point; } diff --git a/src/main/java/com/l2jserver/model/world/character/event/CharacterStopMoveEvent.java b/src/main/java/com/l2jserver/model/world/character/event/CharacterStopMoveEvent.java index 74427e05a..89d83ab13 100644 --- a/src/main/java/com/l2jserver/model/world/character/event/CharacterStopMoveEvent.java +++ b/src/main/java/com/l2jserver/model/world/character/event/CharacterStopMoveEvent.java @@ -21,7 +21,7 @@ import com.l2jserver.model.world.Actor; import com.l2jserver.model.world.L2Character; import com.l2jserver.model.world.Player; import com.l2jserver.model.world.WorldObject; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event triggered once a character moves @@ -36,7 +36,7 @@ public class CharacterStopMoveEvent implements CharacterEvent { /** * The new point of the character */ - private final Point point; + private final Point3D point; /** * Creates a new instance @@ -46,7 +46,7 @@ public class CharacterStopMoveEvent implements CharacterEvent { * @param point * the character point */ - public CharacterStopMoveEvent(L2Character character, Point point) { + public CharacterStopMoveEvent(L2Character character, Point3D point) { this.character = character; this.point = point; } @@ -54,7 +54,7 @@ public class CharacterStopMoveEvent implements CharacterEvent { /** * @return the point */ - public Point getPoint() { + public Point3D getPoint() { return point; } diff --git a/src/main/java/com/l2jserver/model/world/event/SpawnEvent.java b/src/main/java/com/l2jserver/model/world/event/SpawnEvent.java index 440e7606e..892ea8efa 100644 --- a/src/main/java/com/l2jserver/model/world/event/SpawnEvent.java +++ b/src/main/java/com/l2jserver/model/world/event/SpawnEvent.java @@ -17,7 +17,7 @@ package com.l2jserver.model.world.event; import com.l2jserver.service.game.world.event.WorldEvent; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event for objects spawning @@ -28,5 +28,5 @@ public interface SpawnEvent extends WorldEvent { /** * @return the spawning point */ - Point getPoint(); + Point3D getPoint(); } \ No newline at end of file diff --git a/src/main/java/com/l2jserver/model/world/npc/event/NPCSpawnEvent.java b/src/main/java/com/l2jserver/model/world/npc/event/NPCSpawnEvent.java index 6529a933a..9cc5c6914 100644 --- a/src/main/java/com/l2jserver/model/world/npc/event/NPCSpawnEvent.java +++ b/src/main/java/com/l2jserver/model/world/npc/event/NPCSpawnEvent.java @@ -18,7 +18,7 @@ package com.l2jserver.model.world.npc.event; import com.l2jserver.model.world.NPC; import com.l2jserver.model.world.actor.event.ActorSpawnEvent; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event dispatched once a {@link NPC} has spawned in the world. @@ -32,7 +32,7 @@ public class NPCSpawnEvent extends ActorSpawnEvent implements NPCEvent { * @param point * the spawn point */ - public NPCSpawnEvent(NPC npc, Point point) { + public NPCSpawnEvent(NPC npc, Point3D point) { super(npc, point); } diff --git a/src/main/java/com/l2jserver/model/world/player/event/PlayerSpawnEvent.java b/src/main/java/com/l2jserver/model/world/player/event/PlayerSpawnEvent.java index 8f7866e32..2bc1978d5 100644 --- a/src/main/java/com/l2jserver/model/world/player/event/PlayerSpawnEvent.java +++ b/src/main/java/com/l2jserver/model/world/player/event/PlayerSpawnEvent.java @@ -18,7 +18,7 @@ package com.l2jserver.model.world.player.event; import com.l2jserver.model.world.Player; import com.l2jserver.model.world.actor.event.ActorSpawnEvent; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event dispatcher once an player has spawned in the world @@ -32,7 +32,7 @@ public class PlayerSpawnEvent extends ActorSpawnEvent implements PlayerEvent { * @param point * the spawn point */ - public PlayerSpawnEvent(Player player, Point point) { + public PlayerSpawnEvent(Player player, Point3D point) { super(player, point); } diff --git a/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportedEvent.java b/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportedEvent.java index b272aae22..001adb8f1 100644 --- a/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportedEvent.java +++ b/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportedEvent.java @@ -17,7 +17,7 @@ package com.l2jserver.model.world.player.event; import com.l2jserver.model.world.Player; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event dispatched once an player has completed its teleportation to another @@ -34,7 +34,7 @@ public class PlayerTeleportedEvent extends PlayerSpawnEvent { * @param point * the teleport point */ - public PlayerTeleportedEvent(Player player, Point point) { + public PlayerTeleportedEvent(Player player, Point3D point) { super(player, point); } } diff --git a/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportingEvent.java b/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportingEvent.java index 1ea4fa7f8..f26952fe2 100644 --- a/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportingEvent.java +++ b/src/main/java/com/l2jserver/model/world/player/event/PlayerTeleportingEvent.java @@ -20,7 +20,7 @@ import com.l2jserver.model.id.ObjectID; import com.l2jserver.model.world.Actor; import com.l2jserver.model.world.Player; import com.l2jserver.model.world.WorldObject; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Point3D; /** * Event dispatched once an player has started his teleported to another @@ -30,7 +30,7 @@ import com.l2jserver.util.dimensional.Point; */ public class PlayerTeleportingEvent implements PlayerEvent { private final Player player; - private final Point point; + private final Point3D point; /** * Creates a new instance @@ -40,7 +40,7 @@ public class PlayerTeleportingEvent implements PlayerEvent { * @param point * the teleport point */ - public PlayerTeleportingEvent(Player player, Point point) { + public PlayerTeleportingEvent(Player player, Point3D point) { this.player = player; this.point = point; } @@ -65,7 +65,7 @@ public class PlayerTeleportingEvent implements PlayerEvent { * * @return the teleported point */ - public Point getPoint() { + public Point3D getPoint() { return point; } diff --git a/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java b/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java new file mode 100644 index 000000000..d4119a5cc --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/AbstractReferenceCache.java @@ -0,0 +1,113 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.l2jserver.service.cache.SoftCacheService.SoftCache; +import com.l2jserver.util.factory.CollectionFactory; +import com.sun.beans.WeakCache; + +/** + * Base class for {@link WeakCache} and {@link SoftCache} + * + * @author Rogiel + * + * @param + * the key type + * @param + * the value type + */ +abstract class AbstractReferenceCache implements Cache { + protected final Logger log = LoggerFactory.getLogger(this.getClass()); + + /** + * The cache name + */ + protected final String cacheName; + + /** + * Map storing references to cached objects + */ + protected final Map> cacheMap = CollectionFactory.newMap(); + /** + * The reference queue + */ + protected final ReferenceQueue refQueue = CollectionFactory + .newReferenceQueue(); + + /** + * @param cacheName + * the cache name + */ + protected AbstractReferenceCache(String cacheName) { + this.cacheName = cacheName; + } + + @Override + public void put(K key, V value) { + cleanQueue(); + + Reference entry = newReference(key, value, refQueue); + cacheMap.put(key, entry); + + log.debug("{}: added for key: {}", cacheName, key); + } + + @Override + public V get(K key) { + cleanQueue(); + + Reference reference = cacheMap.get(key); + if (reference == null) + return null; + + V res = reference.get(); + if (res != null) + log.debug("{}: obtained for key: {}", cacheName, key); + + return res; + } + + @Override + public boolean contains(K key) { + cleanQueue(); + return cacheMap.containsKey(key); + } + + protected abstract void cleanQueue(); + + @Override + public void remove(K key) { + cacheMap.remove(key); + log.debug("{}: removed for key: {}", cacheName, key); + } + + @Override + public void clear() { + cacheMap.clear(); + log.debug("{}: cleared", cacheName); + } + + protected abstract Reference newReference(K key, V value, + ReferenceQueue queue); +} diff --git a/src/main/java/com/l2jserver/service/cache/Cache.java b/src/main/java/com/l2jserver/service/cache/Cache.java new file mode 100644 index 000000000..370b2e69f --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/Cache.java @@ -0,0 +1,64 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +/** + * This interface represents a Map structure for cache usage. + * + * @author Rogiel + */ +public interface Cache { + /** + * Adds a pair to cache.
+ *
+ * + * NOTICE: if there is already a value with + * given id in the map, {@link IllegalArgumentException} will be thrown. + * + * @param key + * @param value + */ + void put(K key, V value); + + /** + * Returns cached value correlated to given key. + * + * @param key + * @return + */ + V get(K key); + + /** + * Checks whether this map contains a value related to given key. + * + * @param key + * @return + */ + boolean contains(K key); + + /** + * Removes an entry from the map, that has given key. + * + * @param key + */ + void remove(K key); + + /** + * Clears this cache + */ + void clear(); +} diff --git a/src/main/java/com/l2jserver/service/cache/CacheService.java b/src/main/java/com/l2jserver/service/cache/CacheService.java index 5b0e17a46..7de117c10 100644 --- a/src/main/java/com/l2jserver/service/cache/CacheService.java +++ b/src/main/java/com/l2jserver/service/cache/CacheService.java @@ -16,8 +16,6 @@ */ package com.l2jserver.service.cache; -import net.sf.ehcache.Cache; - import com.l2jserver.service.Service; /** @@ -31,7 +29,6 @@ import com.l2jserver.service.Service; * {@link IgnoreCaching} * * @author Rogiel - * */ public interface CacheService extends Service { /** @@ -50,39 +47,60 @@ public interface CacheService extends Service { /** * Creates a new cache with default configurations. Eviction mode is LRU - * (Last Recently Used). If you wish more customization, you should manually - * create the cache and register it using {@link #register(Cache)}. + * (Last Recently Used). The size is only a guarantee that you can store + * at least n items. * + * @param + * the cache key type + * @param + * the cache value type * @param name * the cache name * @size the maximum cache size * @return the created cache */ - Cache createCache(String name, int size); + Cache createCache(String name, int size); + + /** + * Creates a new eternal cache with default configurations. An eternal cache + * is guaranteed to never automatically expire items. The size is only a + * guarantee that you can store at least n items. + * + * @param + * the cache key type + * @param + * the cache value type + * @param name + * the cache name + * @size the maximum cache size + * @return the created cache + */ + Cache createEternalCache(String name, int size); /** * Creates a new cache with default configurations. The default cache size - * is 200. + * is 200. The size is only a guarantee that you can store at least + * 200 items. * + * @param + * the cache key type + * @param + * the cache value type * @param name * the cache name * @return the created cache */ - Cache createCache(String name); + Cache createCache(String name); /** - * Registers a new cache + * Disposes the cache. Once the cache is disposed it cannot be used anymore. * + * @param + * the cache key type + * @param + * the cache value type * @param cache * the cache */ - void register(Cache cache); - - /** - * Unregisters an already registered cache - * - * @param cache - * the cache - */ - void unregister(Cache cache); + void dispose(Cache cache); } diff --git a/src/main/java/com/l2jserver/service/cache/EhCacheService.java b/src/main/java/com/l2jserver/service/cache/EhCacheService.java index 7ba709670..07d19e95d 100644 --- a/src/main/java/com/l2jserver/service/cache/EhCacheService.java +++ b/src/main/java/com/l2jserver/service/cache/EhCacheService.java @@ -20,9 +20,7 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.Arrays; -import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.config.CacheConfiguration; @@ -48,7 +46,7 @@ public class EhCacheService extends AbstractService implements CacheService { /** * The interface cache */ - private Cache interfaceCache; + private Cache interfaceCache; @Override protected void doStart() throws ServiceStartException { @@ -76,10 +74,7 @@ public class EhCacheService extends AbstractService implements CacheService { return method.invoke(instance, args); final MethodInvocation invocation = new MethodInvocation( method, args); - Element element = interfaceCache.get(invocation); - if (element == null) - return doInvoke(invocation, proxy, method, args); - Object result = element.getObjectValue(); + Object result = interfaceCache.get(invocation); if (result == null) return doInvoke(invocation, proxy, method, args); return result; @@ -90,43 +85,94 @@ public class EhCacheService extends AbstractService implements CacheService { throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Object result = method.invoke(instance, args); - interfaceCache.put(new Element(invocation, result)); + interfaceCache.put(invocation, result); return result; } }); return proxy; } + // @Override + // public Cache createCache(String name, int size) { + // Preconditions.checkNotNull(name, "name"); + // Preconditions.checkArgument(size > 0, "size <= 0"); + // + // Cache cache = new Cache(new CacheConfiguration(name, size) + // .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) + // .overflowToDisk(true).eternal(false).timeToLiveSeconds(60) + // .timeToIdleSeconds(30).diskPersistent(false) + // .diskExpiryThreadIntervalSeconds(0)); + // register(cache); + // return cache; + // } + // + // @Override + // public Cache createCache(String name) { + // Preconditions.checkNotNull(name, "name"); + // return createCache(name, 200); + // } + // + // @Override + // public void register(Cache cache) { + // Preconditions.checkNotNull(cache, "cache"); + // manager.addCache(cache); + // } + // + // @Override + // public void unregister(Cache cache) { + // Preconditions.checkNotNull(cache, "cache"); + // manager.removeCache(cache.getName()); + // } + @Override - public Cache createCache(String name, int size) { + public Cache createCache(String name, int size) { Preconditions.checkNotNull(name, "name"); Preconditions.checkArgument(size > 0, "size <= 0"); - Cache cache = new Cache(new CacheConfiguration(name, size) - .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) - .overflowToDisk(true).eternal(false).timeToLiveSeconds(60) - .timeToIdleSeconds(30).diskPersistent(false) - .diskExpiryThreadIntervalSeconds(0)); - register(cache); - return cache; - } - - @Override - public Cache createCache(String name) { - Preconditions.checkNotNull(name, "name"); - return createCache(name, 200); - } - - @Override - public void register(Cache cache) { - Preconditions.checkNotNull(cache, "cache"); + net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache( + new CacheConfiguration(name, size) + .memoryStoreEvictionPolicy( + MemoryStoreEvictionPolicy.LRU) + .overflowToDisk(true).eternal(false) + .timeToLiveSeconds(60).timeToIdleSeconds(30) + .diskPersistent(false) + .diskExpiryThreadIntervalSeconds(0)); manager.addCache(cache); + return new EhCacheFacade(cache); } @Override - public void unregister(Cache cache) { - Preconditions.checkNotNull(cache, "cache"); - manager.removeCache(cache.getName()); + public Cache createEternalCache(String name, int size) { + Preconditions.checkNotNull(name, "name"); + Preconditions.checkArgument(size > 0, "size <= 0"); + + net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache( + new CacheConfiguration(name, size) + .memoryStoreEvictionPolicy( + MemoryStoreEvictionPolicy.LRU) + .overflowToDisk(true).eternal(true) + .diskExpiryThreadIntervalSeconds(0)); + manager.addCache(cache); + return new EhCacheFacade(cache); + } + + @Override + public Cache createCache(String name) { + net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache( + new CacheConfiguration(name, 200) + .memoryStoreEvictionPolicy( + MemoryStoreEvictionPolicy.LRU) + .overflowToDisk(true).eternal(true) + .diskExpiryThreadIntervalSeconds(0)); + manager.addCache(cache); + return new EhCacheFacade(cache); + } + + @Override + public void dispose(Cache cache) { + if (cache instanceof EhCacheFacade) { + manager.removeCache(((EhCacheFacade) cache).cache.getName()); + } } @Override @@ -136,42 +182,40 @@ public class EhCacheService extends AbstractService implements CacheService { interfaceCache = null; } - private static class MethodInvocation { - private final Method method; - private final Object[] args; + private class EhCacheFacade implements Cache { + private final net.sf.ehcache.Cache cache; - public MethodInvocation(Method method, Object[] args) { - this.method = method; - this.args = args; + public EhCacheFacade(net.sf.ehcache.Cache cache) { + this.cache = cache; } @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Arrays.hashCode(args); - result = prime * result - + ((method == null) ? 0 : method.hashCode()); - return result; + public void put(K key, V value) { + cache.put(new Element(key, value)); } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - MethodInvocation other = (MethodInvocation) obj; - if (!Arrays.equals(args, other.args)) - return false; - if (method == null) { - if (other.method != null) - return false; - } else if (!method.equals(other.method)) - return false; - return true; + @SuppressWarnings("unchecked") + public V get(K key) { + final Element element = cache.get(key); + if (element == null) + return null; + return (V) element.getValue(); + } + + @Override + public boolean contains(K key) { + return cache.get(key) != null; + } + + @Override + public void remove(K key) { + cache.remove(key); + } + + @Override + public void clear() { + cache.removeAll(); } } } diff --git a/src/main/java/com/l2jserver/service/cache/EternalCache.java b/src/main/java/com/l2jserver/service/cache/EternalCache.java new file mode 100644 index 000000000..41d55da7d --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/EternalCache.java @@ -0,0 +1,87 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.l2jserver.util.factory.CollectionFactory; + +/** + * Cache class for an eternal cache + * + * @author Rogiel + * + * @param + * the key type + * @param + * the value type + */ +class EternalCache implements Cache { + protected final Logger log = LoggerFactory.getLogger(this.getClass()); + + /** + * The cache name + */ + protected final String cacheName; + + /** + * Map storing references to cached objects + */ + protected final Map cacheMap = CollectionFactory.newMap(); + + /** + * @param cacheName + * the cache name + */ + protected EternalCache(String cacheName) { + this.cacheName = cacheName; + } + + @Override + public void put(K key, V value) { + cacheMap.put(key, value); + log.debug("{}: added for key: {}", cacheName, key); + } + + @Override + public V get(K key) { + V obj = cacheMap.get(key); + if (obj != null) + log.debug("{}: obtained for key: {}", cacheName, key); + return obj; + } + + @Override + public boolean contains(K key) { + return cacheMap.containsKey(key); + } + + @Override + public void remove(K key) { + cacheMap.remove(key); + log.debug("{}: removed for key: {}", cacheName, key); + } + + @Override + public void clear() { + cacheMap.clear(); + log.debug("{}: cleared", cacheName); + } +} diff --git a/src/main/java/com/l2jserver/service/cache/MethodInvocation.java b/src/main/java/com/l2jserver/service/cache/MethodInvocation.java new file mode 100644 index 000000000..84a3bb386 --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/MethodInvocation.java @@ -0,0 +1,69 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +import java.lang.reflect.Method; +import java.util.Arrays; + +/** + * Simple class used to store method invocations for the proxied cache. + * + * @author Rogiel + */ +class MethodInvocation { + /** + * The invoked method + */ + private final Method method; + /** + * The invocation arguments + */ + private final Object[] args; + + public MethodInvocation(Method method, Object[] args) { + this.method = method; + this.args = args; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(args); + result = prime * result + ((method == null) ? 0 : method.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MethodInvocation other = (MethodInvocation) obj; + if (!Arrays.equals(args, other.args)) + return false; + if (method == null) { + if (other.method != null) + return false; + } else if (!method.equals(other.method)) + return false; + return true; + } +} \ No newline at end of file diff --git a/src/main/java/com/l2jserver/service/cache/SoftCacheService.java b/src/main/java/com/l2jserver/service/cache/SoftCacheService.java new file mode 100644 index 000000000..edddab5bf --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/SoftCacheService.java @@ -0,0 +1,164 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import com.google.common.base.Preconditions; +import com.l2jserver.service.AbstractService; +import com.l2jserver.service.ServiceStartException; +import com.l2jserver.service.ServiceStopException; + +/** + * This {@link Cache} service implementation uses a {@link SoftReference} to + * store values. + * + * @author Rogiel + */ +public class SoftCacheService extends AbstractService implements CacheService { + /** + * The interface cache + */ + private Cache interfaceCache; + + @Override + protected void doStart() throws ServiceStartException { + interfaceCache = createCache("interface-cache"); + } + + @Override + public T decorate(final Class interfaceType, + final T instance) { + Preconditions.checkNotNull(interfaceType, "interfaceType"); + Preconditions.checkNotNull(instance, "instance"); + + if (!interfaceType.isInterface()) + return null; + @SuppressWarnings("unchecked") + final T proxy = (T) Proxy.newProxyInstance(this.getClass() + .getClassLoader(), new Class[] { interfaceType }, + new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, + Object[] args) throws Throwable { + if (method.isAnnotationPresent(IgnoreCaching.class)) + return method.invoke(instance, args); + final MethodInvocation invocation = new MethodInvocation( + method, args); + Object result = interfaceCache.get(invocation); + if (result == null) + return doInvoke(invocation, proxy, method, args); + return result; + } + + private Object doInvoke(MethodInvocation invocation, + Object proxy, Method method, Object[] args) + throws IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + Object result = method.invoke(instance, args); + interfaceCache.put(invocation, result); + return result; + } + }); + return proxy; + } + + @Override + public Cache createCache(String name, int size) { + return new SoftCache(name); + } + + @Override + public Cache createEternalCache(String name, int size) { + return new EternalCache(name); + } + + @Override + public Cache createCache(String name) { + return new SoftCache(name); + } + + @Override + public void dispose(Cache cache) { + cache.clear(); + } + + @Override + protected void doStop() throws ServiceStopException { + dispose(interfaceCache); + interfaceCache = null; + } + + /** + * This class is a simple map implementation for cache usage.
+ *
+ * Value may be stored in map really long, but it for sure will be removed + * if there is low memory (and of course there isn't any strong reference to + * value object) + * + * @author Rogiel + */ + public class SoftCache extends AbstractReferenceCache implements + Cache { + /** + * This class is a {@link SoftReference} with additional responsibility + * of holding key object + * + * @author Rogiel + */ + private class SoftEntry extends SoftReference { + private K key; + + SoftEntry(K key, V referent, ReferenceQueue q) { + super(referent, q); + this.key = key; + } + + K getKey() { + return key; + } + } + + public SoftCache(String cacheName) { + super(cacheName); + } + + @Override + @SuppressWarnings("unchecked") + protected synchronized void cleanQueue() { + SoftEntry en = null; + while ((en = (SoftEntry) refQueue.poll()) != null) { + K key = en.getKey(); + if (log.isDebugEnabled()) + log.debug("{} : cleaned up {} for key: {}", cacheName, key); + cacheMap.remove(key); + } + } + + @Override + protected Reference newReference(K key, V value, + ReferenceQueue vReferenceQueue) { + return new SoftEntry(key, value, vReferenceQueue); + } + } +} diff --git a/src/main/java/com/l2jserver/service/cache/WeakCacheService.java b/src/main/java/com/l2jserver/service/cache/WeakCacheService.java new file mode 100644 index 000000000..b5cca9590 --- /dev/null +++ b/src/main/java/com/l2jserver/service/cache/WeakCacheService.java @@ -0,0 +1,164 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.service.cache; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; +import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import com.google.common.base.Preconditions; +import com.l2jserver.service.AbstractService; +import com.l2jserver.service.ServiceStartException; +import com.l2jserver.service.ServiceStopException; + +/** + * This {@link Cache} service implementation uses a {@link SoftReference} to + * store values. + * + * @author Rogiel + */ +public class WeakCacheService extends AbstractService implements CacheService { + /** + * The interface cache + */ + private Cache interfaceCache; + + @Override + protected void doStart() throws ServiceStartException { + interfaceCache = createCache("interface-cache"); + } + + @Override + public T decorate(final Class interfaceType, + final T instance) { + Preconditions.checkNotNull(interfaceType, "interfaceType"); + Preconditions.checkNotNull(instance, "instance"); + + if (!interfaceType.isInterface()) + return null; + @SuppressWarnings("unchecked") + final T proxy = (T) Proxy.newProxyInstance(this.getClass() + .getClassLoader(), new Class[] { interfaceType }, + new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, + Object[] args) throws Throwable { + if (method.isAnnotationPresent(IgnoreCaching.class)) + return method.invoke(instance, args); + final MethodInvocation invocation = new MethodInvocation( + method, args); + Object result = interfaceCache.get(invocation); + if (result == null) + return doInvoke(invocation, proxy, method, args); + return result; + } + + private Object doInvoke(MethodInvocation invocation, + Object proxy, Method method, Object[] args) + throws IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + Object result = method.invoke(instance, args); + interfaceCache.put(invocation, result); + return result; + } + }); + return proxy; + } + + @Override + public Cache createCache(String name, int size) { + return new WeakCache(name); + } + + @Override + public Cache createEternalCache(String name, int size) { + return new EternalCache(name); + } + + @Override + public Cache createCache(String name) { + return new WeakCache(name); + } + + @Override + public void dispose(Cache cache) { + cache.clear(); + } + + @Override + protected void doStop() throws ServiceStopException { + dispose(interfaceCache); + interfaceCache = null; + } + + /** + * This class is a simple map implementation for cache usage.
+ *
+ * Values from the map will be removed after the first garbage collector run + * if there isn't any strong reference to the value object. + * + * @author Rogiel + */ + private class WeakCache extends AbstractReferenceCache + implements Cache { + /** + * This class is a {@link WeakReference} with additional responsibility + * of holding key object + * + * @author Rogiel + */ + private class Entry extends WeakReference { + private K key; + + Entry(K key, V referent, ReferenceQueue q) { + super(referent, q); + this.key = key; + } + + K getKey() { + return key; + } + } + + WeakCache(String cacheName) { + super(cacheName); + } + + @Override + @SuppressWarnings("unchecked") + protected synchronized void cleanQueue() { + Entry en = null; + while ((en = (Entry) refQueue.poll()) != null) { + K key = en.getKey(); + if (log.isDebugEnabled()) + log.debug("{}: cleaned up for key: {}", cacheName, key); + cacheMap.remove(key); + } + } + + @Override + protected Reference newReference(K key, V value, + ReferenceQueue vReferenceQueue) { + return new Entry(key, value, vReferenceQueue); + } + } +} diff --git a/src/main/java/com/l2jserver/service/database/JDBCDatabaseService.java b/src/main/java/com/l2jserver/service/database/JDBCDatabaseService.java index 438ae9e01..8fcc392a0 100644 --- a/src/main/java/com/l2jserver/service/database/JDBCDatabaseService.java +++ b/src/main/java/com/l2jserver/service/database/JDBCDatabaseService.java @@ -28,11 +28,6 @@ import java.util.List; import javax.sql.DataSource; -import net.sf.ehcache.Cache; -import net.sf.ehcache.Element; -import net.sf.ehcache.config.CacheConfiguration; -import net.sf.ehcache.store.MemoryStoreEvictionPolicy; - import org.apache.commons.dbcp.ConnectionFactory; import org.apache.commons.dbcp.DriverManagerConnectionFactory; import org.apache.commons.dbcp.PoolableConnectionFactory; @@ -53,6 +48,7 @@ import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService.Depends; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStopException; +import com.l2jserver.service.cache.Cache; import com.l2jserver.service.cache.CacheService; import com.l2jserver.service.configuration.ConfigurationService; import com.l2jserver.service.core.LoggingService; @@ -105,7 +101,7 @@ public class JDBCDatabaseService extends AbstractService implements /** * An cache object */ - private Cache objectCache; + private Cache objectCache; @Inject public JDBCDatabaseService(ConfigurationService configService, @@ -125,12 +121,8 @@ public class JDBCDatabaseService extends AbstractService implements // cache must be large enough for all world objects, to avoid // duplication... this would endanger non-persistent states - objectCache = new Cache(new CacheConfiguration("database-service", - IDAllocator.ALLOCABLE_IDS) - .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) - .overflowToDisk(true).eternal(true).diskPersistent(false) - .diskExpiryThreadIntervalSeconds(0)); - cacheService.register(objectCache); + objectCache = cacheService.createEternalCache("database-service", + IDAllocator.ALLOCABLE_IDS); } @Override @@ -186,21 +178,18 @@ public class JDBCDatabaseService extends AbstractService implements public Object getCachedObject(Object id) { Preconditions.checkNotNull(id, "id"); - final Element element = objectCache.get(id); - if (element == null) - return null; - return element.getObjectValue(); + return objectCache.get(id); } public boolean hasCachedObject(Object id) { Preconditions.checkNotNull(id, "id"); - return objectCache.get(id) != null; + return objectCache.contains(id); } public void updateCache(Object key, Object value) { Preconditions.checkNotNull(key, "key"); Preconditions.checkNotNull(value, "value"); - objectCache.put(new Element(key, value)); + objectCache.put(key, value); } public void removeCache(Object key) { @@ -210,8 +199,7 @@ public class JDBCDatabaseService extends AbstractService implements @Override protected void doStop() throws ServiceStopException { - if (objectCache != null) - objectCache.dispose(); + cacheService.dispose(objectCache); objectCache = null; try { diff --git a/src/main/java/com/l2jserver/service/game/ai/AIService.java b/src/main/java/com/l2jserver/service/game/ai/AIService.java index 7f9b9b148..3adaf9c66 100644 --- a/src/main/java/com/l2jserver/service/game/ai/AIService.java +++ b/src/main/java/com/l2jserver/service/game/ai/AIService.java @@ -18,7 +18,7 @@ package com.l2jserver.service.game.ai; import com.l2jserver.model.world.Actor; import com.l2jserver.service.Service; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * This service executes AI operations diff --git a/src/main/java/com/l2jserver/service/game/ai/AIServiceImpl.java b/src/main/java/com/l2jserver/service/game/ai/AIServiceImpl.java index 977eb7156..6c67ee684 100644 --- a/src/main/java/com/l2jserver/service/game/ai/AIServiceImpl.java +++ b/src/main/java/com/l2jserver/service/game/ai/AIServiceImpl.java @@ -27,7 +27,7 @@ import com.l2jserver.service.game.template.TemplateService; 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; +import com.l2jserver.util.geometry.Coordinate; /** * Default implementation for {@link AIService}. diff --git a/src/main/java/com/l2jserver/service/game/ai/script/WalkingAIScript.java b/src/main/java/com/l2jserver/service/game/ai/script/WalkingAIScript.java index affec4a87..cbb1efc3e 100644 --- a/src/main/java/com/l2jserver/service/game/ai/script/WalkingAIScript.java +++ b/src/main/java/com/l2jserver/service/game/ai/script/WalkingAIScript.java @@ -18,7 +18,7 @@ package com.l2jserver.service.game.ai.script; import com.l2jserver.model.world.PositionableObject; import com.l2jserver.service.game.ai.AIScript; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; /** * @author RogielThis implementation does not validate pathing! @@ -97,7 +97,7 @@ public class MapperPathingService extends AbstractService implements CharacterMoveEvent.class) { @Override protected boolean dispatch(CharacterMoveEvent e) { - final Point point = e.getPoint(); + final Point3D point = e.getPoint(); final CoordinateStruct struct = CoordinateStruct .fromCoordinate(point.getCoordinate()); try { diff --git a/src/main/java/com/l2jserver/service/game/spawn/SpawnService.java b/src/main/java/com/l2jserver/service/game/spawn/SpawnService.java index d2c26720d..506dae575 100644 --- a/src/main/java/com/l2jserver/service/game/spawn/SpawnService.java +++ b/src/main/java/com/l2jserver/service/game/spawn/SpawnService.java @@ -24,8 +24,8 @@ import com.l2jserver.model.world.event.SpawnEvent; import com.l2jserver.model.world.player.event.PlayerTeleportedEvent; import com.l2jserver.model.world.player.event.PlayerTeleportingEvent; import com.l2jserver.service.Service; -import com.l2jserver.util.dimensional.Coordinate; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Coordinate; +import com.l2jserver.util.geometry.Point3D; /** * This service is responsible for spawning monsters, npcs and players. @@ -50,7 +50,7 @@ public interface SpawnService extends Service { * @throws AlreadySpawnedServiceException * if the object is already spawned in the world */ - void spawn(PositionableObject object, Point point) + void spawn(PositionableObject object, Point3D point) throws SpawnPointNotFoundServiceException, AlreadySpawnedServiceException; diff --git a/src/main/java/com/l2jserver/service/game/spawn/SpawnServiceImpl.java b/src/main/java/com/l2jserver/service/game/spawn/SpawnServiceImpl.java index 75e44f928..16101bf22 100644 --- a/src/main/java/com/l2jserver/service/game/spawn/SpawnServiceImpl.java +++ b/src/main/java/com/l2jserver/service/game/spawn/SpawnServiceImpl.java @@ -37,8 +37,8 @@ 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; -import com.l2jserver.util.dimensional.Point; +import com.l2jserver.util.geometry.Coordinate; +import com.l2jserver.util.geometry.Point3D; /** * Default implementation for {@link SpawnService} @@ -69,7 +69,7 @@ public class SpawnServiceImpl extends AbstractService implements SpawnService { } @Override - public void spawn(PositionableObject object, Point point) + public void spawn(PositionableObject object, Point3D point) throws SpawnPointNotFoundServiceException { Preconditions.checkNotNull(object, "object"); // sanitize diff --git a/src/main/java/com/l2jserver/service/game/template/ScriptTemplateService.java b/src/main/java/com/l2jserver/service/game/template/ScriptTemplateService.java index cba026536..b78495980 100644 --- a/src/main/java/com/l2jserver/service/game/template/ScriptTemplateService.java +++ b/src/main/java/com/l2jserver/service/game/template/ScriptTemplateService.java @@ -96,11 +96,6 @@ public class ScriptTemplateService extends AbstractService implements // TODO templates.remove(t); } - @Override - public void reload() { - context.reload(); - } - @Override protected void doStop() throws ServiceStopException { if (context.isInitialized()) diff --git a/src/main/java/com/l2jserver/service/game/template/TemplateService.java b/src/main/java/com/l2jserver/service/game/template/TemplateService.java index 94dfe42ad..1f09642f2 100644 --- a/src/main/java/com/l2jserver/service/game/template/TemplateService.java +++ b/src/main/java/com/l2jserver/service/game/template/TemplateService.java @@ -31,9 +31,4 @@ public interface TemplateService extends Service { * @return the template */ > T getTemplate(TemplateID id); - - /** - * Reload the template list. - */ - void reload(); } diff --git a/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java b/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java index 72e5abd19..35a2436ce 100644 --- a/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java +++ b/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java @@ -19,7 +19,6 @@ package com.l2jserver.service.game.template; import java.io.IOException; import java.io.InputStream; import java.util.List; -import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -46,22 +45,25 @@ import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService.Depends; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStopException; +import com.l2jserver.service.cache.Cache; +import com.l2jserver.service.cache.CacheService; import com.l2jserver.service.configuration.ConfigurationService; import com.l2jserver.service.core.LoggingService; import com.l2jserver.service.core.vfs.VFSService; -import com.l2jserver.util.factory.CollectionFactory; import com.l2jserver.util.jaxb.CharacterTemplateIDAdapter; import com.l2jserver.util.jaxb.ItemTemplateIDAdapter; import com.l2jserver.util.jaxb.NPCTemplateIDAdapter; import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; import com.l2jserver.util.vfs.ExtensionFileSelector; -@Depends({ LoggingService.class, VFSService.class, ConfigurationService.class }) +@Depends({ LoggingService.class, VFSService.class, CacheService.class, + ConfigurationService.class }) public class XMLTemplateService extends AbstractService implements TemplateService { private final Logger log = LoggerFactory.getLogger(this.getClass()); private final VFSService vfsService; + private final CacheService cacheService; private final XMLTemplateServiceConfiguration config; private final NPCTemplateIDAdapter npcTemplateIdAdapter; @@ -73,16 +75,17 @@ public class XMLTemplateService extends AbstractService implements private Unmarshaller unmarshaller; @SuppressWarnings("rawtypes") - private Map templates = CollectionFactory.newMap(); + private Cache templates; @Inject public XMLTemplateService(final VFSService vfsService, - ConfigurationService configService, + CacheService cacheService, ConfigurationService configService, NPCTemplateIDAdapter npcTemplateIdAdapter, ItemTemplateIDAdapter itemTemplateIdAdapter, CharacterTemplateIDAdapter charIdTemplateAdapter, TeleportationTemplateIDAdapter teleportationIdTemplateAdapter) { this.vfsService = vfsService; + this.cacheService = cacheService; this.config = configService.get(XMLTemplateServiceConfiguration.class); this.npcTemplateIdAdapter = npcTemplateIdAdapter; this.itemTemplateIdAdapter = itemTemplateIdAdapter; @@ -92,6 +95,7 @@ public class XMLTemplateService extends AbstractService implements @Override protected void doStart() throws ServiceStartException { + templates = cacheService.createEternalCache("templates", 100 * 1000); try { log.debug("Creating JAXBContext instance"); context = JAXBContext.newInstance(CharacterTemplate.class, @@ -112,9 +116,9 @@ public class XMLTemplateService extends AbstractService implements final FileObject root = vfsService.resolve(config .getTemplateDirectory()); - + log.info("Scanning {} for XML templates", root); - + FileObject[] files = root.findFiles(ExtensionFileSelector .ext("xml")); @@ -167,15 +171,10 @@ public class XMLTemplateService extends AbstractService implements templates.remove(template.getID()); } - @Override - public void reload() { - // TODO Auto-generated method stub - - } - @Override protected void doStop() throws ServiceStopException { - templates.clear(); + cacheService.dispose(templates); + templates = null; unmarshaller = null; context = null; } diff --git a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java index 78d036b0e..002eea2a5 100644 --- a/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java +++ b/src/main/java/com/l2jserver/service/game/world/CachedWorldIDService.java @@ -18,11 +18,6 @@ package com.l2jserver.service.game.world; import java.util.Collection; -import net.sf.ehcache.Cache; -import net.sf.ehcache.Element; -import net.sf.ehcache.config.CacheConfiguration; -import net.sf.ehcache.store.MemoryStoreEvictionPolicy; - import com.google.common.base.Preconditions; import com.google.inject.Inject; import com.l2jserver.db.dao.CharacterDAO; @@ -35,6 +30,7 @@ import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractService.Depends; import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStopException; +import com.l2jserver.service.cache.Cache; import com.l2jserver.service.cache.CacheService; import com.l2jserver.service.database.DatabaseService; @@ -73,7 +69,7 @@ public class CachedWorldIDService extends AbstractService implements /** * The ID cache */ - private Cache cache; + private Cache> cache; /** * The loaded state @@ -94,12 +90,8 @@ public class CachedWorldIDService extends AbstractService implements @Override protected void doStart() throws ServiceStartException { // we allocate an cache which can fit all ids - cache = new Cache(new CacheConfiguration("id-cache", - IDAllocator.ALLOCABLE_IDS) - .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) - .overflowToDisk(true).eternal(true).diskPersistent(false) - .diskExpiryThreadIntervalSeconds(0)); - cacheService.register(cache); + cache = cacheService.createEternalCache("id-cache", + IDAllocator.ALLOCABLE_IDS); } @Override @@ -112,7 +104,7 @@ public class CachedWorldIDService extends AbstractService implements @Override public void unload() { - cache.removeAll(); + cache.clear(); } /** @@ -137,17 +129,13 @@ public class CachedWorldIDService extends AbstractService implements // ignore resolving before all IDs are loaded return null; } - - final Element element = cache.get(id); - if (element == null) - return null; - return (I) element.getObjectValue(); + return (I) cache.get(id); } @Override public > void add(I id) { Preconditions.checkNotNull(id, "id"); - cache.put(new Element(id.getID(), id)); + cache.put(id.getID(), id); } @Override @@ -158,7 +146,7 @@ public class CachedWorldIDService extends AbstractService implements @Override protected void doStop() throws ServiceStopException { - cacheService.unregister(cache); + cacheService.dispose(cache); cache = null; allocator.clear(); } diff --git a/src/main/java/com/l2jserver/service/game/world/filter/impl/KnownListUpdateFilter.java b/src/main/java/com/l2jserver/service/game/world/filter/impl/KnownListUpdateFilter.java index 9215df940..248de5038 100644 --- a/src/main/java/com/l2jserver/service/game/world/filter/impl/KnownListUpdateFilter.java +++ b/src/main/java/com/l2jserver/service/game/world/filter/impl/KnownListUpdateFilter.java @@ -20,7 +20,7 @@ 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; +import com.l2jserver.util.geometry.Point3D; /** * This filter will only accept {@link WorldObject} which are in vision of @@ -32,7 +32,7 @@ public class KnownListUpdateFilter extends AndFilter { public static final int KNOWNLIST_RANGE = 2000; @SuppressWarnings("unchecked") - public KnownListUpdateFilter(PositionableObject object, Point old) { + public KnownListUpdateFilter(PositionableObject object, Point3D old) { super(new KnownListFilter(object), new NotFilter( new RangePointFilter(old, KNOWNLIST_RANGE))); } diff --git a/src/main/java/com/l2jserver/service/game/world/filter/impl/RangePointFilter.java b/src/main/java/com/l2jserver/service/game/world/filter/impl/RangePointFilter.java index 7a260bee8..e288ca00d 100644 --- a/src/main/java/com/l2jserver/service/game/world/filter/impl/RangePointFilter.java +++ b/src/main/java/com/l2jserver/service/game/world/filter/impl/RangePointFilter.java @@ -19,7 +19,7 @@ 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; +import com.l2jserver.util.geometry.Point3D; /** * Filter objects that are in the range of coordinate @@ -30,7 +30,7 @@ public class RangePointFilter implements WorldObjectFilter { /** * The coordinate point */ - private final Point point; + private final Point3D point; /** * The desired maximum distance of the object */ @@ -44,7 +44,7 @@ public class RangePointFilter implements WorldObjectFilter { * @param range * the desired maximum distance of the object */ - public RangePointFilter(final Point point, final int range) { + public RangePointFilter(final Point3D point, final int range) { Preconditions.checkNotNull(point, "point"); Preconditions.checkState(range >= 0, "range < 0"); this.point = point; diff --git a/src/main/java/com/l2jserver/util/MathUtil.java b/src/main/java/com/l2jserver/util/MathUtil.java new file mode 100644 index 000000000..3d6bc3cd3 --- /dev/null +++ b/src/main/java/com/l2jserver/util/MathUtil.java @@ -0,0 +1,211 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util; + +import com.l2jserver.util.geometry.Point; +import com.l2jserver.util.geometry.Point3D; + +/** + * Class with basic math.
+ * Thanks to: + *

    + *
  • http://geom-java.sourceforge.net/
  • + *
  • + * http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/DistancePoint.java
  • + *
+ *
+ *
+ * + * @author Rogiel + */ +public class MathUtil { + /** + * Returns distance between two 2D points + * + * @param point1 + * first point + * @param point2 + * second point + * @return distance between points + */ + public static double getDistance(Point point1, Point point2) { + return getDistance(point1.x, point1.y, point2.x, point2.y); + } + + /** + * Returns distance between two sets of coords + * + * @param x1 + * first x coord + * @param y1 + * first y coord + * @param x2 + * second x coord + * @param y2 + * second y coord + * @return distance between sets of coords + */ + public static double getDistance(int x1, int y1, int x2, int y2) { + // using long to avoid possible overflows when multiplying + long dx = x2 - x1; + long dy = y2 - y1; + + // return Math.hypot(x2 - x1, y2 - y1); // Extremely slow + // return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); // 20 times faster + // than hypot + return Math.sqrt(dx * dx + dy * dy); // 10 times faster then previous + // line + } + + /** + * Returns distance between two 3D points + * + * @param point1 + * first point + * @param point2 + * second point + * @return distance between points + */ + public static double getDistance(Point3D point1, Point3D point2) { + return getDistance(point1.getX(), point1.getY(), point1.getZ(), + point2.getX(), point2.getY(), point2.getZ()); + } + + /** + * Returns distance between 3D set of coords + * + * @param x1 + * first x coord + * @param y1 + * first y coord + * @param z1 + * first z coord + * @param x2 + * second x coord + * @param y2 + * second y coord + * @param z2 + * second z coord + * @return distance between coords + */ + public static double getDistance(float x1, float y1, float z1, float x2, + float y2, float z2) { + float dx = x1 - x2; + float dy = y1 - y2; + float dz = z1 - z2; + + // We should avoid Math.pow or Math.hypot due to performance reasons + return Math.sqrt(dx * dx + dy * dy + dz * dz); + } + + /** + * Returns closest point on segment to point + * + * @param ss + * segment start point + * @param se + * segment end point + * @param p + * point to found closest point on segment + * @return closest point on segment to p + */ + public static Point getClosestPointOnSegment(Point ss, Point se, Point p) { + return getClosestPointOnSegment(ss.x, ss.y, se.x, se.y, p.x, p.y); + } + + /** + * Returns closest point on segment to point + * + * @param sx1 + * segment x coord 1 + * @param sy1 + * segment y coord 1 + * @param sx2 + * segment x coord 2 + * @param sy2 + * segment y coord 2 + * @param px + * point x coord + * @param py + * point y coord + * @return closets point on segment to point + */ + public static Point getClosestPointOnSegment(int sx1, int sy1, int sx2, + int sy2, int px, int py) { + double xDelta = sx2 - sx1; + double yDelta = sy2 - sy1; + + if ((xDelta == 0) && (yDelta == 0)) { + throw new IllegalArgumentException( + "Segment start equals segment end"); + } + + double u = ((px - sx1) * xDelta + (py - sy1) * yDelta) + / (xDelta * xDelta + yDelta * yDelta); + + final Point closestPoint; + if (u < 0) { + closestPoint = new Point(sx1, sy1); + } else if (u > 1) { + closestPoint = new Point(sx2, sy2); + } else { + closestPoint = new Point((int) Math.round(sx1 + u * xDelta), + (int) Math.round(sy1 + u * yDelta)); + } + + return closestPoint; + } + + /** + * Returns distance to segment + * + * @param ss + * segment start point + * @param se + * segment end point + * @param p + * point to found closest point on segment + * @return distance to segment + */ + public static double getDistanceToSegment(Point ss, Point se, Point p) { + return getDistanceToSegment(ss.x, ss.y, se.x, se.y, p.x, p.y); + } + + /** + * Returns distance to segment + * + * @param sx1 + * segment x coord 1 + * @param sy1 + * segment y coord 1 + * @param sx2 + * segment x coord 2 + * @param sy2 + * segment y coord 2 + * @param px + * point x coord + * @param py + * point y coord + * @return distance to segment + */ + public static double getDistanceToSegment(int sx1, int sy1, int sx2, + int sy2, int px, int py) { + Point closestPoint = getClosestPointOnSegment(sx1, sy1, sx2, sy2, px, + py); + return getDistance(closestPoint.x, closestPoint.y, px, py); + } +} diff --git a/src/main/java/com/l2jserver/util/factory/CollectionFactory.java b/src/main/java/com/l2jserver/util/factory/CollectionFactory.java index 00ac9fa57..17dc10669 100644 --- a/src/main/java/com/l2jserver/util/factory/CollectionFactory.java +++ b/src/main/java/com/l2jserver/util/factory/CollectionFactory.java @@ -16,6 +16,7 @@ */ package com.l2jserver.util.factory; +import java.lang.ref.ReferenceQueue; import java.util.Collection; import java.util.List; import java.util.Map; @@ -81,6 +82,15 @@ public class CollectionFactory { return new PriorityQueue(); } + /** + * Creates a new reference queue of type T + * + * @return the created queue + */ + public static final ReferenceQueue newReferenceQueue() { + return new ReferenceQueue(); + } + /** * Creates a new map. * diff --git a/src/main/java/com/l2jserver/util/geometry/AbstractArea.java b/src/main/java/com/l2jserver/util/geometry/AbstractArea.java new file mode 100644 index 000000000..e8f2536c3 --- /dev/null +++ b/src/main/java/com/l2jserver/util/geometry/AbstractArea.java @@ -0,0 +1,125 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util.geometry; + +/** + * Class with basic method implementation for areas.
+ * If possible it should be subclassed.
+ * In other case {@link Area} should be implemented directly + * + * @author Rogiel + */ +public abstract class AbstractArea implements Area { + /** + * Minimal z of area + */ + private final int minZ; + + /** + * Maximal Z of area + */ + private final int maxZ; + + /** + * Creates new AbstractArea with min and max z + * + * @param minZ + * min z + * @param maxZ + * max z + */ + protected AbstractArea(int minZ, int maxZ) { + if (minZ > maxZ) { + throw new IllegalArgumentException("minZ(" + minZ + ") > maxZ(" + + maxZ + ")"); + } + this.minZ = minZ; + this.maxZ = maxZ; + } + + @Override + public boolean isInside2D(Point point) { + return isInside2D(point.x, point.y); + } + + @Override + public boolean isInside3D(Point3D point) { + return isInside3D(point.getX(), point.getY(), point.getZ()); + } + + @Override + public boolean isInside3D(int x, int y, int z) { + return isInsideZ(z) && isInside2D(x, y); + } + + @Override + public boolean isInsideZ(Point3D point) { + return isInsideZ(point.getZ()); + } + + @Override + public boolean isInsideZ(int z) { + return z >= getMinZ() && z <= getMaxZ(); + } + + @Override + public double getDistance2D(Point point) { + return getDistance2D(point.x, point.y); + } + + @Override + public double getDistance3D(Point3D point) { + return getDistance3D(point.getX(), point.getY(), point.getZ()); + } + + @Override + public Point getClosestPoint(Point point) { + return getClosestPoint(point.x, point.y); + } + + @Override + public Point3D getClosestPoint(Point3D point) { + return getClosestPoint(point.getX(), point.getY(), point.getZ()); + } + + @Override + public Point3D getClosestPoint(int x, int y, int z) { + Point closest2d = getClosestPoint(x, y); + + int zCoord; + + if (isInsideZ(z)) { + zCoord = z; + } else if (z < getMinZ()) { + zCoord = getMinZ(); + } else { + zCoord = getMaxZ(); + } + + return new Point3D(closest2d.x, closest2d.y, zCoord); + } + + @Override + public int getMinZ() { + return minZ; + } + + @Override + public int getMaxZ() { + return maxZ; + } +} diff --git a/src/main/java/com/l2jserver/util/geometry/Area.java b/src/main/java/com/l2jserver/util/geometry/Area.java new file mode 100644 index 000000000..51118bdbd --- /dev/null +++ b/src/main/java/com/l2jserver/util/geometry/Area.java @@ -0,0 +1,196 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util.geometry; + +/** + * Basic interface for all areas.
+ * It should be implemented in different ways for performance reasons.
+ * For instance, we don't need complex math for squares or circles, but we need + * it for more complex polygons. + * + * @author Rogiel + */ +public interface Area { + /** + * Returns true if point is inside area ignoring z value + * + * @param point + * point to check + * @return point is inside or not + */ + boolean isInside2D(Point point); + + /** + * Returns true if coords are inside area ignoring z value + * + * @param x + * x coord + * @param y + * y coord + * @return coords are inside or not + */ + boolean isInside2D(int x, int y); + + /** + * Returns true if point is inside area + * + * @param point + * point to check + * @return true if point is inside + */ + boolean isInside3D(Point3D point); + + /** + * Returns true if coors are inside area + * + * @param x + * x coord + * @param y + * y coord + * @param z + * z coord + * @return true if coords are inside + */ + boolean isInside3D(int x, int y, int z); + + /** + * Checks if z coord is insize + * + * @param point + * point to check + * @return is z inside or not + */ + boolean isInsideZ(Point3D point); + + /** + * Checks is z coord is inside + * + * @param z + * z coord + * @return is z inside or not + */ + boolean isInsideZ(int z); + + /** + * Returns distance from point to closest point of this area ignoring z.
+ * Returns 0 if point is inside area. + * + * @param point + * point to calculate distance from + * @return distance or 0 if is inside area + */ + double getDistance2D(Point point); + + /** + * Returns distance from point to closest point of this area ignoring z.
+ * Returns 0 point is inside area. + * + * @param x + * x coord + * @param y + * y coord + * @return distance or 0 if is inside area + */ + double getDistance2D(int x, int y); + + /** + * Returns distance from point to this area.
+ * Returns 0 if is inside. + * + * @param point + * point to check + * @return distance or 0 if is inside + */ + double getDistance3D(Point3D point); + + /** + * Returns distance from coords to this area + * + * @param x + * x coord + * @param y + * y coord + * @param z + * z coord + * @return distance or 0 if is inside + */ + double getDistance3D(int x, int y, int z); + + /** + * Returns closest point of area to given point.
+ * Returns point with coords = point arg if is inside + * + * @param point + * point to check + * @return closest point + */ + Point getClosestPoint(Point point); + + /** + * Returns closest point of area to given coords.
+ * Returns point with coords x and y if coords are inside + * + * @param x + * x coord + * @param y + * y coord + * @return closest point + */ + Point getClosestPoint(int x, int y); + + /** + * Returns closest point of area to given point.
+ * Works exactly like {@link #getClosestPoint(int, int)} if + * {@link #isInsideZ(int)} returns true.
+ * In other case closest z edge is set as z coord. + * + * @param point + * point to check + * @return closest point of area to point + */ + Point3D getClosestPoint(Point3D point); + + /** + * Returns closest point of area to given coords.
+ * Works exactly like {@link #getClosestPoint(int, int)} if + * {@link #isInsideZ(int)} returns true.
+ * In other case closest z edge is set as z coord. + * + * @param x + * x coord + * @param y + * y coord + * @param z + * z coord + * @return closest point of area to point + */ + Point3D getClosestPoint(int x, int y, int z); + + /** + * Return minimal z of this area + * + * @return minimal z of this area + */ + int getMinZ(); + + /** + * Returns maximal z of this area + * + * @return maximal z of this area + */ + int getMaxZ(); +} diff --git a/src/main/java/com/l2jserver/util/dimensional/Coordinate.java b/src/main/java/com/l2jserver/util/geometry/Coordinate.java similarity index 95% rename from src/main/java/com/l2jserver/util/dimensional/Coordinate.java rename to src/main/java/com/l2jserver/util/geometry/Coordinate.java index d2e957bc1..b8dde8e2b 100644 --- a/src/main/java/com/l2jserver/util/dimensional/Coordinate.java +++ b/src/main/java/com/l2jserver/util/geometry/Coordinate.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.util.dimensional; +package com.l2jserver.util.geometry; import org.apache.commons.math.geometry.Vector3D; @@ -91,7 +91,7 @@ public class Coordinate { return "Coordinate [" + vector + "]"; } - public Point toPoint() { - return new Point(this, 0); + public Point3D toPoint() { + return new Point3D(this, 0); } } diff --git a/src/main/java/com/l2jserver/util/geometry/CylinderArea.java b/src/main/java/com/l2jserver/util/geometry/CylinderArea.java new file mode 100644 index 000000000..e5e544bd6 --- /dev/null +++ b/src/main/java/com/l2jserver/util/geometry/CylinderArea.java @@ -0,0 +1,122 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util.geometry; + +import com.l2jserver.util.MathUtil; + +/** + * This class implements cylinder area + * + * @author Rogiel + */ +public class CylinderArea extends AbstractArea { + /** + * Center of cylinder + */ + private final int centerX; + /** + * Center of cylinder + */ + private final int centerY; + /** + * Cylinder radius + */ + private final int radius; + + /** + * Creates new cylinder with given radius + * + * @param center + * center of the circle + * @param radius + * radius of the circle + * @param minZ + * min z + * @param maxZ + * max z + */ + public CylinderArea(Point center, int radius, int minZ, int maxZ) { + this(center.x, center.y, radius, minZ, maxZ); + } + + /** + * Creates new cylider with given radius + * + * @param x + * center coord + * @param y + * center coord + * @param radius + * radius of the circle + * @param minZ + * min z + * @param maxZ + * max z + */ + public CylinderArea(int x, int y, int radius, int minZ, int maxZ) { + super(minZ, maxZ); + this.centerX = x; + this.centerY = y; + this.radius = radius; + } + + @Override + public boolean isInside2D(int x, int y) { + return MathUtil.getDistance(centerX, centerY, x, y) < radius; + } + + @Override + public double getDistance2D(int x, int y) { + if (isInside2D(x, y)) { + return 0; + } else { + return Math.abs(MathUtil.getDistance(centerX, centerY, x, y) + - radius); + } + } + + @Override + public double getDistance3D(int x, int y, int z) { + if (isInside3D(x, y, z)) { + return 0; + } else if (isInsideZ(z)) { + return getDistance2D(x, y); + } else { + if (z < getMinZ()) { + return MathUtil.getDistance(centerX, centerY, getMinZ(), x, y, + z); + } else { + return MathUtil.getDistance(centerX, centerY, getMaxZ(), x, y, + z); + } + } + } + + @Override + public Point getClosestPoint(int x, int y) { + if (isInside2D(x, y)) { + return new Point(x, y); + } else { + int vX = x - this.centerX; + int vY = y - this.centerY; + double magV = MathUtil.getDistance(centerX, centerY, x, y); + double pointX = centerX + vX / magV * radius; + double pointY = centerY + vY / magV * radius; + return new Point((int) Math.round(pointX), (int) Math.round(pointY)); + } + } +} diff --git a/data/script/ai/script/ai/scanner/AIScanner.java b/src/main/java/com/l2jserver/util/geometry/Point.java similarity index 62% rename from data/script/ai/script/ai/scanner/AIScanner.java rename to src/main/java/com/l2jserver/util/geometry/Point.java index c603c42b4..5a4f11821 100644 --- a/data/script/ai/script/ai/scanner/AIScanner.java +++ b/src/main/java/com/l2jserver/util/geometry/Point.java @@ -14,12 +14,37 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package script.ai.scanner; +package com.l2jserver.util.geometry; /** + * An two dimensional point + * * @author Rogiel - * */ -public interface AIScanner { +public class Point { + /** + * The X axis + */ + public final int x; + /** + * The Y axis + */ + public final int y; + /** + * Creates a new two dimensional point + * + * @param x + * the x axis + * @param y + * the y axis + */ + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public static final Point fromXY(int x, int y) { + return new Point(x, y); + } } diff --git a/src/main/java/com/l2jserver/util/dimensional/Point.java b/src/main/java/com/l2jserver/util/geometry/Point3D.java similarity index 70% rename from src/main/java/com/l2jserver/util/dimensional/Point.java rename to src/main/java/com/l2jserver/util/geometry/Point3D.java index 3c10058ba..63f402f53 100644 --- a/src/main/java/com/l2jserver/util/dimensional/Point.java +++ b/src/main/java/com/l2jserver/util/geometry/Point3D.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with l2jserver. If not, see . */ -package com.l2jserver.util.dimensional; +package com.l2jserver.util.geometry; /** * An point is composed of an Coordinate and an angle. The angle represents the @@ -22,7 +22,7 @@ package com.l2jserver.util.dimensional; * * @author Rogiel */ -public class Point { +public class Point3D { /** * The point coordinate */ @@ -40,14 +40,28 @@ public class Point { * @param angle * the angle */ - public Point(Coordinate coordinate, double angle) { + public Point3D(Coordinate coordinate, double angle) { this.coordinate = coordinate; this.angle = angle; } + /** + * Creates a new point with 3 axis + * + * @param x + * the x axis + * @param y + * the y axis + * @param z + * the z axis + */ + public Point3D(int x, int y, int z) { + this(new Coordinate(x, y, z), 0); + } + /** * @return the x - * @see com.l2jserver.util.dimensional.Coordinate#getX() + * @see com.l2jserver.util.geometry.Coordinate#getX() */ public int getX() { return coordinate.getX(); @@ -55,7 +69,7 @@ public class Point { /** * @return the y - * @see com.l2jserver.util.dimensional.Coordinate#getY() + * @see com.l2jserver.util.geometry.Coordinate#getY() */ public int getY() { return coordinate.getY(); @@ -63,7 +77,7 @@ public class Point { /** * @return the z - * @see com.l2jserver.util.dimensional.Coordinate#getZ() + * @see com.l2jserver.util.geometry.Coordinate#getZ() */ public int getZ() { return coordinate.getZ(); @@ -73,7 +87,7 @@ public class Point { * @param other * the other coordinate * @return the distance - * @see com.l2jserver.util.dimensional.Coordinate#getDistance(com.l2jserver.util.dimensional.Coordinate) + * @see com.l2jserver.util.geometry.Coordinate#getDistance(com.l2jserver.util.geometry.Coordinate) */ public double getDistance(Coordinate other) { return coordinate.getDistance(other); @@ -83,9 +97,9 @@ public class Point { * @param other * the other point * @return the distance - * @see com.l2jserver.util.dimensional.Coordinate#getDistance(com.l2jserver.util.dimensional.Coordinate) + * @see com.l2jserver.util.geometry.Coordinate#getDistance(com.l2jserver.util.geometry.Coordinate) */ - public double getDistance(Point other) { + public double getDistance(Point3D other) { return coordinate.getDistance(other.coordinate); } @@ -114,10 +128,10 @@ public class Point { * the z point * @param angle * the angle - * @return the new {@link Point} object created + * @return the new {@link Point3D} object created */ - public static Point fromXYZA(int x, int y, int z, double angle) { - return new Point(Coordinate.fromXYZ(x, y, z), angle); + public static Point3D fromXYZA(int x, int y, int z, double angle) { + return new Point3D(Coordinate.fromXYZ(x, y, z), angle); } /** @@ -129,9 +143,9 @@ public class Point { * the y point * @param z * the z point - * @return the new {@link Point} object created + * @return the new {@link Point3D} object created */ - public static Point fromXYZ(int x, int y, int z) { + public static Point3D fromXYZ(int x, int y, int z) { return fromXYZA(x, y, z, 0); } } diff --git a/src/main/java/com/l2jserver/util/geometry/PolygonArea.java b/src/main/java/com/l2jserver/util/geometry/PolygonArea.java new file mode 100644 index 000000000..ec92e9064 --- /dev/null +++ b/src/main/java/com/l2jserver/util/geometry/PolygonArea.java @@ -0,0 +1,155 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util.geometry; + +import java.awt.Polygon; +import java.util.Collection; + +import com.l2jserver.util.MathUtil; + +/** + * Area of free form + * + * @author Rogiel + */ +public class PolygonArea extends AbstractArea { + /** + * Collection of x points + */ + private final int[] xPoints; + + /** + * Collection of y points + */ + private final int[] yPoints; + + /** + * Polygon used to calculate isInside() + */ + private final Polygon poly; + + /** + * Creates new area from given points + * + * @param points + * list of points + * @param zMin + * minimal z + * @param zMax + * maximal z + */ + public PolygonArea(Collection points, int zMin, int zMax) { + this(points.toArray(new Point[points.size()]), zMin, zMax); + } + + /** + * Creates new area from given points + * + * @param points + * list of points + * @param zMin + * minimal z + * @param zMax + * maximal z + */ + public PolygonArea(Point[] points, int zMin, int zMax) { + super(zMin, zMax); + + if (points.length < 3) { + throw new IllegalArgumentException( + "Not enough points, needed at least 3 but got " + + points.length); + } + + this.xPoints = new int[points.length]; + this.yPoints = new int[points.length]; + + Polygon polygon = new Polygon(); + for (int i = 0, n = points.length; i < n; i++) { + Point p = points[i]; + polygon.addPoint(p.x, p.y); + xPoints[i] = p.x; + yPoints[i] = p.y; + } + this.poly = polygon; + } + + @Override + public boolean isInside2D(int x, int y) { + return poly.contains(x, y); + } + + @Override + public double getDistance2D(int x, int y) { + if (isInside2D(x, y)) { + return 0; + } else { + Point cp = getClosestPoint(x, y); + return MathUtil.getDistance(cp.x, cp.y, x, y); + } + } + + @Override + public double getDistance3D(int x, int y, int z) { + if (isInside3D(x, y, z)) { + return 0; + } else if (isInsideZ(z)) { + return getDistance2D(x, y); + } else { + Point3D cp = getClosestPoint(x, y, z); + return MathUtil.getDistance(cp.getX(), cp.getY(), cp.getZ(), x, y, + z); + } + } + + @Override + public Point getClosestPoint(int x, int y) { + + Point closestPoint = null; + double closestDistance = 0; + + for (int i = 0; i < xPoints.length; i++) { + int nextIndex = i + 1; + if (nextIndex == xPoints.length) { + nextIndex = 0; + } + + int p1x = xPoints[i]; + int p1y = yPoints[i]; + int p2x = xPoints[nextIndex]; + int p2y = yPoints[nextIndex]; + + Point point = MathUtil.getClosestPointOnSegment(p1x, p1y, p2x, p2y, + x, y); + + if (closestPoint == null) { + closestPoint = point; + closestDistance = MathUtil.getDistance(closestPoint.x, + closestPoint.y, x, y); + } else { + double newDistance = MathUtil.getDistance(point.x, point.y, x, + y); + if (newDistance < closestDistance) { + closestPoint = point; + closestDistance = newDistance; + } + } + } + + return closestPoint; + } +} diff --git a/src/main/java/com/l2jserver/util/geometry/RectangleArea.java b/src/main/java/com/l2jserver/util/geometry/RectangleArea.java new file mode 100644 index 000000000..6237ec5b2 --- /dev/null +++ b/src/main/java/com/l2jserver/util/geometry/RectangleArea.java @@ -0,0 +1,177 @@ +/* + * This file is part of l2jserver . + * + * 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 . + */ +package com.l2jserver.util.geometry; + +import java.awt.Rectangle; + +import com.l2jserver.util.MathUtil; + +/** + * Rectangle area, most wide spread in the game + * + * @author Rogiel + */ +public class RectangleArea extends AbstractArea { + /** + * Min x point + */ + private final int minX; + + /** + * Max x point + */ + private final int maxX; + + /** + * Min y point + */ + private final int minY; + + /** + * Max y point + */ + private final int maxY; + + /** + * Creates new area from given points. Point order doesn't matter + * + * @param p1 + * point + * @param p2 + * point + * @param p3 + * point + * @param p4 + * point + * @param minZ + * minimal z + * @param maxZ + * maximal z + */ + public RectangleArea(Point p1, Point p2, Point p3, Point p4, int minZ, + int maxZ) { + super(minZ, maxZ); + + // we should use a better way to do this + Rectangle r = new Rectangle(); + r.add(new java.awt.Point(p1.x, p1.y)); + r.add(new java.awt.Point(p2.x, p2.y)); + r.add(new java.awt.Point(p3.x, p3.y)); + r.add(new java.awt.Point(p4.x, p4.y)); + + minX = (int) r.getMinX(); + maxX = (int) r.getMaxX(); + minY = (int) r.getMinY(); + maxY = (int) r.getMaxY(); + } + + /** + * Creates new are from given coords + * + * @param minX + * mimal x point + * @param minY + * minimal y point + * @param maxX + * maximal x point + * @param maxY + * maximal y point + * @param minZ + * minimal z point + * @param maxZ + * maximal z point + */ + public RectangleArea(int minX, int minY, int maxX, int maxY, int minZ, + int maxZ) { + super(minZ, maxZ); + this.minX = minX; + this.maxX = maxX; + this.minY = minY; + this.maxY = maxY; + } + + @Override + public boolean isInside2D(int x, int y) { + return x >= minX && x <= maxX && y >= minY && y <= maxY; + } + + @Override + public double getDistance2D(int x, int y) { + if (isInside2D(x, y)) { + return 0; + } else { + Point cp = getClosestPoint(x, y); + return MathUtil.getDistance(x, y, cp.x, cp.y); + } + } + + @Override + public double getDistance3D(int x, int y, int z) { + if (isInside3D(x, y, z)) { + return 0; + } else if (isInsideZ(z)) { + return getDistance2D(x, y); + } else { + Point3D cp = getClosestPoint(x, y, z); + return MathUtil.getDistance(x, y, z, cp.getX(), cp.getY(), + cp.getZ()); + } + } + + @Override + public Point getClosestPoint(int x, int y) { + + if (isInside2D(x, y)) { + return new Point(x, y); + } else { + // bottom edge + Point closestPoint = MathUtil.getClosestPointOnSegment(minX, minY, + maxX, minY, x, y); + double distance = MathUtil.getDistance(x, y, closestPoint.x, + closestPoint.y); + + // top edge + Point cp = MathUtil.getClosestPointOnSegment(minX, maxY, maxX, + maxY, x, y); + double d = MathUtil.getDistance(x, y, cp.x, cp.y); + if (d < distance) { + closestPoint = cp; + distance = d; + } + + // left edge + cp = MathUtil + .getClosestPointOnSegment(minX, minY, minX, maxY, x, y); + d = MathUtil.getDistance(x, y, cp.x, cp.y); + if (d < distance) { + closestPoint = cp; + distance = d; + } + + // Right edge + cp = MathUtil + .getClosestPointOnSegment(maxX, minY, maxX, maxY, x, y); + d = MathUtil.getDistance(x, y, cp.x, cp.y); + if (d < distance) { + closestPoint = cp; + // distance = d; + } + + return closestPoint; + } + } +} diff --git a/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java b/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java index 5a1b9bc03..f2c8b4b77 100644 --- a/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java +++ b/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java @@ -19,7 +19,7 @@ package com.l2jserver.util.jaxb; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.adapters.XmlAdapter; -import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.geometry.Coordinate; import com.l2jserver.util.jaxb.CoordinateAdapter.CoordinateElement; /** diff --git a/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java b/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java index 1c0564555..07099f17e 100644 --- a/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java +++ b/src/test/java/com/l2jserver/model/id/factory/IDFactoryTest.java @@ -23,7 +23,7 @@ import org.junit.Test; import com.google.inject.Guice; import com.google.inject.Injector; -import com.l2jserver.db.dao.H2DAOModule; +import com.l2jserver.db.H2DAOModule; import com.l2jserver.model.id.ID; import com.l2jserver.model.id.object.CharacterID; import com.l2jserver.model.id.object.provider.CharacterIDProvider; diff --git a/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java b/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java index b9ff9dcb6..6f0fed730 100644 --- a/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java +++ b/src/test/java/com/l2jserver/service/game/template/StaticTemplateServiceTest.java @@ -20,7 +20,7 @@ import org.junit.Test; import com.google.inject.Guice; import com.google.inject.Injector; -import com.l2jserver.db.dao.H2DAOModule; +import com.l2jserver.db.H2DAOModule; import com.l2jserver.model.id.provider.IDProviderModule; import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; import com.l2jserver.service.ServiceManager; diff --git a/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java b/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java index 9b9024b4f..e0107ecab 100644 --- a/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java +++ b/src/tool/java/com/l2jserver/model/template/NPCTemplateConverter.java @@ -68,8 +68,8 @@ import com.l2jserver.model.world.npc.controller.NPCController; import com.l2jserver.model.world.npc.controller.NotImplementedNPCController; import com.l2jserver.model.world.npc.controller.TeleporterController; import com.l2jserver.service.game.template.XMLTemplateService.TeleportationTemplateContainer; -import com.l2jserver.util.dimensional.Coordinate; import com.l2jserver.util.factory.CollectionFactory; +import com.l2jserver.util.geometry.Coordinate; import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer; diff --git a/test.xsd b/test.xsd deleted file mode 100644 index a4f5d71af..000000000 --- a/test.xsd +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -