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