Skip to content
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

Merged
merged 10 commits into from
Nov 21, 2019
Merged

Spawnutil #3505

merged 10 commits into from
Nov 21, 2019

Conversation

LlmDl
Copy link
Member

@LlmDl LlmDl commented Nov 21, 2019

No description provided.

Comment on lines +104 to +139
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);
}
}
}
Copy link
Contributor

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;
		}
	}
}

@LlmDl LlmDl merged commit f66a98a into master Nov 21, 2019
@LlmDl LlmDl deleted the spawnutil branch November 21, 2019 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants