mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-07 16:03:10 +00:00
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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> {
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<TeleportationTemplate> templates;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 <a href="http://www.rogiel.com">Rogiel</a>
|
||||
@@ -44,7 +47,8 @@ public class XMLMappingTest {
|
||||
public static void main(String[] args) throws JAXBException, IOException {
|
||||
// final List<NPCTemplate> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
68
src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java
Normal file
68
src/main/java/com/l2jserver/util/jaxb/CoordinateAdapter.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.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 <tt>x</tt>, <tt>y</tt> and
|
||||
* <tt>z</tt> to an {@link Coordinate} object. Please note that the tag name is
|
||||
* indifferent.
|
||||
*
|
||||
* <pre>
|
||||
* <coordinate x="200" y="100" z="0" />
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class CoordinateAdapter extends
|
||||
XmlAdapter<CoordinateElement, Coordinate> {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.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 <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class TeleportationTemplateIDAdapter extends
|
||||
XmlAdapter<String, TeleportationTemplateID> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user