mcMMO/src/main/java/com/gmail/nossr50/skills/Page.java

94 lines
3.1 KiB
Java
Raw Normal View History

2013-01-25 04:53:02 +01:00
package com.gmail.nossr50.skills;
import java.util.ArrayList;
import org.bukkit.entity.Player;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
2013-01-25 04:53:02 +01:00
import com.gmail.nossr50.util.Misc;
2013-01-26 23:01:55 +01:00
public final class Page {
private Page() {}
2012-06-05 15:57:10 +02:00
2013-01-26 23:01:55 +01:00
public static int getTotalPageNumber(String address) {
2012-04-27 11:47:11 +02:00
String[] addressSplit = LocaleLoader.getString(address).split("\n");
2013-01-26 23:01:55 +01:00
if (addressSplit.length <= 8) {
return 1;
2013-01-26 23:01:55 +01:00
}
2013-01-10 04:43:21 +01:00
return (addressSplit.length/8)+1;
}
2012-06-05 15:57:10 +02:00
2013-01-26 23:01:55 +01:00
public static ArrayList<String> grabPageContents(String header, String address, int pagenum) {
int pageIndexStart = 0;
2012-06-05 15:57:10 +02:00
//Determine what string to start at
2013-01-26 23:01:55 +01:00
if (pagenum > 1) {
pageIndexStart = 8*(pagenum-1);
}
2012-06-05 15:57:10 +02:00
ArrayList<String> allStrings = new ArrayList<String>();
2012-04-27 11:47:11 +02:00
String split[] = LocaleLoader.getString(address).split("\n");
2012-06-05 15:57:10 +02:00
2013-01-16 17:42:01 +01:00
allStrings.add(LocaleLoader.getString("Guides.Header", new Object[] {header} ));
2012-06-05 15:57:10 +02:00
//Add targeted strings
2013-01-26 23:01:55 +01:00
while (allStrings.size() < 9) {
if (pageIndexStart+allStrings.size() > split.length) {
allStrings.add("");
2013-01-26 23:01:55 +01:00
}
else {
2012-03-03 21:43:50 +01:00
allStrings.add(split[pageIndexStart+allStrings.size()-1]);
2013-01-26 23:01:55 +01:00
}
}
2012-06-05 15:57:10 +02:00
allStrings.add("Page "+pagenum+" of "+getTotalPageNumber(address));
return allStrings;
}
2012-06-05 15:57:10 +02:00
2013-01-26 23:01:55 +01:00
public static void clearChat(Player player) {
for (int x = 0; x < 20; x++) {
player.sendMessage("");
}
}
2012-06-05 15:57:10 +02:00
2013-01-16 17:42:01 +01:00
public static void grabGuidePageForSkill(SkillType skilltype, Player player, String[] args) {
String skillName = skilltype.toString();
String capitalized = Misc.getCapitalized(skillName);
2013-01-26 23:01:55 +01:00
String localized = SkillTools.localizeSkillName(skilltype);
2013-01-16 17:42:01 +01:00
player.sendMessage(LocaleLoader.getString("Guides.Available", new Object[] {localized, localized.toLowerCase()} ));
2012-06-05 15:57:10 +02:00
2013-01-26 23:01:55 +01:00
if (args.length >= 1 && args[0].equals("?")) {
String address = "Guides." + capitalized;
int totalPages = Page.getTotalPageNumber(address);
2012-06-05 15:57:10 +02:00
2013-01-26 23:01:55 +01:00
if (args.length == 1) {
Page.clearChat(player);
for (String target : Page.grabPageContents(localized, address, 1)) {
player.sendMessage(target);
2013-01-16 17:42:01 +01:00
}
2013-01-26 23:01:55 +01:00
}
else {
if (!Misc.isInt(args[1])) {
player.sendMessage(LocaleLoader.getString("Guides.Page.Invalid"));
}
if (Misc.getInt(args[1]) <= totalPages) {
Page.clearChat(player);
2013-01-26 23:01:55 +01:00
for (String target : Page.grabPageContents(localized, address, Misc.getInt(args[1]))) {
player.sendMessage(target);
}
}
2013-01-26 23:01:55 +01:00
else {
player.sendMessage(LocaleLoader.getString("Guides.Page.OutOfRange", new Object[] {totalPages} ));
}
}
}
2013-01-26 23:01:55 +01:00
else {
player.sendMessage(LocaleLoader.getString("Guides.Usage", new Object[] {localized.toLowerCase()} ));
}
}
}