1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import org.bukkit.Location; |
4 |
import org.bukkit.World; |
5 |
import org.bukkit.command.Command; |
6 |
import org.bukkit.command.CommandExecutor; |
7 |
import org.bukkit.command.CommandSender; |
8 |
import org.bukkit.entity.CreatureType; |
9 |
import org.bukkit.entity.Player; |
10 |
|
11 |
public class SpawnCommand implements CommandExecutor { |
12 |
|
13 |
|
14 |
@Override |
15 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
16 |
|
17 |
if (!(sender instanceof Player)) { |
18 |
return false; |
19 |
} |
20 |
Player p = (Player) sender; |
21 |
|
22 |
if (args.length < 1 || args.length > 2) { |
23 |
p.sendMessage("Usage: /spawn <creature> [count] "); |
24 |
return true; |
25 |
} |
26 |
|
27 |
CreatureType creature = CreatureType.fromName( args[0] ); |
28 |
|
29 |
if (creature == null) { |
30 |
try { |
31 |
creature = CreatureType.valueOf( args[0].toUpperCase() ); |
32 |
} catch (IllegalArgumentException e) {} |
33 |
} |
34 |
|
35 |
if (creature == null) { |
36 |
p.sendMessage("No creature named : " + args[0] ); |
37 |
StringBuilder sb = new StringBuilder(); |
38 |
for(CreatureType ct : CreatureType.values() ) { |
39 |
sb.append( " "); |
40 |
sb.append( ct.getName() ); |
41 |
} |
42 |
p.sendMessage("Valid creatures:" + sb.toString() ); |
43 |
return true; |
44 |
} |
45 |
|
46 |
|
47 |
int count = 1; |
48 |
if (args.length == 2) { |
49 |
try { |
50 |
count = Integer.parseInt(args[1]); |
51 |
if (count > 16) |
52 |
count = 16; |
53 |
} catch (Exception e) { |
54 |
p.sendMessage("Invalid number : " + args[1]); |
55 |
return true; |
56 |
} |
57 |
} |
58 |
|
59 |
|
60 |
World w = p.getWorld(); |
61 |
Location l = p.getLocation(); |
62 |
for (int i=0; i<count; i++) { |
63 |
w.spawnCreature(l, creature); |
64 |
} |
65 |
|
66 |
return true; |
67 |
} |
68 |
} |