From 9fe09ceebfd56f69a1a857da6468c626d4b18c97 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 21 May 2020 19:25:06 +0200 Subject: [PATCH] Book automatically open when first joining the server --- src/fr/topeka/HelpRulesBook/Book.java | 43 +++++++++++++++++++ src/fr/topeka/HelpRulesBook/Main.java | 22 +++++----- .../HelpRulesBook/commands/CommandNew.java | 34 ++++++--------- .../listener/PlayerJoinListener.java | 26 +++++++++++ 4 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 src/fr/topeka/HelpRulesBook/Book.java create mode 100644 src/fr/topeka/HelpRulesBook/listener/PlayerJoinListener.java diff --git a/src/fr/topeka/HelpRulesBook/Book.java b/src/fr/topeka/HelpRulesBook/Book.java new file mode 100644 index 0000000..659907b --- /dev/null +++ b/src/fr/topeka/HelpRulesBook/Book.java @@ -0,0 +1,43 @@ +package fr.topeka.HelpRulesBook; + +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; + +public class Book { + + private ItemStack book; + private Main main; + public String _title, _author; + public List _pages; + + public Book(Main main) { + this.main = main; + } + + public boolean createBook() { + book = new ItemStack(Material.WRITTEN_BOOK, 1); + BookMeta meta = (BookMeta) book.getItemMeta(); + meta.setTitle(_title); + meta.setAuthor(_author); + for(int i=0;i<_pages.size();i++) + meta.addPage(_pages.get(i)); + if(book.setItemMeta(meta)) + return true; + return false; + } + + public void loadConfig() { + _title = main.getConfig().getString("book.title"); + _author = main.getConfig().getString("book.author"); + _pages = main.getConfig().getStringList("book.pages"); + } + + public void openBook(Player player) { + player.openBook(book); + } + +} diff --git a/src/fr/topeka/HelpRulesBook/Main.java b/src/fr/topeka/HelpRulesBook/Main.java index 688ea02..7e5d160 100644 --- a/src/fr/topeka/HelpRulesBook/Main.java +++ b/src/fr/topeka/HelpRulesBook/Main.java @@ -1,21 +1,26 @@ package fr.topeka.HelpRulesBook; -import java.util.List; - +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import fr.topeka.HelpRulesBook.commands.CommandNew; +import fr.topeka.HelpRulesBook.listener.PlayerJoinListener; public class Main extends JavaPlugin{ - public String _title, _author; - public List _pages; + public Book _book; @Override public void onEnable() { saveDefaultConfig(); - loadConfig(); + _book = new Book(this); + _book.loadConfig(); + if(!_book.createBook()) { + getServer().getPluginManager().disablePlugin(this); + } getCommand("new").setExecutor(new CommandNew(this)); + PluginManager pm = getServer().getPluginManager(); + pm.registerEvents(new PlayerJoinListener(this), this); } @Override @@ -23,11 +28,4 @@ public class Main extends JavaPlugin{ } - - public void loadConfig() { - this._title = getConfig().getString("book.title"); - this._author = getConfig().getString("book.author"); - this._pages = getConfig().getStringList("book.pages"); - } - } diff --git a/src/fr/topeka/HelpRulesBook/commands/CommandNew.java b/src/fr/topeka/HelpRulesBook/commands/CommandNew.java index fa7c8cf..3f6c441 100644 --- a/src/fr/topeka/HelpRulesBook/commands/CommandNew.java +++ b/src/fr/topeka/HelpRulesBook/commands/CommandNew.java @@ -1,12 +1,9 @@ package fr.topeka.HelpRulesBook.commands; -import org.bukkit.Material; 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 fr.topeka.HelpRulesBook.Main; @@ -23,29 +20,22 @@ public class CommandNew implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) { if(args.length == 1 && args[0].toUpperCase().equals("RELOAD") && sender.hasPermission("HelpRulesBook.admin")) { main.reloadConfig(); - main.loadConfig(); - sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Plugin reloaded"); - return true; + main._book.loadConfig(); + if(main._book.createBook()) { + sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Plugin reloaded"); + return true; + }else { + sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Error during configuration reload"); + return false; + } }else { if(sender instanceof Player) { Player player = (Player) sender; - ItemStack book = new ItemStack(Material.WRITTEN_BOOK, 1); - BookMeta meta = (BookMeta) book.getItemMeta(); - meta.setTitle(main._title); - meta.setAuthor(main._author); - for(int i=0;i