-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinecraftCoder_v2.java
244 lines (200 loc) · 6.78 KB
/
MinecraftCoder_v2.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
/**
* @(#)MinecraftCoder_v2.java
*
*
* @author
* @version 1.00 2015/3/11
*/
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
public class MinecraftCoder_v2
{
/* Outdated method
public static void compile(String filename)
{
try
{
LinkedList<String> commands = new LinkedList<String>();
LinkedList<String> inputs;
String line;
BufferedReader reader = new BufferedReader(new FileReader(new File(filename + ".txt")));
while((line=reader.readLine())!=null)
{
if(!line.substring(0,1).equals(";"))
{
commands.push(line);
}
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename + "_compiled_v2.txt")));
writer.write("/summon FallingSand ~ ~2 ~ {Block:redstone_block,Time:1,Riding:\r\n");
writer.write("{id:FallingSand,Block:command_block,TileEntityData:\r\n");
writer.write("\t{Command:" + addCommands(commands,1) + "},Time:1}}\r\n");
writer.flush();
writer.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}*/
public static File compile(File file,EditorOverlord output)
{
try
{
LinkedList<String> commands = new LinkedList<String>();
LinkedList<String> inputs;
String line;
BufferedReader reader = new BufferedReader(new FileReader(file));
int charCount = 0,xOffset = 0,yOffset = 0,zOffset = 0;
while((line=reader.readLine())!=null)
{
if(!line.substring(0,1).equals(";"))
{
if(line.substring(0,1).equals("@"))
{
switch (line.charAt(1))
{
case 'x':
xOffset = Integer.parseInt(line.substring(2).trim());
break;
case 'y':
yOffset = Integer.parseInt(line.substring(2).trim());
break;
case 'z':
zOffset = Integer.parseInt(line.substring(2).trim());
break;
}
}
else
{
commands.push(line);
}
}
}
reader.close();
String filePath = file.getPath();
filePath = filePath.substring(0,filePath.length()-5) + "_compiled.txt";
File compiledFile = new File(filePath);
BufferedWriter writer = new BufferedWriter(new FileWriter(compiledFile));
writer.write("/summon FallingSand ~ ~2 ~ {Block:redstone_block,Time:1,Riding:\r\n");
charCount += 63;
writer.write("{id:FallingSand,Block:command_block,TileEntityData:\r\n");
charCount += 51;
String fillCommands = addCommands(commands,1,xOffset,yOffset,zOffset,0);
writer.write("\t{Command:" + fillCommands + "},Time:1}}\r\n");
charCount += fillCommands.length() + 19;
output.addToOutput("Compiled file " + file.getName());
output.addToOutput("Length of complied file: " + charCount + "/32767");
writer.flush();
writer.close();
return compiledFile;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
public static String addCommands(LinkedList<String> commands,int stacks,int xOffset,int yOffset,int zOffset,int summonOffset)
{
String writer = "";
LinkedList<String> inputs = new LinkedList<String>();
int brackets = 1;
int stackHeight = 60;
for(int i=0;i<stackHeight*2.0/3.0;i++)
{
if(commands.size()>0)
inputs.push(commands.pollLast());
else
break;
}
if(commands.size()!=0)
{
inputs.push(addCommands(commands,stacks+1,xOffset,yOffset,zOffset,inputs.size()+1));
}
else
{
inputs.push("/summon FallingSand ~-1 ~ ~ {Block:redstone_block,Time:1,Riding:{id:FallingSand,Block:command_block,TileEntityData:{Command:fill ~" + (stacks+1) + " ~0 ~0 ~0 ~" + (stackHeight+1) + " ~0 air},Time:1}}");
}
writer += (getCommand(inputs,xOffset+stacks,yOffset,zOffset,summonOffset));
return writer;
}
public static String getCommand(LinkedList<String> commands,int xOffset,int yOffset,int zOffset,int summonOffset)
{
int brackets = (int)Math.floor(commands.size()*3/2.0)+1;
int yCordFix = -brackets+1+yOffset;
String writer = "",line;
writer += ("/summon FallingSand ~-1 ~" + (2-summonOffset) + " ~" + zOffset + " {Block:redstone_block,Time:1,Riding:\r\n");
while(commands.size()>=2)
{
line = fixCords(commands.pop(),xOffset,yCordFix,zOffset);
writer += ("{id:FallingSand,Block:command_block,TileEntityData:\r\n");
writer += ("\t{Command:" + line + "},\r\n");
writer += ("\tTime:1,Riding:\r\n");
yCordFix++;
line = fixCords(commands.pop(),xOffset,yCordFix,zOffset);
writer += ("{id:FallingSand,Block:command_block,TileEntityData:\r\n");
writer += ("\t{Command:" + line + "},\r\n");
writer += ("\tTime:1,Riding:\r\n");
yCordFix++;
writer += ("{id:FallingSand,Block:redstone_block,Time:1,Riding:\r\n");
yCordFix++;
}
if(commands.size()==1)
{
line = fixCords(commands.pop(),xOffset,yCordFix,zOffset);
writer += ("{id:FallingSand,Block:command_block,TileEntityData:\r\n");
writer += ("\t{Command:" + line + "},\r\n");
writer += ("\tTime:1\r\n");
yCordFix++;
}
for(int i=0;i<brackets;i++)
{
writer += ("}");
}
return writer;
}
public static String fixCords(String command,int xOffset,int yOffset,int zOffset)
{
try
{
int startXCord = command.indexOf('~');
int startYCord,startZCord,endZCord;
double xCord,yCord,zCord;
while(startXCord!=-1 && startXCord<50 && !command.substring(0,19).equals("/summon FallingSand"))
{
startYCord = command.indexOf('~',startXCord+1);
xCord = Double.parseDouble(command.substring(startXCord+1,startYCord-1));
xCord += xOffset;
command = command.substring(0,startXCord+1)+xCord+command.substring(startYCord-1);
startYCord = command.indexOf('~',startXCord+1);
startZCord = command.indexOf('~',startYCord+1);
yCord = Double.parseDouble(command.substring(startYCord+1,startZCord-1));
yCord += yOffset;
command = command.substring(0,startYCord+1)+yCord+command.substring(startZCord-1);
startZCord = command.indexOf('~',startYCord+1);
endZCord = command.indexOf(' ',startZCord);
zCord = Double.parseDouble(command.substring(startZCord+1,endZCord));
zCord += zOffset;
command = command.substring(0,startZCord+1)+zCord+command.substring(endZCord);
startXCord = command.indexOf('~',endZCord);
}
return command;
}
catch(Exception ex)
{
System.out.println(command);
ex.printStackTrace();
return "";
}
}
public static void main(String[] args)
{
//compile(args[0]);
}
}