Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
6ae15acc72 | |||
2c80e4117e | |||
6d539a14df | |||
184f78d935 | |||
95b0b42fd1 | |||
73eb903517 | |||
b577b1ff75 | |||
c27756f7dc | |||
90bab4a148 | |||
f005b8f8e5 | |||
0fdfd81579 | |||
6820b71dde | |||
28b7849ff5 | |||
e893684dec | |||
f1a8db4f5e | |||
47fabe5806 | |||
ec6def15f9 | |||
13cbe883ed | |||
d2403d247b | |||
c6c018ee88 | |||
8b47aeb8f2 | |||
396c0a9d41 | |||
c995a4fc0f | |||
40512dd771 | |||
5d340af6f2 | |||
70ad6390db | |||
2a5c5b310d | |||
94375eee4b | |||
1eb9c370bc | |||
1100f181be | |||
7467645bcd | |||
11108011a5 | |||
ce249a93b3 | |||
7c229fb459 | |||
f243bf32e7 | |||
a7c284ade2 | |||
0d4d87373c | |||
542cd03bdc | |||
5e85dfd3e4 | |||
af0b0fd12e | |||
a963733734 | |||
7dd201d0d0 | |||
a95d737414 | |||
0ce85af61b | |||
ed54ae84b5 | |||
27d5980fa8 | |||
544f6f69fe | |||
055d7fca60 | |||
8f7cfc591f | |||
2aa25295af | |||
d423b1e109 | |||
7acaa9fc81 | |||
94589faba0 |
131
README.md
131
README.md
@ -1,10 +1,9 @@
|
|||||||
# Books Without Borders
|
# Books Without Borders
|
||||||
|
|
||||||
This is an attempt at a rewrite of the Books Without Borders plugin. This rewrite uses the source code given
|
This is a rewrite of the Books Without Borders plugin. This rewrite originally used the source code given
|
||||||
at [the original bukkit page](https://dev.bukkit.org/projects/books-without-borders). I'm not planning any new features
|
at [the original bukkit page](https://dev.bukkit.org/projects/books-without-borders). While the old plugin still worked
|
||||||
at this time. The only goal is to make it 1.17.1 compliant, but I'll make the code more maintainable along the way.
|
the last time I checked, this plugin does not use any depreciated function calls, making sure it works for the
|
||||||
While the original version still works, it's using a lot of depreciated function calls which will most likely break in
|
foreseeable future.
|
||||||
the future.
|
|
||||||
|
|
||||||
## Books without Borders!
|
## Books without Borders!
|
||||||
|
|
||||||
@ -13,40 +12,128 @@ Books without Borders has got your back!
|
|||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
* Export written books and book and quills to .txt or .yml files
|
- Export written books and book and quills to .txt or .yml files
|
||||||
* Import books from files as written books or unsigned books
|
- Import books from files as written books or unsigned books
|
||||||
* Text files can be any length, and the import process fits the content to the correct page length
|
- Text files can be any length, and the import process fits the content to the correct page length
|
||||||
* Books can be saved privately, or to a directory visible server wide
|
- Books can be saved privately, or to a directory visible server wide
|
||||||
* Encrypt books to prevent other players from reading them
|
- Encrypt books to prevent other players from reading them
|
||||||
* Give, encrypt, or decrypt held books with signs
|
- Give, encrypt, or decrypt held books with signs
|
||||||
* Give players books via command blocks
|
- Give players books via command blocks
|
||||||
* Unsign or copy held books with a simple command
|
- Unsign or copy held books with a simple command
|
||||||
* Give first time players a single book or a set of books when they join
|
- Give first time players a single book or a set of books when they join
|
||||||
* Configurable option to require certain items or pay via Vault compatible economy to create books via command
|
- Configurable option to require certain items or pay via Vault compatible economy to create books via command
|
||||||
* Add lore to any item with a simple command
|
- Add lore to any item with a simple command
|
||||||
* Supports adding and saving color to title, lore, and book contents
|
- Supports adding and saving color to title, lore, and book contents
|
||||||
|
- Color and formatting codes can be manually turned into formatting using /formatbook
|
||||||
|
- Clear a book, and start anew with /clearbook
|
||||||
|
- Formatting and color codes can be turned into formatting once any book is signed. This is enabled through a config
|
||||||
|
value
|
||||||
|
- Change generation of books. Create tattered books for your RPG server!
|
||||||
|
- Optionally, make it impossible to duplicate the original version of a book
|
||||||
|
|
||||||
#### Group encryption
|
#### Group encryption
|
||||||
|
|
||||||
* Group encryption allows every player with the bookswithoutborders.decrypt.\<group> permission to decrypt the encrypted
|
- Group encryption allows every player with the bookswithoutborders.decrypt.\<group> permission to decrypt the encrypted
|
||||||
book without using a password.
|
book without using a password.
|
||||||
|
|
||||||
|
### Commands:
|
||||||
|
|
||||||
|
An in-game description of available commands is available through the /bwb command.
|
||||||
|
|
||||||
|
| Command | Arguments | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| /bookswithoutborders | None | Displays information about commands (and permissions if the user has bookswithoutborders.admin) |
|
||||||
|
| /copybook | \<# of copies> | Copies the book the player is holding |
|
||||||
|
| /decryptbook | \<key> | Decrypts the book the player is holding. "key" is required and MUST be IDENTICAL to the key used to encrypt the held book |
|
||||||
|
| /deletebook | \<file name or number> | Deletes the specified file in the player's directory |
|
||||||
|
| /deletepublicbook | \<file name or number> | Same as deletebook, but deletes files in the public directory |
|
||||||
|
| /encryptbook | \<key> \[encryption style] | Encrypts the book the player is holding. "key" is required and can be any phrase or number excluding spaces. "style" is not required. Possible values are "DNA" or "" |
|
||||||
|
| /formatbook | None | Formats the held written book (converts color and formatting codes to the corresponding formatted text) |
|
||||||
|
| /givebook | \<file name or number> \<playername> \[# of copies (num)] \[signed (true/false)] | Gives the selected player a book from your personal directory |
|
||||||
|
| /givepublicbook | \<file name or number> \<playername> \[# of copies (num)] \[signed (true/false)] | Same as givebook, but uses books from the public directory |
|
||||||
|
| /loadbook | \<file name or number> \[# of copies] \[signed (true/false)] | Creates a book from the specified file and gives it to the player. If no file is specified, a list of available files is returned. If true is specified, the book will be signed, if false it will be unsigned |
|
||||||
|
| /loadpublicbook | \<file name or number> \[# of copies] \[signed (true/false)] | Same as loadbook, but views files in the public directory |
|
||||||
|
| /reload | None | Reloads BwB's configuration file |
|
||||||
|
| /savebook | \[overwrite (true/false)] | Saves the book the player is holding to a text file in a private directory. If true is specified, a book of the same name by the same author will be overwritten by the new book |
|
||||||
|
| /savepublicbook | \[overwrite (true/false)] | Same as savebook, but saves files in the public directory |
|
||||||
|
| /setbookauthor | \<author> | Sets the author of the book the player is holding |
|
||||||
|
| /setbookgeneration | \<generation> | Sets the generation of the held book (ORIGINAL, COPY_OF_ORIGINAL, COPY_OF_COPY, TATTERED) |
|
||||||
|
| /setbookprice | \<item/eco> \<quantity> | Sets the per-book price to create a book via commands. If "Item", the item in the player's hand in the amount of \<quantity> will be the price. If "Eco", a Vault based economy will be used for price. If neither \<Item/Eco> nor \<quantity> are specified, the current price to create books will be removed. |
|
||||||
|
| /setlore | \<new lore> | Sets the lore of the item the player is holding. Insert the lore_line_separator character to force a new line ("~" by default) |
|
||||||
|
| /settitle | \<title> | Sets the title of the book/item the player is holding |
|
||||||
|
| /unsignbook | None | Un-signs the book the player is holding |
|
||||||
|
| /clearbook | None | Removes all text from the held un-signed book |
|
||||||
|
|
||||||
|
### Permissions:
|
||||||
|
|
||||||
|
| Node | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| bookswithoutborders.* | Grants all permissions |
|
||||||
|
| bookswithoutborders.admin | Grants all permissions |
|
||||||
|
| bookswithoutborders.use | Allows player to use commands to save/load/delete in their personal directory |
|
||||||
|
| bookswithoutborders.alterbooks | Allows player to change books' data such as lore/title/author/generation/formatting and un-signing books |
|
||||||
|
| bookswithoutborders.reload | Allows player to reload this plugin |
|
||||||
|
| bookswithoutborders.format | Allows a player to format a book |
|
||||||
|
| bookswithoutborders.save | Allows a player to save books to their personal directory |
|
||||||
|
| bookswithoutborders.load | Allows player to load books from their personal directory |
|
||||||
|
| bookswithoutborders.delete | Allows player to delete books from their personal directory |
|
||||||
|
| bookswithoutborders.unsign | Allows player to un-sign books |
|
||||||
|
| bookswithoutborders.copy | Allows player to copy books |
|
||||||
|
| bookswithoutborders.loadpublic | Allows player to load from the public directory |
|
||||||
|
| bookswithoutborders.savepublic | Allows player to save to the public directory |
|
||||||
|
| bookswithoutborders.encrypt | Allows player to encrypt books |
|
||||||
|
| bookswithoutborders.groupencrypt | Allows player to use group-based encryption |
|
||||||
|
| bookswithoutborders.decrypt | Allows player to decrypt books |
|
||||||
|
| bookswithoutborders.decrypt.agroup | Allows player to decrypt books group-encrypted for group "agroup" |
|
||||||
|
| bookswithoutborders.signs | Allows player to create signs that give/encrypt/decrypt books |
|
||||||
|
| bookswithoutborders.give | Allows player to give another player one of their privately saved books |
|
||||||
|
| bookswithoutborders.givepublic | Allows a player to give another player a book from the public directory |
|
||||||
|
| bookswithoutborders.settitle | Allows player to set the title of the currently held book |
|
||||||
|
| bookswithoutborders.setauthor | Allows player to set the author of the currently held book |
|
||||||
|
| bookswithoutborders.setlore | Allows player to set the lore of the currently held item |
|
||||||
|
| bookswithoutborders.bypassauthoronlycopy | Allows player to ignore Author_Only_Copy config setting |
|
||||||
|
| bookswithoutborders.bypassauthoronlyunsign | Allows player to ignore Author_Only_Unsign config setting |
|
||||||
|
| bookswithoutborders.bypassauthoronlysave | Allows player to ignore Author_Only_Save config setting |
|
||||||
|
| bookswithoutborders.bypassbookprice | Allows player to ignore Price_to_create_book config setting |
|
||||||
|
| bookswithoutborders.setbookprice | Allows player to set the cost of creating a book |
|
||||||
|
| bookswithoutborders.setgeneration | Allows player to change the generation of a book (Original, Copy, Copy of Copy) |
|
||||||
|
| bookswithoutborders.clear | Allows player to clear the contents of the held writable book |
|
||||||
|
|
||||||
### Signs
|
### Signs
|
||||||
|
|
||||||
This plugin supports several custom signs with special functionality. Each plugin sign must have [BwB] on its first
|
This plugin supports several custom signs with special functionality. Each plugin sign must have \[BwB] on its first
|
||||||
line.
|
line.
|
||||||
|
|
||||||
#### Give sign
|
#### Give sign
|
||||||
|
|
||||||
The **_give_** sign must have **[Give]** on its second line. The third and fourth line contains the book to be loaded.
|
The **_give_** sign must have **\[Give]** on its second line. The third and fourth line contains the book to be loaded.
|
||||||
This can either be a numerical id pointing to a publicly saved book, or the full text identifier of the book (book name,
|
This can either be a numerical id pointing to a publicly saved book, or the full text identifier of the book (book name,
|
||||||
author).
|
author).
|
||||||
|
|
||||||
#### Encrypt sign
|
#### Encrypt sign
|
||||||
|
|
||||||
The **_encrypt_** sign must have **[Encrypt]** on its second line. The third line must contain the encryption key The
|
The **_encrypt_** sign must have **\[Encrypt]** on its second line. The third line must contain the encryption key The
|
||||||
fourth line can be empty or contain "dna" for dna-based encryption.
|
fourth line can be empty or contain "dna" for dna-based encryption.
|
||||||
|
|
||||||
#### Decrypt sign
|
#### Decrypt sign
|
||||||
|
|
||||||
The **_decrypt_** sign must have **[Decrypt]** on its second line. The third line must contain the decryption key
|
The **_decrypt_** sign must have **\[Decrypt]** on its second line. The third line must contain the decryption key
|
||||||
|
|
||||||
|
### Configuration options:
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| Save_Books_in_Yaml_Format | Whether to use YAML for saved books instead of just storing them as text |
|
||||||
|
| Max_Number_of_Duplicates | The maximum number of duplicates of a saved book allowed |
|
||||||
|
| Author_Separator | The separator used to separate the book title and the book author |
|
||||||
|
| Lore_line_separator | The separator used to denote a new line in the book/item lore |
|
||||||
|
| Books_for_new_players | A list of books given to new players the first time they join the server |
|
||||||
|
| Message_for_new_players | An optional message displayed to new players the first time they join the server |
|
||||||
|
| Price_to_create_book.Item_type | The item type used as currency for copying books. Use "Economy" to use money instead of items |
|
||||||
|
| Price_to_create_book.Required_quantity | The quantity of currency required to pay for each book produced |
|
||||||
|
| Admin_Auto_Decrypt | Whether any admin can decrypt any book regardless of the group it was encrypted for |
|
||||||
|
| Author_Only_Copy | Whether to only allow the author of a book to create copies |
|
||||||
|
| Author_Only_Unsign | Whether to only allow the author of a book to unsign it |
|
||||||
|
| Author_Only_Save | Whether to only allow saving a player's own books with /savebook |
|
||||||
|
| Format_Book_After_Signing | Whether to automatically format every book when it's signed |
|
||||||
|
| Change_Generation_On_Copy | Whether to display "COPY" or "COPY_OF_COPY" instead of "ORIGINAL" when a book is copied. This also uses the vanilla behavior where a copy of a copy or tattered book cannot be copied further. |
|
16
pom.xml
16
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>net.knarcraft</groupId>
|
<groupId>net.knarcraft</groupId>
|
||||||
<artifactId>BooksWithoutBorders</artifactId>
|
<artifactId>BooksWithoutBorders</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.3.3</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
<description>A continuation of the original Books Without Borders</description>
|
<description>A continuation of the original Books Without Borders</description>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>16</java.version>
|
<java.version>17</java.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
@ -31,8 +31,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${java.version}</source>
|
<source>17</source>
|
||||||
<target>${java.version}</target>
|
<target>17</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -75,7 +75,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>1.17.1-R0.1-SNAPSHOT</version>
|
<version>1.19.2-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -84,5 +84,11 @@
|
|||||||
<version>1.7</version>
|
<version>1.7</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<version>23.0.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package net.knarcraft.bookswithoutborders;
|
package net.knarcraft.bookswithoutborders;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandBooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.command.CommandBooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.command.CommandClear;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandCopy;
|
import net.knarcraft.bookswithoutborders.command.CommandCopy;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandDecrypt;
|
import net.knarcraft.bookswithoutborders.command.CommandDecrypt;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandDelete;
|
import net.knarcraft.bookswithoutborders.command.CommandDelete;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandDeletePublic;
|
import net.knarcraft.bookswithoutborders.command.CommandDeletePublic;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandEncrypt;
|
import net.knarcraft.bookswithoutborders.command.CommandEncrypt;
|
||||||
|
import net.knarcraft.bookswithoutborders.command.CommandFormat;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandGive;
|
import net.knarcraft.bookswithoutborders.command.CommandGive;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandGivePublic;
|
import net.knarcraft.bookswithoutborders.command.CommandGivePublic;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
|
import net.knarcraft.bookswithoutborders.command.CommandGroupEncrypt;
|
||||||
@ -16,25 +18,24 @@ import net.knarcraft.bookswithoutborders.command.CommandSave;
|
|||||||
import net.knarcraft.bookswithoutborders.command.CommandSavePublic;
|
import net.knarcraft.bookswithoutborders.command.CommandSavePublic;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandSetAuthor;
|
import net.knarcraft.bookswithoutborders.command.CommandSetAuthor;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice;
|
import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice;
|
||||||
|
import net.knarcraft.bookswithoutborders.command.CommandSetGeneration;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandSetLore;
|
import net.knarcraft.bookswithoutborders.command.CommandSetLore;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandSetTitle;
|
import net.knarcraft.bookswithoutborders.command.CommandSetTitle;
|
||||||
import net.knarcraft.bookswithoutborders.command.CommandUnSign;
|
import net.knarcraft.bookswithoutborders.command.CommandUnSign;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.listener.BookEventListener;
|
||||||
import net.knarcraft.bookswithoutborders.listener.PlayerEventListener;
|
import net.knarcraft.bookswithoutborders.listener.PlayerEventListener;
|
||||||
import net.knarcraft.bookswithoutborders.listener.SignEventListener;
|
import net.knarcraft.bookswithoutborders.listener.SignEventListener;
|
||||||
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
|
||||||
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
|
||||||
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import org.bukkit.Material;
|
import net.knarcraft.bookswithoutborders.utility.UpdateChecker;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.configuration.Configuration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemFactory;
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
@ -43,28 +44,20 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getErrorColor;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSuccessColor;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSuccessColor;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main Books Without Borders class
|
||||||
|
*/
|
||||||
public class BooksWithoutBorders extends JavaPlugin {
|
public class BooksWithoutBorders extends JavaPlugin {
|
||||||
|
|
||||||
private static int bookDuplicateLimit;
|
|
||||||
private static String titleAuthorSeparator;
|
|
||||||
private static String loreSeparator;
|
|
||||||
private static List<String> firstBooks;
|
|
||||||
private static String welcomeMessage;
|
|
||||||
private static Material bookPriceType = null;
|
|
||||||
private static double bookPriceQuantity;
|
|
||||||
private static boolean authorOnlyCopy;
|
|
||||||
private static boolean useYml;
|
|
||||||
private static boolean adminDecrypt;
|
|
||||||
|
|
||||||
private static ItemFactory itemFactory;
|
private static ItemFactory itemFactory;
|
||||||
private static Map<String, List<String>> playerBooksList;
|
private static Map<UUID, List<String>> playerBooksList;
|
||||||
private static List<String> publicBooksList;
|
private static List<String> publicBooksList;
|
||||||
private static BooksWithoutBorders booksWithoutBorders;
|
private static BooksWithoutBorders booksWithoutBorders;
|
||||||
private static ConsoleCommandSender consoleSender;
|
private static ConsoleCommandSender consoleSender;
|
||||||
@ -88,13 +81,15 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
public static List<String> getAvailableBooks(CommandSender sender, boolean getPublic) {
|
public static List<String> getAvailableBooks(CommandSender sender, boolean getPublic) {
|
||||||
if (getPublic) {
|
if (getPublic) {
|
||||||
return new ArrayList<>(publicBooksList);
|
return new ArrayList<>(publicBooksList);
|
||||||
} else {
|
} else if (sender instanceof Player player) {
|
||||||
String senderName = sender.getName();
|
UUID playerUUID = player.getUniqueId();
|
||||||
if (!playerBooksList.containsKey(senderName)) {
|
if (!playerBooksList.containsKey(playerUUID)) {
|
||||||
List<String> newFiles = FileHelper.listFiles(sender, false);
|
List<String> newFiles = FileHelper.listFiles(sender, false);
|
||||||
playerBooksList.put(senderName, newFiles);
|
playerBooksList.put(playerUUID, newFiles);
|
||||||
}
|
}
|
||||||
return playerBooksList.get(senderName);
|
return playerBooksList.get(playerUUID);
|
||||||
|
} else {
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,18 +103,25 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
List<String> newFiles = FileHelper.listFiles(sender, updatePublic);
|
List<String> newFiles = FileHelper.listFiles(sender, updatePublic);
|
||||||
if (updatePublic) {
|
if (updatePublic) {
|
||||||
publicBooksList = newFiles;
|
publicBooksList = newFiles;
|
||||||
} else {
|
} else if (sender instanceof Player player) {
|
||||||
playerBooksList.put(sender.getName(), newFiles);
|
playerBooksList.put(player.getUniqueId(), newFiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
FileConfiguration config = this.getConfig();
|
||||||
|
config.options().copyDefaults(true);
|
||||||
|
this.saveDefaultConfig();
|
||||||
|
|
||||||
|
//Get plugin info
|
||||||
|
PluginDescriptionFile pluginDescriptionFile = this.getDescription();
|
||||||
|
String pluginVersion = pluginDescriptionFile.getVersion();
|
||||||
|
|
||||||
booksWithoutBorders = this;
|
booksWithoutBorders = this;
|
||||||
consoleSender = this.getServer().getConsoleSender();
|
consoleSender = this.getServer().getConsoleSender();
|
||||||
playerBooksList = new HashMap<>();
|
playerBooksList = new HashMap<>();
|
||||||
firstBooks = new ArrayList<>();
|
BooksWithoutBordersConfig.initialize(this);
|
||||||
BooksWithoutBordersSettings.initialize(this);
|
|
||||||
publicBooksList = FileHelper.listFiles(consoleSender, true);
|
publicBooksList = FileHelper.listFiles(consoleSender, true);
|
||||||
|
|
||||||
PluginManager pluginManager = this.getServer().getPluginManager();
|
PluginManager pluginManager = this.getServer().getPluginManager();
|
||||||
@ -127,125 +129,15 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
if (getSlash() != null && initialize()) {
|
if (getSlash() != null && initialize()) {
|
||||||
pluginManager.registerEvents(new PlayerEventListener(), this);
|
pluginManager.registerEvents(new PlayerEventListener(), this);
|
||||||
pluginManager.registerEvents(new SignEventListener(), this);
|
pluginManager.registerEvents(new SignEventListener(), this);
|
||||||
|
pluginManager.registerEvents(new BookEventListener(), this);
|
||||||
} else {
|
} else {
|
||||||
this.getPluginLoader().disablePlugin(this);
|
this.getPluginLoader().disablePlugin(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerCommands();
|
registerCommands();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=96069",
|
||||||
* Gets whether only the author of a book should be able to copy it
|
() -> pluginVersion, null);
|
||||||
*
|
|
||||||
* @return <p>Whether only the book author can copy it</p>
|
|
||||||
*/
|
|
||||||
public static boolean getAuthorOnlyCopy() {
|
|
||||||
return authorOnlyCopy;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets whether to use YML, not TXT, for saving books
|
|
||||||
*
|
|
||||||
* @return <p>Whether to use YML for saving books</p>
|
|
||||||
*/
|
|
||||||
public static boolean getUseYml() {
|
|
||||||
return useYml;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets whether admins should be able to decrypt books without a password, and decrypt all group encrypted books
|
|
||||||
*
|
|
||||||
* @return <p>Whether admins can bypass the encryption password</p>
|
|
||||||
*/
|
|
||||||
public static boolean getAdminDecrypt() {
|
|
||||||
return adminDecrypt;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the quantity of items/currency necessary for copying books
|
|
||||||
*
|
|
||||||
* @param newQuantity <p>The new quantity necessary for payment</p>
|
|
||||||
*/
|
|
||||||
public static void setBookPriceQuantity(double newQuantity) {
|
|
||||||
bookPriceQuantity = newQuantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the quantity of items/currency necessary for copying books
|
|
||||||
*
|
|
||||||
* @return <p>The quantity necessary for payment</p>
|
|
||||||
*/
|
|
||||||
public static double getBookPriceQuantity() {
|
|
||||||
return bookPriceQuantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the item type used for book pricing
|
|
||||||
*
|
|
||||||
* <p>This item is the one a player has to pay for copying books. AIR is used to denote economy. null is used if
|
|
||||||
* payment is disabled. Otherwise, any item can be used.</p>
|
|
||||||
*
|
|
||||||
* @param newType <p>The new item type to use for book pricing</p>
|
|
||||||
*/
|
|
||||||
public static void setBookPriceType(Material newType) {
|
|
||||||
bookPriceType = newType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the item type used for book pricing
|
|
||||||
*
|
|
||||||
* <p>This item is the one a player has to pay for copying books. AIR is used to denote economy. null is used if
|
|
||||||
* payment is disabled. Otherwise, any item can be used.</p>
|
|
||||||
*
|
|
||||||
* @return <p>The item type used for book pricing</p>
|
|
||||||
*/
|
|
||||||
public static Material getBookPriceType() {
|
|
||||||
return bookPriceType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the welcome message to show to new players
|
|
||||||
*
|
|
||||||
* @return <p>The welcome message to show new players</p>
|
|
||||||
*/
|
|
||||||
public static String getWelcomeMessage() {
|
|
||||||
return welcomeMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the limit of duplicates for each book
|
|
||||||
*
|
|
||||||
* @return <p>The book duplicate limit</p>
|
|
||||||
*/
|
|
||||||
public static int getBookDuplicateLimit() {
|
|
||||||
return bookDuplicateLimit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the separator used to split book title from book author
|
|
||||||
*
|
|
||||||
* @return <p>The separator between title and author</p>
|
|
||||||
*/
|
|
||||||
public static String getTitleAuthorSeparator() {
|
|
||||||
return titleAuthorSeparator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the separator used to denote a newline in a lore string
|
|
||||||
*
|
|
||||||
* @return <p>The separator used to denote lore newline</p>
|
|
||||||
*/
|
|
||||||
public static String getLoreSeparator() {
|
|
||||||
return loreSeparator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a copy of the list of books to give new players
|
|
||||||
*
|
|
||||||
* @return <p>The books to give new players</p>
|
|
||||||
*/
|
|
||||||
public static List<String> getFirstBooks() {
|
|
||||||
return new ArrayList<>(firstBooks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -280,6 +172,9 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
registerCommand("loadPublicBook", new CommandLoadPublic());
|
registerCommand("loadPublicBook", new CommandLoadPublic());
|
||||||
registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders());
|
registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders());
|
||||||
registerCommand("reload", new CommandReload());
|
registerCommand("reload", new CommandReload());
|
||||||
|
registerCommand("formatBook", new CommandFormat());
|
||||||
|
registerCommand("setBookGeneration", new CommandSetGeneration());
|
||||||
|
registerCommand("clearBook", new CommandClear());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -299,6 +194,7 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the plugin, loading and fixing the config file
|
* Initializes the plugin, loading and fixing the config file
|
||||||
|
*
|
||||||
* @return <p>True if successful</p>
|
* @return <p>True if successful</p>
|
||||||
*/
|
*/
|
||||||
private boolean initialize() {
|
private boolean initialize() {
|
||||||
@ -313,16 +209,25 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Load config
|
//Load config
|
||||||
if (!loadConfig()) {
|
if (!BooksWithoutBordersConfig.loadConfig()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Save config with loaded values to fix invalid config values
|
//Save config with loaded values to fix invalid config values
|
||||||
saveConfigValues();
|
BooksWithoutBordersConfig.saveConfigValues();
|
||||||
|
|
||||||
return testFileSaving();
|
return testFileSaving();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the server's item factory
|
||||||
|
*
|
||||||
|
* @return <p>The server's item factory</p>
|
||||||
|
*/
|
||||||
|
public static ItemFactory getItemFactory() {
|
||||||
|
return itemFactory;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes sure necessary folders exist
|
* Makes sure necessary folders exist
|
||||||
*
|
*
|
||||||
@ -356,215 +261,6 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the config
|
|
||||||
*/
|
|
||||||
public void saveConfigValues() {
|
|
||||||
Configuration config = this.getConfig();
|
|
||||||
config.set("Options.Save_Books_in_Yaml_Format", useYml);
|
|
||||||
config.set("Options.Max_Number_of_Duplicates", bookDuplicateLimit);
|
|
||||||
config.set("Options.Title-Author_Separator", titleAuthorSeparator);
|
|
||||||
config.set("Options.Lore_line_separator", loreSeparator);
|
|
||||||
config.set("Options.Books_for_new_players", firstBooks);
|
|
||||||
config.set("Options.Message_for_new_players", welcomeMessage);
|
|
||||||
if (bookPriceType != null) {
|
|
||||||
if (bookPriceType != Material.AIR) {
|
|
||||||
config.set("Options.Price_to_create_book.Item_type", bookPriceType.toString());
|
|
||||||
} else {
|
|
||||||
config.set("Options.Price_to_create_book.Item_type", "Economy");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
config.set("Options.Price_to_create_book.Item_type", "Item type name");
|
|
||||||
}
|
|
||||||
config.set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
|
|
||||||
config.set("Options.Admin_Auto_Decrypt", adminDecrypt);
|
|
||||||
config.set("Options.Author_Only_Copy", authorOnlyCopy);
|
|
||||||
|
|
||||||
//Handles old book and quill settings
|
|
||||||
if (config.contains("Options.Require_book_and_quill_to_create_book")) {
|
|
||||||
sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting \"Require_book_and_quill_to_create_book\"");
|
|
||||||
sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings");
|
|
||||||
|
|
||||||
if (config.getBoolean("Options.Require_book_and_quill_to_create_book")) {
|
|
||||||
bookPriceType = Material.WRITABLE_BOOK;
|
|
||||||
bookPriceQuantity = 1;
|
|
||||||
config.set("Options.Price_to_create_book.Item_type", bookPriceType.toString());
|
|
||||||
config.set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
config.set("Options.Require_book_and_quill_to_create_book", null);
|
|
||||||
}
|
|
||||||
this.saveConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the config
|
|
||||||
* @return <p>True if the config was loaded successfully</p>
|
|
||||||
*/
|
|
||||||
public boolean loadConfig() {
|
|
||||||
this.reloadConfig();
|
|
||||||
Configuration config = this.getConfig();
|
|
||||||
try {
|
|
||||||
useYml = config.getBoolean("Options.Save_Books_in_Yaml_Format", true);
|
|
||||||
bookDuplicateLimit = config.getInt("Options.Max_Number_of_Duplicates", 5);
|
|
||||||
titleAuthorSeparator = config.getString("Options.Title-Author_Separator", ",");
|
|
||||||
loreSeparator = config.getString("Options.Lore_line_separator", "~");
|
|
||||||
adminDecrypt = config.getBoolean("Options.Admin_Auto_Decrypt", false);
|
|
||||||
authorOnlyCopy = config.getBoolean("Options.Author_Only_Copy", false);
|
|
||||||
|
|
||||||
//Set books to give new players
|
|
||||||
firstBooks = config.getStringList("Options.Books_for_new_players");
|
|
||||||
if (config.contains("Options.Book_for_new_players")) {
|
|
||||||
firstBooks.add(config.getString("Options.Book_for_new_players"));
|
|
||||||
}
|
|
||||||
if (firstBooks.isEmpty()) {
|
|
||||||
firstBooks.add(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
welcomeMessage = config.getString("Options.Message_for_new_players", " ");
|
|
||||||
|
|
||||||
//Convert string into material
|
|
||||||
String paymentMaterial = config.getString("Options.Price_to_create_book.Item_type", " ");
|
|
||||||
if (paymentMaterial.equalsIgnoreCase("Economy")) {
|
|
||||||
if (EconomyHelper.setupEconomy()) {
|
|
||||||
bookPriceType = Material.AIR;
|
|
||||||
} else {
|
|
||||||
sendErrorMessage(consoleSender, "BooksWithoutBorders failed to hook into Vault! Book price not set!");
|
|
||||||
bookPriceType = null;
|
|
||||||
}
|
|
||||||
} else if (!paymentMaterial.equalsIgnoreCase(" ")) {
|
|
||||||
Material material = Material.matchMaterial(paymentMaterial);
|
|
||||||
if (material != null) {
|
|
||||||
bookPriceType = material;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bookPriceQuantity = config.getDouble("Options.Price_to_create_book.Required_quantity", 0);
|
|
||||||
|
|
||||||
//Make sure titleAuthorSeparator is a valid value
|
|
||||||
titleAuthorSeparator = cleanString(titleAuthorSeparator);
|
|
||||||
if (titleAuthorSeparator.length() != 1) {
|
|
||||||
sendErrorMessage(consoleSender, "Title-Author_Separator is set to an invalid value!");
|
|
||||||
sendErrorMessage(consoleSender, "Reverting to default value of \",\"");
|
|
||||||
titleAuthorSeparator = ",";
|
|
||||||
config.set("Options.Title-Author_Separator", titleAuthorSeparator);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
sendErrorMessage(consoleSender, "Warning! Config.yml failed to load!");
|
|
||||||
sendErrorMessage(consoleSender, "Try Looking for settings that are missing values!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the given book
|
|
||||||
*
|
|
||||||
* @param sender <p>The command sender trying to load the book</p>
|
|
||||||
* @param fileName <p>The index or file name of the book to load</p>
|
|
||||||
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
|
||||||
* @param directory <p>The directory to save the book in</p>
|
|
||||||
* @return <p>The loaded book</p>
|
|
||||||
*/
|
|
||||||
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory) {
|
|
||||||
return loadBook(sender, fileName, isSigned, directory, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the given book
|
|
||||||
*
|
|
||||||
* @param sender <p>The command sender trying to load the book</p>
|
|
||||||
* @param fileName <p>The index or file name of the book to load</p>
|
|
||||||
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
|
||||||
* @param directory <p>The directory to save the book in</p>
|
|
||||||
* @param numCopies <p>The number of copies to load</p>
|
|
||||||
* @return <p>The loaded book</p>
|
|
||||||
*/
|
|
||||||
public ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory, int numCopies) {
|
|
||||||
BookDirectory bookDirectory = BookDirectory.getFromString(directory);
|
|
||||||
|
|
||||||
//Find the filename if a book index is given
|
|
||||||
try {
|
|
||||||
int bookIndex = Integer.parseInt(fileName);
|
|
||||||
List<String> availableFiles = getAvailableBooks(sender, bookDirectory == BookDirectory.PUBLIC);
|
|
||||||
if (bookIndex <= availableFiles.size()) {
|
|
||||||
fileName = availableFiles.get(Integer.parseInt(fileName) - 1);
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get the full path of the book to load
|
|
||||||
File file = getFullPath(sender, fileName, bookDirectory, directory);
|
|
||||||
if (file == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Make sure the player can pay for the book
|
|
||||||
if (booksHavePrice() && !sender.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
|
||||||
(bookDirectory == BookDirectory.PUBLIC || bookDirectory == BookDirectory.PLAYER) &&
|
|
||||||
EconomyHelper.cannotPayForBookPrinting((Player) sender, numCopies)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Generate a new empty book
|
|
||||||
ItemStack book;
|
|
||||||
BookMeta bookMetadata = (BookMeta) itemFactory.getItemMeta(Material.WRITTEN_BOOK);
|
|
||||||
if (isSigned.equalsIgnoreCase("true")) {
|
|
||||||
book = new ItemStack(Material.WRITTEN_BOOK);
|
|
||||||
} else {
|
|
||||||
book = new ItemStack(Material.WRITABLE_BOOK);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Load the book from the given file
|
|
||||||
BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
|
||||||
if (bookMetadata == null) {
|
|
||||||
sendErrorMessage(sender, "File was blank!!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Remove "encrypted" from the book lore
|
|
||||||
if (bookDirectory == BookDirectory.ENCRYPTED && bookMetadata.hasLore()) {
|
|
||||||
List<String> oldLore = bookMetadata.getLore();
|
|
||||||
if (oldLore != null) {
|
|
||||||
List<String> newLore = new ArrayList<>(oldLore);
|
|
||||||
newLore.remove(0);
|
|
||||||
bookMetadata.setLore(newLore);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set the metadata and amount to the new book
|
|
||||||
book.setItemMeta(bookMetadata);
|
|
||||||
book.setAmount(numCopies);
|
|
||||||
|
|
||||||
return book;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a File pointing to the wanted book
|
|
||||||
*
|
|
||||||
* @param sender <p>The sender to send errors to</p>
|
|
||||||
* @param fileName <p>The name of the book file</p>
|
|
||||||
* @param bookDirectory <p>The book directory the file resides in</p>
|
|
||||||
* @param directory <p>The relative directory given</p>
|
|
||||||
* @return <p>A file or null if it does not exist</p>
|
|
||||||
*/
|
|
||||||
private File getFullPath(CommandSender sender, String fileName, BookDirectory bookDirectory, String directory) {
|
|
||||||
File file = null;
|
|
||||||
if (bookDirectory == BookDirectory.PUBLIC) {
|
|
||||||
file = FileHelper.getBookFile(getBookFolder() + fileName);
|
|
||||||
} else if (bookDirectory == BookDirectory.PLAYER) {
|
|
||||||
file = FileHelper.getBookFile(getBookFolder() + cleanString(sender.getName()) + getSlash() + fileName);
|
|
||||||
} else if (bookDirectory == BookDirectory.ENCRYPTED) {
|
|
||||||
file = FileHelper.getBookFile(getBookFolder() + "Encrypted" + getSlash() + directory + getSlash() + fileName);
|
|
||||||
}
|
|
||||||
if (file == null || !file.isFile()) {
|
|
||||||
sendErrorMessage(sender, "Incorrect file name!");
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a success message to a command sender (player or a console)
|
* Sends a success message to a command sender (player or a console)
|
||||||
*
|
*
|
||||||
@ -585,13 +281,4 @@ public class BooksWithoutBorders extends JavaPlugin {
|
|||||||
sender.sendMessage(getErrorColor() + message);
|
sender.sendMessage(getErrorColor() + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks whether books have a price for printing them
|
|
||||||
*
|
|
||||||
* @return <p>True if players need to pay for printing books</p>
|
|
||||||
*/
|
|
||||||
public boolean booksHavePrice() {
|
|
||||||
return (bookPriceType != null && bookPriceQuantity > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,77 +0,0 @@
|
|||||||
package net.knarcraft.bookswithoutborders;
|
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class for getting various settings
|
|
||||||
*/
|
|
||||||
public class BooksWithoutBordersSettings {
|
|
||||||
|
|
||||||
//Static settings
|
|
||||||
private static final ChatColor errorColor = ChatColor.RED;
|
|
||||||
private static final ChatColor successColor = ChatColor.GREEN;
|
|
||||||
private static final ChatColor commandColor = ChatColor.YELLOW;
|
|
||||||
private static final String SLASH = System.getProperty("file.separator");
|
|
||||||
private static boolean isInitialized;
|
|
||||||
public static String bookFolder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the books without borders settings class
|
|
||||||
*
|
|
||||||
* @param booksWithoutBorders <p>The books without borders object used for getting required data</p>
|
|
||||||
*/
|
|
||||||
public static void initialize(BooksWithoutBorders booksWithoutBorders) {
|
|
||||||
if (isInitialized) {
|
|
||||||
throw new IllegalArgumentException("Settings class initialized twice. This should not happen!");
|
|
||||||
}
|
|
||||||
isInitialized = true;
|
|
||||||
bookFolder = booksWithoutBorders.getDataFolder().getAbsolutePath() + getSlash() + "Books" + getSlash();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the folder used for storing books
|
|
||||||
*
|
|
||||||
* @return <p>The folder used for storing books</p>
|
|
||||||
*/
|
|
||||||
public static String getBookFolder() {
|
|
||||||
return bookFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the color to use for error messages
|
|
||||||
*
|
|
||||||
* @return <p>The color to use for error messages</p>
|
|
||||||
*/
|
|
||||||
public static ChatColor getErrorColor() {
|
|
||||||
return errorColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the color to use for success messages
|
|
||||||
*
|
|
||||||
* @return <p>The color to use for success messages</p>
|
|
||||||
*/
|
|
||||||
public static ChatColor getSuccessColor() {
|
|
||||||
return successColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the color used to color commands
|
|
||||||
*
|
|
||||||
* @return <p>The color used to color commands</p>
|
|
||||||
*/
|
|
||||||
public static ChatColor getCommandColor() {
|
|
||||||
return commandColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the correct slash to use for the used OS
|
|
||||||
*
|
|
||||||
* @return <p>The slash to use for file separators</p>
|
|
||||||
*/
|
|
||||||
public static String getSlash() {
|
|
||||||
return SLASH;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@ -8,25 +9,25 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendErrorMessage;
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendErrorMessage;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getCommandColor;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSuccessColor;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSuccessColor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the books without borders (bwb) command
|
* Command executor for the books without borders (bwb) command
|
||||||
*/
|
*/
|
||||||
public class CommandBooksWithoutBorders implements TabExecutor {
|
public class CommandBooksWithoutBorders implements TabExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
sender.sendMessage(getCommandColor() + "[] denote optional parameters");
|
sender.sendMessage(getCommandColor() + "[] denote optional parameters");
|
||||||
sender.sendMessage(getCommandColor() + "<> denote required parameters");
|
sender.sendMessage(getCommandColor() + "<> denote required parameters");
|
||||||
|
sender.sendMessage(getCommandColor() + "{} denote required permission");
|
||||||
sender.sendMessage(getCommandColor() + "In some cases, commands with required parameters can be called with no parameters");
|
sender.sendMessage(getCommandColor() + "In some cases, commands with required parameters can be called with no parameters");
|
||||||
if (sender instanceof Player) {
|
if (sender instanceof Player) {
|
||||||
showPlayerCommands(sender);
|
showPlayerCommands(sender);
|
||||||
@ -43,9 +44,9 @@ public class CommandBooksWithoutBorders implements TabExecutor {
|
|||||||
*/
|
*/
|
||||||
private void showConsoleCommands(CommandSender sender) {
|
private void showConsoleCommands(CommandSender sender) {
|
||||||
sender.sendMessage(getCommandColor() + "Commands:");
|
sender.sendMessage(getCommandColor() + "Commands:");
|
||||||
showCommandInfo("reload", sender);
|
|
||||||
showCommandInfo("givePublicBook", sender);
|
|
||||||
showCommandInfo("deletePublicBook", sender);
|
showCommandInfo("deletePublicBook", sender);
|
||||||
|
showCommandInfo("givePublicBook", sender);
|
||||||
|
showCommandInfo("reload", sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,9 +56,9 @@ public class CommandBooksWithoutBorders implements TabExecutor {
|
|||||||
*/
|
*/
|
||||||
private void showPlayerCommands(CommandSender sender) {
|
private void showPlayerCommands(CommandSender sender) {
|
||||||
//Lists all commands
|
//Lists all commands
|
||||||
Material bookPriceType = BooksWithoutBorders.getBookPriceType();
|
Material bookPriceType = BooksWithoutBordersConfig.getBookPriceType();
|
||||||
double bookPriceQuantity = BooksWithoutBorders.getBookPriceQuantity();
|
double bookPriceQuantity = BooksWithoutBordersConfig.getBookPriceQuantity();
|
||||||
if (booksWithoutBorders.booksHavePrice()) {
|
if (BooksWithoutBordersConfig.booksHavePrice()) {
|
||||||
if (bookPriceType != Material.AIR) {
|
if (bookPriceType != Material.AIR) {
|
||||||
sendErrorMessage(sender, "[" + (int) bookPriceQuantity + " " + bookPriceType.toString() +
|
sendErrorMessage(sender, "[" + (int) bookPriceQuantity + " " + bookPriceType.toString() +
|
||||||
"(s) are required to create a book]");
|
"(s) are required to create a book]");
|
||||||
@ -68,24 +69,26 @@ public class CommandBooksWithoutBorders implements TabExecutor {
|
|||||||
}
|
}
|
||||||
sender.sendMessage(getCommandColor() + "Commands:");
|
sender.sendMessage(getCommandColor() + "Commands:");
|
||||||
|
|
||||||
showCommandInfo("loadBook", sender);
|
showCommandInfo("copyBook", sender);
|
||||||
showCommandInfo("loadPublicBook", sender);
|
showCommandInfo("decryptBook", sender);
|
||||||
showCommandInfo("saveBook", sender);
|
|
||||||
showCommandInfo("savePublicBook", sender);
|
|
||||||
showCommandInfo("giveBook", sender);
|
|
||||||
showCommandInfo("givePublicBook", sender);
|
|
||||||
showCommandInfo("deleteBook", sender);
|
showCommandInfo("deleteBook", sender);
|
||||||
showCommandInfo("deletePublicBook", sender);
|
showCommandInfo("deletePublicBook", sender);
|
||||||
showCommandInfo("unsignBook", sender);
|
|
||||||
showCommandInfo("copyBook", sender);
|
|
||||||
showCommandInfo("encryptBook", sender);
|
showCommandInfo("encryptBook", sender);
|
||||||
|
showCommandInfo("formatBook", sender);
|
||||||
|
showCommandInfo("giveBook", sender);
|
||||||
|
showCommandInfo("givePublicBook", sender);
|
||||||
showCommandInfo("groupEncryptBook", sender);
|
showCommandInfo("groupEncryptBook", sender);
|
||||||
showCommandInfo("decryptBook", sender);
|
showCommandInfo("loadBook", sender);
|
||||||
showCommandInfo("setTitle", sender);
|
showCommandInfo("loadPublicBook", sender);
|
||||||
showCommandInfo("setAuthor", sender);
|
|
||||||
showCommandInfo("setLore", sender);
|
|
||||||
showCommandInfo("setBookPrice", sender);
|
|
||||||
showCommandInfo("reload", sender);
|
showCommandInfo("reload", sender);
|
||||||
|
showCommandInfo("saveBook", sender);
|
||||||
|
showCommandInfo("savePublicBook", sender);
|
||||||
|
showCommandInfo("setAuthor", sender);
|
||||||
|
showCommandInfo("setBookGeneration", sender);
|
||||||
|
showCommandInfo("setBookPrice", sender);
|
||||||
|
showCommandInfo("setLore", sender);
|
||||||
|
showCommandInfo("setTitle", sender);
|
||||||
|
showCommandInfo("unsignBook", sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -99,15 +102,22 @@ public class CommandBooksWithoutBorders implements TabExecutor {
|
|||||||
if (pluginCommand != null) {
|
if (pluginCommand != null) {
|
||||||
String permission = pluginCommand.getPermission();
|
String permission = pluginCommand.getPermission();
|
||||||
if (permission == null || sender.hasPermission(permission)) {
|
if (permission == null || sender.hasPermission(permission)) {
|
||||||
sender.sendMessage("\n" + getCommandColor() +
|
String commandInfo = "\n" + getCommandColor() + pluginCommand.getUsage().replace("<command>",
|
||||||
pluginCommand.getUsage().replace("<command>", pluginCommand.getName()) + ": " +
|
pluginCommand.getName()) + ": " + getSuccessColor() + pluginCommand.getDescription();
|
||||||
getSuccessColor() + pluginCommand.getDescription());
|
if (sender.hasPermission("bookswithoutborders.admin")) {
|
||||||
|
if (permission == null) {
|
||||||
|
permission = "None";
|
||||||
|
}
|
||||||
|
commandInfo += getCommandColor() + " {" + permission + "}";
|
||||||
|
}
|
||||||
|
sender.sendMessage(commandInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command executor for the clear command
|
||||||
|
*/
|
||||||
|
public class CommandClear implements TabExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||||
|
@NotNull String[] args) {
|
||||||
|
if (!(sender instanceof Player player)) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (InventoryHelper.notHoldingOneWritableBookCheck(player, "You must be holding a writable book to " +
|
||||||
|
"clear it!", "You cannot clear two books at once!")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Clear the player's held book
|
||||||
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, false);
|
||||||
|
BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
|
||||||
|
if (bookMeta == null) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "Unable to get metadata for the held book!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bookMeta.setPages("");
|
||||||
|
bookMeta.setAuthor(null);
|
||||||
|
bookMeta.setGeneration(null);
|
||||||
|
bookMeta.setTitle(null);
|
||||||
|
heldBook.setItemMeta(bookMeta);
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(sender, "Book cleared!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||||
|
@NotNull String[] args) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +1,19 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -21,10 +24,8 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
public class CommandCopy implements TabExecutor {
|
public class CommandCopy implements TabExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -40,19 +41,43 @@ public class CommandCopy implements TabExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||||
int copies = Integer.parseInt(args[0]);
|
int copies = Integer.parseInt(args[0]);
|
||||||
if (copies > 0) {
|
if (copies <= 0) {
|
||||||
if (BooksWithoutBorders.getAuthorOnlyCopy() && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyCopy")) {
|
throw new NumberFormatException("Number of copies must be larger than 0");
|
||||||
if (!isAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta())))
|
}
|
||||||
|
return performCopy(copies, player, heldBook);
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Book not copied!");
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Number specified was invalid!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (booksWithoutBorders.booksHavePrice() &&
|
/**
|
||||||
!player.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
* Performs the actual copying
|
||||||
EconomyHelper.cannotPayForBookPrinting(player, copies)) {
|
*
|
||||||
|
* @param copies <p>The number of copies to be made</p>
|
||||||
|
* @param player <p>The player requesting the copies</p>
|
||||||
|
* @param heldBook <p>The book to be copied</p>
|
||||||
|
* @return <p>True if the copying was successful</p>
|
||||||
|
*/
|
||||||
|
private boolean performCopy(int copies, Player player, ItemStack heldBook) {
|
||||||
|
//Make sure the player owns the book if authorOnlyCopy is enabled
|
||||||
|
if (BooksWithoutBordersConfig.getAuthorOnlyCopy() &&
|
||||||
|
!player.hasPermission("bookswithoutborders.bypassAuthorOnlyCopy")) {
|
||||||
|
if (BookHelper.isNotAuthor(player, (BookMeta) Objects.requireNonNull(heldBook.getItemMeta()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
|
||||||
|
if (BooksWithoutBordersConfig.changeGenerationOnCopy() && bookMeta != null) {
|
||||||
|
return copyNextGenerationBook(bookMeta, player, copies);
|
||||||
|
} else {
|
||||||
|
//Make sure the player can pay for the copying
|
||||||
|
if (paymentUnSuccessful(player, copies)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,37 +85,66 @@ public class CommandCopy implements TabExecutor {
|
|||||||
BooksWithoutBorders.sendSuccessMessage(player, "Book copied!");
|
BooksWithoutBorders.sendSuccessMessage(player, "Book copied!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
}
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Book not copied!");
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Number specified was invalid!");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the given player is the author of a given book
|
* Tries to take payment from a player
|
||||||
*
|
*
|
||||||
* @param player <p>The player to check</p>
|
* @param player <p>The player to take the payment from</p>
|
||||||
* @param book <p>The book to check</p>
|
* @param copies <p>The number of copies to create for the player</p>
|
||||||
* @return <p>True if the player is the book's author</p>
|
* @return <p>True if the payment failed</p>
|
||||||
*/
|
*/
|
||||||
private boolean isAuthor(Player player, BookMeta book) {
|
private boolean paymentUnSuccessful(Player player, int copies) {
|
||||||
String author = book.getAuthor();
|
return BooksWithoutBordersConfig.booksHavePrice() &&
|
||||||
String playerName = InputCleaningHelper.cleanString(player.getName());
|
!player.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
||||||
if (author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author))) {
|
EconomyHelper.cannotPayForBookPrinting(player, copies);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "You must be the author of this book to use this command!");
|
/**
|
||||||
|
* Copies a book with the next generation relative to the input book
|
||||||
|
*
|
||||||
|
* @param bookMeta <p>The book to copy</p>
|
||||||
|
* @param player <p>The player copying the book</p>
|
||||||
|
* @param copies <p>The number of copies requested</p>
|
||||||
|
* @return <p>True if the book was successfully copied</p>
|
||||||
|
*/
|
||||||
|
private boolean copyNextGenerationBook(BookMeta bookMeta, Player player, int copies) {
|
||||||
|
//Copy the vanilla behavior of refusing copying any further
|
||||||
|
if (bookMeta.getGeneration() == BookMeta.Generation.COPY_OF_COPY ||
|
||||||
|
bookMeta.getGeneration() == BookMeta.Generation.TATTERED) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "You cannot copy this book any further. " +
|
||||||
|
"You must have the original or a direct copy.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//Make sure the player can fit the book in their inventory
|
||||||
|
int nextAvailableSlot = player.getInventory().firstEmpty();
|
||||||
|
if (nextAvailableSlot == -1) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "You need an available slot in your inventory.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//Make sure the player can pay for the copying
|
||||||
|
if (paymentUnSuccessful(player, copies)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ItemStack itemStack = new ItemStack(Material.WRITTEN_BOOK);
|
||||||
|
itemStack.setItemMeta(bookMeta);
|
||||||
|
//Increase the generation of the book
|
||||||
|
BookHelper.increaseGeneration(itemStack);
|
||||||
|
itemStack.setAmount(copies);
|
||||||
|
|
||||||
|
player.getInventory().addItem(itemStack);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||||
|
@NotNull String[] args) {
|
||||||
int argumentCount = args.length;
|
int argumentCount = args.length;
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
return TabCompletionHelper.getNumbers(1, 20);
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getNumbers(1, 20), args[0]);
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@ -9,13 +11,14 @@ import org.bukkit.command.TabExecutor;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the decrypt command
|
* Command executor for the decrypt command
|
||||||
@ -23,7 +26,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
|
|||||||
public class CommandDecrypt implements TabExecutor {
|
public class CommandDecrypt implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -43,14 +46,8 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Warning: admin decrypt only allows decrypting files created by the same player. Not sure if intended
|
//Warning: admin decrypt only allows decrypting files created by the same player. Not sure if intended
|
||||||
if (args.length == 0 && BooksWithoutBorders.getAdminDecrypt() && player.hasPermission("bookswithoutborders.admin")) {
|
if (args.length == 0 && BooksWithoutBordersConfig.getAdminDecrypt() && player.hasPermission("bookswithoutborders.admin")) {
|
||||||
String path = getBookFolder() + "Encrypted" + getSlash();
|
String path = getBookFolder() + "Encrypted" + getSlash();
|
||||||
String fileName;
|
|
||||||
if (bookMetadata.hasTitle()) {
|
|
||||||
fileName = bookMetadata.getTitle() + BooksWithoutBorders.getTitleAuthorSeparator() + bookMetadata.getAuthor();
|
|
||||||
} else {
|
|
||||||
fileName = "Untitled," + player.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
File encryptedDirectory = new File(path);
|
File encryptedDirectory = new File(path);
|
||||||
String[] encryptedFiles = encryptedDirectory.list();
|
String[] encryptedFiles = encryptedDirectory.list();
|
||||||
@ -62,7 +59,7 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
//Get the "encryption key" from the filename
|
//Get the "encryption key" from the filename
|
||||||
String key = "";
|
String key = "";
|
||||||
for (String encryptedFile : encryptedFiles) {
|
for (String encryptedFile : encryptedFiles) {
|
||||||
if (encryptedFile.contains(fileName)) {
|
if (encryptedFile.contains(BookHelper.getBookFile(bookMetadata, player, true))) {
|
||||||
key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]"));
|
key = encryptedFile.substring(encryptedFile.indexOf("[") + 1, encryptedFile.indexOf("]"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -102,7 +99,7 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
int argumentCount = args.length;
|
int argumentCount = args.length;
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
List<String> info = new ArrayList<>();
|
List<String> info = new ArrayList<>();
|
||||||
@ -111,4 +108,5 @@ public class CommandDecrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the delete command
|
* Command executor for the delete command
|
||||||
*/
|
*/
|
||||||
public class CommandDelete implements TabExecutor {
|
public class CommandDelete implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player)) {
|
if (!(sender instanceof Player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -80,13 +80,9 @@ public class CommandDelete implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Get the file to be deleted
|
//Get the file to be deleted
|
||||||
File file;
|
String bookDirectory = BookHelper.getBookDirectoryPathString(
|
||||||
if (isPublic) {
|
isPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender);
|
||||||
file = FileHelper.getBookFile(getBookFolder() + fileName);
|
File file = FileHelper.getBookFile(bookDirectory + fileName);
|
||||||
} else {
|
|
||||||
file = FileHelper.getBookFile(getBookFolder() +
|
|
||||||
InputCleaningHelper.cleanString(sender.getName()) + getSlash() + fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Send message if no such file could be found
|
//Send message if no such file could be found
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
@ -107,12 +103,13 @@ public class CommandDelete implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return doTabCompletion(sender, args, false);
|
return doTabCompletion(sender, args, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs tab completion
|
* Performs tab completion
|
||||||
|
*
|
||||||
* @param sender <p>The sender of the command</p>
|
* @param sender <p>The sender of the command</p>
|
||||||
* @param args <p>The arguments given</p>
|
* @param args <p>The arguments given</p>
|
||||||
* @param deletePublic <p>Whether to delete a public book</p>
|
* @param deletePublic <p>Whether to delete a public book</p>
|
||||||
@ -121,7 +118,8 @@ public class CommandDelete implements TabExecutor {
|
|||||||
protected List<String> doTabCompletion(CommandSender sender, String[] args, boolean deletePublic) {
|
protected List<String> doTabCompletion(CommandSender sender, String[] args, boolean deletePublic) {
|
||||||
int argumentCount = args.length;
|
int argumentCount = args.length;
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
return BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
|
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, deletePublic),
|
||||||
|
args[0]);
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -12,12 +13,12 @@ import java.util.List;
|
|||||||
public class CommandDeletePublic extends CommandDelete implements TabExecutor {
|
public class CommandDeletePublic extends CommandDelete implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
return deleteBook(sender, args, true);
|
return deleteBook(sender, args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
return doTabCompletion(sender, args, true);
|
return doTabCompletion(sender, args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,14 @@ import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
|||||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -21,7 +23,7 @@ import java.util.List;
|
|||||||
public class CommandEncrypt implements TabExecutor {
|
public class CommandEncrypt implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (performPreChecks(sender, args, 1, "You must specify a key to encrypt a book!") == null) {
|
if (performPreChecks(sender, args, 1, "You must specify a key to encrypt a book!") == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -96,12 +98,13 @@ public class CommandEncrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
return doTabCompletion(args, false);
|
return doTabCompletion(args, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of string for tab completions
|
* Gets a list of string for tab completions
|
||||||
|
*
|
||||||
* @param args <p>The arguments given</p>
|
* @param args <p>The arguments given</p>
|
||||||
* @param groupEncrypt <p>Whether to auto-complete for group encryption</p>
|
* @param groupEncrypt <p>Whether to auto-complete for group encryption</p>
|
||||||
* @return <p>The strings to auto-complete</p>
|
* @return <p>The strings to auto-complete</p>
|
||||||
@ -123,12 +126,13 @@ public class CommandEncrypt implements TabExecutor {
|
|||||||
info.add("<group>");
|
info.add("<group>");
|
||||||
return info;
|
return info;
|
||||||
} else {
|
} else {
|
||||||
return encryptionStyles;
|
return TabCompletionHelper.filterMatchingStartsWith(encryptionStyles, args[1]);
|
||||||
}
|
}
|
||||||
} else if (argumentsCount == 3 && groupEncrypt) {
|
} else if (argumentsCount == 3 && groupEncrypt) {
|
||||||
return encryptionStyles;
|
return TabCompletionHelper.filterMatchingStartsWith(encryptionStyles, args[2]);
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A command for converting color codes to color formatting
|
||||||
|
*/
|
||||||
|
public class CommandFormat implements TabExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
|
if (!(sender instanceof Player player)) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to format it!",
|
||||||
|
"You cannot format two books at once!")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||||
|
heldBook.setItemMeta(BookFormatter.formatPages((BookMeta) heldBook.getItemMeta()));
|
||||||
|
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(sender, "Book formatted!");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
@ -10,6 +11,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -22,7 +24,7 @@ public class CommandGive implements TabExecutor {
|
|||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player)) {
|
if (!(sender instanceof Player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -67,6 +69,20 @@ public class CommandGive implements TabExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Try and find the target player
|
||||||
|
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
||||||
|
if (receivingPlayer == null) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "Player not found!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make sure the receiver is able to fit the book
|
||||||
|
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "Receiving player must have space in their inventory" +
|
||||||
|
" to receive books!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//Load books available to the player
|
//Load books available to the player
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(bookIdentifier);
|
Integer.parseInt(bookIdentifier);
|
||||||
@ -74,29 +90,8 @@ public class CommandGive implements TabExecutor {
|
|||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Player receivingPlayer = booksWithoutBorders.getServer().getPlayer(receivingPlayerName);
|
|
||||||
if (receivingPlayer == null) {
|
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Player not found!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (receivingPlayer.getInventory().firstEmpty() == -1) {
|
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Receiving player must have space in their inventory to receive books!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
|
||||||
try {
|
try {
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(sender, bookToLoad, isSigned, folder, Integer.parseInt(copies));
|
return loadAndGiveBook(bookIdentifier, sender, receivingPlayer, isSigned, folder, copies);
|
||||||
if (newBook != null) {
|
|
||||||
receivingPlayer.getInventory().addItem(newBook);
|
|
||||||
BooksWithoutBorders.sendSuccessMessage(sender, "Book sent!");
|
|
||||||
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Book failed to load!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Invalid number of book copies specified!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Invalid number of book copies specified!");
|
||||||
return false;
|
return false;
|
||||||
@ -104,12 +99,13 @@ public class CommandGive implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return doTabCompletion(sender, args, false);
|
return doTabCompletion(sender, args, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the actual tab completion
|
* Performs the actual tab completion
|
||||||
|
*
|
||||||
* @param sender <p>The sender of the command</p>
|
* @param sender <p>The sender of the command</p>
|
||||||
* @param args <p>The arguments given</p>
|
* @param args <p>The arguments given</p>
|
||||||
* @param listPublic <p>Whether to list public files or player files</p>
|
* @param listPublic <p>Whether to list public files or player files</p>
|
||||||
@ -129,22 +125,51 @@ public class CommandGive implements TabExecutor {
|
|||||||
|
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
//Return list of books
|
//Return list of books
|
||||||
return BooksWithoutBorders.getAvailableBooks(sender, listPublic);
|
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, listPublic),
|
||||||
|
args[0]);
|
||||||
} else if (argumentCount == 2) {
|
} else if (argumentCount == 2) {
|
||||||
//Return online players
|
//Return online players
|
||||||
return null;
|
return null;
|
||||||
} else if (argumentCount == 3) {
|
} else if (argumentCount == 3) {
|
||||||
//Number of copies
|
//Number of copies
|
||||||
return TabCompletionHelper.getBooleansAndNumbers(1, 3);
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleansAndNumbers(1, 3), args[2]);
|
||||||
} else if (argumentCount == 4) {
|
} else if (argumentCount == 4) {
|
||||||
//Signed
|
//Signed
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(args[2]);
|
Integer.parseInt(args[2]);
|
||||||
return TabCompletionHelper.getBooleans();
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleans(), args[3]);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a book and gives it to the correct player
|
||||||
|
*
|
||||||
|
* @param bookIdentifier <p>The file name specified by the user</p>
|
||||||
|
* @param sender <p>The player trying to give the book</p>
|
||||||
|
* @param receivingPlayer <p>The player which is the receiver of the book</p>
|
||||||
|
* @param isSigned <p>The value given for if the given book should be signed or not</p>
|
||||||
|
* @param folder <p>The folder containing the book to load</p>
|
||||||
|
* @param copies <p>The number of copies the player wants to give</p>
|
||||||
|
* @return <p>True if the book was successfully given</p>
|
||||||
|
*/
|
||||||
|
private boolean loadAndGiveBook(String bookIdentifier, CommandSender sender, Player receivingPlayer,
|
||||||
|
String isSigned, String folder, String copies) throws NumberFormatException {
|
||||||
|
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||||
|
ItemStack newBook = BookLoader.loadBook(sender, bookToLoad, isSigned, folder, Integer.parseInt(copies));
|
||||||
|
if (newBook != null) {
|
||||||
|
//NOTE: As this method bypasses cost, it should also bypass the generation change
|
||||||
|
receivingPlayer.getInventory().addItem(newBook);
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(sender, "Book sent!");
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(receivingPlayer, "Book received!");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "Book failed to load!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -12,12 +13,12 @@ import java.util.List;
|
|||||||
public class CommandGivePublic extends CommandGive implements TabExecutor {
|
public class CommandGivePublic extends CommandGive implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
return giveBook(sender, args, true, "public");
|
return giveBook(sender, args, true, "public");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
return doTabCompletion(sender, args, true);
|
return doTabCompletion(sender, args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ import java.util.List;
|
|||||||
public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor {
|
public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
BookMeta bookMetadata = performPreChecks(sender, args, 2,
|
BookMeta bookMetadata = performPreChecks(sender, args, 2,
|
||||||
"You must specify a group name and key to encrypt a book!");
|
"You must specify a group name and key to encrypt a book!");
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ public class CommandGroupEncrypt extends CommandEncrypt implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
return doTabCompletion(args, true);
|
return doTabCompletion(args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
@ -9,6 +10,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -18,10 +20,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class CommandLoad implements TabExecutor {
|
public class CommandLoad implements TabExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
return loadBook(sender, args, "player", false);
|
return loadBook(sender, args, "player", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ public class CommandLoad implements TabExecutor {
|
|||||||
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
String bookToLoad = InputCleaningHelper.cleanString(bookIdentifier);
|
||||||
try {
|
try {
|
||||||
//Give the new book if it can be loaded
|
//Give the new book if it can be loaded
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));
|
ItemStack newBook = BookLoader.loadBook(player, bookToLoad, isSigned, directory, Integer.parseInt(copies));
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
player.getInventory().addItem(newBook);
|
player.getInventory().addItem(newBook);
|
||||||
BooksWithoutBorders.sendSuccessMessage(player, "Book created!");
|
BooksWithoutBorders.sendSuccessMessage(player, "Book created!");
|
||||||
@ -94,12 +94,13 @@ public class CommandLoad implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return doTabCompletion(sender, args, false);
|
return doTabCompletion(sender, args, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the actual tab completion
|
* Performs the actual tab completion
|
||||||
|
*
|
||||||
* @param sender <p>The sender of the command</p>
|
* @param sender <p>The sender of the command</p>
|
||||||
* @param args <p>The arguments given</p>
|
* @param args <p>The arguments given</p>
|
||||||
* @param loadPublic <p>Whether to list public files or player files</p>
|
* @param loadPublic <p>Whether to list public files or player files</p>
|
||||||
@ -109,19 +110,21 @@ public class CommandLoad implements TabExecutor {
|
|||||||
int argumentCount = args.length;
|
int argumentCount = args.length;
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
//Return list of books
|
//Return list of books
|
||||||
return BooksWithoutBorders.getAvailableBooks(sender, loadPublic);
|
return TabCompletionHelper.filterMatchingContains(BooksWithoutBorders.getAvailableBooks(sender, loadPublic),
|
||||||
|
args[0]);
|
||||||
} else if (argumentCount == 2) {
|
} else if (argumentCount == 2) {
|
||||||
//Number of copies
|
//Number of copies
|
||||||
return TabCompletionHelper.getBooleansAndNumbers(1, 3);
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleansAndNumbers(1, 3), args[1]);
|
||||||
} else if (argumentCount == 3) {
|
} else if (argumentCount == 3) {
|
||||||
//Signed
|
//Signed
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(args[1]);
|
Integer.parseInt(args[1]);
|
||||||
return TabCompletionHelper.getBooleans();
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getBooleans(), args[2]);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -12,12 +13,12 @@ import java.util.List;
|
|||||||
public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
|
public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
return loadBook(sender, args, "public", true);
|
return loadBook(sender, args, "public", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
return doTabCompletion(sender, args, true);
|
return doTabCompletion(sender, args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -13,11 +15,9 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class CommandReload implements TabExecutor {
|
public class CommandReload implements TabExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (booksWithoutBorders.loadConfig()) {
|
if (BooksWithoutBordersConfig.loadConfig()) {
|
||||||
BooksWithoutBorders.sendSuccessMessage(sender, "BooksWithoutBorders configuration reloaded!");
|
BooksWithoutBorders.sendSuccessMessage(sender, "BooksWithoutBorders configuration reloaded!");
|
||||||
} else {
|
} else {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "Reload Failed!");
|
BooksWithoutBorders.sendErrorMessage(sender, "Reload Failed!");
|
||||||
@ -27,7 +27,8 @@ public class CommandReload implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
@ -11,19 +14,16 @@ import org.bukkit.command.TabExecutor;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.getTitleAuthorSeparator;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getCommandColor;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getErrorColor;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getTitleAuthorSeparator;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getErrorColor;
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the save command
|
* Command executor for the save command
|
||||||
@ -31,7 +31,7 @@ import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixN
|
|||||||
public class CommandSave implements TabExecutor {
|
public class CommandSave implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
return saveHeldBook(sender, args, false);
|
return saveHeldBook(sender, args, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ public class CommandSave implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
|
ItemSlot holdingSlot = InventoryHelper.getHeldSlotBook(player, false, false, false, false);
|
||||||
if (holdingSlot != ItemSlot.NONE) {
|
if (holdingSlot != null && holdingSlot != ItemSlot.NONE) {
|
||||||
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
|
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
|
||||||
boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]);
|
boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]);
|
||||||
saveBook(player, holdingItem, duplicate, savePublic);
|
saveBook(player, holdingItem, duplicate, savePublic);
|
||||||
@ -76,32 +76,30 @@ public class CommandSave implements TabExecutor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String savePath;
|
//Only allow saving of own books if enabled
|
||||||
if (saveToPublicFolder) {
|
if (BooksWithoutBordersConfig.getAuthorOnlySave() && !saveToPublicFolder &&
|
||||||
savePath = getBookFolder();
|
(!player.hasPermission("bookswithoutborders.bypassAuthorOnlySave") &&
|
||||||
} else {
|
BookHelper.isNotAuthor(player, book))) {
|
||||||
savePath = getBookFolder() + cleanString(player.getName()) + getSlash();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String savePath = BookHelper.getBookDirectoryPathString(
|
||||||
|
saveToPublicFolder ? BookDirectory.PUBLIC : BookDirectory.PLAYER, player);
|
||||||
|
|
||||||
//Generate book filename
|
//Generate book filename
|
||||||
String fileName;
|
String fileName = BookHelper.getBookFile(book, player, saveToPublicFolder);
|
||||||
if (!book.hasTitle()) {
|
|
||||||
fileName = "Untitled," + player.getName();
|
|
||||||
} else {
|
|
||||||
fileName = book.getTitle() + getTitleAuthorSeparator() + book.getAuthor();
|
|
||||||
}
|
|
||||||
fileName = cleanString(fileName);
|
|
||||||
fileName = fixName(fileName, false);
|
|
||||||
|
|
||||||
//Make sure the used folders exist
|
//Make sure the used folders exist
|
||||||
File file = new File(savePath);
|
File file = new File(savePath);
|
||||||
if (!file.exists() && !file.mkdir()) {
|
if (!file.exists() && !file.mkdir()) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!");
|
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult" +
|
||||||
|
" a server admin!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
File[] foundFiles = file.listFiles();
|
File[] foundFiles = file.listFiles();
|
||||||
if (foundFiles == null) {
|
if (foundFiles == null) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!");
|
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult" +
|
||||||
|
" a server admin!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,27 +110,30 @@ public class CommandSave implements TabExecutor {
|
|||||||
if (foundDuplicates > 0) {
|
if (foundDuplicates > 0) {
|
||||||
//TODO: Decide if this makes sense or needs to be changed
|
//TODO: Decide if this makes sense or needs to be changed
|
||||||
//Skip duplicate book
|
//Skip duplicate book
|
||||||
if (!fileName.contains("Untitled") && !overwrite) {
|
if (!fileName.contains("Untitled" + getTitleAuthorSeparator()) && !overwrite) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Book is already saved!");
|
BooksWithoutBorders.sendErrorMessage(player, "Book is already saved!");
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " + getErrorColor() + "to overwrite!");
|
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " +
|
||||||
|
getErrorColor() + "to overwrite!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Skip if duplicate limit is reached
|
//Skip if duplicate limit is reached
|
||||||
if (foundDuplicates > BooksWithoutBorders.getBookDuplicateLimit()) {
|
if (foundDuplicates > BooksWithoutBordersConfig.getBookDuplicateLimit()) {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!");
|
BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName +
|
||||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " + getErrorColor() + "to overwrite!");
|
" duplicates reached!");
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/savebook true " +
|
||||||
|
getErrorColor() + "to overwrite!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Alter duplicated filename
|
//Alter duplicated filename
|
||||||
if (fileName.contains("Untitled") && !overwrite) {
|
if (fileName.contains("Untitled" + getTitleAuthorSeparator()) && !overwrite) {
|
||||||
fileName = "(" + foundDuplicates + ")" + fileName;
|
fileName = "(" + foundDuplicates + ")" + fileName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (BooksWithoutBorders.getUseYml()) {
|
if (BooksWithoutBordersConfig.getUseYml()) {
|
||||||
BookToFromTextHelper.bookToYml(savePath, fileName, book);
|
BookToFromTextHelper.bookToYml(savePath, fileName, book);
|
||||||
} else {
|
} else {
|
||||||
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
||||||
@ -148,7 +149,9 @@ public class CommandSave implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||||
|
@NotNull String[] args) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.knarcraft.bookswithoutborders.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the save public command
|
* Command executor for the save public command
|
||||||
@ -10,7 +11,7 @@ import org.bukkit.command.TabExecutor;
|
|||||||
public class CommandSavePublic extends CommandSave implements TabExecutor {
|
public class CommandSavePublic extends CommandSave implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
return saveHeldBook(sender, args, true);
|
return saveHeldBook(sender, args, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import org.bukkit.command.TabExecutor;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -19,7 +20,7 @@ import java.util.List;
|
|||||||
public class CommandSetAuthor implements TabExecutor {
|
public class CommandSetAuthor implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -49,11 +50,12 @@ public class CommandSetAuthor implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper;
|
||||||
@ -10,6 +11,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -20,9 +22,10 @@ import java.util.List;
|
|||||||
public class CommandSetBookPrice implements TabExecutor {
|
public class CommandSetBookPrice implements TabExecutor {
|
||||||
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||||
|
private List<String> paymentTypes;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
//Clear the current price
|
//Clear the current price
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
clearItemPrice(sender);
|
clearItemPrice(sender);
|
||||||
@ -65,10 +68,10 @@ public class CommandSetBookPrice implements TabExecutor {
|
|||||||
* @param sender <p>The sender of the command</p>
|
* @param sender <p>The sender of the command</p>
|
||||||
*/
|
*/
|
||||||
private void clearItemPrice(CommandSender sender) {
|
private void clearItemPrice(CommandSender sender) {
|
||||||
BooksWithoutBorders.setBookPriceType(null);
|
BooksWithoutBordersConfig.setBookPriceType(null);
|
||||||
BooksWithoutBorders.setBookPriceQuantity(0);
|
BooksWithoutBordersConfig.setBookPriceQuantity(0);
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Item type name");
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Item type name");
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", BooksWithoutBorders.getBookPriceQuantity());
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", BooksWithoutBordersConfig.getBookPriceQuantity());
|
||||||
booksWithoutBorders.saveConfig();
|
booksWithoutBorders.saveConfig();
|
||||||
|
|
||||||
BooksWithoutBorders.sendSuccessMessage(sender, "Price to create books removed!");
|
BooksWithoutBorders.sendSuccessMessage(sender, "Price to create books removed!");
|
||||||
@ -93,10 +96,10 @@ public class CommandSetBookPrice implements TabExecutor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BooksWithoutBorders.setBookPriceType(heldItem.getType());
|
BooksWithoutBordersConfig.setBookPriceType(heldItem.getType());
|
||||||
BooksWithoutBorders.setBookPriceQuantity(price);
|
BooksWithoutBordersConfig.setBookPriceQuantity(price);
|
||||||
String newPriceType = BooksWithoutBorders.getBookPriceType().toString();
|
String newPriceType = BooksWithoutBordersConfig.getBookPriceType().toString();
|
||||||
double newPriceQuantity = BooksWithoutBorders.getBookPriceQuantity();
|
double newPriceQuantity = BooksWithoutBordersConfig.getBookPriceQuantity();
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", newPriceType);
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", newPriceType);
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", newPriceQuantity);
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", newPriceQuantity);
|
||||||
booksWithoutBorders.saveConfig();
|
booksWithoutBorders.saveConfig();
|
||||||
@ -115,9 +118,9 @@ public class CommandSetBookPrice implements TabExecutor {
|
|||||||
*/
|
*/
|
||||||
private boolean setEconomyPrice(CommandSender sender, double price) {
|
private boolean setEconomyPrice(CommandSender sender, double price) {
|
||||||
if (EconomyHelper.setupEconomy()) {
|
if (EconomyHelper.setupEconomy()) {
|
||||||
BooksWithoutBorders.setBookPriceQuantity(price);
|
BooksWithoutBordersConfig.setBookPriceQuantity(price);
|
||||||
BooksWithoutBorders.setBookPriceType(Material.AIR);
|
BooksWithoutBordersConfig.setBookPriceType(Material.AIR);
|
||||||
double newPriceQuantity = BooksWithoutBorders.getBookPriceQuantity();
|
double newPriceQuantity = BooksWithoutBordersConfig.getBookPriceQuantity();
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Economy");
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Item_type", "Economy");
|
||||||
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", newPriceQuantity);
|
booksWithoutBorders.getConfig().set("Options.Price_to_create_book.Required_quantity", newPriceQuantity);
|
||||||
booksWithoutBorders.saveConfig();
|
booksWithoutBorders.saveConfig();
|
||||||
@ -132,16 +135,27 @@ public class CommandSetBookPrice implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
|
if (paymentTypes == null) {
|
||||||
|
initializeTabCompleteLists();
|
||||||
|
}
|
||||||
|
|
||||||
int argumentCount = args.length;
|
int argumentCount = args.length;
|
||||||
if (argumentCount == 1) {
|
if (argumentCount == 1) {
|
||||||
List<String> paymentTypes = new ArrayList<>();
|
return TabCompletionHelper.filterMatchingStartsWith(paymentTypes, args[0]);
|
||||||
paymentTypes.add("item");
|
|
||||||
paymentTypes.add("eco");
|
|
||||||
return paymentTypes;
|
|
||||||
} else if (argumentCount == 2) {
|
} else if (argumentCount == 2) {
|
||||||
return TabCompletionHelper.getNumbers(1, 3);
|
return TabCompletionHelper.filterMatchingStartsWith(TabCompletionHelper.getNumbers(1, 3), args[1]);
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the lists of tab complete values
|
||||||
|
*/
|
||||||
|
private void initializeTabCompleteLists() {
|
||||||
|
paymentTypes = new ArrayList<>();
|
||||||
|
paymentTypes.add("item");
|
||||||
|
paymentTypes.add("eco");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,74 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command executor for the set generation command
|
||||||
|
*/
|
||||||
|
public class CommandSetGeneration implements TabExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||||
|
@NotNull String[] args) {
|
||||||
|
if (!(sender instanceof Player player)) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (InventoryHelper.notHoldingOneWrittenBookCheck(player, "You must be holding a written book to" +
|
||||||
|
" change its generation!", "You cannot change the generation of two books at once!")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.length < 1) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "You must specify the new generation for your book!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookMeta.Generation generation;
|
||||||
|
try {
|
||||||
|
generation = BookMeta.Generation.valueOf(args[0]);
|
||||||
|
} catch (IllegalArgumentException exception) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Invalid book generation specified!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, true);
|
||||||
|
BookMeta bookMeta = (BookMeta) heldBook.getItemMeta();
|
||||||
|
if (bookMeta != null) {
|
||||||
|
bookMeta.setGeneration(generation);
|
||||||
|
heldBook.setItemMeta(bookMeta);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, "Unable to get book metadata!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||||
|
@NotNull String[] args) {
|
||||||
|
if (args.length == 1) {
|
||||||
|
List<String> generations = new ArrayList<>();
|
||||||
|
for (BookMeta.Generation generation : BookMeta.Generation.values()) {
|
||||||
|
generations.add(generation.name());
|
||||||
|
}
|
||||||
|
return generations;
|
||||||
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -10,6 +11,7 @@ import org.bukkit.command.TabExecutor;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -21,7 +23,7 @@ import java.util.List;
|
|||||||
public class CommandSetLore implements TabExecutor {
|
public class CommandSetLore implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -42,8 +44,8 @@ public class CommandSetLore implements TabExecutor {
|
|||||||
String rawLore = String.join(" ", args);
|
String rawLore = String.join(" ", args);
|
||||||
|
|
||||||
//Format lore
|
//Format lore
|
||||||
rawLore = ChatColor.translateAlternateColorCodes('&', rawLore);
|
rawLore = BookFormatter.translateAllColorCodes(rawLore);
|
||||||
String[] loreParts = rawLore.split(BooksWithoutBorders.getLoreSeparator());
|
String[] loreParts = rawLore.split(BooksWithoutBordersConfig.getLoreSeparator());
|
||||||
List<String> newLore = new ArrayList<>(Arrays.asList(loreParts));
|
List<String> newLore = new ArrayList<>(Arrays.asList(loreParts));
|
||||||
|
|
||||||
//Update lore
|
//Update lore
|
||||||
@ -59,10 +61,11 @@ public class CommandSetLore implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
//TODO: Figure out if there is a better way to display that an argument is required
|
//TODO: Figure out if there is a better way to display that an argument is required
|
||||||
List<String> options = new ArrayList<>();
|
List<String> options = new ArrayList<>();
|
||||||
options.add("<new lore>");
|
options.add("<new lore>");
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -21,7 +22,8 @@ import java.util.List;
|
|||||||
public class CommandSetTitle implements TabExecutor {
|
public class CommandSetTitle implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||||
|
@NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -39,7 +41,7 @@ public class CommandSetTitle implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String title = String.join(" ", args);
|
String title = String.join(" ", args);
|
||||||
title = ChatColor.translateAlternateColorCodes('&', title);
|
title = BookFormatter.translateAllColorCodes(title);
|
||||||
|
|
||||||
ItemMeta itemMetadata = heldItem.getItemMeta();
|
ItemMeta itemMetadata = heldItem.getItemMeta();
|
||||||
if (itemMetadata == null) {
|
if (itemMetadata == null) {
|
||||||
@ -65,9 +67,11 @@ public class CommandSetTitle implements TabExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||||
|
@NotNull String[] args) {
|
||||||
List<String> options = new ArrayList<>();
|
List<String> options = new ArrayList<>();
|
||||||
options.add("<new title>");
|
options.add("<new title>");
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package net.knarcraft.bookswithoutborders.command;
|
package net.knarcraft.bookswithoutborders.command;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@ -10,9 +12,11 @@ import org.bukkit.command.TabExecutor;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command executor for the unsign command
|
* Command executor for the unsign command
|
||||||
@ -20,7 +24,7 @@ import java.util.List;
|
|||||||
public class CommandUnSign implements TabExecutor {
|
public class CommandUnSign implements TabExecutor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!(sender instanceof Player player)) {
|
||||||
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
BooksWithoutBorders.sendErrorMessage(sender, "This command can only be used by a player!");
|
||||||
return false;
|
return false;
|
||||||
@ -46,16 +50,26 @@ public class CommandUnSign implements TabExecutor {
|
|||||||
public void unSignHeldBook(Player player, boolean mainHand) {
|
public void unSignHeldBook(Player player, boolean mainHand) {
|
||||||
//Get the old book
|
//Get the old book
|
||||||
BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand);
|
BookMeta oldBook = InventoryHelper.getHeldBookMetadata(player, mainHand);
|
||||||
|
ItemStack heldBook = InventoryHelper.getHeldBook(player, mainHand);
|
||||||
|
|
||||||
|
//Only allow the owner to un-sign the book
|
||||||
|
if (BooksWithoutBordersConfig.getAuthorOnlyUnsign() && !player.hasPermission("bookswithoutborders.bypassAuthorOnlyUnsign")) {
|
||||||
|
if (BookHelper.isNotAuthor(player, Objects.requireNonNull(oldBook))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//UnSign the book
|
//UnSign the book
|
||||||
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
|
ItemStack newBook = new ItemStack(Material.WRITABLE_BOOK);
|
||||||
|
newBook.setAmount(heldBook.getAmount());
|
||||||
newBook.setItemMeta(oldBook);
|
newBook.setItemMeta(oldBook);
|
||||||
|
|
||||||
InventoryHelper.replaceHeldItem(player, newBook, mainHand);
|
InventoryHelper.replaceHeldItem(player, newBook, mainHand);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,413 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.config;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.EconomyHelper;
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
|
import org.bukkit.configuration.Configuration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendErrorMessage;
|
||||||
|
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.sendSuccessMessage;
|
||||||
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A config class that keeps track of all config values
|
||||||
|
*/
|
||||||
|
public class BooksWithoutBordersConfig {
|
||||||
|
|
||||||
|
private static final ChatColor errorColor = ChatColor.RED;
|
||||||
|
private static final ChatColor successColor = ChatColor.GREEN;
|
||||||
|
private static final ChatColor commandColor = ChatColor.YELLOW;
|
||||||
|
private static final String SLASH = System.getProperty("file.separator");
|
||||||
|
private static boolean isInitialized;
|
||||||
|
public static String bookFolder;
|
||||||
|
|
||||||
|
private static int bookDuplicateLimit;
|
||||||
|
private static String titleAuthorSeparator;
|
||||||
|
private static String loreSeparator;
|
||||||
|
private static List<String> firstBooks = new ArrayList<>();
|
||||||
|
private static String welcomeMessage;
|
||||||
|
private static Material bookPriceType = null;
|
||||||
|
private static double bookPriceQuantity;
|
||||||
|
private static boolean authorOnlyCopy;
|
||||||
|
private static boolean authorOnlyUnsign;
|
||||||
|
private static boolean authorOnlySave;
|
||||||
|
private static boolean useYml;
|
||||||
|
private static boolean adminDecrypt;
|
||||||
|
private static boolean formatBooks;
|
||||||
|
private static boolean changeGenerationOnCopy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the books without borders settings class
|
||||||
|
*
|
||||||
|
* @param booksWithoutBorders <p>The books without borders object used for getting required data</p>
|
||||||
|
*/
|
||||||
|
public static void initialize(BooksWithoutBorders booksWithoutBorders) {
|
||||||
|
if (isInitialized) {
|
||||||
|
throw new IllegalArgumentException("Settings class initialized twice. This should not happen!");
|
||||||
|
}
|
||||||
|
isInitialized = true;
|
||||||
|
bookFolder = booksWithoutBorders.getDataFolder().getAbsolutePath() + getSlash() + "Books" + getSlash();
|
||||||
|
loadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the folder used for storing books
|
||||||
|
*
|
||||||
|
* @return <p>The folder used for storing books</p>
|
||||||
|
*/
|
||||||
|
public static String getBookFolder() {
|
||||||
|
return bookFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the color to use for error messages
|
||||||
|
*
|
||||||
|
* @return <p>The color to use for error messages</p>
|
||||||
|
*/
|
||||||
|
public static ChatColor getErrorColor() {
|
||||||
|
return errorColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the color to use for success messages
|
||||||
|
*
|
||||||
|
* @return <p>The color to use for success messages</p>
|
||||||
|
*/
|
||||||
|
public static ChatColor getSuccessColor() {
|
||||||
|
return successColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the color used to color commands
|
||||||
|
*
|
||||||
|
* @return <p>The color used to color commands</p>
|
||||||
|
*/
|
||||||
|
public static ChatColor getCommandColor() {
|
||||||
|
return commandColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the correct slash to use for the used OS
|
||||||
|
*
|
||||||
|
* @return <p>The slash to use for file separators</p>
|
||||||
|
*/
|
||||||
|
public static String getSlash() {
|
||||||
|
return SLASH;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether only the author of a book should be able to copy it
|
||||||
|
*
|
||||||
|
* @return <p>Whether only the book author can copy it</p>
|
||||||
|
*/
|
||||||
|
public static boolean getAuthorOnlyCopy() {
|
||||||
|
return authorOnlyCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether only the author of a book should be able to unsign it
|
||||||
|
*
|
||||||
|
* @return <p>Whether only the book author can unsign it</p>
|
||||||
|
*/
|
||||||
|
public static boolean getAuthorOnlyUnsign() {
|
||||||
|
return authorOnlyUnsign;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether a player can only save their own books with /savebook
|
||||||
|
*
|
||||||
|
* @return <p>Whether a player can only save their own books</p>
|
||||||
|
*/
|
||||||
|
public static boolean getAuthorOnlySave() {
|
||||||
|
return authorOnlySave;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether to use YML, not TXT, for saving books
|
||||||
|
*
|
||||||
|
* @return <p>Whether to use YML for saving books</p>
|
||||||
|
*/
|
||||||
|
public static boolean getUseYml() {
|
||||||
|
return useYml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether admins should be able to decrypt books without a password, and decrypt all group encrypted books
|
||||||
|
*
|
||||||
|
* @return <p>Whether admins can bypass the encryption password</p>
|
||||||
|
*/
|
||||||
|
public static boolean getAdminDecrypt() {
|
||||||
|
return adminDecrypt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the quantity of items/currency necessary for copying books
|
||||||
|
*
|
||||||
|
* @param newQuantity <p>The new quantity necessary for payment</p>
|
||||||
|
*/
|
||||||
|
public static void setBookPriceQuantity(double newQuantity) {
|
||||||
|
bookPriceQuantity = newQuantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the quantity of items/currency necessary for copying books
|
||||||
|
*
|
||||||
|
* @return <p>The quantity necessary for payment</p>
|
||||||
|
*/
|
||||||
|
public static double getBookPriceQuantity() {
|
||||||
|
return bookPriceQuantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the item type used for book pricing
|
||||||
|
*
|
||||||
|
* <p>This item is the one a player has to pay for copying books. AIR is used to denote economy. null is used if
|
||||||
|
* payment is disabled. Otherwise, any item can be used.</p>
|
||||||
|
*
|
||||||
|
* @param newType <p>The new item type to use for book pricing</p>
|
||||||
|
*/
|
||||||
|
public static void setBookPriceType(Material newType) {
|
||||||
|
bookPriceType = newType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the item type used for book pricing
|
||||||
|
*
|
||||||
|
* <p>This item is the one a player has to pay for copying books. AIR is used to denote economy. null is used if
|
||||||
|
* payment is disabled. Otherwise, any item can be used.</p>
|
||||||
|
*
|
||||||
|
* @return <p>The item type used for book pricing</p>
|
||||||
|
*/
|
||||||
|
public static Material getBookPriceType() {
|
||||||
|
return bookPriceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the welcome message to show to new players
|
||||||
|
*
|
||||||
|
* @return <p>The welcome message to show new players</p>
|
||||||
|
*/
|
||||||
|
public static String getWelcomeMessage() {
|
||||||
|
return welcomeMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the limit of duplicates for each book
|
||||||
|
*
|
||||||
|
* @return <p>The book duplicate limit</p>
|
||||||
|
*/
|
||||||
|
public static int getBookDuplicateLimit() {
|
||||||
|
return bookDuplicateLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether books should change their generation during copy
|
||||||
|
*
|
||||||
|
* @return <p>True if books should change their generation</p>
|
||||||
|
*/
|
||||||
|
public static boolean changeGenerationOnCopy() {
|
||||||
|
return changeGenerationOnCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the separator used to split book title from book author
|
||||||
|
*
|
||||||
|
* @return <p>The separator between title and author</p>
|
||||||
|
*/
|
||||||
|
public static String getTitleAuthorSeparator() {
|
||||||
|
return titleAuthorSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the separator used to denote a newline in a lore string
|
||||||
|
*
|
||||||
|
* @return <p>The separator used to denote lore newline</p>
|
||||||
|
*/
|
||||||
|
public static String getLoreSeparator() {
|
||||||
|
return loreSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether all books should be formatted when they are signed
|
||||||
|
*
|
||||||
|
* @return <p>Whether all books should be formatted</p>
|
||||||
|
*/
|
||||||
|
public static boolean formatBooks() {
|
||||||
|
return formatBooks;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a copy of the list of books to give new players
|
||||||
|
*
|
||||||
|
* @return <p>The books to give new players</p>
|
||||||
|
*/
|
||||||
|
public static List<String> getFirstBooks() {
|
||||||
|
return new ArrayList<>(firstBooks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether books have a price for printing them
|
||||||
|
*
|
||||||
|
* @return <p>True if players need to pay for printing books</p>
|
||||||
|
*/
|
||||||
|
public static boolean booksHavePrice() {
|
||||||
|
return (bookPriceType != null && bookPriceQuantity > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the config
|
||||||
|
*/
|
||||||
|
public static void saveConfigValues() {
|
||||||
|
ConsoleCommandSender consoleSender = BooksWithoutBorders.getInstance().getServer().getConsoleSender();
|
||||||
|
Configuration config = BooksWithoutBorders.getInstance().getConfig();
|
||||||
|
config.set(ConfigOption.USE_YAML.getConfigNode(), useYml);
|
||||||
|
config.set(ConfigOption.MAX_DUPLICATES.getConfigNode(), bookDuplicateLimit);
|
||||||
|
config.set(ConfigOption.TITLE_AUTHOR_SEPARATOR.getConfigNode(), titleAuthorSeparator);
|
||||||
|
config.set(ConfigOption.LORE_LINE_SEPARATOR.getConfigNode(), loreSeparator);
|
||||||
|
config.set(ConfigOption.BOOKS_FOR_NEW_PLAYERS.getConfigNode(), firstBooks);
|
||||||
|
config.set(ConfigOption.MESSAGE_FOR_NEW_PLAYERS.getConfigNode(), welcomeMessage);
|
||||||
|
config.set(ConfigOption.FORMAT_AFTER_SIGNING.getConfigNode(), formatBooks);
|
||||||
|
|
||||||
|
String itemTypeNode = ConfigOption.PRICE_ITEM_TYPE.getConfigNode();
|
||||||
|
if (bookPriceType != null) {
|
||||||
|
if (bookPriceType != Material.AIR) {
|
||||||
|
config.set(itemTypeNode, bookPriceType.toString());
|
||||||
|
} else {
|
||||||
|
config.set(itemTypeNode, "Economy");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
config.set(itemTypeNode, "Item type name");
|
||||||
|
}
|
||||||
|
|
||||||
|
config.set(ConfigOption.PRICE_QUANTITY.getConfigNode(), bookPriceQuantity);
|
||||||
|
config.set(ConfigOption.ADMIN_AUTO_DECRYPT.getConfigNode(), adminDecrypt);
|
||||||
|
config.set(ConfigOption.AUTHOR_ONLY_COPY.getConfigNode(), authorOnlyCopy);
|
||||||
|
config.set(ConfigOption.AUTHOR_ONLY_UNSIGN.getConfigNode(), authorOnlyUnsign);
|
||||||
|
config.set(ConfigOption.AUTHOR_ONLY_SAVE.getConfigNode(), authorOnlySave);
|
||||||
|
config.set(ConfigOption.CHANGE_GENERATION_ON_COPY.getConfigNode(), changeGenerationOnCopy);
|
||||||
|
|
||||||
|
//Handles old book and quill settings
|
||||||
|
if (config.contains("Options.Require_book_and_quill_to_create_book")) {
|
||||||
|
sendSuccessMessage(consoleSender, "[BooksWithoutBorders] Found old config setting " +
|
||||||
|
"\"Require_book_and_quill_to_create_book\"");
|
||||||
|
sendSuccessMessage(consoleSender, "Updating to \"Price_to_create_book\" settings");
|
||||||
|
|
||||||
|
if (config.getBoolean("Options.Require_book_and_quill_to_create_book")) {
|
||||||
|
bookPriceType = Material.WRITABLE_BOOK;
|
||||||
|
bookPriceQuantity = 1;
|
||||||
|
config.set("Options.Price_to_create_book.Item_type", bookPriceType.toString());
|
||||||
|
config.set("Options.Price_to_create_book.Required_quantity", bookPriceQuantity);
|
||||||
|
}
|
||||||
|
config.set("Options.Require_book_and_quill_to_create_book", null);
|
||||||
|
}
|
||||||
|
BooksWithoutBorders.getInstance().saveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the config
|
||||||
|
*
|
||||||
|
* @return <p>True if the config was loaded successfully</p>
|
||||||
|
*/
|
||||||
|
public static boolean loadConfig() {
|
||||||
|
ConsoleCommandSender consoleSender = BooksWithoutBorders.getInstance().getServer().getConsoleSender();
|
||||||
|
BooksWithoutBorders.getInstance().reloadConfig();
|
||||||
|
Configuration config = BooksWithoutBorders.getInstance().getConfig();
|
||||||
|
try {
|
||||||
|
useYml = getBoolean(config, ConfigOption.USE_YAML);
|
||||||
|
bookDuplicateLimit = getInt(config, ConfigOption.MAX_DUPLICATES);
|
||||||
|
titleAuthorSeparator = getString(config, ConfigOption.TITLE_AUTHOR_SEPARATOR);
|
||||||
|
loreSeparator = getString(config, ConfigOption.LORE_LINE_SEPARATOR);
|
||||||
|
adminDecrypt = getBoolean(config, ConfigOption.ADMIN_AUTO_DECRYPT);
|
||||||
|
authorOnlyCopy = getBoolean(config, ConfigOption.AUTHOR_ONLY_COPY);
|
||||||
|
authorOnlyUnsign = getBoolean(config, ConfigOption.AUTHOR_ONLY_UNSIGN);
|
||||||
|
authorOnlySave = getBoolean(config, ConfigOption.AUTHOR_ONLY_SAVE);
|
||||||
|
firstBooks = config.getStringList(ConfigOption.BOOKS_FOR_NEW_PLAYERS.getConfigNode());
|
||||||
|
welcomeMessage = getString(config, ConfigOption.MESSAGE_FOR_NEW_PLAYERS);
|
||||||
|
formatBooks = getBoolean(config, ConfigOption.FORMAT_AFTER_SIGNING);
|
||||||
|
changeGenerationOnCopy = getBoolean(config, ConfigOption.CHANGE_GENERATION_ON_COPY);
|
||||||
|
|
||||||
|
//Convert string into material
|
||||||
|
String paymentMaterial = getString(config, ConfigOption.PRICE_ITEM_TYPE);
|
||||||
|
if (paymentMaterial.equalsIgnoreCase("Economy")) {
|
||||||
|
if (EconomyHelper.setupEconomy()) {
|
||||||
|
bookPriceType = Material.AIR;
|
||||||
|
} else {
|
||||||
|
sendErrorMessage(consoleSender,
|
||||||
|
"BooksWithoutBorders failed to hook into Vault! Book price not set!");
|
||||||
|
bookPriceType = null;
|
||||||
|
}
|
||||||
|
} else if (!paymentMaterial.trim().isEmpty()) {
|
||||||
|
Material material = Material.matchMaterial(paymentMaterial);
|
||||||
|
if (material != null) {
|
||||||
|
bookPriceType = material;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bookPriceQuantity = getDouble(config, ConfigOption.PRICE_QUANTITY);
|
||||||
|
|
||||||
|
//Make sure titleAuthorSeparator is a valid value
|
||||||
|
titleAuthorSeparator = cleanString(titleAuthorSeparator);
|
||||||
|
if (titleAuthorSeparator.length() != 1) {
|
||||||
|
sendErrorMessage(consoleSender, "Title-Author_Separator is set to an invalid value!");
|
||||||
|
sendErrorMessage(consoleSender, "Reverting to default value of \",\"");
|
||||||
|
titleAuthorSeparator = ",";
|
||||||
|
config.set("Options.Title-Author_Separator", titleAuthorSeparator);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
sendErrorMessage(consoleSender, "Warning! Config.yml failed to load!");
|
||||||
|
sendErrorMessage(consoleSender, "Try Looking for settings that are missing values!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the double value of the given config option
|
||||||
|
*
|
||||||
|
* @param config <p>The configuration to read from</p>
|
||||||
|
* @param configOption <p>The configuration option to get the value for</p>
|
||||||
|
* @return <p>The value of the option</p>
|
||||||
|
*/
|
||||||
|
private static double getDouble(Configuration config, ConfigOption configOption) {
|
||||||
|
return config.getDouble(configOption.getConfigNode(), (Double) configOption.getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the integer value of the given config option
|
||||||
|
*
|
||||||
|
* @param config <p>The configuration to read from</p>
|
||||||
|
* @param configOption <p>The configuration option to get the value for</p>
|
||||||
|
* @return <p>The value of the option</p>
|
||||||
|
*/
|
||||||
|
private static int getInt(Configuration config, ConfigOption configOption) {
|
||||||
|
return config.getInt(configOption.getConfigNode(), (Integer) configOption.getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the string value of the given config option
|
||||||
|
*
|
||||||
|
* @param config <p>The configuration to read from</p>
|
||||||
|
* @param configOption <p>The configuration option to get the value for</p>
|
||||||
|
* @return <p>The value of the option</p>
|
||||||
|
*/
|
||||||
|
private static String getString(Configuration config, ConfigOption configOption) {
|
||||||
|
return config.getString(configOption.getConfigNode(), (String) configOption.getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the boolean value of the given config option
|
||||||
|
*
|
||||||
|
* @param config <p>The configuration to read from</p>
|
||||||
|
* @param configOption <p>The configuration option to get the value for</p>
|
||||||
|
* @return <p>The value of the option</p>
|
||||||
|
*/
|
||||||
|
private static boolean getBoolean(Configuration config, ConfigOption configOption) {
|
||||||
|
return config.getBoolean(configOption.getConfigNode(), (Boolean) configOption.getDefaultValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A representation of the different available config options
|
||||||
|
*/
|
||||||
|
public enum ConfigOption {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether YAML should be used to store books instead of simple text files
|
||||||
|
*/
|
||||||
|
USE_YAML("Options.Save_Books_in_Yaml_Format", true),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The max duplicates of a book that can be saved
|
||||||
|
*/
|
||||||
|
MAX_DUPLICATES("Options.Max_Number_of_Duplicates", 5),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The separator used to separate book title and book author
|
||||||
|
*/
|
||||||
|
TITLE_AUTHOR_SEPARATOR("Options.Title-Author_Separator", ","),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The separator used to specify a new line in an item's lore
|
||||||
|
*/
|
||||||
|
LORE_LINE_SEPARATOR("Options.Lore_line_separator", "~"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The books given to new players when they first join
|
||||||
|
*/
|
||||||
|
BOOKS_FOR_NEW_PLAYERS("Options.Books_for_new_players", "[]"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to display to new players when they first join
|
||||||
|
*/
|
||||||
|
MESSAGE_FOR_NEW_PLAYERS("Options.Message_for_new_players", ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The item type used to pay for book copying
|
||||||
|
*/
|
||||||
|
PRICE_ITEM_TYPE("Options.Price_to_create_book.Item_type", ""),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The amount of items used to pay for book copying
|
||||||
|
*/
|
||||||
|
PRICE_QUANTITY("Options.Price_to_create_book.Required_quantity", 0.0),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether admins should be able to decrypt books for all groups
|
||||||
|
*/
|
||||||
|
ADMIN_AUTO_DECRYPT("Options.Admin_Auto_Decrypt", false),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether only the book author should be able to copy a book
|
||||||
|
*/
|
||||||
|
AUTHOR_ONLY_COPY("Options.Author_Only_Copy", false),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether only the book author should be able to unsign a book
|
||||||
|
*/
|
||||||
|
AUTHOR_ONLY_UNSIGN("Options.Author_Only_Unsign", false),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether a player can only save their own books with /savebook
|
||||||
|
*/
|
||||||
|
AUTHOR_ONLY_SAVE("Options.Author_Only_Save", false),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to turn Original into Copy when copying books
|
||||||
|
*/
|
||||||
|
CHANGE_GENERATION_ON_COPY("Options.Change_Generation_On_Copy", false),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to automatically format every signed book
|
||||||
|
*/
|
||||||
|
FORMAT_AFTER_SIGNING("Options.Format_Book_After_Signing", false);
|
||||||
|
|
||||||
|
private final String configNode;
|
||||||
|
private final Object defaultValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new config option
|
||||||
|
*
|
||||||
|
* @param configNode <p>The config node in the config file this option represents</p>
|
||||||
|
* @param defaultValue <p>The default value for this config option</p>
|
||||||
|
*/
|
||||||
|
ConfigOption(String configNode, Object defaultValue) {
|
||||||
|
this.configNode = configNode;
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the config node used for loading/saving this config value
|
||||||
|
*
|
||||||
|
* @return <p>The config node</p>
|
||||||
|
*/
|
||||||
|
public String getConfigNode() {
|
||||||
|
return this.configNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the default value of this config option
|
||||||
|
*
|
||||||
|
* @return <p>The default value of this config option</p>
|
||||||
|
*/
|
||||||
|
public Object getDefaultValue() {
|
||||||
|
return this.defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -14,8 +14,6 @@ public class GenenCrypt {
|
|||||||
|
|
||||||
private final Random ranGen;
|
private final Random ranGen;
|
||||||
private final String[] bases;
|
private final String[] bases;
|
||||||
private final ArrayList<String> originalCodonList;
|
|
||||||
private final ArrayList<String> shuffledCodonList;
|
|
||||||
private final String[] charList;
|
private final String[] charList;
|
||||||
private final HashMap<String, String[]> codonTable;
|
private final HashMap<String, String[]> codonTable;
|
||||||
private final HashMap<String, String> decryptTable;
|
private final HashMap<String, String> decryptTable;
|
||||||
@ -29,7 +27,7 @@ public class GenenCrypt {
|
|||||||
public GenenCrypt(String key) {
|
public GenenCrypt(String key) {
|
||||||
|
|
||||||
// define the initial, unshuffled codon list of 4 base codons
|
// define the initial, unshuffled codon list of 4 base codons
|
||||||
originalCodonList = new ArrayList<>();
|
ArrayList<String> originalCodonList = new ArrayList<>();
|
||||||
bases = new String[]{"A", "T", "G", "C"};
|
bases = new String[]{"A", "T", "G", "C"};
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
for (int j = 0; j < 4; j++) {
|
for (int j = 0; j < 4; j++) {
|
||||||
@ -50,7 +48,7 @@ public class GenenCrypt {
|
|||||||
ranGen = new java.util.Random(longKey);
|
ranGen = new java.util.Random(longKey);
|
||||||
|
|
||||||
// use the random number generator and the originalCodonList to make a shuffled list
|
// use the random number generator and the originalCodonList to make a shuffled list
|
||||||
shuffledCodonList = new ArrayList<>();
|
ArrayList<String> shuffledCodonList = new ArrayList<>();
|
||||||
while (originalCodonList.size() > 0) {
|
while (originalCodonList.size() > 0) {
|
||||||
int index = ranGen.nextInt(originalCodonList.size());
|
int index = ranGen.nextInt(originalCodonList.size());
|
||||||
shuffledCodonList.add(originalCodonList.get(index));
|
shuffledCodonList.add(originalCodonList.get(index));
|
||||||
@ -90,24 +88,6 @@ public class GenenCrypt {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints the shuffled codon list used for generating the codon table
|
|
||||||
*/
|
|
||||||
public void printShuffledList() {
|
|
||||||
for (String s : shuffledCodonList) {
|
|
||||||
System.out.println(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints the original codon list before it was shuffled
|
|
||||||
*/
|
|
||||||
public void printOriginalList() {
|
|
||||||
for (String s : originalCodonList) {
|
|
||||||
System.out.println(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints the codon table used for encryption and decryption
|
* Prints the codon table used for encryption and decryption
|
||||||
*/
|
*/
|
||||||
@ -181,4 +161,5 @@ public class GenenCrypt {
|
|||||||
}
|
}
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@ package net.knarcraft.bookswithoutborders.encryption;
|
|||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple substitution cipher
|
||||||
|
*/
|
||||||
public class SubstitutionCipher {
|
public class SubstitutionCipher {
|
||||||
|
|
||||||
public SubstitutionCipher() {
|
public SubstitutionCipher() {
|
||||||
@ -83,5 +86,6 @@ public class SubstitutionCipher {
|
|||||||
}
|
}
|
||||||
return output.toString();
|
return output.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.listener;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookFormatter;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerEditBookEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A listener for listening to book events
|
||||||
|
*
|
||||||
|
* <p>Mainly used for auto-formatting signed books if enabled</p>
|
||||||
|
*/
|
||||||
|
public class BookEventListener implements Listener {
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBookSign(PlayerEditBookEvent event) {
|
||||||
|
if (event.isCancelled() || !event.isSigning() || !BooksWithoutBordersConfig.formatBooks()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.setNewBookMeta(BookFormatter.formatPages(event.getNewBookMeta()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,10 @@
|
|||||||
package net.knarcraft.bookswithoutborders.listener;
|
package net.knarcraft.bookswithoutborders.listener;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookHelper;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -15,13 +19,13 @@ import org.bukkit.inventory.meta.BookMeta;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
/**
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
* A listener for listening to player-related events such as joining or holding a book
|
||||||
|
*/
|
||||||
public class PlayerEventListener implements Listener {
|
public class PlayerEventListener implements Listener {
|
||||||
|
|
||||||
private final String slash = getSlash();
|
|
||||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@ -53,12 +57,22 @@ public class PlayerEventListener implements Listener {
|
|||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
|
//If a book directory exists with this player's name, move it to this player's UUID
|
||||||
|
String bookFolder = BooksWithoutBordersConfig.getBookFolder();
|
||||||
|
File file = new File(bookFolder + InputCleaningHelper.cleanString(player.getName()));
|
||||||
|
if (file.exists()) {
|
||||||
|
if (!file.renameTo(new File(bookFolder + player.getUniqueId()))) {
|
||||||
|
BooksWithoutBorders.getInstance().getLogger().log(Level.WARNING, "Unable to migrate player book " +
|
||||||
|
"directory for player " + player.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Handle new players
|
//Handle new players
|
||||||
if (!player.hasPlayedBefore()) {
|
if (!player.hasPlayedBefore()) {
|
||||||
boolean sendMessage = true;
|
boolean sendMessage = true;
|
||||||
|
|
||||||
//Gives new players necessary books
|
//Gives new players necessary books
|
||||||
for (String bookName : BooksWithoutBorders.getFirstBooks()) {
|
for (String bookName : BooksWithoutBordersConfig.getFirstBooks()) {
|
||||||
sendMessage = giveBookToNewPlayer(bookName, player, sendMessage);
|
sendMessage = giveBookToNewPlayer(bookName, player, sendMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,13 +102,13 @@ public class PlayerEventListener implements Listener {
|
|||||||
if (!bookName.trim().isEmpty()) {
|
if (!bookName.trim().isEmpty()) {
|
||||||
|
|
||||||
//Give the book to the player if it exists
|
//Give the book to the player if it exists
|
||||||
ItemStack newBook = booksWithoutBorders.loadBook(player, bookName, "true", "public");
|
ItemStack newBook = BookLoader.loadBook(player, bookName, "true", "public");
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
player.getInventory().addItem(newBook);
|
player.getInventory().addItem(newBook);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Send the player a welcome message if it exists
|
//Send the player a welcome message if it exists
|
||||||
String welcomeMessage = BooksWithoutBorders.getWelcomeMessage();
|
String welcomeMessage = BooksWithoutBordersConfig.getWelcomeMessage();
|
||||||
if (!welcomeMessage.trim().isEmpty() && newBook != null && sendMessage) {
|
if (!welcomeMessage.trim().isEmpty() && newBook != null && sendMessage) {
|
||||||
sendMessage = false;
|
sendMessage = false;
|
||||||
booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(booksWithoutBorders,
|
booksWithoutBorders.getServer().getScheduler().scheduleSyncDelayedTask(booksWithoutBorders,
|
||||||
@ -143,22 +157,23 @@ public class PlayerEventListener implements Listener {
|
|||||||
//Unknown author is ignored
|
//Unknown author is ignored
|
||||||
fileName = oldBook.getTitle();
|
fileName = oldBook.getTitle();
|
||||||
} else {
|
} else {
|
||||||
fileName = oldBook.getTitle() + BooksWithoutBorders.getTitleAuthorSeparator() + oldBook.getAuthor();
|
fileName = oldBook.getTitle() + BooksWithoutBordersConfig.getTitleAuthorSeparator() + oldBook.getAuthor();
|
||||||
}
|
}
|
||||||
|
|
||||||
String cleanPlayerName = InputCleaningHelper.cleanString(player.getName());
|
String playerFolderPath = BookHelper.getBookDirectoryPathString(BookDirectory.PLAYER, player);
|
||||||
|
String publicFolderPath = BookHelper.getBookDirectoryPathString(BookDirectory.PUBLIC, player);
|
||||||
|
|
||||||
String[] possiblePaths = new String[]{
|
String[] possiblePaths = new String[]{
|
||||||
getBookFolder() + fileName + ".yml",
|
publicFolderPath + fileName + ".yml",
|
||||||
getBookFolder() + fileName + ".txt",
|
publicFolderPath + fileName + ".txt",
|
||||||
getBookFolder() + cleanPlayerName + slash + fileName + ".yml",
|
playerFolderPath + fileName + ".yml",
|
||||||
getBookFolder() + cleanPlayerName + slash + fileName + ".txt"
|
playerFolderPath + fileName + ".txt"
|
||||||
};
|
};
|
||||||
|
|
||||||
for (String path : possiblePaths) {
|
for (String path : possiblePaths) {
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
return booksWithoutBorders.loadBook(player, fileName, "true", "player");
|
return BookLoader.loadBook(player, fileName, "true", "player");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
package net.knarcraft.bookswithoutborders.listener;
|
package net.knarcraft.bookswithoutborders.listener;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||||
|
import net.knarcraft.bookswithoutborders.utility.BookLoader;
|
||||||
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
import net.knarcraft.bookswithoutborders.utility.EncryptionHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||||
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
|
||||||
import org.bukkit.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Tag;
|
import org.bukkit.Tag;
|
||||||
import org.bukkit.block.Sign;
|
import org.bukkit.block.Sign;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
@ -22,10 +25,13 @@ import org.bukkit.inventory.meta.BookMeta;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListIndex;
|
import static net.knarcraft.bookswithoutborders.utility.FileHelper.isBookListIndex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A listener for relevant sign events such as clicking a decryption sign
|
||||||
|
*/
|
||||||
public class SignEventListener implements Listener {
|
public class SignEventListener implements Listener {
|
||||||
|
|
||||||
private final String slash = getSlash();
|
private final String slash = getSlash();
|
||||||
@ -84,10 +90,14 @@ public class SignEventListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ItemStack heldItem = playerInventory.getItem(hand);
|
ItemStack heldItem = playerInventory.getItem(hand);
|
||||||
|
if (heldItem == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Material heldItemType = heldItem.getType();
|
Material heldItemType = heldItem.getType();
|
||||||
|
|
||||||
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && (Tag.SIGNS.isTagged(clickedBlockType) ||
|
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && (Tag.SIGNS.isTagged(clickedBlockType) ||
|
||||||
Tag.WALL_SIGNS.isTagged(clickedBlockType))) {
|
Tag.WALL_SIGNS.isTagged(clickedBlockType))) {
|
||||||
|
event.setUseItemInHand(Event.Result.DENY);
|
||||||
//The player right-clicked a sign
|
//The player right-clicked a sign
|
||||||
Sign sign = (Sign) event.getClickedBlock().getState();
|
Sign sign = (Sign) event.getClickedBlock().getState();
|
||||||
if (signLineEquals(sign, 0, "[BwB]", ChatColor.DARK_GREEN)) {
|
if (signLineEquals(sign, 0, "[BwB]", ChatColor.DARK_GREEN)) {
|
||||||
@ -148,7 +158,7 @@ public class SignEventListener implements Listener {
|
|||||||
private ChatColor getSignLine2Color(Sign sign) {
|
private ChatColor getSignLine2Color(Sign sign) {
|
||||||
String line = sign.getLine(2);
|
String line = sign.getLine(2);
|
||||||
if (!ChatColor.stripColor(line).equals(line)) {
|
if (!ChatColor.stripColor(line).equals(line)) {
|
||||||
return ChatColor.getByChar(sign.getLine(2).substring(1, 2));
|
return ChatColor.getByChar(sign.getLine(2).substring(1, 2).charAt(0));
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -177,7 +187,8 @@ public class SignEventListener implements Listener {
|
|||||||
*/
|
*/
|
||||||
private void generateGiveSign(SignChangeEvent event, String[] lines, Player player) {
|
private void generateGiveSign(SignChangeEvent event, String[] lines, Player player) {
|
||||||
if (lines[2].length() > 13 || lines[3].length() > 13) {
|
if (lines[2].length() > 13 || lines[3].length() > 13) {
|
||||||
player.sendMessage(ChatColor.RED + "[Give] signs' 3rd and 4th lines must be 13 characters or less!");
|
BooksWithoutBorders.sendErrorMessage(player,
|
||||||
|
"[Give] signs' 3rd and 4th lines must be 13 characters or less!");
|
||||||
markGiveSignValidity(event, false);
|
markGiveSignValidity(event, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -234,11 +245,11 @@ public class SignEventListener implements Listener {
|
|||||||
|
|
||||||
//Permission check
|
//Permission check
|
||||||
if (!player.hasPermission("bookswithoutborders.decrypt." + groupName) &&
|
if (!player.hasPermission("bookswithoutborders.decrypt." + groupName) &&
|
||||||
!(BooksWithoutBorders.getAdminDecrypt() && player.hasPermission("bookswithoutborders.admin"))) {
|
!(BooksWithoutBordersConfig.getAdminDecrypt() && player.hasPermission("bookswithoutborders.admin"))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileName = oldBook.getTitle() + BooksWithoutBorders.getTitleAuthorSeparator() + oldBook.getAuthor();
|
String fileName = oldBook.getTitle() + BooksWithoutBordersConfig.getTitleAuthorSeparator() + oldBook.getAuthor();
|
||||||
|
|
||||||
String encryptionFile = InputCleaningHelper.cleanString(groupName) + slash + fileName + ".yml";
|
String encryptionFile = InputCleaningHelper.cleanString(groupName) + slash + fileName + ".yml";
|
||||||
|
|
||||||
@ -249,7 +260,7 @@ public class SignEventListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", groupName, heldItem.getAmount());
|
newBook = BookLoader.loadBook(player, fileName, "true", groupName, heldItem.getAmount());
|
||||||
|
|
||||||
if (newBook == null) {
|
if (newBook == null) {
|
||||||
return;
|
return;
|
||||||
@ -257,7 +268,7 @@ public class SignEventListener implements Listener {
|
|||||||
|
|
||||||
player.getInventory().setItem(hand, newBook);
|
player.getInventory().setItem(hand, newBook);
|
||||||
player.closeInventory();
|
player.closeInventory();
|
||||||
player.sendMessage(ChatColor.GREEN + "Book auto-decrypted!");
|
BooksWithoutBorders.sendSuccessMessage(player, "Book auto-decrypted!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -304,13 +315,13 @@ public class SignEventListener implements Listener {
|
|||||||
fileName += ChatColor.stripColor(thirdLine);
|
fileName += ChatColor.stripColor(thirdLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack newBook = BooksWithoutBorders.getInstance().loadBook(player, fileName, "true", "public");
|
ItemStack newBook = BookLoader.loadBook(player, fileName, "true", "public");
|
||||||
|
|
||||||
if (newBook != null) {
|
if (newBook != null) {
|
||||||
player.getInventory().addItem(newBook);
|
player.getInventory().addItem(newBook);
|
||||||
player.sendMessage(ChatColor.GREEN + "Received book!");
|
BooksWithoutBorders.sendSuccessMessage(player, "Received book!");
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage(ChatColor.RED + "Book failed to load!");
|
BooksWithoutBorders.sendErrorMessage(player, "Book failed to load!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,20 @@ package net.knarcraft.bookswithoutborders.state;
|
|||||||
* This enum represents the different directories books can be saved in
|
* This enum represents the different directories books can be saved in
|
||||||
*/
|
*/
|
||||||
public enum BookDirectory {
|
public enum BookDirectory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The public directory
|
||||||
|
*/
|
||||||
PUBLIC,
|
PUBLIC,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A player directory
|
||||||
|
*/
|
||||||
PLAYER,
|
PLAYER,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The encrypted directory
|
||||||
|
*/
|
||||||
ENCRYPTED;
|
ENCRYPTED;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,4 +37,5 @@ public enum BookDirectory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ package net.knarcraft.bookswithoutborders.state;
|
|||||||
* This enum represents the different available encryption styles
|
* This enum represents the different available encryption styles
|
||||||
*/
|
*/
|
||||||
public enum EncryptionStyle {
|
public enum EncryptionStyle {
|
||||||
|
|
||||||
DNA("dna"),
|
DNA("dna"),
|
||||||
SUBSTITUTION("substitution");
|
SUBSTITUTION("substitution");
|
||||||
|
|
||||||
@ -27,4 +28,5 @@ public enum EncryptionStyle {
|
|||||||
}
|
}
|
||||||
return SUBSTITUTION;
|
return SUBSTITUTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class for formatting text to fit books
|
* A class for formatting text to fit books
|
||||||
@ -8,7 +15,6 @@ import java.util.List;
|
|||||||
public final class BookFormatter {
|
public final class BookFormatter {
|
||||||
|
|
||||||
private BookFormatter() {
|
private BookFormatter() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,4 +100,35 @@ public final class BookFormatter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats every page in the given book meta by converting color and formatting codes
|
||||||
|
*
|
||||||
|
* @param bookMeta <p>The book meta to change</p>
|
||||||
|
* @return <p>The changed book meta</p>
|
||||||
|
*/
|
||||||
|
public static BookMeta formatPages(BookMeta bookMeta) {
|
||||||
|
List<String> formattedPages = new ArrayList<>(Objects.requireNonNull(bookMeta).getPageCount());
|
||||||
|
for (String page : bookMeta.getPages()) {
|
||||||
|
formattedPages.add(BookFormatter.translateAllColorCodes(page));
|
||||||
|
}
|
||||||
|
bookMeta.setPages(formattedPages);
|
||||||
|
return bookMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translates all found color codes to formatting in a string
|
||||||
|
*
|
||||||
|
* @param message <p>The string to search for color codes</p>
|
||||||
|
* @return <p>The message with color codes translated</p>
|
||||||
|
*/
|
||||||
|
public static String translateAllColorCodes(String message) {
|
||||||
|
message = ChatColor.translateAlternateColorCodes('&', message);
|
||||||
|
Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})");
|
||||||
|
Matcher matcher = pattern.matcher(message);
|
||||||
|
while (matcher.find()) {
|
||||||
|
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group()));
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,170 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||||
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class for getting abstract book information
|
||||||
|
*/
|
||||||
|
public final class BookHelper {
|
||||||
|
|
||||||
|
private BookHelper() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the author of a book from UUID if necessary
|
||||||
|
*
|
||||||
|
* @param author <p>The author string</p>
|
||||||
|
* @return <p>The author string, converted if it was a UUID</p>
|
||||||
|
*/
|
||||||
|
public static String authorFromUUID(String author) {
|
||||||
|
try {
|
||||||
|
UUID authorID = UUID.fromString(author);
|
||||||
|
Player player = Bukkit.getPlayer(authorID);
|
||||||
|
if (player != null) {
|
||||||
|
author = player.getName();
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
}
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the file path of the selected book directory
|
||||||
|
*
|
||||||
|
* @param bookDirectory <p>The book directory to get (ENCRYPTED is not supported here)</p>
|
||||||
|
* @param sender <p>The command sender trying to get the directory</p>
|
||||||
|
* @return <p>The path of the directory, or null if not possible to get</p>
|
||||||
|
*/
|
||||||
|
public static File getBookDirectoryPath(BookDirectory bookDirectory, CommandSender sender) {
|
||||||
|
String bookFolderString = getBookDirectoryPathString(bookDirectory, sender);
|
||||||
|
if (bookFolderString == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new File(bookFolderString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the string path of the selected book directory
|
||||||
|
*
|
||||||
|
* @param bookDirectory <p>The book directory to get (ENCRYPTED is not supported here)</p>
|
||||||
|
* @param sender <p>The command sender trying to get the directory</p>
|
||||||
|
* @return <p>The path of the directory, or null if not possible to get</p>
|
||||||
|
*/
|
||||||
|
public static String getBookDirectoryPathString(BookDirectory bookDirectory, CommandSender sender) {
|
||||||
|
String folder = null;
|
||||||
|
String bookFolder = BooksWithoutBordersConfig.getBookFolder();
|
||||||
|
if (bookDirectory == BookDirectory.PUBLIC) {
|
||||||
|
folder = bookFolder;
|
||||||
|
} else if (bookDirectory == BookDirectory.PLAYER && sender instanceof Player player) {
|
||||||
|
folder = bookFolder + player.getUniqueId() + getSlash();
|
||||||
|
}
|
||||||
|
return folder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increases the generation of the given book, if necessary
|
||||||
|
*
|
||||||
|
* @param bookItem <p>The book item to increase the generation of</p>
|
||||||
|
*/
|
||||||
|
public static void increaseGeneration(ItemStack bookItem) {
|
||||||
|
BookMeta bookMeta = (BookMeta) bookItem.getItemMeta();
|
||||||
|
if (BooksWithoutBordersConfig.changeGenerationOnCopy() && bookMeta != null) {
|
||||||
|
bookMeta.setGeneration(BookHelper.getNextGeneration(bookMeta.getGeneration()));
|
||||||
|
bookItem.setItemMeta(bookMeta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the next generation of the given book
|
||||||
|
*
|
||||||
|
* <p>If an original book is given, this will yield a copy of the original. If a copy of original is given, this
|
||||||
|
* will yield a copy of a copy. In all other cases, the generation will stay the same</p>
|
||||||
|
*
|
||||||
|
* @param currentGeneration <p>The current generation of the book</p>
|
||||||
|
* @return <p>The next generation of the book</p>
|
||||||
|
*/
|
||||||
|
public static BookMeta.Generation getNextGeneration(BookMeta.Generation currentGeneration) {
|
||||||
|
if (currentGeneration == null) {
|
||||||
|
return BookMeta.Generation.COPY_OF_ORIGINAL;
|
||||||
|
}
|
||||||
|
return switch (currentGeneration) {
|
||||||
|
case ORIGINAL -> BookMeta.Generation.COPY_OF_ORIGINAL;
|
||||||
|
case COPY_OF_ORIGINAL -> BookMeta.Generation.COPY_OF_COPY;
|
||||||
|
default -> currentGeneration;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the file name of the given book
|
||||||
|
*
|
||||||
|
* @param book <p>The book to get the file of</p>
|
||||||
|
* @param player <p>The player trying to do something with the book</p>
|
||||||
|
* @return <p>The book file</p>
|
||||||
|
*/
|
||||||
|
public static String getBookFile(BookMeta book, Player player, boolean isPublic) {
|
||||||
|
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||||
|
String bookName;
|
||||||
|
if (book.hasTitle()) {
|
||||||
|
bookName = book.getTitle();
|
||||||
|
} else {
|
||||||
|
bookName = "Untitled";
|
||||||
|
}
|
||||||
|
|
||||||
|
String authorName;
|
||||||
|
if ((!book.hasAuthor() || isAuthor(player.getName(), book.getAuthor())) && !isPublic) {
|
||||||
|
//Store as unique id to account for name changes
|
||||||
|
authorName = player.getUniqueId().toString();
|
||||||
|
} else if (!book.hasAuthor()) {
|
||||||
|
authorName = player.getName();
|
||||||
|
} else {
|
||||||
|
authorName = book.getAuthor();
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixName(cleanString(bookName + titleAuthorSeparator + authorName), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the given player is the author of a given book
|
||||||
|
*
|
||||||
|
* @param player <p>The player to check</p>
|
||||||
|
* @param book <p>The book to check</p>
|
||||||
|
* @return <p>True if the player is not the book's author</p>
|
||||||
|
*/
|
||||||
|
public static boolean isNotAuthor(Player player, BookMeta book) {
|
||||||
|
if (isAuthor(player.getName(), book.getAuthor())) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player,
|
||||||
|
"You must be the author of this book to use this command!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether the given player name is equal to the given book author
|
||||||
|
*
|
||||||
|
* @param playerName <p>The player name to check</p>
|
||||||
|
* @param author <p>The author to check</p>
|
||||||
|
* @return <p>True if the player is the author</p>
|
||||||
|
*/
|
||||||
|
private static boolean isAuthor(String playerName, String author) {
|
||||||
|
playerName = InputCleaningHelper.cleanString(playerName);
|
||||||
|
return author != null && playerName.equalsIgnoreCase(InputCleaningHelper.cleanString(author));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,150 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A helper class for loading books from files
|
||||||
|
*/
|
||||||
|
public final class BookLoader {
|
||||||
|
|
||||||
|
private BookLoader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the given book
|
||||||
|
*
|
||||||
|
* @param sender <p>The command sender trying to load the book</p>
|
||||||
|
* @param fileName <p>The index or file name of the book to load</p>
|
||||||
|
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
||||||
|
* @param directory <p>The directory to save the book in</p>
|
||||||
|
* @return <p>The loaded book</p>
|
||||||
|
*/
|
||||||
|
public static ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory) {
|
||||||
|
return loadBook(sender, fileName, isSigned, directory, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the given book
|
||||||
|
*
|
||||||
|
* @param sender <p>The command sender trying to load the book</p>
|
||||||
|
* @param fileName <p>The index or file name of the book to load</p>
|
||||||
|
* @param isSigned <p>Whether to load the book as signed, and not unsigned</p>
|
||||||
|
* @param directory <p>The directory to save the book in</p>
|
||||||
|
* @param numCopies <p>The number of copies to load</p>
|
||||||
|
* @return <p>The loaded book</p>
|
||||||
|
*/
|
||||||
|
public static ItemStack loadBook(CommandSender sender, String fileName, String isSigned, String directory, int numCopies) {
|
||||||
|
BookDirectory bookDirectory = BookDirectory.getFromString(directory);
|
||||||
|
|
||||||
|
//Find the filename if a book index is given
|
||||||
|
try {
|
||||||
|
int bookIndex = Integer.parseInt(fileName);
|
||||||
|
List<String> availableFiles = BooksWithoutBorders.getAvailableBooks(sender, bookDirectory == BookDirectory.PUBLIC);
|
||||||
|
if (bookIndex <= availableFiles.size()) {
|
||||||
|
fileName = availableFiles.get(Integer.parseInt(fileName) - 1);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get the full path of the book to load
|
||||||
|
File file = getFullPath(sender, fileName, bookDirectory, directory);
|
||||||
|
if (file == null) {
|
||||||
|
//Try converting the username to UUID
|
||||||
|
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||||
|
String[] data = fileName.split(titleAuthorSeparator);
|
||||||
|
String extension = data[1].substring(data[1].length() - 4);
|
||||||
|
String userName = data[1].substring(0, data[1].length() - 4);
|
||||||
|
Player player = Bukkit.getPlayer(userName);
|
||||||
|
if (player != null) {
|
||||||
|
data[1] = player.getUniqueId() + extension;
|
||||||
|
file = getFullPath(sender, String.join(titleAuthorSeparator, data), bookDirectory, directory);
|
||||||
|
if (file == null) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect file name!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make sure the player can pay for the book
|
||||||
|
if (BooksWithoutBordersConfig.booksHavePrice() &&
|
||||||
|
!sender.hasPermission("bookswithoutborders.bypassBookPrice") &&
|
||||||
|
(bookDirectory == BookDirectory.PUBLIC || bookDirectory == BookDirectory.PLAYER) &&
|
||||||
|
EconomyHelper.cannotPayForBookPrinting((Player) sender, numCopies)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Generate a new empty book
|
||||||
|
ItemStack book;
|
||||||
|
BookMeta bookMetadata = (BookMeta) BooksWithoutBorders.getItemFactory().getItemMeta(Material.WRITTEN_BOOK);
|
||||||
|
if (isSigned.equalsIgnoreCase("true")) {
|
||||||
|
book = new ItemStack(Material.WRITTEN_BOOK);
|
||||||
|
} else {
|
||||||
|
book = new ItemStack(Material.WRITABLE_BOOK);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Load the book from the given file
|
||||||
|
BookToFromTextHelper.bookFromFile(file, bookMetadata);
|
||||||
|
if (bookMetadata == null) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(sender, "File was blank!!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Remove "encrypted" from the book lore
|
||||||
|
if (bookDirectory == BookDirectory.ENCRYPTED && bookMetadata.hasLore()) {
|
||||||
|
List<String> oldLore = bookMetadata.getLore();
|
||||||
|
if (oldLore != null) {
|
||||||
|
List<String> newLore = new ArrayList<>(oldLore);
|
||||||
|
newLore.remove(0);
|
||||||
|
bookMetadata.setLore(newLore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set the metadata and amount to the new book
|
||||||
|
book.setItemMeta(bookMetadata);
|
||||||
|
//Increase book generation if enabled
|
||||||
|
BookHelper.increaseGeneration(book);
|
||||||
|
book.setAmount(numCopies);
|
||||||
|
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a File pointing to the wanted book
|
||||||
|
*
|
||||||
|
* @param sender <p>The sender to send errors to</p>
|
||||||
|
* @param fileName <p>The name of the book file</p>
|
||||||
|
* @param bookDirectory <p>The book directory the file resides in</p>
|
||||||
|
* @param directory <p>The relative directory given</p>
|
||||||
|
* @return <p>A file or null if it does not exist</p>
|
||||||
|
*/
|
||||||
|
private static File getFullPath(CommandSender sender, String fileName, BookDirectory bookDirectory, String directory) {
|
||||||
|
File file;
|
||||||
|
String slash = BooksWithoutBordersConfig.getSlash();
|
||||||
|
String bookFolder = BooksWithoutBordersConfig.getBookFolder();
|
||||||
|
if (bookDirectory == BookDirectory.ENCRYPTED) {
|
||||||
|
file = FileHelper.getBookFile(bookFolder + "Encrypted" + slash + directory + slash + fileName);
|
||||||
|
} else {
|
||||||
|
file = FileHelper.getBookFile(BookHelper.getBookDirectoryPathString(bookDirectory, sender) + fileName);
|
||||||
|
}
|
||||||
|
if (file == null || !file.isFile()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
@ -14,6 +14,7 @@ import java.io.PrintWriter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.knarcraft.bookswithoutborders.utility.BookHelper.authorFromUUID;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,6 +42,11 @@ public final class BookToFromTextHelper {
|
|||||||
if (bookMetadata.hasAuthor()) {
|
if (bookMetadata.hasAuthor()) {
|
||||||
bookYml.set("Author", bookMetadata.getAuthor());
|
bookYml.set("Author", bookMetadata.getAuthor());
|
||||||
}
|
}
|
||||||
|
BookMeta.Generation generation = bookMetadata.getGeneration();
|
||||||
|
if (generation == null) {
|
||||||
|
generation = BookMeta.Generation.ORIGINAL;
|
||||||
|
}
|
||||||
|
bookYml.set("Generation", generation.name());
|
||||||
if (bookMetadata.hasPages()) {
|
if (bookMetadata.hasPages()) {
|
||||||
bookYml.set("Pages", bookMetadata.getPages());
|
bookYml.set("Pages", bookMetadata.getPages());
|
||||||
}
|
}
|
||||||
@ -81,8 +87,14 @@ public final class BookToFromTextHelper {
|
|||||||
PrintWriter printWriter = new PrintWriter(fileWriter);
|
PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||||
List<String> pages = bookMetadata.getPages();
|
List<String> pages = bookMetadata.getPages();
|
||||||
|
|
||||||
|
BookMeta.Generation generation = bookMetadata.getGeneration();
|
||||||
|
if (generation == null) {
|
||||||
|
generation = BookMeta.Generation.ORIGINAL;
|
||||||
|
}
|
||||||
|
String generationString = ":" + generation.name();
|
||||||
|
|
||||||
//Save each page of the book as a text line
|
//Save each page of the book as a text line
|
||||||
printWriter.println("[Book]");
|
printWriter.println("[Book]" + generationString);
|
||||||
for (String page : pages) {
|
for (String page : pages) {
|
||||||
printWriter.println(page);
|
printWriter.println(page);
|
||||||
}
|
}
|
||||||
@ -100,8 +112,9 @@ public final class BookToFromTextHelper {
|
|||||||
try {
|
try {
|
||||||
FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file);
|
FileConfiguration bookYml = YamlConfiguration.loadConfiguration(file);
|
||||||
|
|
||||||
|
bookMetadata.setGeneration(BookMeta.Generation.valueOf(bookYml.getString("Generation", "ORIGINAL")));
|
||||||
bookMetadata.setTitle(bookYml.getString("Title", "Untitled"));
|
bookMetadata.setTitle(bookYml.getString("Title", "Untitled"));
|
||||||
bookMetadata.setAuthor(bookYml.getString("Author", "Unknown"));
|
bookMetadata.setAuthor(authorFromUUID(bookYml.getString("Author", "Unknown")));
|
||||||
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
bookMetadata.setPages(bookYml.getStringList("Pages"));
|
||||||
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
bookMetadata.setLore(bookYml.getStringList("Lore"));
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@ -121,15 +134,18 @@ public final class BookToFromTextHelper {
|
|||||||
private static BookMeta bookFromTXT(String fileName, File file, BookMeta bookMetadata) {
|
private static BookMeta bookFromTXT(String fileName, File file, BookMeta bookMetadata) {
|
||||||
String author;
|
String author;
|
||||||
String title;
|
String title;
|
||||||
String titleAuthorSeparator = BooksWithoutBorders.getTitleAuthorSeparator();
|
String titleAuthorSeparator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||||
|
|
||||||
|
//Remove .txt extension
|
||||||
|
fileName = fileName.substring(0, fileName.length() - 4);
|
||||||
//Get title and author from the file name
|
//Get title and author from the file name
|
||||||
if (fileName.contains(titleAuthorSeparator)) {
|
if (fileName.contains(titleAuthorSeparator)) {
|
||||||
author = fileName.substring(fileName.indexOf(titleAuthorSeparator) + 1, fileName.length() - 4);
|
String[] titleAuthor = fileName.split(titleAuthorSeparator);
|
||||||
title = fileName.substring(0, fileName.indexOf(titleAuthorSeparator));
|
title = titleAuthor[0];
|
||||||
|
author = titleAuthor[1];
|
||||||
} else {
|
} else {
|
||||||
author = "Unknown";
|
author = "Unknown";
|
||||||
title = fileName.substring(0, fileName.length() - 4);
|
title = fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Replace underscores with spaces
|
//Replace underscores with spaces
|
||||||
@ -144,11 +160,17 @@ public final class BookToFromTextHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Parse the generation from the book data
|
||||||
|
if (rawPages != null && rawPages.size() > 0 && rawPages.get(0).startsWith("Generation:")) {
|
||||||
|
bookMetadata.setGeneration(BookMeta.Generation.valueOf(rawPages.get(0).split(":")[1]));
|
||||||
|
rawPages.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
//Remove any empty pages
|
//Remove any empty pages
|
||||||
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
List<String> pages = new ArrayList<>(InputCleaningHelper.cleanList(rawPages));
|
||||||
|
|
||||||
//Update the metadata of the book with its new values
|
//Update the metadata of the book with its new values
|
||||||
bookMetadata.setAuthor(author);
|
bookMetadata.setAuthor(authorFromUUID(author));
|
||||||
bookMetadata.setTitle(title);
|
bookMetadata.setTitle(title);
|
||||||
bookMetadata.setPages(pages);
|
bookMetadata.setPages(pages);
|
||||||
|
|
||||||
@ -172,8 +194,11 @@ public final class BookToFromTextHelper {
|
|||||||
bufferedReader.close();
|
bufferedReader.close();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (firstLine.equalsIgnoreCase("[Book]")) {
|
if (firstLine.toLowerCase().startsWith("[book]")) {
|
||||||
//Read every line directly as a page, as this is a saved book
|
//Read every line directly as a page, as this is a saved book
|
||||||
|
if (firstLine.contains(":")) {
|
||||||
|
rawPages.add("Generation:" + firstLine.split(":")[1]);
|
||||||
|
}
|
||||||
String readLine;
|
String readLine;
|
||||||
do {
|
do {
|
||||||
readLine = bufferedReader.readLine();
|
readLine = bufferedReader.readLine();
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
import org.bukkit.plugin.ServicesManager;
|
import org.bukkit.plugin.ServicesManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,7 +25,6 @@ public final class EconomyHelper {
|
|||||||
private static Economy economy;
|
private static Economy economy;
|
||||||
|
|
||||||
private EconomyHelper() {
|
private EconomyHelper() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,12 +70,16 @@ public final class EconomyHelper {
|
|||||||
public static boolean cannotPayForBookPrinting(Player player, int numCopies) {
|
public static boolean cannotPayForBookPrinting(Player player, int numCopies) {
|
||||||
//BookPriceQuantity: How many items are required to pay for each book
|
//BookPriceQuantity: How many items are required to pay for each book
|
||||||
//BookPriceType: Which item is used to pay for the books. AIR = use economy
|
//BookPriceType: Which item is used to pay for the books. AIR = use economy
|
||||||
Material bookCurrency = BooksWithoutBorders.getBookPriceType();
|
Material bookCurrency = BooksWithoutBordersConfig.getBookPriceType();
|
||||||
double cost = BooksWithoutBorders.getBookPriceQuantity() * numCopies;
|
double cost = BooksWithoutBordersConfig.getBookPriceQuantity() * numCopies;
|
||||||
int itemCost = (int) cost;
|
int itemCost = (int) cost;
|
||||||
|
|
||||||
if (bookCurrency == Material.AIR) {
|
if (bookCurrency == Material.AIR) {
|
||||||
return !EconomyHelper.payForBookPrintingEconomy(player, cost, numCopies);
|
return !EconomyHelper.payForBookPrintingEconomy(player, cost, numCopies);
|
||||||
|
} else {
|
||||||
|
if (bookCurrency == Material.WRITABLE_BOOK) {
|
||||||
|
//Writable books are treated as a special case to prevent WIP books from being used
|
||||||
|
return !takeWritableBookPayment(player, itemCost);
|
||||||
} else {
|
} else {
|
||||||
if (player.getInventory().contains(bookCurrency, itemCost)) {
|
if (player.getInventory().contains(bookCurrency, itemCost)) {
|
||||||
payForBookPrintingItem(player, itemCost);
|
payForBookPrintingItem(player, itemCost);
|
||||||
@ -84,6 +91,74 @@ public final class EconomyHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes a writable book payment, ignoring any books containing text
|
||||||
|
*
|
||||||
|
* @param player <p>The player to take payment from</p>
|
||||||
|
* @param itemCost <p>The number of writable books to pay</p>
|
||||||
|
* @return <p>True if the payment was successful</p>
|
||||||
|
*/
|
||||||
|
private static boolean takeWritableBookPayment(Player player, int itemCost) {
|
||||||
|
List<ItemStack> books = getPlayersEmptyBooks(player);
|
||||||
|
if (countItems(books) < itemCost) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, itemCost + " empty " + Material.WRITABLE_BOOK +
|
||||||
|
"(s) are required for this command!");
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
int clearedAmount = 0;
|
||||||
|
for (ItemStack itemStack : books) {
|
||||||
|
if (itemStack.getAmount() > itemCost) {
|
||||||
|
//If encountering an item stack with more than enough books, remove the necessary books
|
||||||
|
itemStack.setAmount(itemStack.getAmount() - itemCost);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
clearedAmount += itemStack.getAmount();
|
||||||
|
player.getInventory().removeItem(itemStack);
|
||||||
|
}
|
||||||
|
if (clearedAmount >= itemCost) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the total number of items contained in the given list of items
|
||||||
|
*
|
||||||
|
* @param items <p>The items to count</p>
|
||||||
|
* @return <p>The total number of items</p>
|
||||||
|
*/
|
||||||
|
private static int countItems(List<ItemStack> items) {
|
||||||
|
int totalItems = 0;
|
||||||
|
for (ItemStack itemStack : items) {
|
||||||
|
totalItems += itemStack.getAmount();
|
||||||
|
}
|
||||||
|
return totalItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all empty books in a player's inventory
|
||||||
|
*
|
||||||
|
* @param player <p>The player to get books for</p>
|
||||||
|
* @return <p>The empty books in the player's inventory</p>
|
||||||
|
*/
|
||||||
|
private static List<ItemStack> getPlayersEmptyBooks(Player player) {
|
||||||
|
List<ItemStack> validBooks = new ArrayList<>();
|
||||||
|
for (ItemStack itemStack : player.getInventory().getContents()) {
|
||||||
|
if (itemStack == null || itemStack.getType() != Material.WRITABLE_BOOK) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
BookMeta book = (BookMeta) itemStack.getItemMeta();
|
||||||
|
//Only accept empty books
|
||||||
|
if (book != null && (!book.hasPages() || (book.getPageCount() == 1 && book.getPage(1).trim().isEmpty()))) {
|
||||||
|
validBooks.add(itemStack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return validBooks;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses economy to take payment for printing a number of books
|
* Uses economy to take payment for printing a number of books
|
||||||
@ -96,8 +171,10 @@ public final class EconomyHelper {
|
|||||||
private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) {
|
private static boolean payForBookPrintingEconomy(Player player, double cost, int numCopies) {
|
||||||
if ((economy.getBalance(player) - cost) >= 0) {
|
if ((economy.getBalance(player) - cost) >= 0) {
|
||||||
economy.withdrawPlayer(player, cost);
|
economy.withdrawPlayer(player, cost);
|
||||||
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " + numCopies + " book(s)");
|
BooksWithoutBorders.sendSuccessMessage(player, economy.format(cost) + " withdrawn to create " +
|
||||||
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " + economy.format(economy.getBalance(player)));
|
numCopies + " book(s)");
|
||||||
|
BooksWithoutBorders.sendSuccessMessage(player, "New balance: " +
|
||||||
|
economy.format(economy.getBalance(player)));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!");
|
BooksWithoutBorders.sendErrorMessage(player, economy.format(cost) + " is required for this command!");
|
||||||
@ -116,7 +193,7 @@ public final class EconomyHelper {
|
|||||||
|
|
||||||
int clearedAmount = 0;
|
int clearedAmount = 0;
|
||||||
while (clearedAmount < itemCost) {
|
while (clearedAmount < itemCost) {
|
||||||
int firstItemIndex = playerInventory.first(BooksWithoutBorders.getBookPriceType());
|
int firstItemIndex = playerInventory.first(BooksWithoutBordersConfig.getBookPriceType());
|
||||||
ItemStack firstItem = playerInventory.getItem(firstItemIndex);
|
ItemStack firstItem = playerInventory.getItem(firstItemIndex);
|
||||||
|
|
||||||
if (Objects.requireNonNull(firstItem).getAmount() <= itemCost - clearedAmount) {
|
if (Objects.requireNonNull(firstItem).getAmount() <= itemCost - clearedAmount) {
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.GenenCrypt;
|
import net.knarcraft.bookswithoutborders.encryption.GenenCrypt;
|
||||||
import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
|
import net.knarcraft.bookswithoutborders.encryption.SubstitutionCipher;
|
||||||
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
import net.knarcraft.bookswithoutborders.state.EncryptionStyle;
|
||||||
import org.bukkit.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -16,8 +17,8 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getSlash;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixName;
|
||||||
|
|
||||||
@ -27,7 +28,6 @@ import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.fixN
|
|||||||
public final class EncryptionHelper {
|
public final class EncryptionHelper {
|
||||||
|
|
||||||
private EncryptionHelper() {
|
private EncryptionHelper() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,11 +198,8 @@ public final class EncryptionHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileName = (!bookMetadata.hasTitle()) ? "Untitled," + player.getName() : bookMetadata.getTitle() +
|
String fileName = "[" + key + "]" + BookHelper.getBookFile(bookMetadata, player, true);
|
||||||
BooksWithoutBorders.getTitleAuthorSeparator() + bookMetadata.getAuthor();
|
fileName = fixName(cleanString(fileName), false);
|
||||||
fileName = "[" + key + "]" + fileName;
|
|
||||||
fileName = cleanString(fileName);
|
|
||||||
fileName = fixName(fileName, false);
|
|
||||||
|
|
||||||
File file = new File(path + fileName + ".yml");
|
File file = new File(path + fileName + ".yml");
|
||||||
if (!file.isFile()) {
|
if (!file.isFile()) {
|
||||||
@ -263,12 +260,8 @@ public final class EncryptionHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Creates file
|
//Generate file name
|
||||||
String fileName = (!bookMetadata.hasTitle()) ? "Untitled," + player.getName() :
|
String fileName = BookHelper.getBookFile(bookMetadata, player, true);
|
||||||
bookMetadata.getTitle() + BooksWithoutBorders.getTitleAuthorSeparator() + bookMetadata.getAuthor();
|
|
||||||
|
|
||||||
fileName = cleanString(fileName);
|
|
||||||
fileName = fixName(fileName, false);
|
|
||||||
|
|
||||||
List<String> newLore = new ArrayList<>();
|
List<String> newLore = new ArrayList<>();
|
||||||
newLore.add(ChatColor.GRAY + "[" + groupName + " encrypted]");
|
newLore.add(ChatColor.GRAY + "[" + groupName + " encrypted]");
|
||||||
@ -281,7 +274,8 @@ public final class EncryptionHelper {
|
|||||||
bookMetadata.setLore(newLore);
|
bookMetadata.setLore(newLore);
|
||||||
|
|
||||||
//Save file
|
//Save file
|
||||||
File file = (BooksWithoutBorders.getUseYml()) ? new File(path + fileName + ".yml") : new File(path + fileName + ".txt");
|
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") :
|
||||||
|
new File(path + fileName + ".txt");
|
||||||
if (!file.isFile()) {
|
if (!file.isFile()) {
|
||||||
try {
|
try {
|
||||||
BookToFromTextHelper.bookToYml(path, fileName, bookMetadata);
|
BookToFromTextHelper.bookToYml(path, fileName, bookMetadata);
|
||||||
@ -305,15 +299,13 @@ public final class EncryptionHelper {
|
|||||||
*/
|
*/
|
||||||
private static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) {
|
private static Boolean saveEncryptedBook(Player player, BookMeta bookMetaData, String key) {
|
||||||
String path = getBookFolder() + "Encrypted" + getSlash();
|
String path = getBookFolder() + "Encrypted" + getSlash();
|
||||||
String fileName = (!bookMetaData.hasTitle()) ? "Untitled," + player.getName() :
|
|
||||||
bookMetaData.getTitle() + BooksWithoutBorders.getTitleAuthorSeparator() + bookMetaData.getAuthor();
|
|
||||||
|
|
||||||
fileName = "[" + key + "]" + fileName;
|
String fileName = "[" + key + "]" + BookHelper.getBookFile(bookMetaData, player, true);
|
||||||
fileName = cleanString(fileName);
|
fileName = fixName(cleanString(fileName), false);
|
||||||
fileName = fixName(fileName, false);
|
|
||||||
|
|
||||||
//cancels saving if file is already encrypted
|
//cancels saving if file is already encrypted
|
||||||
File file = (BooksWithoutBorders.getUseYml()) ? new File(path + fileName + ".yml") : new File(path + fileName + ".txt");
|
File file = (BooksWithoutBordersConfig.getUseYml()) ? new File(path + fileName + ".yml") :
|
||||||
|
new File(path + fileName + ".txt");
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,22 @@
|
|||||||
package net.knarcraft.bookswithoutborders.utility;
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||||
import org.bukkit.ChatColor;
|
import net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig;
|
||||||
|
import net.knarcraft.bookswithoutborders.state.BookDirectory;
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
import static net.knarcraft.bookswithoutborders.config.BooksWithoutBordersConfig.getBookFolder;
|
||||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getSlash;
|
|
||||||
import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.cleanString;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for dealing with files
|
* Helper class for dealing with files
|
||||||
@ -20,7 +24,6 @@ import static net.knarcraft.bookswithoutborders.utility.InputCleaningHelper.clea
|
|||||||
public final class FileHelper {
|
public final class FileHelper {
|
||||||
|
|
||||||
private FileHelper() {
|
private FileHelper() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,11 +98,9 @@ public final class FileHelper {
|
|||||||
* @return <p>A list of available files</p>
|
* @return <p>A list of available files</p>
|
||||||
*/
|
*/
|
||||||
public static List<String> listFiles(CommandSender sender, Boolean listPublic) {
|
public static List<String> listFiles(CommandSender sender, Boolean listPublic) {
|
||||||
File file;
|
File file = BookHelper.getBookDirectoryPath(listPublic ? BookDirectory.PUBLIC : BookDirectory.PLAYER, sender);
|
||||||
if (listPublic) {
|
if (file == null) {
|
||||||
file = new File(getBookFolder());
|
return new ArrayList<>();
|
||||||
} else {
|
|
||||||
file = new File(getBookFolder() + cleanString(sender.getName()) + getSlash());
|
|
||||||
}
|
}
|
||||||
return FileHelper.listFiles(sender, file);
|
return FileHelper.listFiles(sender, file);
|
||||||
}
|
}
|
||||||
@ -123,6 +124,9 @@ public final class FileHelper {
|
|||||||
*/
|
*/
|
||||||
public static void printFiles(CommandSender sender, List<String> fileList) {
|
public static void printFiles(CommandSender sender, List<String> fileList) {
|
||||||
BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:");
|
BooksWithoutBorders.sendSuccessMessage(sender, "Available Books:");
|
||||||
|
if (fileList == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int listSize = fileList.size();
|
int listSize = fileList.size();
|
||||||
for (int fileIndex = 0; fileIndex < listSize; fileIndex++) {
|
for (int fileIndex = 0; fileIndex < listSize; fileIndex++) {
|
||||||
sender.sendMessage(ChatColor.GRAY + "[" + (fileIndex + 1) + "] " + fileList.get(fileIndex));
|
sender.sendMessage(ChatColor.GRAY + "[" + (fileIndex + 1) + "] " + fileList.get(fileIndex));
|
||||||
@ -146,9 +150,20 @@ public final class FileHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (File foundFile : existingFiles) {
|
for (File foundFile : existingFiles) {
|
||||||
if (foundFile.isFile()) {
|
if (!foundFile.isFile()) {
|
||||||
fileList.add(foundFile.getName());
|
continue;
|
||||||
}
|
}
|
||||||
|
String fileName = foundFile.getName();
|
||||||
|
String separator = BooksWithoutBordersConfig.getTitleAuthorSeparator();
|
||||||
|
if (fileName.contains(separator)) {
|
||||||
|
//Convert the UUID into a username if necessary
|
||||||
|
String[] data = fileName.split(separator);
|
||||||
|
String extension = data[1].substring(data[1].length() - 4);
|
||||||
|
String userName = data[1].substring(0, data[1].length() - 4);
|
||||||
|
data[1] = BookHelper.authorFromUUID(userName) + extension;
|
||||||
|
fileList.add(String.join(separator, data));
|
||||||
|
}
|
||||||
|
fileList.add(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileList;
|
return fileList;
|
||||||
@ -171,4 +186,15 @@ public final class FileHelper {
|
|||||||
return foundDuplicates;
|
return foundDuplicates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a buffered reader given an input stream
|
||||||
|
*
|
||||||
|
* @param inputStream <p>The input stream to read</p>
|
||||||
|
* @return <p>A buffered reader reading the input stream</p>
|
||||||
|
*/
|
||||||
|
public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream) {
|
||||||
|
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||||
|
return new BufferedReader(inputStreamReader);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import java.util.List;
|
|||||||
public final class InputCleaningHelper {
|
public final class InputCleaningHelper {
|
||||||
|
|
||||||
private InputCleaningHelper() {
|
private InputCleaningHelper() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,7 +14,6 @@ import org.bukkit.inventory.meta.BookMeta;
|
|||||||
public final class InventoryHelper {
|
public final class InventoryHelper {
|
||||||
|
|
||||||
private InventoryHelper() {
|
private InventoryHelper() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,6 +51,31 @@ public final class InventoryHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs checks to validate that a player contains exactly one unwritten book
|
||||||
|
*
|
||||||
|
* @param player <p>The player to validate</p>
|
||||||
|
* @param noBookMessage <p>The message to display if the player is not holding a book</p>
|
||||||
|
* @param twoBooksMessage <p>The message to display if the player is holding one book in each hand</p>
|
||||||
|
* @return <p>False if the player is holding exactly one book</p>
|
||||||
|
*/
|
||||||
|
public static boolean notHoldingOneWritableBookCheck(Player player, String noBookMessage, String twoBooksMessage) {
|
||||||
|
BookHoldingState holdingState = getBookHoldingState(player);
|
||||||
|
|
||||||
|
if (holdingState == BookHoldingState.NONE || holdingState == BookHoldingState.SIGNED_BOTH_HANDS ||
|
||||||
|
holdingState == BookHoldingState.SIGNED_MAIN_HAND || holdingState == BookHoldingState.SIGNED_OFF_HAND) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, noBookMessage);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (holdingState == BookHoldingState.UNSIGNED_BOTH_HANDS) {
|
||||||
|
BooksWithoutBorders.sendErrorMessage(player, twoBooksMessage);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs checks to validate that a player contains exactly one written book
|
* Performs checks to validate that a player contains exactly one written book
|
||||||
*
|
*
|
||||||
@ -97,7 +121,7 @@ public final class InventoryHelper {
|
|||||||
if (state == BookHoldingState.SIGNED_BOTH_HANDS ||
|
if (state == BookHoldingState.SIGNED_BOTH_HANDS ||
|
||||||
state == BookHoldingState.UNSIGNED_BOTH_HANDS ||
|
state == BookHoldingState.UNSIGNED_BOTH_HANDS ||
|
||||||
state == BookHoldingState.NONE) {
|
state == BookHoldingState.NONE) {
|
||||||
return null;
|
return ItemSlot.NONE;
|
||||||
}
|
}
|
||||||
if (handMatters && typeMatters) {
|
if (handMatters && typeMatters) {
|
||||||
if (mainHand && mainHandItem.getType() == requiredMaterial) {
|
if (mainHand && mainHandItem.getType() == requiredMaterial) {
|
||||||
|
@ -9,7 +9,40 @@ import java.util.List;
|
|||||||
public final class TabCompletionHelper {
|
public final class TabCompletionHelper {
|
||||||
|
|
||||||
private TabCompletionHelper() {
|
private TabCompletionHelper() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds tab complete values that contain the typed text
|
||||||
|
*
|
||||||
|
* @param values <p>The values to filter</p>
|
||||||
|
* @param typedText <p>The text the player has started typing</p>
|
||||||
|
* @return <p>The given string values that contain the player's typed text</p>
|
||||||
|
*/
|
||||||
|
public static List<String> filterMatchingContains(List<String> values, String typedText) {
|
||||||
|
List<String> configValues = new ArrayList<>();
|
||||||
|
for (String value : values) {
|
||||||
|
if (value.toLowerCase().contains(typedText.toLowerCase())) {
|
||||||
|
configValues.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return configValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds tab complete values that match the start of the typed text
|
||||||
|
*
|
||||||
|
* @param values <p>The values to filter</p>
|
||||||
|
* @param typedText <p>The text the player has started typing</p>
|
||||||
|
* @return <p>The given string values that start with the player's typed text</p>
|
||||||
|
*/
|
||||||
|
public static List<String> filterMatchingStartsWith(List<String> values, String typedText) {
|
||||||
|
List<String> configValues = new ArrayList<>();
|
||||||
|
for (String value : values) {
|
||||||
|
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
|
||||||
|
configValues.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return configValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,92 @@
|
|||||||
|
package net.knarcraft.bookswithoutborders.utility;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The update checker is responsible for looking for new updates
|
||||||
|
*/
|
||||||
|
public final class UpdateChecker {
|
||||||
|
|
||||||
|
private final static String updateNotice = "A new update is available: %s (You are still on %s)";
|
||||||
|
|
||||||
|
private UpdateChecker() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if there's a new update available, and alerts the user if necessary
|
||||||
|
*/
|
||||||
|
public static void checkForUpdate(Plugin plugin, String apiResourceURL, Supplier<String> getVersionMethod,
|
||||||
|
Consumer<String> setVersionMethod) {
|
||||||
|
BukkitScheduler scheduler = plugin.getServer().getScheduler();
|
||||||
|
scheduler.runTaskAsynchronously(plugin, () -> UpdateChecker.queryAPI(plugin, apiResourceURL, getVersionMethod,
|
||||||
|
setVersionMethod));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the spigot API to check for a newer version, and informs the user
|
||||||
|
*/
|
||||||
|
private static void queryAPI(Plugin plugin, String APIResourceURL, Supplier<String> getVersionMethod,
|
||||||
|
Consumer<String> setVersionMethod) {
|
||||||
|
try {
|
||||||
|
InputStream inputStream = new URL(APIResourceURL).openStream();
|
||||||
|
BufferedReader reader = FileHelper.getBufferedReaderFromInputStream(inputStream);
|
||||||
|
//There should only be one line of output
|
||||||
|
String newVersion = reader.readLine();
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
String oldVersion = getVersionMethod.get();
|
||||||
|
//If there is a newer version, notify the user
|
||||||
|
if (isVersionHigher(oldVersion, newVersion)) {
|
||||||
|
plugin.getLogger().log(Level.INFO, getUpdateAvailableString(newVersion, oldVersion));
|
||||||
|
if (setVersionMethod != null) {
|
||||||
|
setVersionMethod.accept(newVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().log(Level.WARNING, "Unable to get newest version.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the string to display to a user to alert about a new update
|
||||||
|
*
|
||||||
|
* @param newVersion <p>The new available plugin version</p>
|
||||||
|
* @param oldVersion <p>The old (current) plugin version</p>
|
||||||
|
* @return <p>The string to display</p>
|
||||||
|
*/
|
||||||
|
public static String getUpdateAvailableString(String newVersion, String oldVersion) {
|
||||||
|
return String.format(updateNotice, newVersion, oldVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decides whether one version number is higher than another
|
||||||
|
*
|
||||||
|
* @param oldVersion <p>The old version to check</p>
|
||||||
|
* @param newVersion <p>The new version to check</p>
|
||||||
|
* @return <p>True if the new version is higher than the old one</p>
|
||||||
|
*/
|
||||||
|
public static boolean isVersionHigher(String oldVersion, String newVersion) {
|
||||||
|
String[] oldVersionParts = oldVersion.split("\\.");
|
||||||
|
String[] newVersionParts = newVersion.split("\\.");
|
||||||
|
int versionLength = Math.max(oldVersionParts.length, newVersionParts.length);
|
||||||
|
for (int i = 0; i < versionLength; i++) {
|
||||||
|
int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0;
|
||||||
|
int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0;
|
||||||
|
if (newVersionNumber != oldVersionNumber) {
|
||||||
|
return newVersionNumber > oldVersionNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
src/main/resources/config.yml
Normal file
33
src/main/resources/config.yml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
Options:
|
||||||
|
# Whether to use YAML for saved books instead of just storing them as text
|
||||||
|
Save_Books_in_Yaml_Format: true
|
||||||
|
# The maximum number of duplicates of a saved book allowed
|
||||||
|
Max_Number_of_Duplicates: 5
|
||||||
|
# The separator used to separate the book title and the book author
|
||||||
|
Title-Author_Separator: ","
|
||||||
|
# The separator used to denote a new line in the book/item lore
|
||||||
|
Lore_line_separator: "~"
|
||||||
|
# A list of books given to new players the first time they join the server
|
||||||
|
Books_for_new_players: [ ]
|
||||||
|
# An optional message displayed to new players the first time they join the server
|
||||||
|
Message_for_new_players: ""
|
||||||
|
# Price settings for book copying
|
||||||
|
Price_to_create_book:
|
||||||
|
# The item type used as currency for copying books. Use "Economy" to use money instead of items
|
||||||
|
Item_type: ""
|
||||||
|
# The quantity of currency required to pay for each book produced
|
||||||
|
Required_quantity: 0
|
||||||
|
# Whether any admin can decrypt any book regardless of the group it was encrypted for
|
||||||
|
Admin_Auto_Decrypt: false
|
||||||
|
# Whether to only allow the author of a book to create copies
|
||||||
|
Author_Only_Copy: false
|
||||||
|
# Whether to only allow the author of a book to unsign it
|
||||||
|
Author_Only_Unsign: false
|
||||||
|
# Whether to only allow saving a player's own books with /savebook
|
||||||
|
Author_Only_Save: false
|
||||||
|
# Whether to automatically format every book when it's signed
|
||||||
|
Format_Book_After_Signing: false
|
||||||
|
# Whether to display "COPY" or "COPY_OF_COPY" instead of "ORIGINAL" when a book is copied. This also uses the
|
||||||
|
# vanilla behavior where a copy of a copy cannot be copied further.
|
||||||
|
Change_Generation_On_Copy: false
|
||||||
|
|
@ -1,18 +1,26 @@
|
|||||||
name: BooksWithoutBorders
|
name: BooksWithoutBorders
|
||||||
version: '${project.version}'
|
version: '${project.version}'
|
||||||
main: net.knarcraft.bookswithoutborders.BooksWithoutBorders
|
main: net.knarcraft.bookswithoutborders.BooksWithoutBorders
|
||||||
api-version: 1.17
|
api-version: 1.18
|
||||||
prefix: Books Without Borders
|
prefix: Books Without Borders
|
||||||
authors: [ EpicKnarvik97, AkiraAkiba ]
|
authors: [ EpicKnarvik97, AkiraAkiba ]
|
||||||
description: A continuation of the original Books Without Borders
|
description: A continuation of the original Books Without Borders
|
||||||
softdepend: [ Vault ]
|
softdepend: [ Vault ]
|
||||||
website: ????
|
website: https://www.spigotmc.org/resources/books-without-borders-updated.96069/
|
||||||
dev-url: https://git.knarcraft.net/EpicKnarvik97/Books-Without-Borders
|
dev-url: https://git.knarcraft.net/EpicKnarvik97/Books-Without-Borders
|
||||||
commands:
|
commands:
|
||||||
bookswithoutborders:
|
bookswithoutborders:
|
||||||
description: Lists Books Without Borders's commands and uses.
|
description: Lists Books Without Borders's commands and uses.
|
||||||
aliases: [ bwb ]
|
aliases: [ bwb ]
|
||||||
usage: /<command>
|
usage: /<command>
|
||||||
|
decryptbook:
|
||||||
|
description: Decrypts the book the player is holding. "key" is required and MUST be IDENTICAL to the key used to encrypt held book
|
||||||
|
usage: /<command> <key>
|
||||||
|
permission: bookswithoutborders.decrypt
|
||||||
|
formatbook:
|
||||||
|
description: Replaces color/formatting codes in a written book with formatted text
|
||||||
|
usage: /<command>
|
||||||
|
permission: bookswithoutborders.format
|
||||||
givebook:
|
givebook:
|
||||||
description: Gives the selected player a book from your personal directory
|
description: Gives the selected player a book from your personal directory
|
||||||
usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
|
usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
|
||||||
@ -21,10 +29,6 @@ commands:
|
|||||||
description: Same as givebook, but uses books from the public directory
|
description: Same as givebook, but uses books from the public directory
|
||||||
usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
|
usage: /<command> <file name or number> <playername> [# of copies (num)] [signed (true/false)]
|
||||||
permission: bookswithoutborders.givepublic
|
permission: bookswithoutborders.givepublic
|
||||||
decryptbook:
|
|
||||||
description: Decrypts the book the player is holding. "key" is required and MUST be IDENTICAL to the key used to encrypt held book
|
|
||||||
usage: /<command> <key>
|
|
||||||
permission: bookswithoutborders.decrypt
|
|
||||||
groupencryptbook:
|
groupencryptbook:
|
||||||
description: Encrypts book so that only players with the bookswithoutborders.decrypt.<group name> permission may decrypt the book by holding and left clicking the book
|
description: Encrypts book so that only players with the bookswithoutborders.decrypt.<group name> permission may decrypt the book by holding and left clicking the book
|
||||||
usage: /<command> <group name> <key> [encryption style]
|
usage: /<command> <group name> <key> [encryption style]
|
||||||
@ -41,6 +45,10 @@ commands:
|
|||||||
description: Copies the book the player is holding
|
description: Copies the book the player is holding
|
||||||
usage: /<command> <# of copies>
|
usage: /<command> <# of copies>
|
||||||
permission: bookswithoutborders.copy
|
permission: bookswithoutborders.copy
|
||||||
|
clearbook:
|
||||||
|
description: Removes all text from the book the player is holding
|
||||||
|
usage: /<command>
|
||||||
|
permission: bookswithoutborders.clear
|
||||||
unsignbook:
|
unsignbook:
|
||||||
description: Un-signs the book the player is holding
|
description: Un-signs the book the player is holding
|
||||||
usage: /<command>
|
usage: /<command>
|
||||||
@ -49,12 +57,16 @@ commands:
|
|||||||
description: Encrypts the book the player is holding. "key" is required and can be any phrase or number excluding spaces. "style" is not required. Possible values are "DNA" or ""
|
description: Encrypts the book the player is holding. "key" is required and can be any phrase or number excluding spaces. "style" is not required. Possible values are "DNA" or ""
|
||||||
usage: /<command> <key> [encryption style]
|
usage: /<command> <key> [encryption style]
|
||||||
permission: bookswithoutborders.encrypt
|
permission: bookswithoutborders.encrypt
|
||||||
|
setbookgeneration:
|
||||||
|
description: Sets the generation of the held book
|
||||||
|
usage: /<command> <generation>
|
||||||
|
permission: bookswithoutborders.setgeneration
|
||||||
setbookprice:
|
setbookprice:
|
||||||
description: Sets the per-book-price to create a book via commands. If "Item", the item in the player's hand in the amount of [quantity] will be the price. If "Eco", a Vault based economy will be used for price. If neither <Item/Eco> or <quantity> are specified the current price to create books will be removed.
|
description: Sets the per-book-price to create a book via commands. If "Item", the item in the player's hand in the amount of [quantity] will be the price. If "Eco", a Vault based economy will be used for price. If neither <Item/Eco> or <quantity> are specified, the current price to create books will be removed.
|
||||||
usage: /<command> <item/eco> <quantity>
|
usage: /<command> <item/eco> <quantity>
|
||||||
permission: bookswithoutborders.setbookprice
|
permission: bookswithoutborders.setbookprice
|
||||||
setlore:
|
setlore:
|
||||||
description: Sets the lore of the item the player is holding. Insert the lore_line_separator character to force a new line ("~" by default).
|
description: Sets the lore of the item the player is holding. Insert the lore_line_separator character to force a new line ("~" by default)
|
||||||
usage: /<command> <new lore>
|
usage: /<command> <new lore>
|
||||||
permission: bookswithoutborders.setlore
|
permission: bookswithoutborders.setlore
|
||||||
savepublicbook:
|
savepublicbook:
|
||||||
@ -74,7 +86,7 @@ commands:
|
|||||||
usage: /<command> <title>
|
usage: /<command> <title>
|
||||||
permission: bookswithoutborders.settitle
|
permission: bookswithoutborders.settitle
|
||||||
loadbook:
|
loadbook:
|
||||||
description: Creates a book from the specified file and gives it to the player. If no file is specified, a list of available files is returned. If true is specified the book will be signed, if false it will be unsigned
|
description: Creates a book from the specified file and gives it to the player. If no file is specified, a list of available files is returned. If true is specified, the book will be signed, if false it will be unsigned
|
||||||
usage: /<command> <file name or number> [# of copies] [signed (true/false)]
|
usage: /<command> <file name or number> [# of copies] [signed (true/false)]
|
||||||
permission: bookswithoutborders.load
|
permission: bookswithoutborders.load
|
||||||
loadpublicbook:
|
loadpublicbook:
|
||||||
@ -84,7 +96,7 @@ commands:
|
|||||||
reload:
|
reload:
|
||||||
description: Reloads BwB's configuration file
|
description: Reloads BwB's configuration file
|
||||||
usage: /<command>
|
usage: /<command>
|
||||||
permission: bookswithoutborders.admin
|
permission: bookswithoutborders.reload
|
||||||
permissions:
|
permissions:
|
||||||
bookswithoutborders.*:
|
bookswithoutborders.*:
|
||||||
description: Grants all permissions
|
description: Grants all permissions
|
||||||
@ -96,28 +108,41 @@ permissions:
|
|||||||
default: op
|
default: op
|
||||||
children:
|
children:
|
||||||
bookswithoutborders.use: true
|
bookswithoutborders.use: true
|
||||||
bookswithoutborders.unsign: true
|
bookswithoutborders.alterbooks: true
|
||||||
bookswithoutborders.copy: true
|
bookswithoutborders.copy: true
|
||||||
bookswithoutborders.loadpublic: true
|
bookswithoutborders.loadpublic: true
|
||||||
bookswithoutborders.savepublic: true
|
bookswithoutborders.savepublic: true
|
||||||
bookswithoutborders.encrypt: true
|
bookswithoutborders.encrypt: true
|
||||||
bookswithoutborders.decrypt: true
|
bookswithoutborders.decrypt: true
|
||||||
|
bookswithoutborders.groupencrypt: true
|
||||||
bookswithoutborders.signs: true
|
bookswithoutborders.signs: true
|
||||||
bookswithoutborders.give: true
|
bookswithoutborders.give: true
|
||||||
bookswithoutborders.givepublic: true
|
bookswithoutborders.givepublic: true
|
||||||
bookswithoutborders.settitle: true
|
|
||||||
bookswithoutborders.setauthor: true
|
|
||||||
bookswithoutborders.setlore: true
|
|
||||||
bookswithoutborders.bypassauthoronlycopy: true
|
bookswithoutborders.bypassauthoronlycopy: true
|
||||||
|
bookswithoutborders.bypassauthoronlyunsign: true
|
||||||
|
bookswithoutborders.bypassauthoronlysave: true
|
||||||
bookswithoutborders.bypassbookprice: true
|
bookswithoutborders.bypassbookprice: true
|
||||||
bookswithoutborders.groupencrypt: true
|
|
||||||
bookswithoutborders.setbookprice: true
|
bookswithoutborders.setbookprice: true
|
||||||
|
bookswithoutborders.reload: true
|
||||||
|
bookswithoutborders.setgeneration: true
|
||||||
bookswithoutborders.use:
|
bookswithoutborders.use:
|
||||||
description: Allows player to use commands and to save/load/delete in their personal directory
|
description: Allows player to use commands to save/load/delete in their personal directory
|
||||||
children:
|
children:
|
||||||
bookswithoutborders.save: true
|
bookswithoutborders.save: true
|
||||||
bookswithoutborders.load: true
|
bookswithoutborders.load: true
|
||||||
bookswithoutborders.delete: true
|
bookswithoutborders.delete: true
|
||||||
|
bookswithoutborders.alterbooks:
|
||||||
|
description: Allows player to change books' data such as lore/title/author/generation/formatting and unsigning books
|
||||||
|
children:
|
||||||
|
bookswithoutborders.clear: true
|
||||||
|
bookswithoutborders.unsign: true
|
||||||
|
bookswithoutborders.settitle: true
|
||||||
|
bookswithoutborders.setauthor: true
|
||||||
|
bookswithoutborders.setlore: true
|
||||||
|
bookswithoutborders.format: true
|
||||||
|
bookswithoutborders.setgeneration: true
|
||||||
|
bookswithoutborders.format:
|
||||||
|
description: Allows a player to format a book
|
||||||
bookswithoutborders.save:
|
bookswithoutborders.save:
|
||||||
description: Allows player to save books to their personal directory
|
description: Allows player to save books to their personal directory
|
||||||
bookswithoutborders.load:
|
bookswithoutborders.load:
|
||||||
@ -125,17 +150,19 @@ permissions:
|
|||||||
bookswithoutborders.delete:
|
bookswithoutborders.delete:
|
||||||
description: Allows player to delete books from their personal directory
|
description: Allows player to delete books from their personal directory
|
||||||
bookswithoutborders.unsign:
|
bookswithoutborders.unsign:
|
||||||
description: Allows player to use unsign command
|
description: Allows player to use the unsign command
|
||||||
bookswithoutborders.copy:
|
bookswithoutborders.copy:
|
||||||
description: Allows player to use copy command
|
description: Allows player to use the copy command
|
||||||
|
bookswithoutborders.clear:
|
||||||
|
description: Allows player to use the clear command
|
||||||
bookswithoutborders.loadpublic:
|
bookswithoutborders.loadpublic:
|
||||||
description: Allows player to load in the public directory
|
description: Allows player to load from the public directory
|
||||||
bookswithoutborders.savepublic:
|
bookswithoutborders.savepublic:
|
||||||
description: Allows player to save in the public directory
|
description: Allows player to save to the public directory
|
||||||
bookswithoutborders.encrypt:
|
bookswithoutborders.encrypt:
|
||||||
description: Allows player to encrypt books
|
description: Allows player to encrypt books
|
||||||
bookswithoutborders.groupencrypt:
|
bookswithoutborders.groupencrypt:
|
||||||
description: Allows player to set group based encryption
|
description: Allows player to use group-based encryption
|
||||||
bookswithoutborders.decrypt:
|
bookswithoutborders.decrypt:
|
||||||
description: Allows player to decrypt books
|
description: Allows player to decrypt books
|
||||||
bookswithoutborders.signs:
|
bookswithoutborders.signs:
|
||||||
@ -152,7 +179,15 @@ permissions:
|
|||||||
description: Allows player to set the lore of the currently held item
|
description: Allows player to set the lore of the currently held item
|
||||||
bookswithoutborders.bypassauthoronlycopy:
|
bookswithoutborders.bypassauthoronlycopy:
|
||||||
description: Allows player to ignore Author_Only_Copy config setting
|
description: Allows player to ignore Author_Only_Copy config setting
|
||||||
|
bookswithoutborders.bypassauthoronlyunsign:
|
||||||
|
description: Allows player to ignore Author_Only_Unsign config setting
|
||||||
|
bookswithoutborders.bypassauthoronlysave:
|
||||||
|
description: Allows player to ignore Author_Only_Save config setting
|
||||||
bookswithoutborders.bypassbookprice:
|
bookswithoutborders.bypassbookprice:
|
||||||
description: Allows player to ignore Price_to_create_book config setting
|
description: Allows player to ignore Price_to_create_book config setting
|
||||||
bookswithoutborders.setbookprice:
|
bookswithoutborders.setbookprice:
|
||||||
description: Allows player to set the cost of creating a book
|
description: Allows player to set the cost of creating a book
|
||||||
|
bookswithoutborders.reload:
|
||||||
|
description: Allows player to reload this plugin
|
||||||
|
bookswithoutborders.setgeneration:
|
||||||
|
description: Allows player to change the generation of a book (Original, Copy, Copy of Copy)
|
@ -1,9 +1,9 @@
|
|||||||
package net.knarcraft.bookswithoutborders.encryption;
|
package net.knarcraft.bookswithoutborders.encryption;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
|
|
||||||
public class AESTest {
|
public class AESTest {
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ public class AESTest {
|
|||||||
AES aes = new AES(AES.generateIV(), AES.generateIV());
|
AES aes = new AES(AES.generateIV(), AES.generateIV());
|
||||||
|
|
||||||
String encrypted = aes.encryptDecryptText(plainText, password, true);
|
String encrypted = aes.encryptDecryptText(plainText, password, true);
|
||||||
assertFalse(encrypted.equals(plainText));
|
Assert.assertNotSame(encrypted, plainText);
|
||||||
String decrypted = aes.encryptDecryptText(encrypted, password, false);
|
String decrypted = aes.encryptDecryptText(encrypted, password, false);
|
||||||
assertEquals(plainText, decrypted);
|
assertEquals(plainText, decrypted);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user