1 /*
2  * @(#) src/games/stendhal/server/config/zone/PortalSetupDescriptor.java
3  *
4  * $Id$
5  */
6 
7 package games.stendhal.server.core.config.zone;
8 
9 import org.apache.log4j.Logger;
10 
11 //
12 //
13 
14 import games.stendhal.server.core.engine.StendhalRPZone;
15 import games.stendhal.server.entity.EntityFactoryHelper;
16 import games.stendhal.server.entity.mapstuff.portal.Portal;
17 
18 /**
19  * A portal setup descriptor.
20  */
21 public class PortalSetupDescriptor extends EntitySetupDescriptor {
22 	/**
23 	 * Logger.
24 	 */
25 	private static final Logger logger = Logger.getLogger(PortalSetupDescriptor.class);
26 
27 	/**
28 	 * The named portal identifier.
29 	 */
30 	protected Object identifier;
31 
32 	/**
33 	 * The destination zone name (if any).
34 	 */
35 	protected String destinationZone;
36 
37 	/**
38 	 * The named destination portal (if any).
39 	 */
40 	protected Object destinationIdentifier;
41 
42 	/**
43 	 * Whether replacing an existing portal at that location.
44 	 */
45 	protected boolean replacing;
46 
47 	/**
48 	 * Create a portal setup descriptor.
49 	 *
50 	 * @param x
51 	 *            The X coordinate.
52 	 * @param y
53 	 *            The Y coordinate.
54 	 * @param identifier
55 	 *            The identifier,
56 	 */
PortalSetupDescriptor(final int x, final int y, final Object identifier)57 	public PortalSetupDescriptor(final int x, final int y,
58 			final Object identifier) {
59 		super(x, y);
60 
61 		this.identifier = identifier;
62 
63 		destinationZone = null;
64 		destinationIdentifier = null;
65 		replacing = false;
66 	}
67 
68 	//
69 	// PortalSetupDescriptor
70 	//
71 
72 	/**
73 	 * Get the destination identifier.
74 	 *
75 	 * @return An identifier.
76 	 */
getDestinationIdentifier()77 	public Object getDestinationIdentifier() {
78 		return destinationIdentifier;
79 	}
80 
81 	/**
82 	 * Get the destination zone.
83 	 *
84 	 * @return A zone name.
85 	 */
getDestinationZone()86 	public String getDestinationZone() {
87 		return destinationZone;
88 	}
89 
90 	/**
91 	 * Get the identifier.
92 	 *
93 	 * @return An identifier.
94 	 */
getIdentifier()95 	public Object getIdentifier() {
96 		return identifier;
97 	}
98 
99 	/**
100 	 * Determine if existing portals are replaced.
101 	 *
102 	 * @return <code>true</code> if replacing an existing portal at that
103 	 *         location.
104 	 */
isReplacing()105 	public boolean isReplacing() {
106 		return replacing;
107 	}
108 
109 	/**
110 	 * Set the destination zone/identifier.
111 	 *
112 	 * @param zone
113 	 *            The destination zone name.
114 	 * @param identifier
115 	 *            The named destination portal.
116 	 */
setDestination(final String zone, final Object identifier)117 	public void setDestination(final String zone, final Object identifier) {
118 		this.destinationZone = zone;
119 		this.destinationIdentifier = identifier;
120 	}
121 
122 	/**
123 	 * Set whether to replace any existing portal.
124 	 *
125 	 * @param replacing
126 	 *            Whether replacing an existing portal at that location.
127 	 */
setReplacing(final boolean replacing)128 	public void setReplacing(final boolean replacing) {
129 		this.replacing = replacing;
130 	}
131 
132 	//
133 	// SetupDescriptor
134 	//
135 
136 	/**
137 	 * Do appropriate zone setup.
138 	 *
139 	 * @param zone
140 	 *            The zone.
141 	 */
142 	@Override
setup(final StendhalRPZone zone)143 	public void setup(final StendhalRPZone zone) {
144 		String className = getImplementation();
145 
146 		if (className == null) {
147 			/*
148 			 * Default implementation
149 			 */
150 			className = Portal.class.getName();
151 		}
152 
153 		try {
154 			final Portal portal = (Portal) EntityFactoryHelper.create(className,
155 					getParameters(), getAttributes());
156 			if (portal == null) {
157 				logger.warn("Unable to create portal: " + className);
158 
159 				return;
160 			}
161 
162 			portal.setPosition(getX(), getY());
163 			portal.setIdentifier(getIdentifier());
164 
165 			final Object destIdentifier = getDestinationIdentifier();
166 
167 			if (destIdentifier != null) {
168 				portal.setDestination(getDestinationZone(), destIdentifier);
169 			}
170 
171 			// Set facing direction for portal used as destination.
172 			if (portal.has("face")) {
173 				portal.setFaceDirection(portal.get("face"));
174 			}
175 
176 			// Check for an existing portal at the location
177 			final Portal oportal = zone.getPortal(getX(), getY());
178 			if (oportal != null) {
179 				if (isReplacing()) {
180 					logger.debug("Replacing portal: " + oportal);
181 					zone.remove(oportal);
182 				} else {
183 					// reserved, and told not to replace it. just discard the portal
184 					return;
185 				}
186 			}
187 
188 			zone.add(portal);
189 		} catch (final IllegalArgumentException ex) {
190 			logger.error("Error with portal factory", ex);
191 		}
192 	}
193 }
194