Source code: org/metacosm/framework/command/NewEntity.java
1 /*
2 Metacosm, an object-oriented network game framework
3 Copyright (C) 1999-2001 Metacosm Development Team
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.metacosm.framework.command;
21
22 import org.metacosm.framework.server.Player;
23 import org.metacosm.framework.server.Game;
24 import org.metacosm.framework.entity.Entity;
25 import org.metacosm.framework.entity.BasicPlace;
26 import org.metacosm.framework.entity.BasicItem;
27 import org.metacosm.framework.entity.BasicCreature;
28 import org.metacosm.framework.entity.IdServer;
29 import java.util.ArrayList;
30
31 /**
32 * Creates an Entity.
33 *
34 * syntax: new_entity (place|item|creature) [place_id]<br>
35 * If no place_id is given, current place entity's attribute is set to null.
36 * TODO Worlds management
37 */
38 final public class NewEntity implements PlayerCommand {
39 public Result execute(Player player, ArrayList params) {
40 if ( params.size() < 1) {
41 player.send( help);
42 return Result.NOT_ENOUGH_PARAMETERS;
43 } else if (params.size() > 2) {
44 player.send( help);
45 return Result.TOO_MANY_PARAMETERS;
46 }
47
48 String what = params.get(0).toString();
49 Game game = Game.getInstance();
50 Entity place = null;
51
52 if ( params.size() == 2) {
53 Integer place_id = null;
54 try {
55 place_id = Integer.valueOf( params.get(1).toString());
56 } catch( NumberFormatException e) {
57 player.send( "Error: invalid place id");
58 return Result.BAD_PARAMETER;
59 }
60 if (place_id.compareTo( new Integer(0)) < 0) {
61 player.send( "Error: invalid place id");
62 return Result.BAD_PARAMETER;
63 }
64 place = game.getEntity( place_id);
65 if (place == null) {
66 player.send( "Error: no Entity corresponding to Place id");
67 return Result.BAD_PARAMETER;
68 }
69 if (!(place instanceof BasicPlace)) {
70 player.send( "Error: Entity corresponding to Place id is not a Place");
71 return Result.BAD_PARAMETER;
72 }
73 }
74
75 int eid = IdServer.getInstance().getEntityId();
76 player.send("Entity id " + eid + " reserved.");
77
78 Entity entity = null;
79 if ( what.equals("place")) {
80 entity = new BasicPlace( eid, (BasicPlace)place, null);
81 } else if ( what.equals("item")) {
82 entity = new BasicItem( eid, (BasicPlace)place, null);
83 } else if ( what.equals("creature")) {
84 entity = new BasicCreature( eid, (BasicPlace)place, null);
85 } else {
86 player.send( help);
87 return Result.BAD_PARAMETER;
88 }
89 try {
90 game.addEntity( entity);
91 } catch (IllegalArgumentException e) {
92 player.send("Error: Entity ID already attributed (should NEVER happen)");
93 return Result.BAD_PARAMETER;
94 }
95 player.send("Ok.");
96 return Result.OK;
97 }
98
99 private final static String help = "Usage: new_entity (creature|item|place) [place_id]";
100 }
101