AnvilSaveConverter .java

net.minecraft.world.chunk.storage.AnvilSaveConverter (class): members: convertMapFormat, getSaveVersion, isOldMapFormat, getSaveList, flushCache, getName.

net.minecraft.world.chunk.storageclass291 linescommonextends SaveFormatOld10.3 KB

API Surface (9)

String getName()
List<SaveFormatComparator> getSaveList()
int getSaveVersion()
void flushCache()
ISaveHandler getSaveLoader(String saveName, boolean storePlayerdata)
boolean isConvertible(String saveName)
boolean isOldMapFormat(String saveName)
boolean convertMapFormat(String filename, IProgressUpdate progressCallback)
boolean accept(File p_accept_1_, String p_accept_2_)

Fields & Constants (1)

logger

String literals (13)

"Anvil"
"DIM-1"
"DIM1"
"Scanning folders..."
"Total conversion count is "
"region"
"Unable to create level.dat_mcr backup"
"level.dat"
"level.dat_mcr"
".mcr"
".mca"
"Failed to fetch input stream"
"Level"

Full Source

AnvilSaveConverter.java291 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world.chunk.storage;
2
3
import com.google.common.collect.Lists;
4
import java.io.DataInputStream;
5
import java.io.DataOutputStream;
6
import java.io.File;
7
import java.io.FilenameFilter;
8
import java.io.IOException;
9
import java.util.Collection;
10
import java.util.Collections;
11
import java.util.List;
12
import net.minecraft.client.AnvilConverterException;
13
import net.minecraft.nbt.CompressedStreamTools;
14
import net.minecraft.nbt.NBTTagCompound;
15
import net.minecraft.util.IProgressUpdate;
16
import net.minecraft.world.WorldType;
17
import net.minecraft.world.biome.BiomeGenBase;
18
import net.minecraft.world.biome.WorldChunkManager;
19
import net.minecraft.world.biome.WorldChunkManagerHell;
20
import net.minecraft.world.storage.ISaveHandler;
21
import net.minecraft.world.storage.SaveFormatComparator;
22
import net.minecraft.world.storage.SaveFormatOld;
23
import net.minecraft.world.storage.WorldInfo;
24
import org.apache.commons.lang3.StringUtils;
25
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.Logger;
27
28
public class AnvilSaveConverter extends SaveFormatOld
29
{
30
    private static final Logger logger = LogManager.getLogger();
31
32
    public AnvilSaveConverter(File savesDirectoryIn)
33
    {
34
        super(savesDirectoryIn);
35
    }
36
37
    /**
38
     * Returns the name of the save format.
39
     */
40
    public String getName()
41
    {
42
        return "Anvil";
43
    }
44
45
    public List<SaveFormatComparator> getSaveList() throws AnvilConverterException
46
    {
47
        if (this.savesDirectory != null && this.savesDirectory.exists() && this.savesDirectory.isDirectory())
48
        {
49
            List<SaveFormatComparator> list = Lists.<SaveFormatComparator>newArrayList();
50
            File[] afile = this.savesDirectory.listFiles();
51
52
            for (File file1 : afile)
53
            {
54
                if (file1.isDirectory())
55
                {
56
                    String s = file1.getName();
57
                    WorldInfo worldinfo = this.getWorldInfo(s);
58
59
                    if (worldinfo != null && (worldinfo.getSaveVersion() == 19132 || worldinfo.getSaveVersion() == 19133))
60
                    {
61
                        boolean flag = worldinfo.getSaveVersion() != this.getSaveVersion();
62
                        String s1 = worldinfo.getWorldName();
63
64
                        if (StringUtils.isEmpty(s1))
65
                        {
66
                            s1 = s;
67
                        }
68
69
                        long i = 0L;
70
                        list.add(new SaveFormatComparator(s, s1, worldinfo.getLastTimePlayed(), i, worldinfo.getGameType(), flag, worldinfo.isHardcoreModeEnabled(), worldinfo.areCommandsAllowed()));
71
                    }
72
                }
73
            }
74
75
            return list;
76
        }
77
        else
78
        {
79
            throw new AnvilConverterException("Unable to read or access folder where game worlds are saved!");
80
        }
81
    }
82
83
    protected int getSaveVersion()
84
    {
85
        return 19133;
86
    }
87
88
    public void flushCache()
89
    {
90
        RegionFileCache.clearRegionFileReferences();
91
    }
92
93
    /**
94
     * Returns back a loader for the specified save directory
95
     */
96
    public ISaveHandler getSaveLoader(String saveName, boolean storePlayerdata)
97
    {
98
        return new AnvilSaveHandler(this.savesDirectory, saveName, storePlayerdata);
99
    }
100
101
    public boolean isConvertible(String saveName)
102
    {
103
        WorldInfo worldinfo = this.getWorldInfo(saveName);
104
        return worldinfo != null && worldinfo.getSaveVersion() == 19132;
105
    }
106
107
    /**
108
     * gets if the map is old chunk saving (true) or McRegion (false)
109
     */
110
    public boolean isOldMapFormat(String saveName)
111
    {
112
        WorldInfo worldinfo = this.getWorldInfo(saveName);
113
        return worldinfo != null && worldinfo.getSaveVersion() != this.getSaveVersion();
114
    }
115
116
    /**
117
     * converts the map to mcRegion
118
     */
119
    public boolean convertMapFormat(String filename, IProgressUpdate progressCallback)
120
    {
121
        progressCallback.setLoadingProgress(0);
122
        List<File> list = Lists.<File>newArrayList();
123
        List<File> list1 = Lists.<File>newArrayList();
124
        List<File> list2 = Lists.<File>newArrayList();
125
        File file1 = new File(this.savesDirectory, filename);
126
        File file2 = new File(file1, "DIM-1");
127
        File file3 = new File(file1, "DIM1");
128
        logger.info("Scanning folders...");
129
        this.addRegionFilesToCollection(file1, list);
130
131
        if (file2.exists())
132
        {
133
            this.addRegionFilesToCollection(file2, list1);
134
        }
135
136
        if (file3.exists())
137
        {
138
            this.addRegionFilesToCollection(file3, list2);
139
        }
140
141
        int i = list.size() + list1.size() + list2.size();
142
        logger.info("Total conversion count is " + i);
143
        WorldInfo worldinfo = this.getWorldInfo(filename);
144
        WorldChunkManager worldchunkmanager = null;
145
146
        if (worldinfo.getTerrainType() == WorldType.FLAT)
147
        {
148
            worldchunkmanager = new WorldChunkManagerHell(BiomeGenBase.plains, 0.5F);
149
        }
150
        else
151
        {
152
            worldchunkmanager = new WorldChunkManager(worldinfo.getSeed(), worldinfo.getTerrainType(), worldinfo.getGeneratorOptions());
153
        }
154
155
        this.convertFile(new File(file1, "region"), list, worldchunkmanager, 0, i, progressCallback);
156
        this.convertFile(new File(file2, "region"), list1, new WorldChunkManagerHell(BiomeGenBase.hell, 0.0F), list.size(), i, progressCallback);
157
        this.convertFile(new File(file3, "region"), list2, new WorldChunkManagerHell(BiomeGenBase.sky, 0.0F), list.size() + list1.size(), i, progressCallback);
158
        worldinfo.setSaveVersion(19133);
159
160
        if (worldinfo.getTerrainType() == WorldType.DEFAULT_1_1)
161
        {
162
            worldinfo.setTerrainType(WorldType.DEFAULT);
163
        }
164
165
        this.createFile(filename);
166
        ISaveHandler isavehandler = this.getSaveLoader(filename, false);
167
        isavehandler.saveWorldInfo(worldinfo);
168
        return true;
169
    }
170
171
    /**
172
     * par: filename for the level.dat_mcr backup
173
     */
174
    private void createFile(String filename)
175
    {
176
        File file1 = new File(this.savesDirectory, filename);
177
178
        if (!file1.exists())
179
        {
180
            logger.warn("Unable to create level.dat_mcr backup");
181
        }
182
        else
183
        {
184
            File file2 = new File(file1, "level.dat");
185
186
            if (!file2.exists())
187
            {
188
                logger.warn("Unable to create level.dat_mcr backup");
189
            }
190
            else
191
            {
192
                File file3 = new File(file1, "level.dat_mcr");
193
194
                if (!file2.renameTo(file3))
195
                {
196
                    logger.warn("Unable to create level.dat_mcr backup");
197
                }
198
            }
199
        }
200
    }
201
202
    private void convertFile(File p_75813_1_, Iterable<File> p_75813_2_, WorldChunkManager p_75813_3_, int p_75813_4_, int p_75813_5_, IProgressUpdate p_75813_6_)
203
    {
204
        for (File file1 : p_75813_2_)
205
        {
206
            this.convertChunks(p_75813_1_, file1, p_75813_3_, p_75813_4_, p_75813_5_, p_75813_6_);
207
            ++p_75813_4_;
208
            int i = (int)Math.round(100.0D * (double)p_75813_4_ / (double)p_75813_5_);
209
            p_75813_6_.setLoadingProgress(i);
210
        }
211
    }
212
213
    /**
214
     * copies a 32x32 chunk set from par2File to par1File, via AnvilConverterData
215
     */
216
    private void convertChunks(File p_75811_1_, File p_75811_2_, WorldChunkManager p_75811_3_, int p_75811_4_, int p_75811_5_, IProgressUpdate progressCallback)
217
    {
218
        try
219
        {
220
            String s = p_75811_2_.getName();
221
            RegionFile regionfile = new RegionFile(p_75811_2_);
222
            RegionFile regionfile1 = new RegionFile(new File(p_75811_1_, s.substring(0, s.length() - ".mcr".length()) + ".mca"));
223
224
            for (int i = 0; i < 32; ++i)
225
            {
226
                for (int j = 0; j < 32; ++j)
227
                {
228
                    if (regionfile.isChunkSaved(i, j) && !regionfile1.isChunkSaved(i, j))
229
                    {
230
                        DataInputStream datainputstream = regionfile.getChunkDataInputStream(i, j);
231
232
                        if (datainputstream == null)
233
                        {
234
                            logger.warn("Failed to fetch input stream");
235
                        }
236
                        else
237
                        {
238
                            NBTTagCompound nbttagcompound = CompressedStreamTools.read(datainputstream);
239
                            datainputstream.close();
240
                            NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("Level");
241
                            ChunkLoader.AnvilConverterData chunkloader$anvilconverterdata = ChunkLoader.load(nbttagcompound1);
242
                            NBTTagCompound nbttagcompound2 = new NBTTagCompound();
243
                            NBTTagCompound nbttagcompound3 = new NBTTagCompound();
244
                            nbttagcompound2.setTag("Level", nbttagcompound3);
245
                            ChunkLoader.convertToAnvilFormat(chunkloader$anvilconverterdata, nbttagcompound3, p_75811_3_);
246
                            DataOutputStream dataoutputstream = regionfile1.getChunkDataOutputStream(i, j);
247
                            CompressedStreamTools.write(nbttagcompound2, dataoutputstream);
248
                            dataoutputstream.close();
249
                        }
250
                    }
251
                }
252
253
                int k = (int)Math.round(100.0D * (double)(p_75811_4_ * 1024) / (double)(p_75811_5_ * 1024));
254
                int l = (int)Math.round(100.0D * (double)((i + 1) * 32 + p_75811_4_ * 1024) / (double)(p_75811_5_ * 1024));
255
256
                if (l > k)
257
                {
258
                    progressCallback.setLoadingProgress(l);
259
                }
260
            }
261
262
            regionfile.close();
263
            regionfile1.close();
264
        }
265
        catch (IOException ioexception)
266
        {
267
            ioexception.printStackTrace();
268
        }
269
    }
270
271
    /**
272
     * filters the files in the par1 directory, and adds them to the par2 collections
273
     */
274
    private void addRegionFilesToCollection(File worldDir, Collection<File> collection)
275
    {
276
        File file1 = new File(worldDir, "region");
277
        File[] afile = file1.listFiles(new FilenameFilter()
278
        {
279
            public boolean accept(File p_accept_1_, String p_accept_2_)
280
            {
281
                return p_accept_2_.endsWith(".mcr");
282
            }
283
        });
284
285
        if (afile != null)
286
        {
287
            Collections.addAll(collection, afile);
288
        }
289
    }
290
}
291

Deobfuscated MCP 1.8.9 source of AnvilSaveConverter from Marcelektro/MavenMCP-1.8.9 (net/minecraft/world/chunk/storage/AnvilSaveConverter.java), stored verbatim.