2013-01-13 04:44:28 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.User;
|
2016-11-28 23:13:36 +00:00
|
|
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
2018-12-31 12:53:23 +00:00
|
|
|
import com.earth2me.essentials.utils.EnumUtil;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2013-01-13 04:44:28 +00:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-01-13 22:08:46 +00:00
|
|
|
import org.bukkit.inventory.meta.BookMeta;
|
2013-01-13 04:44:28 +00:00
|
|
|
|
2017-06-11 00:17:43 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
public class Commandbook extends EssentialsCommand {
|
2018-12-31 12:53:23 +00:00
|
|
|
|
|
|
|
private static final Material WRITABLE_BOOK = EnumUtil.getMaterial("WRITABLE_BOOK", "BOOK_AND_QUILL");
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public Commandbook() {
|
|
|
|
super("book");
|
|
|
|
}
|
2013-01-13 04:44:28 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
2016-11-20 20:23:01 +00:00
|
|
|
final ItemStack item = user.getItemInHand();
|
2015-04-15 04:06:16 +00:00
|
|
|
final String player = user.getName();
|
|
|
|
if (item.getType() == Material.WRITTEN_BOOK) {
|
2020-10-03 17:46:05 +00:00
|
|
|
final BookMeta bmeta = (BookMeta) item.getItemMeta();
|
2013-01-13 23:10:14 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (args.length > 1 && args[0].equalsIgnoreCase("author")) {
|
|
|
|
if (user.isAuthorized("essentials.book.author") && (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others"))) {
|
|
|
|
bmeta.setAuthor(args[1]);
|
|
|
|
item.setItemMeta(bmeta);
|
|
|
|
user.sendMessage(tl("bookAuthorSet", getFinalArg(args, 1)));
|
|
|
|
} else {
|
|
|
|
throw new Exception(tl("denyChangeAuthor"));
|
|
|
|
}
|
|
|
|
} else if (args.length > 1 && args[0].equalsIgnoreCase("title")) {
|
|
|
|
if (user.isAuthorized("essentials.book.title") && (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others"))) {
|
|
|
|
bmeta.setTitle(args[1]);
|
|
|
|
item.setItemMeta(bmeta);
|
|
|
|
user.sendMessage(tl("bookTitleSet", getFinalArg(args, 1)));
|
|
|
|
} else {
|
|
|
|
throw new Exception(tl("denyChangeTitle"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others")) {
|
2020-10-03 17:46:05 +00:00
|
|
|
final ItemStack newItem = new ItemStack(WRITABLE_BOOK, item.getAmount());
|
2015-04-15 04:06:16 +00:00
|
|
|
newItem.setItemMeta(bmeta);
|
2016-11-28 23:13:36 +00:00
|
|
|
InventoryWorkaround.setItemInMainHand(user.getBase(), newItem);
|
2015-04-15 04:06:16 +00:00
|
|
|
user.sendMessage(tl("editBookContents"));
|
|
|
|
} else {
|
|
|
|
throw new Exception(tl("denyBookEdit"));
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 12:53:23 +00:00
|
|
|
} else if (item.getType() == WRITABLE_BOOK) {
|
2020-10-03 17:46:05 +00:00
|
|
|
final BookMeta bmeta = (BookMeta) item.getItemMeta();
|
2015-04-15 04:06:16 +00:00
|
|
|
if (!user.isAuthorized("essentials.book.author")) {
|
|
|
|
bmeta.setAuthor(player);
|
|
|
|
}
|
2020-10-03 17:46:05 +00:00
|
|
|
final ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount());
|
2015-04-15 04:06:16 +00:00
|
|
|
newItem.setItemMeta(bmeta);
|
2016-11-28 23:13:36 +00:00
|
|
|
InventoryWorkaround.setItemInMainHand(user.getBase(), newItem);
|
2015-04-15 04:06:16 +00:00
|
|
|
user.sendMessage(tl("bookLocked"));
|
|
|
|
} else {
|
|
|
|
throw new Exception(tl("holdBook"));
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 22:08:46 +00:00
|
|
|
|
2020-10-03 17:46:05 +00:00
|
|
|
private boolean isAuthor(final BookMeta bmeta, final String player) {
|
|
|
|
final String author = bmeta.getAuthor();
|
2015-04-15 04:06:16 +00:00
|
|
|
return author != null && author.equalsIgnoreCase(player);
|
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
|
|
|
|
@Override
|
2020-10-03 17:46:05 +00:00
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
2017-06-11 00:17:43 +00:00
|
|
|
// Right now, we aren't testing what's held in the player's hand - we could, but it's not necessarily worth it
|
|
|
|
if (args.length == 1) {
|
2020-10-03 17:46:05 +00:00
|
|
|
final List<String> options = Lists.newArrayList("sign", "unsign"); // sign and unsign aren't real, but work
|
2017-06-11 00:17:43 +00:00
|
|
|
if (user.isAuthorized("essentials.book.author")) {
|
|
|
|
options.add("author");
|
|
|
|
}
|
|
|
|
if (user.isAuthorized("essentials.book.title")) {
|
|
|
|
options.add("title");
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
} else if (args.length == 2 && args[0].equalsIgnoreCase("author") && user.isAuthorized("essentials.book.author")) {
|
2020-10-03 17:46:05 +00:00
|
|
|
final List<String> options = getPlayers(server, user);
|
2017-06-11 00:17:43 +00:00
|
|
|
options.add("Herobrine"); // #EasterEgg
|
|
|
|
return options;
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 17:57:30 +00:00
|
|
|
}
|