summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandbook.java
blob: 4c7b42f92724e8813b1e0e3cc58bdbff421fe6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package net.ess3.commands;

import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;




public class Commandbook extends EssentialsCommand
{
    //TODO: Translate this
    @Override
    public void run( final IUser user, final String commandLabel, final String[] args) throws Exception
    {

        final Player player = user.getPlayer();
        final ItemStack item = player.getItemInHand();
        if (item.getType() == Material.WRITTEN_BOOK)
        {
            BookMeta bmeta = (BookMeta)item.getItemMeta();

            if (args.length > 1 && args[0].equalsIgnoreCase("author"))
            {
                if (Permissions.BOOK_AUTHOR.isAuthorized(user) && (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user)))
                {
                    bmeta.setAuthor(args[1]);
                    item.setItemMeta(bmeta);
                    user.sendMessage(_("Author of the book set to {0}.", getFinalArg(args, 1)));
                }
                else
                {
                    throw new Exception(_("You cannot change the author of this book."));
                }
            }
            else if (args.length > 1 && args[0].equalsIgnoreCase("title"))
            {
                if (Permissions.BOOK_TITLE.isAuthorized(user) && (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user)))
                {
                    bmeta.setTitle(args[1]);
                    item.setItemMeta(bmeta);
                    user.sendMessage(_("Title of the book set to {0}.", getFinalArg(args, 1)));
                }
                else
                {
                    throw new Exception(_("You cannot change the title of this book."));
                }
            }
            else
            {
                if (isAuthor(bmeta, player.getName()) || Permissions.BOOK_OTHERS.isAuthorized(user))
                {
                    ItemStack newItem = new ItemStack(Material.BOOK_AND_QUILL, item.getAmount());
                    newItem.setItemMeta(bmeta);
                    user.getPlayer().setItemInHand(newItem);
                    user.sendMessage(_("You may now edit the contents of this book."));
                }
                else
                {
                    throw new Exception(_("You cannot unlock this book."));
                }
            }
        }
        else if (item.getType() == Material.BOOK_AND_QUILL)
        {
            BookMeta bmeta = (BookMeta)item.getItemMeta();
            if (!Permissions.BOOK_AUTHOR.isAuthorized(user))
            {
                bmeta.setAuthor(player.getName());
            }
            ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount());
            newItem.setItemMeta(bmeta);
            player.setItemInHand(newItem);
            user.sendMessage(_("This book is now locked."));
        }
        else
        {
            throw new Exception(_("You are not holding a writable book."));
        }
    }

    private boolean isAuthor(BookMeta bmeta, String player)
    {
        String author = bmeta.getAuthor();
        return author != null && author.equalsIgnoreCase(player);
    }
}