-
-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spawnutil #3505
Conversation
/n spawn commands.
Add/improve comments.
try { | ||
if (!split[split.length - 1].contains("name:")) { | ||
index = Integer.parseInt(split[split.length - 1]); | ||
} else { // So now it say's name:123 | ||
split[split.length - 1] = split[split.length - 1].replace("name:", "").replace("_", " "); | ||
for (Location loc : town.getAllOutpostSpawns()) { | ||
TownBlock tboutpost = TownyAPI.getInstance().getTownBlock(loc); | ||
if (tboutpost != null) { | ||
String name = tboutpost.getName(); | ||
if (name.startsWith(split[split.length - 1])) { | ||
index = 1 + town.getAllOutpostSpawns().indexOf(loc); | ||
} | ||
} | ||
} | ||
if (index == null) { // If it persists to be null, so it's not been given a value, set it to the | ||
// fallback (1). | ||
index = 1; | ||
} | ||
} | ||
} catch (NumberFormatException e) { | ||
// invalid entry so assume the first outpost, also note: We DO NOT HAVE a number | ||
// now, which means: if you type abc, you get brought to that outpost. | ||
// Let's consider the fact however: an outpost name begins with "123" and there | ||
// are 123 Outposts. Then we put the prefix name:123 and that solves that. | ||
index = 1; | ||
// Trying to get Outpost names. | ||
split[split.length - 1] = split[split.length - 1].replace("_", " "); | ||
for (Location loc : town.getAllOutpostSpawns()) { | ||
TownBlock tboutpost = TownyAPI.getInstance().getTownBlock(loc); | ||
if (tboutpost != null) { | ||
String name = tboutpost.getName(); | ||
if (name.startsWith(split[split.length - 1])) { | ||
index = 1 + town.getAllOutpostSpawns().indexOf(loc); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be cleaned up.
First, just parse for an integer and catch the numberformat exception. That way we don't have to preemptively duplicate a check for a name.
Then since we are going to check for name anyway, just replace name:
from the string in case it exists.
Also instead of calling town.getAllOutSpawns().indexOf()
which will basically loop through all of the locs again, use a for-i loop.
Thus the new code would look something like:
int index = 1;
String outpostName = split[split.length -1];
try {
index = Integer.parseInt(outpostName);
} catch (NumberFormatException nfe) {
outpostName = outpostName.replace("name:", "").replace("_", " ");
for (int outpostLocIndex = 0; outpostLocIndex < town.getAllOutpostSpawns().size(); outpostLocIndex++) {
Location outpostLoc = town.getAllOutpostSpawns().get(outpostLocIndex);
TownBlock tbOutpost = TownyAPI.getInstance().getTownBlock(outpostLoc);
if(tbOutpost != null && tbOutpost.getName.startsWith(outpostName) {
index += outpostLocIndex; // Remember index is already set to 1
break;
}
}
}
No description provided.