/
Pack.java
166 lines (147 loc) · 5.26 KB
/
Pack.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
package mods.quiddity.redux.json.model;
import com.google.common.collect.ImmutableList;
import mods.quiddity.redux.JavaScript.ReduxJavascriptEngine;
import mods.quiddity.redux.Redux;
import net.minecraftforge.fml.common.FMLCommonHandler;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* JSON Object model to represent Redux packs.
*
* @author winsock on 2/3/15.
*/
@SuppressWarnings("all")
public class Pack {
/**
* <h1>Required</h1>
* The mod id of the pack. Must not conflict with any other {@link net.minecraftforge.fml.common.FMLModContainer}.
* <em>Should contain no spaces or special characters.</em>
* <em>ASCII</em>
*/
@Nonnull
protected String id;
/**
* <h1>Required</h1>
* The human readable name of the mod.
* Will be displayed as a child-mod of Redux in the mods list
*/
@Nonnull
protected String name;
/**
* The author of the Redux pack.
* <em>Curently doesn't display anywhere</em>
*/
@Nullable
protected String author;
/**
* The author's description of the Redux pack.
* <em>Curently doesn't display anywhere</em>
*/
@Nullable
protected String description;
/**
* A list of blocks to load.
* Not <i>required</i> however the pack will not have any new blocks without them
* {@link mods.quiddity.redux.json.model.Block}
*/
@Nullable
protected transient List<Block> block_list;
/**
* A list of items to load.
* Not <i>required</i> however the pack will not have any new items without them
* {@link mods.quiddity.redux.json.model.Item}
*
* <em>Unimplemented</em>
*/
@Nullable
protected List<Item> item_list;
private transient Map<String, Block> idMap = null;
private transient boolean hasAddedBlocks = false;
private transient ReduxJavascriptEngine jsEngine;
public String getName() {
return name;
}
public String getId() {
return id;
}
public List<Block> getBlocks() {
return block_list == null ? null : block_list;
}
public void setBlockList(List<Block> blocks) {
if (!hasAddedBlocks) {
block_list = ImmutableList.copyOf(blocks);
hasAddedBlocks = true;
}
}
public Block getBlockFromId(String id) {
if (getBlocks() == null || id == null)
return null;
if (idMap == null) {
idMap = new HashMap<String, Block>();
for (Block b : getBlocks()) {
idMap.put(b.getId(), b);
}
}
return idMap.get(id);
}
public List<Item> getItems() {
return item_list == null ? null : ImmutableList.copyOf(item_list);
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public ReduxJavascriptEngine getJsEngine() {
if (jsEngine == null) {
jsEngine = new ReduxJavascriptEngine(this);
jsEngine.init();
File packFile = Redux.instance.getReduxConfiguration().getSourceForPack(this);
if (packFile.getParentFile().isDirectory()) {
File scripts = new File(packFile.getParentFile(), "scripts");
for (File script : FileUtils.listFiles(scripts, new String[]{"js"}, true)) {
try {
jsEngine.getEngine().loadScript(FileUtils.readFileToString(script));
} catch (Exception e) {
Redux.instance.getLogger().warn("Redux pack inconsistency. The script file: %s has errors.", scripts.getName());
}
}
} else {
try {
ZipFile packZip = new ZipFile(packFile.getParentFile());
Enumeration<ZipArchiveEntry> entries = packZip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
if (entry.getName().endsWith(".js")) {
InputStream scriptInputStream = packZip.getInputStream(entry);
jsEngine.getEngine().loadScript(IOUtils.toString(scriptInputStream, Charset.defaultCharset()));
}
}
} catch (IOException e) {
Redux.instance.getLogger().warn("Redux pack inconsistency. The pack file %s has vanished!.", packFile.getParentFile().getName());
} catch (ScriptException e) {
Redux.instance.getLogger().warn("Redux pack inconsistency. A script file in pack: %s has errors.", packFile.getParentFile().getName());
}
}
}
return jsEngine;
}
@Override
public String toString() {
return getName();
}
}