1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-17 20:11:21 +02:00

Add Book command

Update pom
This commit is contained in:
ementalo
2013-03-21 23:49:58 +00:00
parent 91653847f5
commit 210c898dd6
5 changed files with 337 additions and 4 deletions

View File

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

View File

@@ -21,6 +21,9 @@ public enum Permissions implements IPermission
BAN_EXEMPT, BAN_EXEMPT,
BAN_NOTIFY, BAN_NOTIFY,
BAN_OFFLINE, BAN_OFFLINE,
BOOK_AUTHOR,
BOOK_OTHERS,
BOOK_TITLE,
BREAK_BEDROCK, BREAK_BEDROCK,
CHAT_COLOR, CHAT_COLOR,
CHAT_IGNORE_EXEMPT, CHAT_IGNORE_EXEMPT,
@@ -30,7 +33,6 @@ public enum Permissions implements IPermission
ECO_LOAN(PermissionDefault.FALSE), ECO_LOAN(PermissionDefault.FALSE),
ENCHANT_UNSAFE(PermissionDefault.FALSE), ENCHANT_UNSAFE(PermissionDefault.FALSE),
ENDERCHEST_OTHERS, ENDERCHEST_OTHERS,
ESSENTIALS,
EXP_GIVE, EXP_GIVE,
EXP_GIVE_OTHERS, EXP_GIVE_OTHERS,
EXP_SET, EXP_SET,
@@ -166,6 +168,7 @@ public enum Permissions implements IPermission
return PermissionFactory.checkPermission(sender, this); 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 ENCHANT = new DotStarPermission("essentials.enchant");
public static final DotStarPermission PERGROUPTELEPORT = new DotStarPermission("essentials.teleport.groups"); public static final DotStarPermission PERGROUPTELEPORT = new DotStarPermission("essentials.teleport.groups");
public static final MaterialDotStarPermission GIVE = new MaterialDotStarPermission("essentials.give", PermissionDefault.TRUE); public static final MaterialDotStarPermission GIVE = new MaterialDotStarPermission("essentials.give", PermissionDefault.TRUE);

View File

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

View File

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

View File

@@ -16,7 +16,6 @@
<module>EssentialsExtra</module> <module>EssentialsExtra</module>
<module>EssentialsGeoIP</module> <module>EssentialsGeoIP</module>
<module>EssentialsGroupBridge</module> <module>EssentialsGroupBridge</module>
<module>EssentialsGroupManager</module>
<module>EssentialsProtect</module> <module>EssentialsProtect</module>
<module>EssentialsSigns</module> <module>EssentialsSigns</module>
<module>EssentialsUpdate</module> <module>EssentialsUpdate</module>
@@ -27,7 +26,7 @@
<repositories> <repositories>
<repository> <repository>
<id>essentials-repo</id> <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> </repository>
</repositories> </repositories>
@@ -75,7 +74,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <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> <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.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> <org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>