Book automatically open when first joining the server

This commit is contained in:
Quentin Legot 2020-05-21 19:25:06 +02:00
parent 4235f9d5b3
commit 9fe09ceebf
4 changed files with 91 additions and 34 deletions

View File

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

View File

@ -1,21 +1,26 @@
package fr.topeka.HelpRulesBook; package fr.topeka.HelpRulesBook;
import java.util.List; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import fr.topeka.HelpRulesBook.commands.CommandNew; import fr.topeka.HelpRulesBook.commands.CommandNew;
import fr.topeka.HelpRulesBook.listener.PlayerJoinListener;
public class Main extends JavaPlugin{ public class Main extends JavaPlugin{
public String _title, _author; public Book _book;
public List<String> _pages;
@Override @Override
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
loadConfig(); _book = new Book(this);
_book.loadConfig();
if(!_book.createBook()) {
getServer().getPluginManager().disablePlugin(this);
}
getCommand("new").setExecutor(new CommandNew(this)); getCommand("new").setExecutor(new CommandNew(this));
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new PlayerJoinListener(this), this);
} }
@Override @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");
}
} }

View File

@ -1,12 +1,9 @@
package fr.topeka.HelpRulesBook.commands; package fr.topeka.HelpRulesBook.commands;
import org.bukkit.Material;
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.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import fr.topeka.HelpRulesBook.Main; import fr.topeka.HelpRulesBook.Main;
@ -23,26 +20,19 @@ public class CommandNew implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) {
if(args.length == 1 && args[0].toUpperCase().equals("RELOAD") && sender.hasPermission("HelpRulesBook.admin")) { if(args.length == 1 && args[0].toUpperCase().equals("RELOAD") && sender.hasPermission("HelpRulesBook.admin")) {
main.reloadConfig(); main.reloadConfig();
main.loadConfig(); main._book.loadConfig();
if(main._book.createBook()) {
sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Plugin reloaded"); sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Plugin reloaded");
return true; return true;
}else { }else {
if(sender instanceof Player) { sender.sendMessage("§0[§aHelp-Rules-Book§0]§f Error during configuration reload");
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<main._pages.size();i++) {
meta.addPage(main._pages.get(i));
}
if(book.setItemMeta(meta)) {
player.openBook(book);
return true;
}else {
player.sendMessage("§0[§aHelp-Rules-Book§0]§f An error occured");
return false; return false;
} }
}else {
if(sender instanceof Player) {
Player player = (Player) sender;
main._book.openBook(player);
return true;
} }
sender.sendMessage("§0[§aHelp-Rules-Book§0]§f You need to be a player to view your tutorial"); sender.sendMessage("§0[§aHelp-Rules-Book§0]§f You need to be a player to view your tutorial");
return false; return false;

View File

@ -0,0 +1,26 @@
package fr.topeka.HelpRulesBook.listener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import fr.topeka.HelpRulesBook.Main;
public class PlayerJoinListener implements Listener {
private Main main;
public PlayerJoinListener(Main main) {
this.main = main;
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if(!player.hasPlayedBefore()) {
main._book.openBook(player);
}
}
}