diff options
author | ementalo <ementalodev@gmx.co.uk> | 2013-03-21 23:49:58 +0000 |
---|---|---|
committer | ementalo <ementalodev@gmx.co.uk> | 2013-03-21 23:49:58 +0000 |
commit | 210c898dd6f32b9433075549a2674007a7d816fe (patch) | |
tree | 98637bc9be8705b832ed1336d0917afd2914bd92 | |
parent | 91653847f532335182e280ad12efd831e3e8fd98 (diff) | |
download | Essentials-210c898dd6f32b9433075549a2674007a7d816fe.tar Essentials-210c898dd6f32b9433075549a2674007a7d816fe.tar.gz Essentials-210c898dd6f32b9433075549a2674007a7d816fe.tar.lz Essentials-210c898dd6f32b9433075549a2674007a7d816fe.tar.xz Essentials-210c898dd6f32b9433075549a2674007a7d816fe.zip |
Add Book command
Update pom
-rw-r--r-- | Essentials/src/net/ess3/commands/CommandBook.java | 91 | ||||
-rw-r--r-- | Essentials/src/net/ess3/permissions/Permissions.java | 5 | ||||
-rw-r--r-- | Essentials/src/net/ess3/utils/textreader/BookInput.java | 129 | ||||
-rw-r--r-- | Essentials/src/net/ess3/utils/textreader/BookPager.java | 111 | ||||
-rw-r--r-- | pom.xml | 5 |
5 files changed, 337 insertions, 4 deletions
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<String> lines; + private final transient List<String> chapters; + private final transient Map<String, Integer> bookmarks; + private final transient long lastChange; + private final static HashMap<String, SoftReference<BookInput>> cache = new HashMap<String, SoftReference<BookInput>>(); + + 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<BookInput> inputRef = cache.get(file.getName()); + BookInput input; + if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) + { + lines = new ArrayList<String>(); + chapters = new ArrayList<String>(); + bookmarks = new HashMap<String, Integer>(); + cache.put(file.getName(), new SoftReference<BookInput>(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<String> getLines() + { + return lines; + } + + @Override + public List<String> getChapters() + { + return chapters; + } + + @Override + public Map<String, Integer> 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<String> getPages(final String pageStr) throws Exception + { + List<String> lines = text.getLines(); + List<String> chapters = text.getChapters(); + List<String> pageLines = new ArrayList<String>(); + Map<String, Integer> 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<String> pages = new ArrayList<String>(); + 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 @@ -16,7 +16,6 @@ <module>EssentialsExtra</module> <module>EssentialsGeoIP</module> <module>EssentialsGroupBridge</module> - <module>EssentialsGroupManager</module> <module>EssentialsProtect</module> <module>EssentialsSigns</module> <module>EssentialsUpdate</module> @@ -27,7 +26,7 @@ <repositories> <repository> <id>essentials-repo</id> - <url>http://ess.ementalo.com/nexus/content/groups/public/</url> + <url>http://ci.ess3.net/nexus/content/groups/public/</url> </repository> </repositories> @@ -75,7 +74,7 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <bukkit.version>1.4.7-R1.0</bukkit.version> + <bukkit.version>1.5.1-R0.1-SNAPSHOT</bukkit.version> <build.number>Unknown</build.number> <org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs> <org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width> |