From 70335180236e3f4f5c88a1083210aeb969eea7bb Mon Sep 17 00:00:00 2001 From: Rogiel Date: Wed, 25 May 2011 02:38:15 -0300 Subject: [PATCH] NPC Chatting Signed-off-by: Rogiel --- data/{ => sample}/char.xml | 0 data/{ => sample}/item.xml | 0 data/{ => sample}/npc.xml | 0 data/teleport.xml | 4 - data/teleports.xml | 10 ++ data/templates/npc.xsd | 1 - data/templates/npc/teleporter/30006-Roxxy.xml | 79 ++++++++++----- .../packet/server/NPCHtmlMessagePacket.java | 6 ++ .../id/template/TeleportationTemplateID.java | 49 ++++++++++ .../TeleportationTemplateIDProvider.java | 29 ++++++ .../model/template/ItemTemplate.java | 9 ++ .../l2jserver/model/template/NPCTemplate.java | 98 ++++++++++++++++--- .../model/template/TeleportationTemplate.java | 73 ++++++++++++++ .../npc/controller/AbstractNPCController.java | 72 ++++++++++++-- .../npc/controller/TeleporterController.java | 56 +++++++++++ .../service/game/npc/NPCServiceImpl.java | 41 +++++--- .../game/template/XMLTemplateService.java | 22 ++++- .../com/l2jserver/test/XMLMappingTest.java | 14 ++- .../util/html/markup/HtmlTemplate.java | 3 +- .../util/jaxb/CoordinateAdapter.java | 68 +++++++++++++ .../jaxb/TeleportationTemplateIDAdapter.java | 54 ++++++++++ 21 files changed, 613 insertions(+), 75 deletions(-) rename data/{ => sample}/char.xml (100%) rename data/{ => sample}/item.xml (100%) rename data/{ => sample}/npc.xml (100%) delete mode 100644 data/teleport.xml create mode 100644 data/teleports.xml create mode 100644 src/main/java/com/l2jserver/model/id/template/TeleportationTemplateID.java create mode 100644 src/main/java/com/l2jserver/model/id/template/provider/TeleportationTemplateIDProvider.java create mode 100644 src/main/java/com/l2jserver/model/template/TeleportationTemplate.java create mode 100644 src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java create mode 100644 src/main/java/com/l2jserver/util/jaxb/TeleportationTemplateIDAdapter.java diff --git a/data/char.xml b/data/sample/char.xml similarity index 100% rename from data/char.xml rename to data/sample/char.xml diff --git a/data/item.xml b/data/sample/item.xml similarity index 100% rename from data/item.xml rename to data/sample/item.xml diff --git a/data/npc.xml b/data/sample/npc.xml similarity index 100% rename from data/npc.xml rename to data/sample/npc.xml diff --git a/data/teleport.xml b/data/teleport.xml deleted file mode 100644 index 59fa9cef8..000000000 --- a/data/teleport.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/data/teleports.xml b/data/teleports.xml new file mode 100644 index 000000000..3c110895c --- /dev/null +++ b/data/teleports.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/data/templates/npc.xsd b/data/templates/npc.xsd index 27b8df640..5e60464e9 100644 --- a/data/templates/npc.xsd +++ b/data/templates/npc.xsd @@ -1,6 +1,5 @@ - diff --git a/data/templates/npc/teleporter/30006-Roxxy.xml b/data/templates/npc/teleporter/30006-Roxxy.xml index ba8f872cc..3ff51471c 100644 --- a/data/templates/npc/teleporter/30006-Roxxy.xml +++ b/data/templates/npc/teleporter/30006-Roxxy.xml @@ -1,26 +1,57 @@ - - - Roxxy - Gatekeeper - 70 - FEMALE - - - - - - - - - - - - - - - 0 - 0 - - + + + Roxxy + Gatekeeper + 70 + FEMALE + + + + + + + + + + + + + + + 0 + 0 + + + + Gatekeeper Roxxy:
+Welcome! I am Gatekeeper Roxxy, dispatched from the Ivory Tower. I'm very happy to return to my hometown. Have you perhaps been to Einhovant's School of Magic? Grand Magister Gallint was my old teacher. Now then, how can I help you?
+Ask about the airship wharf.
+Ask about the Ivory Tower.
+Teleport
+Exchange Dimension Diamonds.
+Noblesse Exclusive Teleport
+Receive your Birthday Gift!
+Quest +]]>
+ Gatekeeper Roxxy:
+The Ivory Tower is the foremost temple of all Mystics. It has been well known as such ever since the age of Elmoreden. I was born here, and graduated from Einhovant's School of Wizardry. And now, I am working as a gatekeeper by the order of the Ivory Tower. If you happen to go to the Ivory Tower later on, please give my best regards to Grand Magister Valerie. She is my teacher and a wonderful, caring person.
+Back +]]>
+ Gatekeeper Roxxy:
+The Airship Field is where Airships heading to and from Gracia dock. A powerful magic force field prevents entry to Gracia from sea or via teleportation.
+But Gracian-made Airships can travel safely through the force field. It is still a dangerous journey, though!
After several years of work, a wharf was constructed from magical stones capable of floating in mid-air. You can board one of the Airships using an Airship Controller.
+If you wish to visit Gracia, this is the only way. I can teleport you to the wharf if you wish.
+Teleport to Gludio Airship Field.
+Back +]]>
+
+ + + + + +
diff --git a/src/main/java/com/l2jserver/game/net/packet/server/NPCHtmlMessagePacket.java b/src/main/java/com/l2jserver/game/net/packet/server/NPCHtmlMessagePacket.java index cb7c2b411..513aafbd5 100644 --- a/src/main/java/com/l2jserver/game/net/packet/server/NPCHtmlMessagePacket.java +++ b/src/main/java/com/l2jserver/game/net/packet/server/NPCHtmlMessagePacket.java @@ -45,6 +45,12 @@ public class NPCHtmlMessagePacket extends AbstractServerPacket { */ private final String html; + public NPCHtmlMessagePacket(NPC npc, String html) { + super(OPCODE); + this.npc = npc; + this.html = html; + } + public NPCHtmlMessagePacket(NPC npc, Html html) { super(OPCODE); this.npc = npc; diff --git a/src/main/java/com/l2jserver/model/id/template/TeleportationTemplateID.java b/src/main/java/com/l2jserver/model/id/template/TeleportationTemplateID.java new file mode 100644 index 000000000..4505a642e --- /dev/null +++ b/src/main/java/com/l2jserver/model/id/template/TeleportationTemplateID.java @@ -0,0 +1,49 @@ +/* + * 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.model.id.template; + +import com.google.inject.Inject; +import com.google.inject.assistedinject.Assisted; +import com.l2jserver.model.id.TemplateID; +import com.l2jserver.model.template.SkillTemplate; +import com.l2jserver.model.template.TeleportationTemplate; +import com.l2jserver.service.game.template.TemplateService; + +/** + * An {@link TemplateID} instance representing an {@link SkillTemplate} object + * + * @author Rogiel + */ +public class TeleportationTemplateID extends + TemplateID { + /** + * The template service + */ + private final TemplateService templateService; + + @Inject + public TeleportationTemplateID(@Assisted String id, + TemplateService templateService) { + super(id); + this.templateService = templateService; + } + + @Override + public TeleportationTemplate getTemplate() { + return templateService.getTemplate(this); + } +} diff --git a/src/main/java/com/l2jserver/model/id/template/provider/TeleportationTemplateIDProvider.java b/src/main/java/com/l2jserver/model/id/template/provider/TeleportationTemplateIDProvider.java new file mode 100644 index 000000000..595d0f8e8 --- /dev/null +++ b/src/main/java/com/l2jserver/model/id/template/provider/TeleportationTemplateIDProvider.java @@ -0,0 +1,29 @@ +/* + * 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.model.id.template.provider; + +import com.l2jserver.model.id.template.SkillTemplateID; +import com.l2jserver.model.id.template.TeleportationTemplateID; + +/** + * Creates new {@link SkillTemplateID} instances + * + * @author Rogiel + */ +public interface TeleportationTemplateIDProvider extends + TemplateIDProvider { +} diff --git a/src/main/java/com/l2jserver/model/template/ItemTemplate.java b/src/main/java/com/l2jserver/model/template/ItemTemplate.java index 69ce3bc94..d7d28ca6b 100644 --- a/src/main/java/com/l2jserver/model/template/ItemTemplate.java +++ b/src/main/java/com/l2jserver/model/template/ItemTemplate.java @@ -50,6 +50,8 @@ public class ItemTemplate extends AbstractTemplate { @XmlJavaTypeAdapter(ItemTemplateIDAdapter.class) protected ItemTemplateID id; + @XmlAttribute(name = "name") + protected String name; @XmlElement(name = "weight") protected int weight = 0; @XmlElement(name = "price") @@ -81,6 +83,13 @@ public class ItemTemplate extends AbstractTemplate { return new Item(id); } + /** + * @return the name + */ + public String getName() { + return name; + } + /** * @return the weight */ diff --git a/src/main/java/com/l2jserver/model/template/NPCTemplate.java b/src/main/java/com/l2jserver/model/template/NPCTemplate.java index 2283e2b54..fd5dafae2 100644 --- a/src/main/java/com/l2jserver/model/template/NPCTemplate.java +++ b/src/main/java/com/l2jserver/model/template/NPCTemplate.java @@ -16,6 +16,7 @@ */ package com.l2jserver.model.template; +import java.util.Collections; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; @@ -31,6 +32,7 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.l2jserver.model.id.template.ItemTemplateID; import com.l2jserver.model.id.template.NPCTemplateID; import com.l2jserver.model.id.template.TeleportationTemplateID; +import com.l2jserver.model.template.NPCTemplate.TeleporterMetadata.TeleporterTeleportMetadata; import com.l2jserver.model.world.Actor.ActorSex; import com.l2jserver.model.world.NPC; import com.l2jserver.util.jaxb.ItemTemplateIDAdapter; @@ -227,31 +229,55 @@ public class NPCTemplate extends ActorTemplate { @XmlType(namespace = "npc") protected static class TeleporterMetadata { + @XmlAttribute(name = "default") + protected String defaultTeleport = null; @XmlElement(name = "teleport") protected List teleports = null; @XmlType(namespace = "npc") protected static class TeleporterTeleportMetadata { @XmlAttribute(name = "id") - @XmlJavaTypeAdapter(TeleportationTemplateIDAdapter.class) - protected TeleportationTemplateID id; + protected String id; @XmlElement(name = "region") - protected List regions = null; - - @XmlType(namespace = "npc") - protected static class TeleporterRegionMetadata { - @XmlAttribute(name = "id") - protected String id = null; - @XmlAttribute(name = "price") - protected int price = 0; - @XmlAttribute(name = "item") - protected int item = 57; - - // TODO implement conditions - } + protected List regions = null; } } + @XmlType(namespace = "npc") + public static class TeleportRegion { + @XmlAttribute(name = "id") + @XmlJavaTypeAdapter(TeleportationTemplateIDAdapter.class) + protected TeleportationTemplateID id; + @XmlAttribute(name = "price") + protected int price = 0; + @XmlAttribute(name = "item") + @XmlJavaTypeAdapter(ItemTemplateIDAdapter.class) + protected ItemTemplateID item; + + /** + * @return the id + */ + public TeleportationTemplateID getID() { + return id; + } + + /** + * @return the price + */ + public int getPrice() { + return price; + } + + /** + * @return the item + */ + public ItemTemplateID getItem() { + return item; + } + + // TODO implement conditions + } + @XmlElement(name = "talk") protected TalkMetadata talk = null; @@ -271,7 +297,7 @@ public class NPCTemplate extends ActorTemplate { @XmlValue protected String html = null; } - + @XmlElementWrapper(name = "droplist") @XmlElement(name = "item") protected List droplist = null; @@ -717,6 +743,46 @@ public class NPCTemplate extends ActorTemplate { return ai.script; } + public List getTeleportRegions(String id) { + if (teleporter == null) + return Collections.emptyList(); + for (final TeleporterTeleportMetadata teleport : teleporter.teleports) { + if (teleport.id.equals(id)) { + return Collections.unmodifiableList(teleport.regions); + } + } + return null; + } + + public List getTeleportRegions() { + if (teleporter == null) + return Collections.emptyList(); + for (final TeleporterTeleportMetadata teleport : teleporter.teleports) { + if (teleport.id.equals(teleporter.defaultTeleport)) { + return Collections.unmodifiableList(teleport.regions); + } + } + return null; + } + + public String getHTML(String id) { + if (talk == null) + return null; + if (id == null) + id = talk.defaultChat; + for (final Chat chat : this.talk.chats) { + if (chat.id.equals(id)) + return chat.html; + } + return null; + } + + public String getDefaultHTML() { + if (talk == null) + return null; + return getHTML(talk.defaultChat); + } + @Override public NPCTemplateID getID() { return id; diff --git a/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java b/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java new file mode 100644 index 000000000..71af9703b --- /dev/null +++ b/src/main/java/com/l2jserver/model/template/TeleportationTemplate.java @@ -0,0 +1,73 @@ +/* + * 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.model.template; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import com.l2jserver.model.id.template.TeleportationTemplateID; +import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.jaxb.CoordinateAdapter; +import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; + +/** + * Template for effects + * + * @author Rogiel + */ +@XmlRootElement(name = "teleport") +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(namespace = "teleports") +public class TeleportationTemplate extends AbstractTemplate { + @XmlAttribute(name = "id") + @XmlJavaTypeAdapter(TeleportationTemplateIDAdapter.class) + private TeleportationTemplateID id; + @XmlElement(name = "name") + private String name; + @XmlElement(name = "coordinate") + @XmlJavaTypeAdapter(CoordinateAdapter.class) + private Coordinate coordinate; + + @Override + public Coordinate create() { + return coordinate; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @return the coordinate + */ + public Coordinate getCoordinate() { + return coordinate; + } + + @Override + public TeleportationTemplateID getID() { + return id; + } +} diff --git a/src/main/java/com/l2jserver/model/world/npc/controller/AbstractNPCController.java b/src/main/java/com/l2jserver/model/world/npc/controller/AbstractNPCController.java index 649929d3d..2ce7d901a 100644 --- a/src/main/java/com/l2jserver/model/world/npc/controller/AbstractNPCController.java +++ b/src/main/java/com/l2jserver/model/world/npc/controller/AbstractNPCController.java @@ -16,8 +16,14 @@ */ package com.l2jserver.model.world.npc.controller; +import java.util.Arrays; + +import org.htmlparser.Parser; +import org.htmlparser.util.ParserException; + import com.l2jserver.game.net.Lineage2Connection; import com.l2jserver.game.net.packet.server.NPCHtmlMessagePacket; +import com.l2jserver.model.template.NPCTemplate; import com.l2jserver.model.world.L2Character; import com.l2jserver.model.world.NPC; import com.l2jserver.util.exception.L2Exception; @@ -33,7 +39,25 @@ public class AbstractNPCController implements NPCController { @Override public void action(NPC npc, Lineage2Connection conn, L2Character character, String... args) throws L2Exception { - + if (args.length == 2) { + if (args[0].equals("Chat")) { + talk(npc, conn, character, + Arrays.copyOfRange(args, 1, args.length)); + return; + } + } else if (args.length == 0 || args.length == 1) { + talk(npc, conn, character, new String[0]); + return; + } else { + final HtmlTemplate template = new HtmlTemplate() { + @Override + protected void build(MarkupTag body) { + body.text("Sorry ${name}, but you cannot interact with me yet!"); + } + }.register("name", character.getName()); + conn.write(new NPCHtmlMessagePacket(npc, template)); + } + conn.sendActionFailed(); } /** @@ -51,15 +75,43 @@ public class AbstractNPCController implements NPCController { */ protected void talk(NPC npc, Lineage2Connection conn, L2Character character, String... args) throws L2Exception { - // not yet available message - final HtmlTemplate template = new HtmlTemplate() { - @Override - protected void build(MarkupTag body) { - body.text("Sorry, but I'm not implemented yet!"); - } - }; - - conn.write(new NPCHtmlMessagePacket(npc, template)); + String id = null; + if (args.length >= 1) { + id = args[0]; + } + conn.write(new NPCHtmlMessagePacket(npc, getHTML(npc, id))); conn.sendActionFailed(); } + + /** + * Returns the NPC HTML message + * + * @param npc + * the npc + * @param id + * the html message id + * @return the html code + */ + protected String getHTML(NPC npc, String id) { + final NPCTemplate template = npc.getTemplate(); + // TODO use an decent template engine + return template.getHTML(id).replaceAll("%objectId%", + npc.getID().getID().toString()); + } + + /** + * This method will try to remove empty lines and all unnecessary space from + * the HTML code. + * + * @param html + * the html code + * @return the trimmed html code + */ + protected String trimHTML(String html) { + try { + return new Parser(html).elements().nextNode().toHtml(); + } catch (ParserException e) { + return html; + } + } } diff --git a/src/main/java/com/l2jserver/model/world/npc/controller/TeleporterController.java b/src/main/java/com/l2jserver/model/world/npc/controller/TeleporterController.java index e9ee700e5..2e68d607d 100644 --- a/src/main/java/com/l2jserver/model/world/npc/controller/TeleporterController.java +++ b/src/main/java/com/l2jserver/model/world/npc/controller/TeleporterController.java @@ -16,8 +16,20 @@ */ package com.l2jserver.model.world.npc.controller; +import java.util.List; + import com.google.inject.Inject; +import com.l2jserver.game.net.Lineage2Connection; +import com.l2jserver.game.net.packet.server.NPCHtmlMessagePacket; +import com.l2jserver.model.id.template.provider.TeleportationTemplateIDProvider; +import com.l2jserver.model.template.NPCTemplate.TeleportRegion; +import com.l2jserver.model.template.TeleportationTemplate; +import com.l2jserver.model.world.L2Character; +import com.l2jserver.model.world.NPC; import com.l2jserver.service.game.spawn.SpawnService; +import com.l2jserver.util.exception.L2Exception; +import com.l2jserver.util.html.markup.HtmlTemplate; +import com.l2jserver.util.html.markup.MarkupTag; /** * This controller is used to control teleporters (e.g. gatekeepers) @@ -30,5 +42,49 @@ public class TeleporterController extends AbstractNPCController { */ @Inject protected SpawnService spawnService; + @Inject + protected TeleportationTemplateIDProvider teleportationIdProvider; + @Override + public void action(NPC npc, Lineage2Connection conn, L2Character character, + String... args) throws L2Exception { + if (args.length >= 2) { + if (args[0].equals("TeleportList")) { + final List regions = getTeleportRegions(npc, + args[1]); + final HtmlTemplate template = new HtmlTemplate() { + @Override + protected void build(MarkupTag body) { + body.textcode(556); + for (final TeleportRegion region : regions) { + body.addLink( + region.getID().getTemplate().getName() + + " - " + + region.getPrice() + + " " + + region.getItem().getTemplate() + .getName(), + "Teleport " + region.getID().getID()).br(); + } + } + }; + conn.write(new NPCHtmlMessagePacket(npc, template)); + } else if (args[0].equals("Teleport")) { + final TeleportationTemplate tele = teleportationIdProvider + .createID(args[1]).getTemplate(); + if (tele == null) { + // TODO chat + conn.sendActionFailed(); + return; + } else { + spawnService.teleport(character, tele.getCoordinate()); + } + } + } + super.action(npc, conn, character, args); + } + + protected List getTeleportRegions(NPC npc, String id) { + return npc.getTemplate().getTeleportRegions(id); + } } diff --git a/src/main/java/com/l2jserver/service/game/npc/NPCServiceImpl.java b/src/main/java/com/l2jserver/service/game/npc/NPCServiceImpl.java index d527ca9b3..38079c1a8 100644 --- a/src/main/java/com/l2jserver/service/game/npc/NPCServiceImpl.java +++ b/src/main/java/com/l2jserver/service/game/npc/NPCServiceImpl.java @@ -21,14 +21,17 @@ import java.util.List; import com.google.common.base.Preconditions; import com.google.inject.Inject; import com.l2jserver.db.dao.NPCDAO; +import com.l2jserver.game.net.Lineage2Connection; import com.l2jserver.game.net.packet.client.CharacterActionPacket.CharacterAction; -import com.l2jserver.model.template.NPCTemplate; import com.l2jserver.model.world.L2Character; import com.l2jserver.model.world.NPC; +import com.l2jserver.model.world.npc.controller.TeleporterController; import com.l2jserver.service.AbstractService; import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException; import com.l2jserver.service.game.spawn.SpawnPointNotFoundServiceException; import com.l2jserver.service.game.spawn.SpawnService; +import com.l2jserver.service.network.NetworkService; +import com.l2jserver.util.exception.L2Exception; /** * Default {@link NPCService} implementation @@ -40,15 +43,23 @@ public class NPCServiceImpl extends AbstractService implements NPCService { * The {@link SpawnService} used to spawn the {@link NPC} instances */ private final SpawnService spawnService; + /** + * The {@link NetworkService} used to discover {@link Lineage2Connection} + */ + private final NetworkService networkService; /** * The {@link NPCDAO} */ private final NPCDAO npcDao; + private final TeleporterController controller = new TeleporterController(); + @Inject - public NPCServiceImpl(SpawnService spawnService, NPCDAO npcDao) { + public NPCServiceImpl(SpawnService spawnService, + NetworkService networkService, NPCDAO npcDao) { this.spawnService = spawnService; + this.networkService = networkService; this.npcDao = npcDao; } @@ -59,12 +70,13 @@ public class NPCServiceImpl extends AbstractService implements NPCService { Preconditions.checkNotNull(character, "character"); Preconditions.checkNotNull(action, "action"); - final NPCTemplate template = npc.getTemplate(); - // try { - // // template.action(npc, character, new String[0]); - // } catch (L2Exception e) { - // throw new ActionServiceException(e); - // } + final Lineage2Connection conn = networkService.discover(character + .getID()); + try { + controller.action(npc, conn, character, new String[0]); + } catch (L2Exception e) { + throw new ActionServiceException(e); + } } @Override @@ -75,12 +87,13 @@ public class NPCServiceImpl extends AbstractService implements NPCService { if (args == null) args = new String[0]; - final NPCTemplate template = npc.getTemplate(); - // try { - // // template.action(npc, character, args); - // } catch (L2Exception e) { - // throw new ActionServiceException(e); - // } + final Lineage2Connection conn = networkService.discover(character + .getID()); + try { + controller.action(npc, conn, character, args); + } catch (L2Exception e) { + throw new ActionServiceException(e); + } } @Override 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 acf02aca1..f517b8447 100644 --- a/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java +++ b/src/main/java/com/l2jserver/service/game/template/XMLTemplateService.java @@ -18,11 +18,17 @@ package com.l2jserver.service.game.template; import java.io.File; import java.util.Collection; +import java.util.List; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; import org.apache.commons.io.FileUtils; @@ -79,7 +85,7 @@ public class XMLTemplateService extends AbstractService implements try { context = JAXBContext.newInstance(CharacterTemplate.class, NPCTemplate.class, ItemTemplate.class, - TeleportationTemplate.class); + TeleportationTemplateContainer.class); unmarshaller = context.createUnmarshaller(); unmarshaller.setAdapter(NPCTemplateIDAdapter.class, @@ -98,6 +104,12 @@ public class XMLTemplateService extends AbstractService implements for (final File file : files) { loadTemplate(file); } + TeleportationTemplateContainer container = (TeleportationTemplateContainer) unmarshaller + .unmarshal(new File(config.getTemplateDirectory(), + "../teleports.xml")); + for (final TeleportationTemplate template : container.templates) { + templates.put(template.getID(), template); + } } catch (JAXBException e) { throw new ServiceStartException(e); } @@ -134,4 +146,12 @@ public class XMLTemplateService extends AbstractService implements unmarshaller = null; context = null; } + + @XmlRootElement(name = "teleports") + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(namespace = "teleports") + public static class TeleportationTemplateContainer { + @XmlElement(name = "teleport") + private List templates; + } } diff --git a/src/main/java/com/l2jserver/test/XMLMappingTest.java b/src/main/java/com/l2jserver/test/XMLMappingTest.java index 198b0600e..b40714b90 100644 --- a/src/main/java/com/l2jserver/test/XMLMappingTest.java +++ b/src/main/java/com/l2jserver/test/XMLMappingTest.java @@ -27,9 +27,12 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.l2jserver.GameServerModule; import com.l2jserver.model.template.CharacterTemplate; +import com.l2jserver.model.template.NPCTemplate; +import com.l2jserver.model.template.TeleportationTemplate; import com.l2jserver.util.jaxb.CharacterTemplateIDAdapter; import com.l2jserver.util.jaxb.ItemTemplateIDAdapter; import com.l2jserver.util.jaxb.NPCTemplateIDAdapter; +import com.l2jserver.util.jaxb.TeleportationTemplateIDAdapter; /** * @author Rogiel @@ -44,7 +47,8 @@ public class XMLMappingTest { public static void main(String[] args) throws JAXBException, IOException { // final List templates = CollectionFactory.newList(); - final JAXBContext c = JAXBContext.newInstance(CharacterTemplate.class); + final JAXBContext c = JAXBContext.newInstance(CharacterTemplate.class, + NPCTemplate.class, TeleportationTemplate.class); final Unmarshaller u = c.createUnmarshaller(); final Injector injector = Guice.createInjector(new GameServerModule()); @@ -54,6 +58,8 @@ public class XMLMappingTest { injector.getInstance(ItemTemplateIDAdapter.class)); u.setAdapter(CharacterTemplateIDAdapter.class, injector.getInstance(CharacterTemplateIDAdapter.class)); + u.setAdapter(TeleportationTemplateIDAdapter.class, + injector.getInstance(TeleportationTemplateIDAdapter.class)); // long start = System.currentTimeMillis(); // for (int i = 0; i < 200 * 1000; i++) { @@ -65,8 +71,8 @@ public class XMLMappingTest { // // System.out.println("Took " + ((end - start) / 1000) + " seconds"); - final CharacterTemplate t = (CharacterTemplate) u.unmarshal(new File( - "data/templates/character/Adventurer.xml")); - System.out.println(t.getFemaleCollisionHeight()); + final TeleportationTemplate t = (TeleportationTemplate) u + .unmarshal(new File("data/templates/teleports.xml")); + System.out.println(t.getName()); } } diff --git a/src/main/java/com/l2jserver/util/html/markup/HtmlTemplate.java b/src/main/java/com/l2jserver/util/html/markup/HtmlTemplate.java index 523b7d069..13260f03e 100644 --- a/src/main/java/com/l2jserver/util/html/markup/HtmlTemplate.java +++ b/src/main/java/com/l2jserver/util/html/markup/HtmlTemplate.java @@ -113,8 +113,9 @@ public abstract class HtmlTemplate { * @param value * the value */ - public void register(String name, String value) { + public HtmlTemplate register(String name, String value) { variables.put(name, value); + return this; } /** diff --git a/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java b/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java new file mode 100644 index 000000000..5a1b9bc03 --- /dev/null +++ b/src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java @@ -0,0 +1,68 @@ +/* + * 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.jaxb; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.adapters.XmlAdapter; + +import com.l2jserver.util.dimensional.Coordinate; +import com.l2jserver.util.jaxb.CoordinateAdapter.CoordinateElement; + +/** + * This adapter converts an simple set of attributes x, y and + * z to an {@link Coordinate} object. Please note that the tag name is + * indifferent. + * + *
+ * <coordinate x="200" y="100" z="0" />
+ * 
+ * + * @author Rogiel + */ +public class CoordinateAdapter extends + XmlAdapter { + @Override + public CoordinateElement marshal(Coordinate coordinate) throws Exception { + return new CoordinateElement(coordinate.getX(), coordinate.getY(), + coordinate.getZ()); + } + + @Override + public Coordinate unmarshal(CoordinateElement elem) throws Exception { + return Coordinate.fromXYZ(elem.x, elem.y, elem.z); + + } + + public static class CoordinateElement { + @XmlAttribute(name = "x") + public int x; + @XmlAttribute(name = "y") + public int y; + @XmlAttribute(name = "z") + public int z; + + protected CoordinateElement() { + } + + public CoordinateElement(int x, int y, int z) { + super(); + this.x = x; + this.y = y; + this.z = z; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/l2jserver/util/jaxb/TeleportationTemplateIDAdapter.java b/src/main/java/com/l2jserver/util/jaxb/TeleportationTemplateIDAdapter.java new file mode 100644 index 000000000..9636d1f99 --- /dev/null +++ b/src/main/java/com/l2jserver/util/jaxb/TeleportationTemplateIDAdapter.java @@ -0,0 +1,54 @@ +/* + * 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.jaxb; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +import com.google.inject.Inject; +import com.l2jserver.model.id.template.TeleportationTemplateID; +import com.l2jserver.model.id.template.provider.ItemTemplateIDProvider; +import com.l2jserver.model.id.template.provider.TeleportationTemplateIDProvider; + +/** + * TODO this should use an {@link ItemTemplateIDProvider}! + * + * @author Rogiel + */ +public class TeleportationTemplateIDAdapter extends + XmlAdapter { + private final TeleportationTemplateIDProvider provider; + + @Inject + public TeleportationTemplateIDAdapter( + TeleportationTemplateIDProvider provider) { + this.provider = provider; + } + + @Override + public TeleportationTemplateID unmarshal(String v) throws Exception { + if (v == null) + return null; + return provider.createID(v); + } + + @Override + public String marshal(TeleportationTemplateID v) throws Exception { + if (v == null) + return null; + return v.getID(); + } +}