Skip to content

Commit e8dbc3d

Browse files
committed
Fix poor uses of Construct.nval()
Generally poor to use nval() in a conditional since it could build a string when just checking for a null. In other cases, nval() is unnecessary when args have already been asserted as non-null.
1 parent a0ce641 commit e8dbc3d

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.laytonsmith.core.constructs.CArray;
1818
import com.laytonsmith.core.constructs.CBoolean;
1919
import com.laytonsmith.core.constructs.CClassType;
20+
import com.laytonsmith.core.constructs.CNull;
2021
import com.laytonsmith.core.constructs.CString;
2122
import com.laytonsmith.core.constructs.CVoid;
2223
import com.laytonsmith.core.constructs.Construct;
@@ -687,7 +688,7 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
687688
final MCServer server = Static.getServer();
688689

689690
// Handle "broadcast(message, [null])".
690-
if(args.length == 1 || Construct.nval(args[1]) == null) { // args.length can only be 1 or 2 due to the numArgs().
691+
if(args.length == 1 || args[1] instanceof CNull) {
691692
server.broadcastMessage(args[0].val());
692693
return CVoid.VOID;
693694
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.laytonsmith.core.constructs.CResource;
4040
import com.laytonsmith.core.constructs.CString;
4141
import com.laytonsmith.core.constructs.CVoid;
42-
import com.laytonsmith.core.constructs.Construct;
4342
import com.laytonsmith.core.constructs.IVariable;
4443
import com.laytonsmith.core.constructs.Target;
4544
import com.laytonsmith.core.constructs.Variable;
@@ -153,7 +152,7 @@ public Integer[] numArgs() {
153152

154153
@Override
155154
public Mixed exec(Target t, final Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
156-
if(Construct.nval(args[1]) == null || args[1].val().isEmpty() || args[1].val().charAt(0) != '/') {
155+
if(args[1] instanceof CNull || args[1].val().isEmpty() || args[1].val().charAt(0) != '/') {
157156
throw new CREFormatException("The first character of the command must be a forward slash (i.e. '/give')", t);
158157
}
159158
String cmd = args[1].val().substring(1);
@@ -275,7 +274,7 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
275274
return new run().exec(t, env, args);
276275
}
277276

278-
if(Construct.nval(command) == null || command.val().isEmpty() || command.val().charAt(0) != '/') {
277+
if(command instanceof CNull || command.val().isEmpty() || command.val().charAt(0) != '/') {
279278
throw new CREFormatException("The first character of a command must be a forward slash (eg. /give)", t);
280279
}
281280

@@ -412,7 +411,7 @@ public Integer[] numArgs() {
412411

413412
@Override
414413
public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
415-
if(Construct.nval(args[0]) == null || args[0].val().isEmpty() || args[0].val().charAt(0) != '/') {
414+
if(args[0] instanceof CNull || args[0].val().isEmpty() || args[0].val().charAt(0) != '/') {
416415
throw new CREFormatException("The first character of the command must be a forward slash (i.e. '/give')", t);
417416
}
418417
MCCommandSender sender = env.getEnv(CommandHelperEnvironment.class).GetCommandSender();

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,8 @@ public String docs() {
896896
public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
897897
Static.AssertNonCNull(t, args);
898898

899-
String teststring = Construct.nval(args[0]);
900-
String keyword = Construct.nval(args[1]);
899+
String teststring = args[0].val();
900+
String keyword = args[1].val();
901901
boolean ret = teststring.startsWith(keyword);
902902

903903
return CBoolean.get(ret);
@@ -964,8 +964,8 @@ public String docs() {
964964
public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
965965
Static.AssertNonCNull(t, args);
966966

967-
String teststring = Construct.nval(args[0]);
968-
String keyword = Construct.nval(args[1]);
967+
String teststring = args[0].val();
968+
String keyword = args[1].val();
969969
boolean ret = teststring.endsWith(keyword);
970970

971971
return CBoolean.get(ret);
@@ -1022,11 +1022,10 @@ public String getName() {
10221022

10231023
@Override
10241024
public Mixed exec(Target t, Environment environment, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
1025-
if(!(Construct.nval(args[0]) instanceof String)) {
1026-
throw new CRECastException(this.getName() + " expects a string as first argument, but type "
1027-
+ args[0].typeof() + " was found.", t);
1025+
if(args[0] instanceof CNull) {
1026+
throw new CRECastException(this.getName() + " expects a string as first argument, but found null.", t);
10281027
}
1029-
String text = Construct.nval(args[0]);
1028+
String text = args[0].val();
10301029
// Enforce the fact we are only taking the first character here
10311030
// Do not let the user pass an entire string (or an empty string, d'oh). Only a single character.
10321031
if(text.length() != 1) {
@@ -1127,9 +1126,9 @@ public Boolean runAsync() {
11271126

11281127
@Override
11291128
public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
1130-
String haystack = Construct.nval(args[0]);
1131-
String needle = Construct.nval(args[1]);
11321129
Static.AssertNonCNull(t, args);
1130+
String haystack = args[0].val();
1131+
String needle = args[1].val();
11331132
return new CInt(haystack.indexOf(needle), t);
11341133
}
11351134

0 commit comments

Comments
 (0)