/
CommandStructureCapture.java
343 lines (318 loc) · 10.6 KB
/
CommandStructureCapture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
Copyright (C) 2017 by jabelar
This file is part of jabelar's Minecraft Forge modding examples; as such,
you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.
*/
package com.blogspot.jabelarminecraft.examplemod.commands;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
// TODO: Auto-generated Javadoc
/**
* This command is used to write information about every block in an area
* to a file, in basically a simplified schematic format. This can be used in
* conjuction with a complementary build structure command that takes the file
* and places the blocks to recreate the structure.
* moved wrongly
* @author jabelar
*
*/
public class CommandStructureCapture extends CommandBase
{
private final List<String> aliases;
World theWorld;
Entity thePlayer;
public static String[][][] blockNameArray;
public static int[][][] blockMetaArray;
int startX;
int startY;
int startZ;
int endX;
int endY;
int endZ;
static int dimX;
static int dimY;
static int dimZ;
static int signX;
static int signY;
static int signZ;
/**
* Instantiates a new command structure capture.
*/
public CommandStructureCapture()
{
aliases = new ArrayList<String>();
aliases.add("capture");
aliases.add("capt");
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#getName()
*/
@Override
public String getName()
{
return "capture";
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#getUsage(net.minecraft.command.ICommandSender)
*/
@Override
public String getUsage(ICommandSender var1)
{
return "capture <int> <int> <int> <int> <int> <int> <text>"; // use "structure <text>"; later when passing name of structure
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#getAliases()
*/
@Override
public List<String> getAliases()
{
return this.aliases;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#execute(net.minecraft.server.MinecraftServer, net.minecraft.command.ICommandSender, java.lang.String[])
*/
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] argString)
{
theWorld = sender.getEntityWorld();
if (theWorld.isRemote)
{
System.out.println("Not processing on Client side");
}
else
{
if (argString.length != 7)
{
sender.sendMessage(new TextComponentString("Invalid number of arguments"));
return;
}
thePlayer = sender.getCommandSenderEntity();
startX = Integer.parseInt(argString[0]);
startY = Integer.parseInt(argString[1]);
startZ = Integer.parseInt(argString[2]);
endX = Integer.parseInt(argString[3]);
endY = Integer.parseInt(argString[4]);
endZ = Integer.parseInt(argString[5]);
dimX = Math.abs(endX - startX);
dimY = Math.abs(endY - startY);
dimZ = Math.abs(endZ - startZ);
if (endX < startX)
{
int temp = startX;
startX = endX;
endX = temp;
}
if (endY < startY)
{
int temp = startY;
startY = endY;
endY = temp;
}
if (endZ < startZ)
{
int temp = startZ;
startZ = endZ;
endZ = temp;
}
if (dimX * dimY * dimZ > 64 * 64 * 64)
{
sender.sendMessage(new TextComponentString("Capture area too big"));
return;
}
sender.sendMessage(new TextComponentString("Capturing Structure from " + startX + ", " + startY + ", " +
startZ + " to " + endX + ", " + endY + ", " + endZ));
blockNameArray = new String[dimX][dimY][dimZ];
blockMetaArray = new int[dimX][dimY][dimZ];
for (int indY = 0; indY < dimY; indY++) // Y first to organize in vertical layers
{
for (int indX = 0; indX < dimX; indX++)
{
for (int indZ = 0; indZ < dimZ; indZ++)
{
BlockPos theBlockPos = new BlockPos(startX + indX, startY + indY, startZ + indZ);
blockNameArray[indX][indY][indZ] = Block.REGISTRY.getNameForObject(theWorld
.getBlockState(theBlockPos).getBlock()).toString();
blockMetaArray[indX][indY][indZ] = theWorld.getBlockState(theBlockPos)
.getBlock().getMetaFromState(theWorld.getBlockState(theBlockPos));
}
}
}
writeFileNameArray(argString[6]);
}
}
/**
* Prints the name array.
*/
protected void printNameArray()
{
System.out.println("// Block Name Array");
System.out.println("{");
for (int indY = 0; indY < dimY; indY++) // Y first to organize in layers
{
System.out.println(" { // Layer =" + indY);
for (int indX = 0; indX < dimX; indX++)
{
String row = "";
for (int indZ = 0; indZ < dimZ; indZ++)
{
if (indZ < dimZ - 1) // not last element in row
{
row = row + blockNameArray[indX][indY][indZ] + ", ";
}
else // last element in row
{
row = row + blockNameArray[indX][indY][indZ];
}
}
if (indX < dimX - 1) // not last element in column
{
System.out.println(" { " + row + " },");
}
else // last element in column
{
System.out.println(" { " + row + " }");
}
}
if (indY < dimY - 1) // not last layer
{
System.out.println(" },");
}
else // last layer
{
System.out.println(" }");
}
}
System.out.println("};");
}
/**
* Prints the meta array.
*/
protected void printMetaArray()
{
System.out.println("// Metadata Array");
System.out.println("{");
for (int indY = 0; indY < dimY; indY++) // Y first to organize in layers
{
System.out.println(" { // Layer =" + indY);
for (int indX = 0; indX < dimX; indX++)
{
String row = "";
for (int indZ = 0; indZ < dimZ; indZ++)
{
if (indZ < dimZ - 1) // not last element in row
{
row = row + blockMetaArray[indX][indY][indZ] + ", ";
}
else // last element in row
{
row = row + blockMetaArray[indX][indY][indZ];
}
}
if (indX < dimX - 1) // not last element in column
{
System.out.println(" { " + row + " },");
}
else // last element in column
{
System.out.println(" { " + row + " }");
}
}
if (indY < dimY - 1) // not last layer
{
System.out.println(" },");
}
else // last layer
{
System.out.println(" }");
}
}
System.out.println("};");
}
/**
* Write file name array.
*
* @param fileName the file name
*/
protected void writeFileNameArray(String fileName)
{
File file = new File(fileName + ".txt");
PrintWriter printOut = null;
try
{
printOut = new PrintWriter(new FileWriter(file));
}
catch (IOException e)
{
e.printStackTrace();
}
// print dimensions to the file
if (printOut != null)
{
printOut.println(dimX);
printOut.println(dimY);
printOut.println(dimZ);
// Write each string in the array on a separate line
for (int indY = 0; indY < dimY; indY++) // Y first to organize in layers
{
for (int indX = 0; indX < dimX; indX++)
{
for (int indZ = 0; indZ < dimZ; indZ++)
{
printOut.println(blockNameArray[indX][indY][indZ]);
printOut.println(blockMetaArray[indX][indY][indZ]);
}
}
}
printOut.close();
}
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#checkPermission(net.minecraft.server.MinecraftServer, net.minecraft.command.ICommandSender)
*/
@Override
public boolean checkPermission(MinecraftServer server, ICommandSender var1)
{
return true;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.command.ICommand#getTabCompletions(net.minecraft.server.MinecraftServer, net.minecraft.command.ICommandSender, java.lang.String[],
* net.minecraft.util.math.BlockPos)
*/
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args,
BlockPos targetPos)
{
return null;
}
}