Skip to content

Commit 48529ab

Browse files
committed
Optimize get|set_block_at()
1 parent 176231d commit 48529ab

File tree

1 file changed

+61
-67
lines changed

1 file changed

+61
-67
lines changed

src/main/java/com/laytonsmith/core/functions/Environment.java

Lines changed: 61 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -84,47 +84,49 @@ public CHVersion since() {
8484
}
8585

8686
@Override
87-
public Construct exec(Target t, com.laytonsmith.core.environments.Environment env, Construct... args) throws CancelCommandException, ConfigRuntimeException {
88-
double x = 0;
89-
double y = 0;
90-
double z = 0;
87+
public Construct exec(Target t, com.laytonsmith.core.environments.Environment env, Construct... args)
88+
throws CancelCommandException, ConfigRuntimeException {
89+
int x;
90+
int y;
91+
int z;
9192
MCWorld w = null;
92-
MCCommandSender sender = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
93-
String world = null;
94-
if (sender instanceof MCPlayer) {
95-
w = ((MCPlayer) sender).getWorld();
93+
MCPlayer player = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
94+
if (player != null) {
95+
w = player.getWorld();
9696
}
97-
if (args.length == 1 || args.length == 2) {
98-
if (args[0] instanceof CArray) {
99-
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], w, t);
100-
x = loc.getX();
101-
y = loc.getY();
102-
z = loc.getZ();
103-
world = loc.getWorld().getName();
104-
} else {
105-
throw new ConfigRuntimeException("get_block_at expects param 1 to be an array", ExceptionType.CastException, t);
97+
if (args.length < 3) {
98+
if (!(args[0] instanceof CArray)) {
99+
throw new ConfigRuntimeException("get_block_at expects param 1 to be an array",
100+
ExceptionType.CastException, t);
106101
}
102+
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], w, t);
103+
x = loc.getBlockX();
104+
y = loc.getBlockY();
105+
z = loc.getBlockZ();
106+
w = loc.getWorld();
107107
if (args.length == 2) {
108-
world = args[1].val();
108+
w = Static.getServer().getWorld(args[1].val());
109+
if (w == null) {
110+
throw new ConfigRuntimeException("The specified world " + args[1].val() + " doesn't exist",
111+
ExceptionType.InvalidWorldException, t);
112+
}
109113
}
110-
} else if (args.length == 3 || args.length == 4) {
111-
x = Static.getDouble(args[0], t);
112-
y = Static.getDouble(args[1], t);
113-
z = Static.getDouble(args[2], t);
114+
} else {
115+
x = (int) java.lang.Math.floor(Static.getNumber(args[0], t));
116+
y = (int) java.lang.Math.floor(Static.getNumber(args[1], t));
117+
z = (int) java.lang.Math.floor(Static.getNumber(args[2], t));
114118
if (args.length == 4) {
115-
world = args[3].val();
119+
w = Static.getServer().getWorld(args[3].val());
120+
if (w == null) {
121+
throw new ConfigRuntimeException("The specified world " + args[4].val() + " doesn't exist",
122+
ExceptionType.InvalidWorldException, t);
123+
}
116124
}
117125
}
118-
if (world != null) {
119-
w = Static.getServer().getWorld(world);
120-
}
121126
if (w == null) {
122-
throw new ConfigRuntimeException("The specified world " + world + " doesn't exist", ExceptionType.InvalidWorldException, t);
127+
throw new ConfigRuntimeException("No world was provided", ExceptionType.InvalidWorldException, t);
123128
}
124-
x = java.lang.Math.floor(x);
125-
y = java.lang.Math.floor(y);
126-
z = java.lang.Math.floor(z);
127-
MCBlock b = w.getBlockAt((int) x, (int) y, (int) z);
129+
MCBlock b = w.getBlockAt(x, y, z);
128130
if (b == null) {
129131
throw new ConfigRuntimeException(
130132
"Could not find the block in " + this.getName() + " (are you running in cmdline mode?)",
@@ -164,7 +166,8 @@ public String docs() {
164166

165167
@Override
166168
public ExceptionType[] thrown() {
167-
return new ExceptionType[]{ExceptionType.CastException, ExceptionType.LengthException, ExceptionType.FormatException, ExceptionType.InvalidWorldException};
169+
return new ExceptionType[]{ExceptionType.CastException, ExceptionType.LengthException,
170+
ExceptionType.FormatException, ExceptionType.InvalidWorldException};
168171
}
169172

170173
@Override
@@ -178,68 +181,59 @@ public CHVersion since() {
178181
}
179182

180183
@Override
181-
public Construct exec(Target t, com.laytonsmith.core.environments.Environment env, Construct... args) throws CancelCommandException, ConfigRuntimeException {
182-
double x = 0;
183-
double y = 0;
184-
double z = 0;
184+
public Construct exec(Target t, com.laytonsmith.core.environments.Environment env, Construct... args)
185+
throws CancelCommandException, ConfigRuntimeException {
186+
int x;
187+
int y;
188+
int z;
185189
boolean physics = true;
186-
String id = null;
187-
String world = null;
190+
String id;
188191
MCWorld w = null;
189-
MCCommandSender sender = env.getEnv(CommandHelperEnvironment.class).GetCommandSender();
190-
if (sender instanceof MCPlayer) {
191-
w = ((MCPlayer) sender).getWorld();
192+
MCPlayer player = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
193+
if (player != null) {
194+
w = player.getWorld();
192195
}
193196
if (args.length < 4) {
194197
if (!(args[0] instanceof CArray)) {
195-
throw new ConfigRuntimeException("set_block_at expects param 1 to be an array", ExceptionType.CastException, t);
198+
throw new ConfigRuntimeException("set_block_at expects param 1 to be an array",
199+
ExceptionType.CastException, t);
196200
}
197201
MCLocation l = ObjectGenerator.GetGenerator().location(args[0], w, t);
198202
x = l.getBlockX();
199203
y = l.getBlockY();
200204
z = l.getBlockZ();
201-
world = l.getWorld().getName();
205+
w = l.getWorld();
202206
id = args[1].val();
203207
if (args.length == 3) {
204208
physics = Static.getBoolean(args[2]);
205209
}
206210

207211
} else {
208-
x = Static.getNumber(args[0], t);
209-
y = Static.getNumber(args[1], t);
210-
z = Static.getNumber(args[2], t);
212+
x = (int) java.lang.Math.floor(Static.getNumber(args[0], t));
213+
y = (int) java.lang.Math.floor(Static.getNumber(args[1], t));
214+
z = (int) java.lang.Math.floor(Static.getNumber(args[2], t));
211215
id = args[3].val();
212216
if (args.length >= 5) {
213-
world = args[4].val();
217+
w = Static.getServer().getWorld(args[4].val());
218+
if (w == null) {
219+
throw new ConfigRuntimeException("The specified world " + args[4].val() + " doesn't exist",
220+
ExceptionType.InvalidWorldException, t);
221+
}
222+
} else if (w == null) {
223+
throw new ConfigRuntimeException("No world was provided",
224+
ExceptionType.InvalidWorldException, t);
214225
}
215226
if (args.length == 6) {
216227
physics = Static.getBoolean(args[2]);
217228
}
218229
}
219-
if (world != null) {
220-
w = Static.getServer().getWorld(world);
221-
}
222-
if (w == null) {
223-
throw new ConfigRuntimeException("The specified world " + world + " doesn't exist", ExceptionType.InvalidWorldException, t);
224-
}
225-
x = java.lang.Math.floor(x);
226-
y = java.lang.Math.floor(y);
227-
z = java.lang.Math.floor(z);
228-
int ix = (int) x;
229-
int iy = (int) y;
230-
int iz = (int) z;
231-
MCBlock b = w.getBlockAt(ix, iy, iz);
230+
MCBlock b = w.getBlockAt(x, y, z);
232231
String[] dataAndMeta = id.split(":");
233232
int data;
234-
byte meta;
233+
byte meta = 0;
235234
try {
236-
if(dataAndMeta.length == 1) {
237-
meta = 0;
238-
} else if(dataAndMeta.length == 2) {
235+
if(dataAndMeta.length == 2) {
239236
meta = Byte.parseByte(dataAndMeta[1]); // Throws NumberFormatException.
240-
} else {
241-
throw new ConfigRuntimeException("id must be formatted as such: 'x:y' where x and y are integers",
242-
ExceptionType.FormatException, t);
243237
}
244238
data = Integer.parseInt(dataAndMeta[0]); // Throws NumberFormatException.
245239
} catch(NumberFormatException e) {

0 commit comments

Comments
 (0)