/
CopyWand.java
403 lines (360 loc) · 12.8 KB
/
CopyWand.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package blockworks.items;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.common.Configuration.UnicodeInputStreamReader;
import net.minecraftforge.common.ForgeDirection;
import blockworks.Blockworks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/*
* The great copy wand
* Move structures from one place to another!
*/
public class CopyWand extends WandBase
{
public int[][][] blockIDs = new int[0][0][0];
public int[][][] metadata = new int[0][0][0];
public HashMap<List, Integer> blockCount = new HashMap<List, Integer>();
public CopyWand(int id)
{
super(id);
}
@Override
public ItemStack onItemRightClick (ItemStack stack, World world, EntityPlayer player)
{
if (player.isSneaking())
rotateStructure(player);
else
copyArea(player, stack);
return stack;
}
@Override
public boolean onEntitySwing (EntityLivingBase player, ItemStack stack)
{
placeCopy(player, stack);
return true;
}
public void rotateStructure (EntityPlayer player)
{
if (!player.worldObj.isRemote && player instanceof EntityPlayer)
((EntityPlayer) player).addChatMessage("Rotating Structure");
int xLength = blockIDs.length;
int yLength = blockIDs[0].length;
int zLength = blockIDs[0][0].length;
int[][][] tempIDs = new int[zLength][yLength][xLength];
int[][][] tempMeta = new int[zLength][yLength][xLength];
for (int y = 0; y < yLength; y++)
{
for (int x = 0; x < xLength; x++)
{
for (int z = 0; z < zLength; z++)
{
tempIDs[zLength - z - 1][y][x] = blockIDs[x][y][z]; //180 degrees?!
tempMeta[zLength - z - 1][y][x] = metadata[x][y][z];
}
}
}
blockIDs = tempIDs;
metadata = tempMeta;
}
boolean inside ()
{
return true;
}
public void copyArea (EntityLivingBase player, ItemStack stack)
{
MovingObjectPosition mop = this.raytraceFromPlayer(player.worldObj, player, false);
if (mop != null)
{
int xPos = mop.blockX;
int yPos = mop.blockY;
int zPos = mop.blockZ;
if (!inside())
{
ForgeDirection sideHit = ForgeDirection.getOrientation(mop.sideHit);
switch (sideHit)
{
case UP:
yPos += 1;
break;
case DOWN:
yPos -= 1;
break;
case NORTH:
zPos -= 1;
break;
case SOUTH:
zPos += 1;
break;
case EAST:
xPos += 1;
break;
case WEST:
xPos -= 1;
break;
default:
break;
}
}
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("Wandworks");
byte clicks = tags.getByte("clicks");
if (clicks == 0)
{
tags.setIntArray("clickOne", new int[] { xPos, yPos, zPos });
if (!player.worldObj.isRemote && player instanceof EntityPlayer)
((EntityPlayer) player).addChatMessage("First coord set at X: " + xPos + ", Y: " + yPos + ", Z: " + zPos);
tags.setByte("clicks", (byte) 1);
}
else
{
int[] start = tags.getIntArray("clickOne");
int startX = xPos < start[0] ? xPos : start[0];
int startY = yPos < start[1] ? yPos : start[1];
int startZ = zPos < start[2] ? zPos : start[2];
int endX = xPos > start[0] ? xPos : start[0];
int endY = yPos > start[1] ? yPos : start[1];
int endZ = zPos > start[2] ? zPos : start[2];
blockIDs = new int[endX - startX + 1][endY - startY + 1][endZ - startZ + 1];
metadata = new int[endX - startX + 1][endY - startY + 1][endZ - startZ + 1];
blockCount.clear();
for (int x = startX; x <= endX; x++)
{
for (int y = startY; y <= endY; y++)
{
for (int z = startZ; z <= endZ; z++)
{
readBlockIntoArray(player.worldObj, x, y, z, startX, startY, startZ);
//player.worldObj.setBlock(x, y, z, tags.getInteger("mainID"), tags.getInteger("mainMeta"), 3);
}
}
}
if (!player.worldObj.isRemote && player instanceof EntityPlayer)
((EntityPlayer) player).addChatMessage("Second coord set at X: " + xPos + ", Y: " + yPos + ", Z: " + zPos);
tags.setByte("clicks", (byte) 0);
tags.setIntArray("clickOne", new int[0]);
}
}
}
void readBlockIntoArray (World world, int x, int y, int z, int startX, int startY, int startZ)
{
int blockID = world.getBlockId(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
blockIDs[x - startX][y - startY][z - startZ] = blockID;
metadata[x - startX][y - startY][z - startZ] = meta;
List key = Arrays.asList(blockID, meta);
int amount = 0;
if (blockCount.containsKey(key))
amount = blockCount.get(key);
amount++;
blockCount.put(key, amount);
}
public void placeCopy (EntityLivingBase player, ItemStack stack)
{
if (blockIDs.length > 0)
{
MovingObjectPosition mop = this.raytraceFromPlayer(player.worldObj, player, false);
if (mop != null)
{
int xPos = mop.blockX;
int yPos = mop.blockY;
int zPos = mop.blockZ;
ForgeDirection sideHit = ForgeDirection.getOrientation(mop.sideHit);
switch (sideHit)
{
case UP:
yPos += 1;
break;
case DOWN:
yPos -= 1;
break;
case NORTH:
zPos -= 1;
break;
case SOUTH:
zPos += 1;
break;
case EAST:
xPos += 1;
break;
case WEST:
xPos -= 1;
break;
default:
break;
}
World world = player.worldObj;
for (int x = 0; x < blockIDs.length; x++)
{
for (int y = 0; y < blockIDs[0].length; y++)
{
for (int z = 0; z < blockIDs[0][0].length; z++)
{
world.setBlock(xPos + x, yPos + y, zPos + z, blockIDs[x][y][z], metadata[x][y][z], 3);
}
}
}
}
}
}
String structureName ()
{
return "structure";
}
@Override
public void saveKey ()
{
System.out.println("Saving structure");
if (blockIDs.length > 0)
{
File structure = new File(Blockworks.structureFolder.getAbsolutePath() + "/" + structureName() + ".txt");
FileOutputStream fos;
try
{
fos = new FileOutputStream(structure);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
writer.write("Structure: Test");
writer.newLine();
writer.write("Size - x:" + blockIDs.length + ", y:" + blockIDs[0].length + ", z:" + blockIDs[0][0].length);
writer.newLine();
writeBlockAmounts(writer);
writer.newLine();
for (int y = blockIDs[0].length - 1; y >= 0; y--)
{
writer.write("startlayer y:" + y);
writer.newLine();
for (int x = 0; x < blockIDs.length; x++)
{
for (int z = 0; z < blockIDs[0][0].length; z++)
{
writer.write(blockIDs[x][y][z] + ":" + metadata[x][y][z] + ",");
}
writer.newLine();
}
writer.write("endlayer");
writer.newLine();
writer.newLine();
}
//buffer.write("Stuff");
writer.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
void writeBlockAmounts (BufferedWriter writer) throws IOException
{
blockCount.remove(Arrays.asList(0, 0));
Iterator iter = blockCount.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry pairs = (Map.Entry) iter.next();
List list = (List) pairs.getKey();
Object[] block = list.toArray();
writer.write(block[0] + ":" + block[1] + " - " + pairs.getValue());
writer.newLine();
}
}
@Override
public void loadKey ()
{
System.out.println("Loading structure");
File file = new File(Blockworks.structureFolder.getAbsolutePath() + "/" + structureName() + ".txt");
BufferedReader reader = null;
UnicodeInputStreamReader input = null;
try
{
input = new UnicodeInputStreamReader(new FileInputStream(file), "UTF-8");
reader = new BufferedReader(input);
reader.readLine();
String line = reader.readLine();
int[] size = new int[3];
String[] sizeString = line.split(",");
for (int i = 0; i < 3; i++)
{
String[] lsize = sizeString[i].split(":");
size[i] = new Integer(lsize[1]);
}
blockIDs = new int[size[0]][size[1]][size[2]];
metadata = new int[size[0]][size[1]][size[2]];
while (true)
{
line = reader.readLine();
if (line == null)
{
break;
}
if (line.contains("startlayer"))
{
String[] split = line.split(":");
Integer i = new Integer(split[1]);
readLayer(reader, new Integer(split[1]));
}
}
input.close();
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void readLayer (BufferedReader reader, Integer y) throws IOException
{
int z = 0;
String line;
while (true)
{
line = reader.readLine();
if (line == null || line.contains("endlayer"))
{
break;
}
String[] blocks = line.split(",");
for (int x = 0; x < blocks.length; x++)
{
String[] ids = blocks[x].split(":");
blockIDs[x][y][z] = new Integer(ids[0]);
metadata[x][y][z] = new Integer(ids[1]);
}
z++;
}
}
@Override
public void getSubItems (int id, CreativeTabs tabs, List list)
{
ItemStack stack = new ItemStack(id, 1, 0);
NBTTagCompound compound = new NBTTagCompound();
NBTTagCompound tags = new NBTTagCompound();
compound.setCompoundTag("Wandworks", tags);
stack.setTagCompound(compound);
list.add(stack);
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation (ItemStack stack, EntityPlayer player, List list, boolean par4)
{
list.add("\u00a7bCopies the world data between two points");
list.add("\u00a7bRight-click: Select two points to copy");
list.add("\u00a7bLeft-click: Paste copied data into the world");
}
}