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

NPC Chatting

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-25 02:38:15 -03:00
parent ad6a2e89d2
commit 7033518023
21 changed files with 613 additions and 75 deletions

View File

@@ -0,0 +1,49 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.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 <a href="http://www.rogiel.com">Rogiel</a>
*/
public class TeleportationTemplateID extends
TemplateID<TeleportationTemplate, String> {
/**
* 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);
}
}

View File

@@ -0,0 +1,29 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.model.id.template.provider;
import com.l2jserver.model.id.template.SkillTemplateID;
import com.l2jserver.model.id.template.TeleportationTemplateID;
/**
* Creates new {@link SkillTemplateID} instances
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface TeleportationTemplateIDProvider extends
TemplateIDProvider<String, TeleportationTemplateID> {
}

View File

@@ -50,6 +50,8 @@ public class ItemTemplate extends AbstractTemplate<Item> {
@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<Item> {
return new Item(id);
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the weight
*/

View File

@@ -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<NPC> {
@XmlType(namespace = "npc")
protected static class TeleporterMetadata {
@XmlAttribute(name = "default")
protected String defaultTeleport = null;
@XmlElement(name = "teleport")
protected List<TeleporterTeleportMetadata> 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<TeleporterRegionMetadata> 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<TeleportRegion> 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<NPC> {
@XmlValue
protected String html = null;
}
@XmlElementWrapper(name = "droplist")
@XmlElement(name = "item")
protected List<DropItemMetadata> droplist = null;
@@ -717,6 +743,46 @@ public class NPCTemplate extends ActorTemplate<NPC> {
return ai.script;
}
public List<TeleportRegion> 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<TeleportRegion> 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;

View File

@@ -0,0 +1,73 @@
/*
* This file is part of l2jserver <l2jserver.com>.
*
* l2jserver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.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 <a href="http://www.rogiel.com">Rogiel</a>
*/
@XmlRootElement(name = "teleport")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "teleports")
public class TeleportationTemplate extends AbstractTemplate<Coordinate> {
@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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -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<TeleportRegion> 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<TeleportRegion> getTeleportRegions(NPC npc, String id) {
return npc.getTemplate().getTeleportRegions(id);
}
}