Skip to content

Commit

Permalink
Handle invalid sound key exceptions (Fixes #1330)
Browse files Browse the repository at this point in the history
Also expands sound stopping functions to support sound array objects, so that the same arrays can be used for playing and stopping.
  • Loading branch information
PseudoKnight committed Oct 2, 2022
1 parent 46fa93d commit 4787360
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
34 changes: 22 additions & 12 deletions src/main/java/com/laytonsmith/core/functions/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ public Mixed exec(Target t,

CArray sa = (CArray) args[1];

path = sa.get("sound", t).val();
path = ArgumentValidation.getStringObject(sa.get("sound", t), t);

if(sa.containsKey("category")) {
try {
Expand All @@ -1828,7 +1828,7 @@ public Mixed exec(Target t,
}

if(args.length == 3) {
java.util.List<MCPlayer> players = new java.util.ArrayList<MCPlayer>();
java.util.List<MCPlayer> players = new java.util.ArrayList<>();
if(args[2].isInstanceOf(CArray.TYPE)) {
for(String key : ((CArray) args[2]).stringKeySet()) {
players.add(Static.GetPlayer(((CArray) args[2]).get(key, t), t));
Expand All @@ -1837,19 +1837,29 @@ public Mixed exec(Target t,
players.add(Static.GetPlayer(args[2], t));
}

if(category == null) {
for(MCPlayer p : players) {
p.playSound(loc, path, volume, pitch);
}
} else {
for(MCPlayer p : players) {
p.playSound(loc, path, category, volume, pitch);
try {
if(category == null) {
for(MCPlayer p : players) {
p.playSound(loc, path, volume, pitch);
}
} else {
for(MCPlayer p : players) {
p.playSound(loc, path, category, volume, pitch);
}
}
} catch(Exception ex) {
throw new CREFormatException(ex.getMessage(), t);
}
} else if(category == null) {
loc.getWorld().playSound(loc, path, volume, pitch);
} else {
loc.getWorld().playSound(loc, path, category, volume, pitch);
try {
if(category == null) {
loc.getWorld().playSound(loc, path, volume, pitch);
} else {
loc.getWorld().playSound(loc, path, category, volume, pitch);
}
} catch(Exception ex) {
throw new CREFormatException(ex.getMessage(), t);
}
}
return CVoid.VOID;
}
Expand Down
64 changes: 52 additions & 12 deletions src/main/java/com/laytonsmith/core/functions/PlayerManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -5459,19 +5459,37 @@ public Mixed exec(Target t, com.laytonsmith.core.environments.Environment enviro

MCPlayer p = Static.GetPlayer(args[0], t);

String soundName;
String categoryName = null;
if(args[1].isInstanceOf(CArray.TYPE)) {
CArray soundArray = (CArray) args[1];
if(!soundArray.isAssociative()) {
throw new CRECastException("Expected an associative array", t);
}
soundName = soundArray.get("sound", t).val();
if(soundArray.containsKey("category")) {
categoryName = soundArray.get("category", t).val();
}
} else {
soundName = args[1].val();
}
if(args.length == 3) {
categoryName = args[2].val();
}

MCSound sound;
try {
sound = MCSound.valueOf(args[1].val().toUpperCase());
sound = MCSound.valueOf(soundName.toUpperCase());
} catch (IllegalArgumentException iae) {
throw new CREFormatException("Sound name '" + args[1].val() + "' is invalid.", t);
throw new CREFormatException("Sound name '" + soundName + "' is invalid.", t);
}

if(args.length == 3) {
if(categoryName != null) {
MCSoundCategory category;
try {
category = MCSoundCategory.valueOf(args[2].val().toUpperCase());
category = MCSoundCategory.valueOf(categoryName.toUpperCase());
} catch (IllegalArgumentException iae) {
throw new CREFormatException("Sound category '" + args[2].val() + "' is invalid.", t);
throw new CREFormatException("Sound category '" + categoryName + "' is invalid.", t);
}
p.stopSound(sound, category);
} else {
Expand Down Expand Up @@ -5528,17 +5546,39 @@ public Mixed exec(Target t, com.laytonsmith.core.environments.Environment enviro
throws ConfigRuntimeException {

MCPlayer p = Static.GetPlayer(args[0], t);
String sound = args[1].val();
String soundName;
String categoryName = null;
if(args[1].isInstanceOf(CArray.TYPE)) {
CArray soundArray = (CArray) args[1];
if(!soundArray.isAssociative()) {
throw new CRECastException("Expected an associative array or sound string", t);
}
soundName = soundArray.get("sound", t).val();
if(soundArray.containsKey("category")) {
categoryName = soundArray.get("category", t).val();
}
} else {
soundName = args[1].val();
}
if(args.length == 3) {
MCSoundCategory category;
categoryName = args[2].val();
}
MCSoundCategory category = null;
if(categoryName != null) {
try {
category = MCSoundCategory.valueOf(args[2].val().toUpperCase());
category = MCSoundCategory.valueOf(categoryName.toUpperCase());
} catch (IllegalArgumentException iae) {
throw new CREFormatException("Sound category '" + args[2].val() + "' is invalid.", t);
throw new CREFormatException("Sound category '" + categoryName + "' is invalid.", t);
}
p.stopSound(sound, category);
} else {
p.stopSound(sound);
}
try {
if(category != null) {
p.stopSound(soundName, category);
} else {
p.stopSound(soundName);
}
} catch(Exception ex) {
throw new CREFormatException(ex.getMessage(), t);
}

return CVoid.VOID;
Expand Down

0 comments on commit 4787360

Please sign in to comment.