/
DATAFileFormatHelper.java
153 lines (138 loc) · 5.22 KB
/
DATAFileFormatHelper.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
package hu.laci200270.mods.skyblockgenerator;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.ForgeChunkManager.Ticket;
import net.minecraftforge.common.ForgeChunkManager.Type;
public class DATAFileFormatHelper {
@SuppressWarnings("deprecation")
public static NBTTagCompound saveWorldRegionToNBT(World world,
BlockPOS pos1, BlockPOS pos2) {
NBTTagCompound compound = new NBTTagCompound();
if (pos1.x > pos2.x) {
int temp1 = pos1.x;
int temp2 = pos2.x;
pos2.x = temp1;
pos1.x = temp2;
}
if (pos1.y > pos2.y) {
int temp1 = pos1.y;
int temp2 = pos2.y;
pos2.y = temp1;
pos1.y = temp2;
}
if (pos1.z > pos2.z) {
int temp1 = pos1.z;
int temp2 = pos2.z;
pos2.z = temp1;
pos1.z = temp2;
}
int height = Math.abs(pos1.y - pos2.y);
int width = Math.abs(pos1.x - pos2.x);
int depth = Math.abs(pos1.z - pos2.z);
System.out.println("Pos1:" + pos1);
System.out.println("Pos2:" + pos2);
System.out.println("height: " + height + "width: " + width + "depth: "
+ depth);
for (int y = pos1.y; y < pos2.y; y++) {
for (int x = pos1.x; x < pos2.x + 1; x++) {
for (int z = pos1.z; z < pos2.z + 1; z++) {
int relativeX = Math.abs(pos1.x - x);
int relativeZ = Math.abs(pos1.z - z);
int relativeY = Math.abs(pos1.y - y);
Block currentBlock = world.getBlock(x, y, z);
NBTTagCompound currentBlockCompound = new NBTTagCompound();
NBTTagCompound tileEntityCompound = new NBTTagCompound();
String currentBlockName = (String) Block.blockRegistry
.getNameForObject(currentBlock);
Integer metadata = world.getBlockMetadata(x, y, z);
System.out.println("processing block at:"
+ new BlockPOS(x, y, z) + "relative condinates:"
+ new BlockPOS(relativeX, relativeY, relativeZ)
+ "Block type:" + currentBlockName + "matedata:"
+ metadata);
currentBlockCompound.setString("blockid", currentBlockName);
currentBlockCompound.setInteger("metadata", metadata);
String name = "Block|" + relativeX + "|" + relativeY + "|"
+ relativeZ;
if (currentBlock.hasTileEntity(metadata)
|| currentBlock.hasTileEntity()) {
currentBlockCompound.setBoolean("hasTileEntity", true);
world.getTileEntity(x, y, z).writeToNBT(
tileEntityCompound);
}
currentBlockCompound.setTag("tileEntityTag",
tileEntityCompound);
compound.setTag(name, currentBlockCompound);
}
}
}
compound.setInteger("width", width);
compound.setInteger("height", height);
compound.setInteger("depth", depth);
return compound;
}
public static void loadWorldRegionFromNBT(World world, BlockPOS pos,
NBTTagCompound compound) {
System.out.println("Starting generating structure at: " + pos);
int chunkX=world.getChunkFromBlockCoords(pos.x, pos.z).xPosition;
int chunkZ=world.getChunkFromBlockCoords(pos.x, pos.z).zPosition;
System.out.println("Loading chunk at:" +chunkX+";"+chunkZ);
world.getChunkFromBlockCoords(chunkX, chunkZ);
Ticket chunkLoaderTicket= ForgeChunkManager.requestTicket(SkyBlockGeneratorModFile.instance, world, Type.NORMAL);
ForgeChunkManager.forceChunk(chunkLoaderTicket, new ChunkCoordIntPair(chunkX, chunkZ));
int height = compound.getInteger("height");
int width = compound.getInteger("width");
int depth = compound.getInteger("depth");
System.out.println("height: " + height + "width: " + width + "depth: "
+ depth);
for (int y = pos.y; y < pos.y + height; y++) {
for (int x = pos.x; x < pos.x + width + 1; x++) {
for (int z = pos.z; z < pos.z + depth + 1; z++) {
int relativeX = 0;
int relativeZ = 0;
if (pos.x > x) {
relativeX = pos.x - x;
} else {
relativeX = x - pos.x;
}
int relativeY = y - pos.y;
if (pos.z > z) {
relativeZ = pos.z - z;
} else {
relativeZ = z - pos.z;
}
NBTTagCompound currentBlockCompound = (NBTTagCompound) compound
.getTag("Block|" + relativeX + "|" + relativeY
+ "|" + relativeZ);
if (currentBlockCompound == null) {
throw new NullPointerException("compound is null");
}
NBTTagCompound tileEntityCompound = currentBlockCompound
.getCompoundTag("tileEntityTag");
int metadata = currentBlockCompound.getInteger("metadata");
Block currentBlock = Block
.getBlockFromName(currentBlockCompound
.getString("blockid"));
System.out.println("Setting block at: "
+ new BlockPOS(x, y, z) + "To this: "
+ currentBlockCompound.getString("blockid"));
world.setBlock(x, y, z, currentBlock, metadata, 2);
if (currentBlockCompound.getBoolean("hasTileEntity")) {
tileEntityCompound.setInteger("x", x);
tileEntityCompound.setInteger("y", y);
tileEntityCompound.setInteger("z", z);
world.getTileEntity(x, y, z).readFromNBT(
tileEntityCompound);
}
}
}
}
System.out.println("Unloading chunk");
ForgeChunkManager.unforceChunk(chunkLoaderTicket, new ChunkCoordIntPair(chunkX, chunkZ));
System.out.println("Releasing ticket");
ForgeChunkManager.releaseTicket(chunkLoaderTicket);
}
}