版本更新至:3.76
调整:开发框架改为 Gradle 新增:Language2 工具新增 [book] 类型
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package me.skymc.taboolib.bookformatter;
|
||||
|
||||
import org.bukkit.Achievement;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.bukkit.Achievement.*;
|
||||
|
||||
public final class BookAchievement {
|
||||
|
||||
private static final HashMap<Achievement, String> achievements = new HashMap<>();
|
||||
|
||||
static {
|
||||
achievements.put(OPEN_INVENTORY, "openInventory");
|
||||
achievements.put(MINE_WOOD, "mineWood");
|
||||
achievements.put(BUILD_WORKBENCH, "buildWorkBench");
|
||||
achievements.put(BUILD_PICKAXE, "buildPickaxe");
|
||||
achievements.put(BUILD_FURNACE, "buildFurnace");
|
||||
achievements.put(ACQUIRE_IRON, "aquireIron");
|
||||
achievements.put(BUILD_HOE, "buildHoe");
|
||||
achievements.put(MAKE_BREAD, "makeBread");
|
||||
achievements.put(BAKE_CAKE,"bakeCake");
|
||||
achievements.put(BUILD_BETTER_PICKAXE,"buildBetterPickaxe");
|
||||
achievements.put(COOK_FISH,"cookFish");
|
||||
achievements.put(ON_A_RAIL,"onARail");
|
||||
achievements.put(BUILD_SWORD,"buildSword");
|
||||
achievements.put(KILL_ENEMY,"killEnemy");
|
||||
achievements.put(KILL_COW,"killCow");
|
||||
achievements.put(FLY_PIG,"flyPig");
|
||||
achievements.put(SNIPE_SKELETON,"snipeSkeleton");
|
||||
achievements.put(GET_DIAMONDS,"diamonds");
|
||||
achievements.put(NETHER_PORTAL,"portal");
|
||||
achievements.put(GHAST_RETURN,"ghast");
|
||||
achievements.put(GET_BLAZE_ROD,"blazerod");
|
||||
achievements.put(BREW_POTION,"potion");
|
||||
achievements.put(END_PORTAL,"thEnd");
|
||||
achievements.put(THE_END,"theEnd2");
|
||||
achievements.put(ENCHANTMENTS,"enchantments");
|
||||
achievements.put(OVERKILL,"overkill");
|
||||
achievements.put(BOOKCASE,"bookacase");
|
||||
achievements.put(EXPLORE_ALL_BIOMES,"exploreAllBiomes");
|
||||
achievements.put(SPAWN_WITHER,"spawnWither");
|
||||
achievements.put(KILL_WITHER,"killWither");
|
||||
achievements.put(FULL_BEACON,"fullBeacon");
|
||||
achievements.put(BREED_COW,"breedCow");
|
||||
achievements.put(DIAMONDS_TO_YOU,"diamondsToYou");
|
||||
achievements.put(OVERPOWERED, "overpowered");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the json id from the bukkit achievement passed as argument
|
||||
* @param achievement the achievement
|
||||
* @return the achievement's id or null if not found
|
||||
*/
|
||||
public static String toId(Achievement achievement) {
|
||||
return achievements.get(achievement);
|
||||
}
|
||||
|
||||
private BookAchievement(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package me.skymc.taboolib.bookformatter;
|
||||
|
||||
import me.skymc.taboolib.bookformatter.builder.BookBuilder;
|
||||
import me.skymc.taboolib.events.CustomBookOpenEvent;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class BookFormatter {
|
||||
|
||||
/**
|
||||
* Opens a book GUI to the player
|
||||
* @param p the player
|
||||
* @param book the book to be opened
|
||||
*/
|
||||
public static void openPlayer(Player p, ItemStack book) {
|
||||
CustomBookOpenEvent event = new CustomBookOpenEvent(p, book, false);
|
||||
//Call the CustomBookOpenEvent
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
//Check if it's cancelled
|
||||
if(event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Close inventory currently
|
||||
p.closeInventory();
|
||||
//Store the previous item
|
||||
ItemStack hand = p.getItemInHand();
|
||||
p.setItemInHand(event.getBook());
|
||||
|
||||
//Opening the GUI
|
||||
BookReflection.openBook(p, event.getBook(), event.getHand() == CustomBookOpenEvent.Hand.OFF_HAND);
|
||||
|
||||
//Returning whatever was on hand.
|
||||
p.setItemInHand(hand);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a book GUI to the player, Bypass the {@link CustomBookOpenEvent}
|
||||
* @param p the player
|
||||
* @param book the book to be opened
|
||||
*/
|
||||
public static void forceOpen(Player p, ItemStack book) {
|
||||
//Close inventory currently
|
||||
p.closeInventory();
|
||||
//Store the previous item
|
||||
ItemStack hand = p.getItemInHand();
|
||||
p.setItemInHand(book);
|
||||
|
||||
//Opening the GUI
|
||||
BookReflection.openBook(p, book, false);
|
||||
|
||||
//Returning whatever was on hand.
|
||||
p.setItemInHand(hand);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a BookBuilder instance with a written book as the Itemstack's type
|
||||
* @return
|
||||
*/
|
||||
public static BookBuilder writtenBook() {
|
||||
return new BookBuilder(new ItemStack(Material.WRITTEN_BOOK));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a BookBuilder instance with a written book as the Itemstack's type
|
||||
* @return
|
||||
*/
|
||||
public static BookBuilder writtenBook(String title, String author) {
|
||||
return new BookBuilder(new ItemStack(Material.WRITTEN_BOOK), title, author);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
package me.skymc.taboolib.bookformatter;
|
||||
|
||||
import lombok.Getter;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The NMS helper for all the Book-API
|
||||
*/
|
||||
public final class BookReflection {
|
||||
|
||||
private static final String version;
|
||||
private static final boolean doubleHands;
|
||||
|
||||
private static final Class<?> craftMetaBookClass;
|
||||
private static final Field craftMetaBookField;
|
||||
private static final Method chatSerializerA;
|
||||
|
||||
private static final Method craftPlayerGetHandle;
|
||||
|
||||
//This method takes an enum that represents the player's hand only in versions >= 1.9
|
||||
//In the other versions it only takes the nms item
|
||||
private static final Method entityPlayerOpenBook;
|
||||
|
||||
//only version >= 1.9
|
||||
private static final Object[] hands;
|
||||
|
||||
//Older versions
|
||||
/*private static final Field entityHumanPlayerConnection;
|
||||
private static final Method playerConnectionSendPacket;
|
||||
|
||||
private static final Constructor<?> packetPlayOutCustomPayloadConstructor;
|
||||
private static final Constructor<?> packetDataSerializerConstructor;*/
|
||||
|
||||
private static final Method nmsItemStackSave;
|
||||
private static final Constructor<?> nbtTagCompoundConstructor;
|
||||
|
||||
private static final Method craftItemStackAsNMSCopy;
|
||||
|
||||
static {
|
||||
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
final int major, minor;
|
||||
Pattern pattern = Pattern.compile("v([0-9]+)_([0-9]+)");
|
||||
Matcher m = pattern.matcher(version);
|
||||
if(m.find()) {
|
||||
major = Integer.parseInt(m.group(1));
|
||||
minor = Integer.parseInt(m.group(2));
|
||||
} else {
|
||||
throw new IllegalStateException("Cannot parse version \"" + version + "\", make sure it follows \"v<major>_<minor>...\"");
|
||||
}
|
||||
doubleHands = major <= 1 && minor >= 9;
|
||||
try {
|
||||
craftMetaBookClass = getCraftClass("inventory.CraftMetaBook");
|
||||
craftMetaBookField = craftMetaBookClass.getDeclaredField("pages");
|
||||
craftMetaBookField.setAccessible(true);
|
||||
Class<?> chatSerializer = getNmsClass("IChatBaseComponent$ChatSerializer", false);
|
||||
if(chatSerializer == null)
|
||||
chatSerializer = getNmsClass("ChatSerializer");
|
||||
|
||||
chatSerializerA = chatSerializer.getDeclaredMethod("a", String.class);
|
||||
|
||||
final Class<?> craftPlayerClass = getCraftClass("entity.CraftPlayer");
|
||||
craftPlayerGetHandle = craftPlayerClass.getMethod("getHandle");
|
||||
|
||||
final Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
|
||||
final Class<?> itemStackClass = getNmsClass("ItemStack");
|
||||
if(doubleHands) {
|
||||
final Class<?> enumHandClass = getNmsClass("EnumHand");
|
||||
entityPlayerOpenBook = entityPlayerClass.getMethod("a", itemStackClass, enumHandClass);
|
||||
hands = enumHandClass.getEnumConstants();
|
||||
} else {
|
||||
entityPlayerOpenBook = entityPlayerClass.getMethod("openBook", itemStackClass);
|
||||
hands = null;
|
||||
}
|
||||
//Older versions
|
||||
/*entityHumanPlayerConnection = entityPlayerClass.getField("playerConnection");
|
||||
final Class<?> playerConnectionClass = getNmsClass("PlayerConnection");
|
||||
playerConnectionSendPacket = playerConnectionClass.getMethod("sendPacket", getNmsClass("Packet"));
|
||||
|
||||
final Class<?> packetDataSerializerClasss = getNmsClass("PacketDataSerializer");
|
||||
packetPlayOutCustomPayloadConstructor = getNmsClass("PacketPlayOutCustomPayload").getConstructor(String.class, packetDataSerializerClasss);
|
||||
packetDataSerializerConstructor = packetDataSerializerClasss.getConstructor(ByteBuf.class);*/
|
||||
|
||||
final Class<?> craftItemStackClass = getCraftClass("inventory.CraftItemStack");
|
||||
craftItemStackAsNMSCopy = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
|
||||
Class<?> nmsItemStackClazz = getNmsClass("ItemStack");
|
||||
Class<?> nbtTagCompoundClazz = getNmsClass("NBTTagCompound");
|
||||
nmsItemStackSave = nmsItemStackClazz.getMethod("save", nbtTagCompoundClazz);
|
||||
nbtTagCompoundConstructor = nbtTagCompoundClazz.getConstructor();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot initiate reflections for " + version, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the pages of the book to the components json equivalent
|
||||
* @param meta the book meta to change
|
||||
* @param components the pages of the book
|
||||
*/
|
||||
@SuppressWarnings("unchecked")//reflections = unchecked warnings
|
||||
public static void setPages(BookMeta meta, BaseComponent[][] components) {
|
||||
try {
|
||||
List<Object> pages = (List<Object>) craftMetaBookField.get(meta);
|
||||
pages.clear();
|
||||
for(BaseComponent[] c : components) {
|
||||
final String json = ComponentSerializer.toString(c);
|
||||
//System.out.println("page:" + json); //Debug
|
||||
pages.add(chatSerializerA.invoke(null, json));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedVersionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the pages of the book to the components json equivalent
|
||||
* @param meta the book meta to change
|
||||
* @param components the pages of the book
|
||||
*/
|
||||
@SuppressWarnings("unchecked")//reflections = unchecked warnings
|
||||
public static void addPages(BookMeta meta, BaseComponent[][] components) {
|
||||
try {
|
||||
List<Object> pages = (List<Object>) craftMetaBookField.get(meta);
|
||||
for(BaseComponent[] c : components) {
|
||||
final String json = ComponentSerializer.toString(c);
|
||||
//System.out.println("page:" + json); //Debug
|
||||
pages.add(chatSerializerA.invoke(null, json));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedVersionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the book to a player (the player needs to have the book in one of his hands)
|
||||
* @param player the player
|
||||
* @param book the book to open
|
||||
* @param offHand false if the book is in the right hand, true otherwise
|
||||
*/
|
||||
public static void openBook(Player player, ItemStack book, boolean offHand) {
|
||||
//nms(player).openBook(nms(player), nms(book), hand);
|
||||
try {
|
||||
//Older versions:
|
||||
/*playerConnectionSendPacket.invoke(
|
||||
entityHumanPlayerConnection.get(toNms(player)),
|
||||
createBookOpenPacket()
|
||||
);*/
|
||||
if(doubleHands) {
|
||||
entityPlayerOpenBook.invoke(
|
||||
toNms(player),
|
||||
nmsCopy(book),
|
||||
hands[offHand ? 1 : 0]
|
||||
);
|
||||
} else {
|
||||
entityPlayerOpenBook.invoke(
|
||||
toNms(player),
|
||||
nmsCopy(book)
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedVersionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
//Older versions
|
||||
/*public static Object createBookOpenPacket() {
|
||||
//new PacketPlayOutCustomPayload("MC|BOpen", new PacketDataSerializer(Unpooled.buffer())));
|
||||
try {
|
||||
return packetPlayOutCustomPayloadConstructor.newInstance(
|
||||
"MC|BOpen",
|
||||
packetDataSerializerConstructor.newInstance(Unpooled.buffer())
|
||||
);
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedVersionException(e);
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Translates an ItemStack to his Chat-Component equivalent
|
||||
* @param item the item to be converted
|
||||
* @return a Chat-Component equivalent of the parameter
|
||||
*/
|
||||
public static BaseComponent[] itemToComponents(ItemStack item) {
|
||||
return jsonToComponents(itemToJson(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a json string to his Chat-Component equivalent
|
||||
* @param json the json string to be converted
|
||||
* @return a Chat-Component equivalent of the parameter
|
||||
*/
|
||||
public static BaseComponent[] jsonToComponents(String json) {
|
||||
return new BaseComponent[] { new TextComponent(json) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates an ItemStack to his json equivalent
|
||||
* @param item the item to be converted
|
||||
* @return a json equivalent of the parameter
|
||||
*/
|
||||
private static String itemToJson(ItemStack item) {
|
||||
try {
|
||||
//net.minecraft.server.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
|
||||
Object nmsItemStack = nmsCopy(item);
|
||||
|
||||
//net.minecraft.server.NBTTagCompound compound = new NBTTagCompound();
|
||||
//compound = nmsItemStack.save(compound);
|
||||
Object emptyTag = nbtTagCompoundConstructor.newInstance();
|
||||
Object json = nmsItemStackSave.invoke(nmsItemStack, emptyTag);
|
||||
return json.toString();
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedVersionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An error thrown when this NMS-helper class doesn't support the running MC version
|
||||
*/
|
||||
public static class UnsupportedVersionException extends RuntimeException {
|
||||
/**
|
||||
* serialVersionUID
|
||||
*/
|
||||
private static final long serialVersionUID = 6835583513394319946L;
|
||||
|
||||
/**
|
||||
* The current running version
|
||||
*/
|
||||
@Getter
|
||||
private final String version = BookReflection.version;
|
||||
|
||||
public UnsupportedVersionException(Exception e) {
|
||||
super("Error while executing reflections, submit to developers the following log (version: " + BookReflection.version + ")", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the EntityPlayer handled by the argument
|
||||
* @param player the Player handler
|
||||
* @return the handled class
|
||||
* @throws InvocationTargetException when some problems are found with the reflection
|
||||
* @throws IllegalAccessException when some problems are found with the reflection
|
||||
*/
|
||||
public static Object toNms(Player player) throws InvocationTargetException, IllegalAccessException {
|
||||
return craftPlayerGetHandle.invoke(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a NMS copy of the parameter
|
||||
* @param item the ItemStack to be nms-copied
|
||||
* @return a NMS-ItemStack that is the equivalent of the one passed as argument
|
||||
* @throws InvocationTargetException when some problems are found with the reflection
|
||||
* @throws IllegalAccessException when some problems are found with the reflection
|
||||
*/
|
||||
public static Object nmsCopy(ItemStack item) throws InvocationTargetException, IllegalAccessException {
|
||||
return craftItemStackAsNMSCopy.invoke(null, item);
|
||||
}
|
||||
|
||||
public static Class<?> getNmsClass(String className, boolean log) {
|
||||
try {
|
||||
return Class.forName("net.minecraft.server." + version + "." + className);
|
||||
} catch(ClassNotFoundException e) {
|
||||
if(log)
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getNmsClass(String className) {
|
||||
return getNmsClass(className, true);
|
||||
}
|
||||
|
||||
|
||||
private static Class<?> getCraftClass(String path) {
|
||||
try {
|
||||
return Class.forName("org.bukkit.craftbukkit." + version + "." + path);
|
||||
} catch(ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package me.skymc.taboolib.bookformatter.action;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018-03-08 22:38:04
|
||||
*/
|
||||
public interface ClickAction {
|
||||
|
||||
/**
|
||||
* Get the Chat-Component action
|
||||
* @return the Chat-Component action
|
||||
*/
|
||||
ClickEvent.Action action();
|
||||
|
||||
/**
|
||||
* The value paired to the action
|
||||
* @return the value paired tot the action
|
||||
*/
|
||||
String value();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a command action: when the player clicks, the command passed as parameter gets executed with the clicker as sender
|
||||
* @param command the command to be executed
|
||||
* @return a new ClickAction
|
||||
*/
|
||||
static ClickAction runCommand(String command) {
|
||||
return new SimpleClickAction(ClickEvent.Action.RUN_COMMAND, command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a suggest_command action: when the player clicks, the book closes and the chat opens with the parameter written into it
|
||||
* @param command the command to be suggested
|
||||
* @return a new ClickAction
|
||||
*/
|
||||
static ClickAction suggestCommand(String command) {
|
||||
return new SimpleClickAction(ClickEvent.Action.SUGGEST_COMMAND, command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a open_utl action: when the player clicks the url passed as argument will open in the browser
|
||||
* @param url the url to be opened
|
||||
* @return a new ClickAction
|
||||
*/
|
||||
static ClickAction openUrl(String url) {
|
||||
if(url.startsWith("http://") || url.startsWith("https://"))
|
||||
return new SimpleClickAction(ClickEvent.Action.OPEN_URL, url);
|
||||
else
|
||||
throw new IllegalArgumentException("Invalid url: \"" + url + "\", it should start with http:// or https://");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a change_page action: when the player clicks the book page will be set at the value passed as argument
|
||||
* @param page the new page
|
||||
* @return a new ClickAction
|
||||
*/
|
||||
static ClickAction changePage(int page) {
|
||||
return new SimpleClickAction(ClickEvent.Action.CHANGE_PAGE, Integer.toString(page));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Accessors(fluent = true)
|
||||
@RequiredArgsConstructor
|
||||
class SimpleClickAction implements ClickAction {
|
||||
private final ClickEvent.Action action;
|
||||
private final String value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package me.skymc.taboolib.bookformatter.action;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Achievement;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.skymc.taboolib.bookformatter.BookAchievement;
|
||||
import me.skymc.taboolib.bookformatter.BookReflection;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018-03-08 22:38:51
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public interface HoverAction {
|
||||
|
||||
/**
|
||||
* Get the Chat-Component action
|
||||
* @return the Chat-Component action
|
||||
*/
|
||||
HoverEvent.Action action();
|
||||
/**
|
||||
* The value paired to the action
|
||||
* @return the value paired tot the action
|
||||
*/
|
||||
BaseComponent[] value();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a show_text action: when the component is hovered the text used as parameter will be displayed
|
||||
* @param text the text to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showText(BaseComponent... text) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_TEXT, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_text action: when the component is hovered the text used as parameter will be displayed
|
||||
* @param text the text to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showText(String text) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_TEXT, new TextComponent(text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_item action: when the component is hovered some item information will be displayed
|
||||
* @param item a component array representing item to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showItem(BaseComponent... item) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ITEM, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_item action: when the component is hovered some item information will be displayed
|
||||
* @param item the item to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showItem(ItemStack item) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ITEM, BookReflection.itemToComponents(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_entity action: when the component is hovered some entity information will be displayed
|
||||
* @param entity a component array representing the item to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showEntity(BaseComponent... entity) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ENTITY, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_entity action: when the component is hovered some entity information will be displayed
|
||||
* @param uuid the entity's UniqueId
|
||||
* @param type the entity's type
|
||||
* @param name the entity's name
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showEntity(UUID uuid, String type, String name) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ENTITY,
|
||||
BookReflection.jsonToComponents(
|
||||
"{id:\"" + uuid + "\",type:\"" + type + "\"name:\"" + name + "\"}"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_entity action: when the component is hovered some entity information will be displayed
|
||||
* @param entity the item to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showEntity(Entity entity) {
|
||||
return showEntity(entity.getUniqueId(), entity.getType().getName(), entity.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_achievement action: when the component is hovered the achievement information will be displayed
|
||||
* @param achievementId the id of the achievement to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showAchievement(String achievementId) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ACHIEVEMENT, new TextComponent("achievement." + achievementId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_achievement action: when the component is hovered the achievement information will be displayed
|
||||
* @param achievement the achievement to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showAchievement(Achievement achievement) {
|
||||
return showAchievement(BookAchievement.toId(achievement));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a show_achievement action: when the component is hovered the statistic information will be displayed
|
||||
* @param statisticId the id of the statistic to display
|
||||
* @return a new HoverAction instance
|
||||
*/
|
||||
static HoverAction showStatistic(String statisticId) {
|
||||
return new SimpleHoverAction(HoverEvent.Action.SHOW_ACHIEVEMENT, new TextComponent("statistic." + statisticId));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Accessors(fluent = true)
|
||||
class SimpleHoverAction implements HoverAction {
|
||||
private final HoverEvent.Action action;
|
||||
private final BaseComponent[] value;
|
||||
|
||||
public SimpleHoverAction(HoverEvent.Action action, BaseComponent... value) {
|
||||
this.action = action;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package me.skymc.taboolib.bookformatter.builder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import me.skymc.taboolib.bookformatter.BookReflection;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018-03-08 22:36:14
|
||||
*/
|
||||
public class BookBuilder {
|
||||
|
||||
private final BookMeta meta;
|
||||
private final ItemStack book;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the BookBuilder from an ItemStack representing the book item
|
||||
* @param book the book's ItemStack
|
||||
*/
|
||||
public BookBuilder(ItemStack book) {
|
||||
this.book = book;
|
||||
this.meta = (BookMeta)book.getItemMeta();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the BookBuilder from an ItemStack representing the book item
|
||||
* @param book the book's ItemStack
|
||||
*/
|
||||
public BookBuilder(ItemStack book, String title, String author) {
|
||||
this.book = book;
|
||||
this.meta = (BookMeta)book.getItemMeta();
|
||||
this.meta.setTitle(title);
|
||||
this.meta.setAuthor(author);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the title of the book
|
||||
* @param title the title of the book
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder title(String title) {
|
||||
meta.setTitle(title);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the author of the book
|
||||
* @param author the author of the book
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder author(String author) {
|
||||
meta.setAuthor(author);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the generation of the book
|
||||
* Only works from MC 1.10
|
||||
* @param generation the Book generation
|
||||
* @return the BookBuilder calling instance
|
||||
*/
|
||||
public BookBuilder generation(BookMeta.Generation generation) {
|
||||
meta.setGeneration(generation);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pages of the book without worrying about json or interactivity
|
||||
* @param pages text-based pages
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder pagesRaw(String... pages) {
|
||||
meta.setPages(pages);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pages of the book without worrying about json or interactivity
|
||||
* @param pages text-based pages
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder pagesRaw(List<String> pages) {
|
||||
meta.setPages(pages);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pages of the book
|
||||
* @param pages the pages of the book
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder pages(BaseComponent[]... pages) {
|
||||
BookReflection.setPages(meta, pages);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pages of the book
|
||||
* @param pages the pages of the book
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder pages(List<BaseComponent[]> pages) {
|
||||
BookReflection.setPages(meta, pages.toArray(new BaseComponent[0][]));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the pages of the book
|
||||
* @param pages the pages of the book
|
||||
* @return the BookBuilder's calling instance
|
||||
*/
|
||||
public BookBuilder addPages(BaseComponent[]... pages) {
|
||||
BookReflection.addPages(meta, pages);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the book
|
||||
* @return the built book
|
||||
*/
|
||||
public ItemStack build() {
|
||||
book.setItemMeta(meta);
|
||||
return book;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package me.skymc.taboolib.bookformatter.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018-03-08 22:36:58
|
||||
*/
|
||||
public class PageBuilder {
|
||||
|
||||
private List<BaseComponent> text = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Adds a simple black-colored text to the page
|
||||
* @param text the text to add
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder add(String text) {
|
||||
this.text.add(TextBuilder.of(text).build());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a component to the page
|
||||
* @param component the component to add
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder add(BaseComponent component) {
|
||||
this.text.add(component);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds one or more components to the page
|
||||
* @param components the components to add
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder add(BaseComponent... components) {
|
||||
this.text.addAll(Arrays.asList(components));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds one or more components to the page
|
||||
* @param components the components to add
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder add(Collection<BaseComponent> components) {
|
||||
this.text.addAll(components);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a newline to the page (equivalent of adding \n to the previous component)
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder newLine() {
|
||||
this.text.add(new TextComponent("\n"));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Another way of newLine(), better resolution (equivalent of adding \n to the previous component)
|
||||
* @return the PageBuilder's calling instance
|
||||
*/
|
||||
public PageBuilder endLine() {
|
||||
return newLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the page
|
||||
* @return an array of BaseComponents representing the page
|
||||
*/
|
||||
public BaseComponent[] build() {
|
||||
return text.toArray(new BaseComponent[0]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new PageBuilder instance wih the parameter as the initial text
|
||||
* @param text the initial text of the page
|
||||
* @return a new PageBuilder with the parameter as the initial text
|
||||
*/
|
||||
public static PageBuilder of(String text) {
|
||||
return new PageBuilder().add(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PageBuilder instance wih the parameter as the initial component
|
||||
* @param text the initial component of the page
|
||||
* @return a new PageBuilder with the parameter as the initial component
|
||||
*/
|
||||
public static PageBuilder of(BaseComponent text) {
|
||||
return new PageBuilder().add(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PageBuilder instance wih the parameter as the initial components
|
||||
* @param text the initial components of the page
|
||||
* @return a new PageBuilder with the parameter as the initial components
|
||||
*/
|
||||
public static PageBuilder of(BaseComponent... text) {
|
||||
PageBuilder res = new PageBuilder();
|
||||
for(BaseComponent b : text)
|
||||
res.add(b);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package me.skymc.taboolib.bookformatter.builder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.skymc.taboolib.bookformatter.action.ClickAction;
|
||||
import me.skymc.taboolib.bookformatter.action.HoverAction;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018-03-08 22:37:27
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@Accessors(fluent = true, chain = true)
|
||||
public class TextBuilder {
|
||||
|
||||
private String text = "";
|
||||
private ClickAction onClick = null;
|
||||
private HoverAction onHover = null;
|
||||
|
||||
public TextBuilder() {}
|
||||
|
||||
public TextBuilder(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the component representing the built text
|
||||
* @return the component representing the built text
|
||||
*/
|
||||
public BaseComponent build() {
|
||||
TextComponent res = new TextComponent(text);
|
||||
if(onClick != null) {
|
||||
res.setClickEvent(new ClickEvent(onClick.action(), onClick.value()));
|
||||
}
|
||||
if(onHover != null) {
|
||||
res.setHoverEvent(new HoverEvent(onHover.action(), onHover.value()));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new TextBuilder with the parameter as his initial text
|
||||
* @param text initial text
|
||||
* @return a new TextBuilder with the parameter as his initial text
|
||||
*/
|
||||
public static TextBuilder of(String text) {
|
||||
return new TextBuilder().text(text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user