From 210c898dd6f32b9433075549a2674007a7d816fe Mon Sep 17 00:00:00 2001 From: ementalo Date: Thu, 21 Mar 2013 23:49:58 +0000 Subject: Add Book command Update pom --- Essentials/src/net/ess3/commands/CommandBook.java | 91 +++++++++++++++ .../src/net/ess3/permissions/Permissions.java | 5 +- .../src/net/ess3/utils/textreader/BookInput.java | 129 +++++++++++++++++++++ .../src/net/ess3/utils/textreader/BookPager.java | 111 ++++++++++++++++++ pom.xml | 5 +- 5 files changed, 337 insertions(+), 4 deletions(-) create mode 100644 Essentials/src/net/ess3/commands/CommandBook.java create mode 100644 Essentials/src/net/ess3/utils/textreader/BookInput.java create mode 100644 Essentials/src/net/ess3/utils/textreader/BookPager.java diff --git a/Essentials/src/net/ess3/commands/CommandBook.java b/Essentials/src/net/ess3/commands/CommandBook.java new file mode 100644 index 000000000..28437e835 --- /dev/null +++ b/Essentials/src/net/ess3/commands/CommandBook.java @@ -0,0 +1,91 @@ +package net.ess3.commands; + +import static net.ess3.I18n._; +import net.ess3.api.IUser; +import net.ess3.permissions.Permissions; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; + + + + +public class Commandbook extends EssentialsCommand +{ + //TODO: Translate this + @Override + public void run( final IUser user, final String commandLabel, final String[] args) throws Exception + { + + final Player player = user.getPlayer(); + final ItemStack item = player.getItemInHand(); + if (item.getType() == Material.WRITTEN_BOOK) + { + BookMeta bmeta = (BookMeta)item.getItemMeta(); + + if (args.length > 1 && args[0].equalsIgnoreCase("author")) + { + if (Permissions.BOOK_AUTHOR.isAuthorized(user) && (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user))) + { + bmeta.setAuthor(args[1]); + item.setItemMeta(bmeta); + user.sendMessage(_("bookAuthorSet", getFinalArg(args, 1))); + } + else + { + throw new Exception(_("denyChangeAuthor")); + } + } + else if (args.length > 1 && args[0].equalsIgnoreCase("title")) + { + if (Permissions.BOOK_TITLE.isAuthorized(user) && (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user))) + { + bmeta.setTitle(args[1]); + item.setItemMeta(bmeta); + user.sendMessage(_("bookTitleSet", getFinalArg(args, 1))); + } + else + { + throw new Exception(_("denyChangeTitle")); + } + } + else + { + if (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user)) + { + ItemStack newItem = new ItemStack(Material.BOOK_AND_QUILL, item.getAmount()); + newItem.setItemMeta(bmeta); + user.getPlayer().setItemInHand(newItem); + user.sendMessage(_("editBookContents")); + } + else + { + throw new Exception(_("denyBookEdit")); + } + } + } + else if (item.getType() == Material.BOOK_AND_QUILL) + { + BookMeta bmeta = (BookMeta)item.getItemMeta(); + if (!Permissions.BOOK_AUTHOR.isAuthorized(user)) + { + bmeta.setAuthor(player.getName()); + } + ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount()); + newItem.setItemMeta(bmeta); + player.setItemInHand(newItem); + user.sendMessage(_("bookLocked")); + } + else + { + throw new Exception(_("holdBook")); + } + } + + private boolean isAuthor(BookMeta bmeta, String player) + { + String author = bmeta.getAuthor(); + return author != null && author.equalsIgnoreCase(player); + } +} \ No newline at end of file diff --git a/Essentials/src/net/ess3/permissions/Permissions.java b/Essentials/src/net/ess3/permissions/Permissions.java index 822194f99..4883a6caa 100644 --- a/Essentials/src/net/ess3/permissions/Permissions.java +++ b/Essentials/src/net/ess3/permissions/Permissions.java @@ -21,6 +21,9 @@ public enum Permissions implements IPermission BAN_EXEMPT, BAN_NOTIFY, BAN_OFFLINE, + BOOK_AUTHOR, + BOOK_OTHERS, + BOOK_TITLE, BREAK_BEDROCK, CHAT_COLOR, CHAT_IGNORE_EXEMPT, @@ -30,7 +33,6 @@ public enum Permissions implements IPermission ECO_LOAN(PermissionDefault.FALSE), ENCHANT_UNSAFE(PermissionDefault.FALSE), ENDERCHEST_OTHERS, - ESSENTIALS, EXP_GIVE, EXP_GIVE_OTHERS, EXP_SET, @@ -166,6 +168,7 @@ public enum Permissions implements IPermission return PermissionFactory.checkPermission(sender, this); } + public static final DotStarPermission ESSENTIALS = new DotStarPermission("essentials."); public static final DotStarPermission ENCHANT = new DotStarPermission("essentials.enchant"); public static final DotStarPermission PERGROUPTELEPORT = new DotStarPermission("essentials.teleport.groups"); public static final MaterialDotStarPermission GIVE = new MaterialDotStarPermission("essentials.give", PermissionDefault.TRUE); diff --git a/Essentials/src/net/ess3/utils/textreader/BookInput.java b/Essentials/src/net/ess3/utils/textreader/BookInput.java new file mode 100644 index 000000000..f38d6490e --- /dev/null +++ b/Essentials/src/net/ess3/utils/textreader/BookInput.java @@ -0,0 +1,129 @@ +package net.ess3.utils.textreader; + +import net.ess3.api.IEssentials; + +import java.io.*; +import java.lang.ref.SoftReference; +import java.util.*; + + +public class BookInput implements IText +{ + private final transient List lines; + private final transient List chapters; + private final transient Map bookmarks; + private final transient long lastChange; + private final static HashMap> cache = new HashMap>(); + + public BookInput(final String filename, final boolean createFile, final IEssentials ess) throws IOException + { + + File file = null; + if (file == null || !file.exists()) + { + file = new File(ess.getPlugin().getDataFolder(), filename + ".txt"); + } + if (!file.exists()) + { + if (createFile) + { + final InputStream input = ess.getPlugin().getResource(filename + ".txt"); + final OutputStream output = new FileOutputStream(file); + try + { + final byte[] buffer = new byte[1024]; + int length = input.read(buffer); + while (length > 0) + { + output.write(buffer, 0, length); + length = input.read(buffer); + } + } + finally + { + output.close(); + input.close(); + } + ess.getLogger().info("File " + filename + ".txt does not exist. Creating one for you."); + } + } + if (!file.exists()) + { + lastChange = 0; + lines = Collections.emptyList(); + chapters = Collections.emptyList(); + bookmarks = Collections.emptyMap(); + throw new FileNotFoundException("Could not create " + filename + ".txt"); + } + else + { + lastChange = file.lastModified(); + boolean readFromfile; + synchronized (cache) + { + final SoftReference inputRef = cache.get(file.getName()); + BookInput input; + if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) + { + lines = new ArrayList(); + chapters = new ArrayList(); + bookmarks = new HashMap(); + cache.put(file.getName(), new SoftReference(this)); + readFromfile = true; + } + else + { + lines = Collections.unmodifiableList(input.getLines()); + chapters = Collections.unmodifiableList(input.getChapters()); + bookmarks = Collections.unmodifiableMap(input.getBookmarks()); + readFromfile = false; + } + } + if (readFromfile) + { + final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + try + { + int lineNumber = 0; + while (bufferedReader.ready()) + { + final String line = bufferedReader.readLine(); + if (line == null) + { + break; + } + if (line.length() > 0 && line.charAt(0) == '#') + { + bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-fk]", ""), lineNumber); + chapters.add(line.substring(1).replace('&', '§').replace("§§", "&")); + } + lines.add(line.replace('&', '§').replace("§§", "&")); + lineNumber++; + } + } + finally + { + bufferedReader.close(); + } + } + } + } + + @Override + public List getLines() + { + return lines; + } + + @Override + public List getChapters() + { + return chapters; + } + + @Override + public Map getBookmarks() + { + return bookmarks; + } +} \ No newline at end of file diff --git a/Essentials/src/net/ess3/utils/textreader/BookPager.java b/Essentials/src/net/ess3/utils/textreader/BookPager.java new file mode 100644 index 000000000..64ea2aa98 --- /dev/null +++ b/Essentials/src/net/ess3/utils/textreader/BookPager.java @@ -0,0 +1,111 @@ +package net.ess3.utils.textreader; + +import static net.ess3.I18n._; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + + +public class BookPager +{ + private final transient IText text; + + public BookPager(final IText text) + { + this.text = text; + } + + public List getPages(final String pageStr) throws Exception + { + List lines = text.getLines(); + List chapters = text.getChapters(); + List pageLines = new ArrayList(); + Map bookmarks = text.getBookmarks(); + + //This checks to see if we have the chapter in the index + if (!bookmarks.containsKey(pageStr.toLowerCase(Locale.ENGLISH))) + { + throw new Exception(_("infoUnknownChapter")); + } + + //Since we have a valid chapter, count the number of lines in the chapter + final int chapterstart = bookmarks.get(pageStr.toLowerCase(Locale.ENGLISH)) + 1; + int chapterend; + for (chapterend = chapterstart; chapterend < lines.size(); chapterend++) + { + final String line = lines.get(chapterend); + if (line.length() > 0 && line.charAt(0) == '#') + { + break; + } + } + + for (int lineNo = chapterstart; lineNo < chapterend; lineNo += 1) + { + String pageLine = "\u00a70" + lines.get(lineNo); + String tempLine; + final double max = 18; + final int lineLength = pageLine.length(); + double length = 0; + int pointer = 0; + int start = 0; + double weight = 1; + + while (pointer < lineLength) + { + if (length >= max) + { + tempLine = pageLine.substring(start, pointer); + pageLines.add(tempLine); + start = pointer; + length = 0; + } + + Character letter = pageLine.charAt(pointer); + + if (letter == '\u00a7') + { + Character nextLetter = pageLine.charAt(pointer + 1); + if (nextLetter == 'l' || nextLetter == 'L') + { + weight = 1.25; + } + else + { + weight = 1; + } + pointer++; + } + else if (letter == ' ') + { + length += (0.7 * weight); + } + else + { + length += weight; + } + pointer++; + } + if (length > 0) + { + tempLine = pageLine.substring(start, lineLength); + pageLines.add(tempLine); + } + } + + List pages = new ArrayList(); + for (int count = 0; count < pageLines.size(); count += 12) + { + StringBuilder newPage = new StringBuilder(); + for (int i = count; i < count + 12 && i < pageLines.size(); i++) + { + newPage.append("\n").append(pageLines.get(i)); + } + + pages.add(newPage.toString()); + } + + return pages; + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index f928c646c..47cf38fef 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,6 @@ EssentialsExtra EssentialsGeoIP EssentialsGroupBridge - EssentialsGroupManager EssentialsProtect EssentialsSigns EssentialsUpdate @@ -27,7 +26,7 @@ essentials-repo - http://ess.ementalo.com/nexus/content/groups/public/ + http://ci.ess3.net/nexus/content/groups/public/ @@ -75,7 +74,7 @@ UTF-8 - 1.4.7-R1.0 + 1.5.1-R0.1-SNAPSHOT Unknown true 2 -- cgit v1.2.3