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 |
torben |
1746 |
|
21 |
torben |
1738 |
/*class Pair { |
22 |
|
|
public int id; |
23 |
|
|
public int subId; |
24 |
torben |
1746 |
|
25 |
torben |
1738 |
public Pair (int i, int s) { |
26 |
|
|
id = i; |
27 |
|
|
subId = s; |
28 |
|
|
} |
29 |
torben |
1746 |
|
30 |
torben |
1738 |
}*/ |
31 |
torben |
1746 |
|
32 |
torben |
1738 |
Plugin plugin; |
33 |
torben |
1746 |
|
34 |
torben |
1738 |
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 |
torben |
1746 |
|
45 |
torben |
1738 |
Player player = (Player) sender; |
46 |
torben |
1746 |
|
47 |
torben |
1738 |
if (! sender.isOp() ) { |
48 |
|
|
sender.sendMessage( "you need to be op to run this command" ); |
49 |
|
|
return true; |
50 |
|
|
} |
51 |
torben |
1746 |
|
52 |
torben |
1738 |
if ( args.length != 1) { |
53 |
|
|
sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>"); |
54 |
|
|
return true; |
55 |
|
|
} |
56 |
torben |
1746 |
|
57 |
torben |
1738 |
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 |
torben |
1746 |
|
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 |
torben |
1738 |
} |
76 |
torben |
1746 |
|
77 |
torben |
1738 |
buildTemplate( player.getLocation(), template); |
78 |
|
|
|
79 |
|
|
return false; |
80 |
|
|
} |
81 |
|
|
|
82 |
torben |
1746 |
|
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 |
torben |
1738 |
|
99 |
torben |
1746 |
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 |
torben |
1738 |
|
106 |
torben |
1746 |
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 |
torben |
1738 |
} |
130 |
torben |
1746 |
|
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 |
torben |
1738 |
} |
139 |
torben |
1746 |
array[x][y][i] = val; |
140 |
|
|
|
141 |
|
|
} catch (Exception e) { |
142 |
|
|
throw new Exception( "Template: invalid value on line " + lines + ": " + elements[i] ); |
143 |
torben |
1738 |
} |
144 |
|
|
} |
145 |
torben |
1746 |
|
146 |
|
|
x++; |
147 |
|
|
if (x >= xsize) { |
148 |
|
|
x = 0; |
149 |
|
|
y++; |
150 |
torben |
1738 |
} |
151 |
|
|
} |
152 |
torben |
1746 |
|
153 |
|
|
if (x != 0) { |
154 |
|
|
throw new Exception( "Template: not enough lines to complete the last level" ); |
155 |
torben |
1738 |
} |
156 |
torben |
1746 |
|
157 |
|
|
if (y != ysize) { |
158 |
|
|
throw new Exception( "Template: not enough levels"); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
|
|
return array; |
163 |
|
|
|
164 |
torben |
1738 |
} |
165 |
torben |
1746 |
|
166 |
torben |
1738 |
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 |
torben |
1746 |
|
175 |
torben |
1743 |
int type = template[i][j][k]; |
176 |
torben |
1746 |
|
177 |
|
|
|
178 |
torben |
1745 |
//plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) ); |
179 |
torben |
1746 |
|
180 |
torben |
1743 |
world.getBlockAt(x, y, z).setTypeId( type ); |
181 |
torben |
1738 |
} |
182 |
|
|
} |
183 |
|
|
} |
184 |
torben |
1746 |
|
185 |
torben |
1738 |
} |
186 |
|
|
|
187 |
|
|
} |