Gets the books without borders instance without sending it in the constuctor
This commit is contained in:
@@ -19,11 +19,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
|
||||
*/
|
||||
public class CommandBooksWithoutBorders implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandBooksWithoutBorders(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -17,11 +17,7 @@ import java.util.Objects;
|
||||
*/
|
||||
public class CommandCopy implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandCopy(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -18,11 +18,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
|
||||
*/
|
||||
public class CommandDelete implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandDelete(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public class CommandDeletePublic extends CommandDelete implements CommandExecutor {
|
||||
|
||||
public CommandDeletePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
super(booksWithoutBorders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return deleteBook(sender, args, true);
|
||||
|
@@ -13,11 +13,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
*/
|
||||
public class CommandGive implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandGive(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public class CommandGivePublic extends CommandGive implements CommandExecutor {
|
||||
|
||||
public CommandGivePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
super(booksWithoutBorders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return giveBook(sender, args, true, "public");
|
||||
|
@@ -13,11 +13,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
*/
|
||||
public class CommandLoad implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandLoad(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public class CommandLoadPublic extends CommandLoad implements CommandExecutor {
|
||||
|
||||
public CommandLoadPublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
super(booksWithoutBorders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return loadBook(sender, args, "public", true);
|
||||
|
@@ -10,11 +10,7 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public class CommandReload implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandReload(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -2,24 +2,33 @@ package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import net.knarcraft.bookswithoutborders.state.ItemSlot;
|
||||
import net.knarcraft.bookswithoutborders.utility.BookToFromTextHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.FileHelper;
|
||||
import net.knarcraft.bookswithoutborders.utility.InventoryHelper;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
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.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBorders.titleAuthorSeparator;
|
||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
|
||||
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getCommandColor;
|
||||
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
|
||||
*/
|
||||
public class CommandSave implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandSave(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return saveHeldBook(sender, args, false);
|
||||
@@ -43,11 +52,99 @@ public class CommandSave implements CommandExecutor {
|
||||
if (holdingSlot != ItemSlot.NONE) {
|
||||
ItemStack holdingItem = InventoryHelper.getHeldItem(player, holdingSlot == ItemSlot.MAIN_HAND);
|
||||
boolean duplicate = args.length == 1 && Boolean.parseBoolean(args[0]);
|
||||
booksWithoutBorders.saveBook(player, holdingItem, duplicate, savePublic);
|
||||
saveBook(player, holdingItem, duplicate, savePublic);
|
||||
return true;
|
||||
} else {
|
||||
BooksWithoutBorders.sendErrorMessage(sender, "You must be holding a written book or book and quill to save it!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a book to a file
|
||||
*
|
||||
* @param player <p>The player holding the book</p>
|
||||
* @param heldBook <p>The book held</p>
|
||||
* @param overwrite <p>Whether to overwrite any existing books</p>
|
||||
* @param saveToPublicFolder <p>Whether to save the book to the public folder instead of the player folder</p>
|
||||
*/
|
||||
public void saveBook(Player player, ItemStack heldBook, boolean overwrite, boolean saveToPublicFolder) {
|
||||
BookMeta book = (BookMeta) heldBook.getItemMeta();
|
||||
if (book == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Unable to get metadata for your held book!");
|
||||
return;
|
||||
}
|
||||
|
||||
String savePath;
|
||||
if (saveToPublicFolder) {
|
||||
savePath = getBookFolder();
|
||||
} else {
|
||||
savePath = getBookFolder() + cleanString(player.getName()) + getSlash();
|
||||
}
|
||||
|
||||
//Generate book filename
|
||||
String fileName;
|
||||
if (!book.hasTitle()) {
|
||||
fileName = "Untitled," + player.getName();
|
||||
} else {
|
||||
fileName = book.getTitle() + titleAuthorSeparator + book.getAuthor();
|
||||
}
|
||||
fileName = cleanString(fileName);
|
||||
fileName = fixName(fileName, false);
|
||||
|
||||
//Make sure the used folders exist
|
||||
File file = new File(savePath);
|
||||
if (!file.exists() && !file.mkdir()) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!");
|
||||
return;
|
||||
}
|
||||
File[] foundFiles = file.listFiles();
|
||||
if (foundFiles == null) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Saving Failed! If this continues to happen, consult server admin!");
|
||||
return;
|
||||
}
|
||||
|
||||
BooksWithoutBorders.sendErrorMessage(player, String.valueOf(overwrite));
|
||||
BooksWithoutBorders.sendErrorMessage(player, fileName);
|
||||
BooksWithoutBorders.sendErrorMessage(player, Arrays.toString(foundFiles));
|
||||
|
||||
//Find any duplicates of the book
|
||||
int foundDuplicates = FileHelper.findDuplicates(foundFiles, fileName);
|
||||
|
||||
//Deal with duplicates
|
||||
if (foundDuplicates > 0) {
|
||||
//TODO: Decide if this makes sense or needs to be changed
|
||||
//Skip duplicate book
|
||||
if (!fileName.contains("Untitled") && !overwrite) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Book is already saved!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!");
|
||||
return;
|
||||
}
|
||||
|
||||
//Skip if duplicate limit is reached
|
||||
if (foundDuplicates > BooksWithoutBorders.bookDuplicateLimit) {
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Maximum amount of " + fileName + " duplicates reached!");
|
||||
BooksWithoutBorders.sendErrorMessage(player, "Use " + getCommandColor() + "/bwb Save true " + getErrorColor() + "to overwrite!");
|
||||
return;
|
||||
}
|
||||
|
||||
//Alter duplicated filename
|
||||
if (fileName.contains("Untitled") && !overwrite) {
|
||||
fileName = "(" + foundDuplicates + ")" + fileName;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (BooksWithoutBorders.useYml) {
|
||||
BookToFromTextHelper.bookToYml(savePath, fileName, book);
|
||||
} else {
|
||||
BookToFromTextHelper.bookToTXT(savePath, fileName, book);
|
||||
}
|
||||
|
||||
BooksWithoutBorders.sendSuccessMessage(player, "Book Saved as \"" + fileName + "\"");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package net.knarcraft.bookswithoutborders.command;
|
||||
|
||||
import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -10,10 +9,6 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public class CommandSavePublic extends CommandSave implements CommandExecutor {
|
||||
|
||||
public CommandSavePublic(BooksWithoutBorders booksWithoutBorders) {
|
||||
super(booksWithoutBorders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return saveHeldBook(sender, args, true);
|
||||
|
@@ -14,11 +14,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
*/
|
||||
public class CommandSetBookPrice implements CommandExecutor {
|
||||
|
||||
private final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public CommandSetBookPrice(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
@@ -16,11 +16,7 @@ import java.util.List;
|
||||
*/
|
||||
public class GiveTabCompleter implements TabCompleter {
|
||||
|
||||
final BooksWithoutBorders booksWithoutBorders;
|
||||
|
||||
public GiveTabCompleter(BooksWithoutBorders booksWithoutBorders) {
|
||||
this.booksWithoutBorders = booksWithoutBorders;
|
||||
}
|
||||
private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance();
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
|
Reference in New Issue
Block a user