-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlueprintDatabase.java
132 lines (114 loc) · 3.77 KB
/
BlueprintDatabase.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
/**
* Copyright (c) SpaceToad, 2011-2012
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.builders.blueprints;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class BlueprintDatabase {
public static File configFolder;
private static Map<UUID, Blueprint> blueprints = new HashMap<UUID, Blueprint>();
public static Blueprint getBlueprint(UUID uuid) {
Blueprint blueprint = blueprints.get(uuid);
if (blueprint == null) {
blueprint = loadBlueprint(uuid);
addBlueprint(blueprint);
}
return blueprint;
}
public static void addBlueprint(Blueprint blueprint) {
if (blueprint == null)
return;
blueprints.put(blueprint.getUUID(), blueprint);
}
private static File getBlueprintFolder() {
File blueprintFolder = new File(configFolder, "buildcraft/blueprints/");
if (!blueprintFolder.exists()) {
blueprintFolder.mkdirs();
}
return blueprintFolder;
}
private static String uuidToString(UUID uuid) {
return String.format(Locale.ENGLISH, "%x%x", uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
public static void saveBlueprint(Blueprint blueprint) {
NBTTagCompound nbt = new NBTTagCompound();
blueprint.writeToNBT(nbt);
File blueprintFile = new File(getBlueprintFolder(), String.format(Locale.ENGLISH, "%x%x-%s.nbt", uuidToString(blueprint.getUUID()), blueprint.getName()));
if (blueprintFile.exists())
return;
try {
CompressedStreamTools.write(nbt, blueprintFile);
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to save Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
}
}
public static void saveBlueprints() {
for (Blueprint blueprint : blueprints.values()) {
saveBlueprint(blueprint);
}
}
private static Blueprint loadBlueprint(final UUID uuid) {
FilenameFilter filter = new FilenameFilter() {
private String uuidString = uuidToString(uuid);
@Override
public boolean accept(File dir, String name) {
return name.startsWith(uuidString);
}
};
NBTTagCompound nbt = null;
File blueprintFolder = getBlueprintFolder();
for (File blueprintFile : blueprintFolder.listFiles(filter)) {
try {
nbt = CompressedStreamTools.read(blueprintFile);
break;
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to load Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
}
}
if (nbt == null) {
return null;
}
return Blueprint.readFromNBT(nbt);
}
public static void loadBlueprints() {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".nbt");
}
};
File blueprintFolder = getBlueprintFolder();
for (File blueprintFile : blueprintFolder.listFiles(filter)) {
try {
NBTTagCompound nbt = CompressedStreamTools.read(blueprintFile);
addBlueprint(Blueprint.readFromNBT(nbt));
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to load Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
}
}
}
@SideOnly(Side.CLIENT)
public static void sendBlueprintsToServer() {
// TODO
}
}