2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.commands.skills;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-09-27 17:47:16 +02:00
|
|
|
import java.util.Arrays;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2018-12-29 14:24:55 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.PrimarySkill;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
|
|
|
import com.gmail.nossr50.util.StringUtils;
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
public class SkillGuideCommand implements CommandExecutor {
|
|
|
|
private String header;
|
2013-09-27 17:47:16 +02:00
|
|
|
private ArrayList<String> guide;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2013-10-29 20:38:20 +01:00
|
|
|
private String invalidPage = LocaleLoader.getString("Guides.Page.Invalid");
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2018-12-29 14:24:55 +01:00
|
|
|
public SkillGuideCommand(PrimarySkill skill) {
|
2013-10-29 20:38:20 +01:00
|
|
|
header = LocaleLoader.getString("Guides.Header", skill.getName());
|
|
|
|
guide = getGuide(skill);
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
2013-03-01 06:52:01 +01:00
|
|
|
switch (args.length) {
|
|
|
|
case 1:
|
|
|
|
if (!args[0].equals("?")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
sendGuide(sender, 1);
|
2013-03-01 06:52:01 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case 2:
|
2013-03-12 21:25:42 +01:00
|
|
|
int totalPages = getTotalPageNumber();
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
if (!StringUtils.isInt(args[1])) {
|
2013-03-12 21:25:42 +01:00
|
|
|
sender.sendMessage(invalidPage);
|
2013-03-01 06:52:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
int pageNumber = Integer.parseInt(args[1]);
|
2013-03-01 06:52:01 +01:00
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
if (pageNumber > totalPages || pageNumber <= 0) {
|
|
|
|
sender.sendMessage(LocaleLoader.getString("Guides.Page.OutOfRange", totalPages));
|
|
|
|
return true;
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
sendGuide(sender, pageNumber);
|
2013-03-01 06:52:01 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 21:25:42 +01:00
|
|
|
|
|
|
|
private int getTotalPageNumber() {
|
2013-09-27 17:47:16 +02:00
|
|
|
return (int) Math.ceil(guide.size() / 8.0);
|
2013-03-12 21:25:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void sendGuide(CommandSender sender, int pageNumber) {
|
|
|
|
for (String target : grabPageContents(pageNumber)) {
|
|
|
|
sender.sendMessage(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 20:38:20 +01:00
|
|
|
private ArrayList<String> grabPageContents(int page) {
|
|
|
|
int pageIndexStart = 8 * (page - 1); // Determine what string to start at
|
2013-03-12 21:25:42 +01:00
|
|
|
ArrayList<String> allStrings = new ArrayList<String>();
|
|
|
|
|
|
|
|
allStrings.add(header);
|
|
|
|
|
|
|
|
// Add targeted strings
|
|
|
|
while (allStrings.size() < 9) {
|
2013-09-27 17:47:16 +02:00
|
|
|
if (pageIndexStart + allStrings.size() > guide.size()) {
|
2013-03-12 21:25:42 +01:00
|
|
|
allStrings.add("");
|
|
|
|
}
|
|
|
|
else {
|
2013-09-27 17:47:16 +02:00
|
|
|
allStrings.add(guide.get(pageIndexStart + (allStrings.size() - 1)));
|
2013-03-12 21:25:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 20:38:20 +01:00
|
|
|
allStrings.add("Page " + page + " of " + getTotalPageNumber());
|
2013-03-12 21:25:42 +01:00
|
|
|
return allStrings;
|
|
|
|
}
|
2013-09-27 17:47:16 +02:00
|
|
|
|
2018-12-29 14:24:55 +01:00
|
|
|
private ArrayList<String> getGuide(PrimarySkill skill) {
|
2013-09-27 17:47:16 +02:00
|
|
|
ArrayList<String> guide = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2013-10-29 20:38:20 +01:00
|
|
|
String[] section = LocaleLoader.getString("Guides." + StringUtils.getCapitalized(skill.toString()) + ".Section." + i).split("\n");
|
2013-09-27 17:47:16 +02:00
|
|
|
|
|
|
|
if (section[0].startsWith("!")) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
guide.addAll(Arrays.asList(section));
|
|
|
|
|
|
|
|
if (section.length < 8) {
|
|
|
|
for (int blankLine = 8 - section.length; blankLine > 0; blankLine--) {
|
|
|
|
guide.add("");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return guide;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|