1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStreamReader; |
8 |
|
9 |
import org.bukkit.ChatColor; |
10 |
import org.bukkit.Location; |
11 |
import org.bukkit.World; |
12 |
import org.bukkit.command.Command; |
13 |
import org.bukkit.command.CommandExecutor; |
14 |
import org.bukkit.command.CommandSender; |
15 |
import org.bukkit.entity.Player; |
16 |
import org.bukkit.plugin.Plugin; |
17 |
|
18 |
public class TemplateCommand implements CommandExecutor { |
19 |
|
20 |
|
21 |
/*class Pair { |
22 |
public int id; |
23 |
public int subId; |
24 |
|
25 |
public Pair (int i, int s) { |
26 |
id = i; |
27 |
subId = s; |
28 |
} |
29 |
|
30 |
}*/ |
31 |
|
32 |
Plugin plugin; |
33 |
|
34 |
public TemplateCommand(Plugin plugin) { |
35 |
this.plugin = plugin; |
36 |
} |
37 |
|
38 |
@Override |
39 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
40 |
if (! (sender instanceof Player) ) { |
41 |
sender.sendMessage( "this command can not be run from console" ); |
42 |
return true; |
43 |
} |
44 |
|
45 |
Player player = (Player) sender; |
46 |
|
47 |
if (! sender.isOp() ) { |
48 |
sender.sendMessage( "you need to be op to run this command" ); |
49 |
return true; |
50 |
} |
51 |
|
52 |
if ( args.length != 1) { |
53 |
sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>"); |
54 |
return true; |
55 |
} |
56 |
|
57 |
File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt"); |
58 |
if (! templateFile.exists() ) { |
59 |
sender.sendMessage( ChatColor.YELLOW + "Template not found" ); |
60 |
return true; |
61 |
} |
62 |
|
63 |
int[][][] template; |
64 |
|
65 |
try { |
66 |
template = parseFile(templateFile); |
67 |
if( template == null) { |
68 |
sender.sendMessage( ChatColor.YELLOW + "Invalid template file" ); |
69 |
return true; |
70 |
} |
71 |
} catch (Exception e) { |
72 |
plugin.getLogger().info( e.getMessage() ); |
73 |
sender.sendMessage( ChatColor.YELLOW + e.getMessage() ); |
74 |
return true; |
75 |
} |
76 |
|
77 |
buildTemplate( player.getLocation(), template); |
78 |
|
79 |
return false; |
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
int[][][] parseFile(File templateFile) throws Exception { |
86 |
|
87 |
|
88 |
int lines = 0; |
89 |
|
90 |
FileInputStream fis = new FileInputStream( templateFile ); |
91 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
92 |
String line = in.readLine(); |
93 |
lines ++; |
94 |
|
95 |
if ( line.charAt(0) != '#') { |
96 |
throw new Exception( "Template: invalid start character" ); |
97 |
} |
98 |
|
99 |
line = line.replace('#', ' ').trim(); |
100 |
String dimensions[] = line.split(","); |
101 |
|
102 |
if ( dimensions.length != 3) { |
103 |
throw new Exception( "Template: invalid dimensions line" ); |
104 |
} |
105 |
|
106 |
int xsize = Integer.parseInt(dimensions[0]); |
107 |
int ysize = Integer.parseInt(dimensions[1]); |
108 |
int zsize = Integer.parseInt(dimensions[2]); |
109 |
|
110 |
int[][][] array = new int[xsize][ysize][zsize]; |
111 |
|
112 |
int x = 0; |
113 |
int y = 0; |
114 |
|
115 |
|
116 |
while ( (line = in.readLine() ) != null ) { |
117 |
lines++; |
118 |
|
119 |
line = line.trim(); |
120 |
if (line.length() == 0) |
121 |
continue; |
122 |
|
123 |
if (line.charAt(0) == '-') |
124 |
continue; |
125 |
|
126 |
String elements[] = line.split(","); |
127 |
if (elements.length != zsize) { |
128 |
throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); |
129 |
} |
130 |
|
131 |
for (int i=0; i<elements.length; i++) { |
132 |
try { |
133 |
String element = elements[i].trim(); |
134 |
int val = Integer.parseInt( element ); |
135 |
|
136 |
if (val < 0 || val>255) { |
137 |
throw new Exception( "Template: invalid value on line " + lines + ": " + val ); |
138 |
} |
139 |
array[x][y][i] = val; |
140 |
|
141 |
} catch (Exception e) { |
142 |
throw new Exception( "Template: invalid value on line " + lines + ": " + elements[i] ); |
143 |
} |
144 |
} |
145 |
|
146 |
x++; |
147 |
if (x >= xsize) { |
148 |
x = 0; |
149 |
y++; |
150 |
} |
151 |
} |
152 |
|
153 |
if (x != 0) { |
154 |
throw new Exception( "Template: not enough lines to complete the last level" ); |
155 |
} |
156 |
|
157 |
if (y != ysize) { |
158 |
throw new Exception( "Template: not enough levels"); |
159 |
} |
160 |
|
161 |
|
162 |
return array; |
163 |
|
164 |
} |
165 |
|
166 |
void buildTemplate(Location loc, int[][][] template) { |
167 |
World world = loc.getWorld(); |
168 |
for (int i=0; i<template.length; i++ ) { |
169 |
for (int j=0; j<template[0].length; j++) { |
170 |
for (int k=0; k<template[0][0].length; k++) { |
171 |
int x = loc.getBlockX() + i + 1; |
172 |
int y = loc.getBlockY() + j; |
173 |
int z = loc.getBlockZ() + k; |
174 |
|
175 |
int type = template[i][j][k]; |
176 |
|
177 |
|
178 |
//plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) ); |
179 |
|
180 |
world.getBlockAt(x, y, z).setTypeId( type ); |
181 |
} |
182 |
} |
183 |
} |
184 |
|
185 |
} |
186 |
|
187 |
} |