This commit is contained in:
坏黑
2018-05-07 14:43:57 +08:00
parent 7181c487f9
commit 6f34cbc2e0
152 changed files with 9256 additions and 9003 deletions

View File

@@ -1,87 +1,55 @@
package me.skymc.taboolib.bookformatter;
import static org.bukkit.Achievement.ACQUIRE_IRON;
import static org.bukkit.Achievement.BAKE_CAKE;
import static org.bukkit.Achievement.BOOKCASE;
import static org.bukkit.Achievement.BREED_COW;
import static org.bukkit.Achievement.BREW_POTION;
import static org.bukkit.Achievement.BUILD_BETTER_PICKAXE;
import static org.bukkit.Achievement.BUILD_FURNACE;
import static org.bukkit.Achievement.BUILD_HOE;
import static org.bukkit.Achievement.BUILD_PICKAXE;
import static org.bukkit.Achievement.BUILD_SWORD;
import static org.bukkit.Achievement.BUILD_WORKBENCH;
import static org.bukkit.Achievement.COOK_FISH;
import static org.bukkit.Achievement.DIAMONDS_TO_YOU;
import static org.bukkit.Achievement.ENCHANTMENTS;
import static org.bukkit.Achievement.END_PORTAL;
import static org.bukkit.Achievement.EXPLORE_ALL_BIOMES;
import static org.bukkit.Achievement.FLY_PIG;
import static org.bukkit.Achievement.FULL_BEACON;
import static org.bukkit.Achievement.GET_BLAZE_ROD;
import static org.bukkit.Achievement.GET_DIAMONDS;
import static org.bukkit.Achievement.GHAST_RETURN;
import static org.bukkit.Achievement.KILL_COW;
import static org.bukkit.Achievement.KILL_ENEMY;
import static org.bukkit.Achievement.KILL_WITHER;
import static org.bukkit.Achievement.MAKE_BREAD;
import static org.bukkit.Achievement.MINE_WOOD;
import static org.bukkit.Achievement.NETHER_PORTAL;
import static org.bukkit.Achievement.ON_A_RAIL;
import static org.bukkit.Achievement.OPEN_INVENTORY;
import static org.bukkit.Achievement.OVERKILL;
import static org.bukkit.Achievement.OVERPOWERED;
import static org.bukkit.Achievement.SNIPE_SKELETON;
import static org.bukkit.Achievement.SPAWN_WITHER;
import static org.bukkit.Achievement.THE_END;
import java.util.HashMap;
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");
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
*/
@@ -89,7 +57,7 @@ public final class BookAchievement {
return achievements.get(achievement);
}
private BookAchievement(){
private BookAchievement() {
}
}

View File

@@ -1,5 +1,13 @@
package me.skymc.taboolib.bookformatter;
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;
@@ -8,21 +16,11 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
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;
/**
* The NMS helper for all the Book-API
*/
public final class BookReflection {
private static final String version;
private static final boolean doubleHands;
@@ -31,11 +29,11 @@ public final class BookReflection {
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;
@@ -56,7 +54,7 @@ public final class BookReflection {
final int major, minor;
Pattern pattern = Pattern.compile("v([0-9]+)_([0-9]+)");
Matcher m = pattern.matcher(version);
if(m.find()) {
if (m.find()) {
major = Integer.parseInt(m.group(1));
minor = Integer.parseInt(m.group(2));
} else {
@@ -68,7 +66,7 @@ public final class BookReflection {
craftMetaBookField = craftMetaBookClass.getDeclaredField("pages");
craftMetaBookField.setAccessible(true);
Class<?> chatSerializer = getNmsClass("IChatBaseComponent$ChatSerializer", false);
if(chatSerializer == null)
if (chatSerializer == null)
chatSerializer = getNmsClass("ChatSerializer");
chatSerializerA = chatSerializer.getDeclaredMethod("a", String.class);
@@ -78,7 +76,7 @@ public final class BookReflection {
final Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
final Class<?> itemStackClass = getNmsClass("ItemStack");
if(doubleHands) {
if (doubleHands) {
final Class<?> enumHandClass = getNmsClass("EnumHand");
entityPlayerOpenBook = entityPlayerClass.getMethod("a", itemStackClass, enumHandClass);
hands = enumHandClass.getEnumConstants();
@@ -110,7 +108,8 @@ public final class BookReflection {
/**
* Sets the pages of the book to the components json equivalent
* @param meta the book meta to change
*
* @param meta the book meta to change
* @param components the pages of the book
*/
@SuppressWarnings("unchecked")//reflections = unchecked warnings
@@ -118,7 +117,7 @@ public final class BookReflection {
try {
List<Object> pages = (List<Object>) craftMetaBookField.get(meta);
pages.clear();
for(BaseComponent[] c : components) {
for (BaseComponent[] c : components) {
final String json = ComponentSerializer.toString(c);
//System.out.println("page:" + json); //Debug
pages.add(chatSerializerA.invoke(null, json));
@@ -127,17 +126,18 @@ public final class BookReflection {
throw new UnsupportedVersionException(e);
}
}
/**
* Append the pages of the book to the components json equivalent
* @param meta the book meta to change
*
* @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 {
try {
List<Object> pages = (List<Object>) craftMetaBookField.get(meta);
for(BaseComponent[] c : components) {
for (BaseComponent[] c : components) {
final String json = ComponentSerializer.toString(c);
//System.out.println("page:" + json); //Debug
pages.add(chatSerializerA.invoke(null, json));
@@ -149,8 +149,9 @@ public final class BookReflection {
/**
* 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 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) {
@@ -161,7 +162,7 @@ public final class BookReflection {
entityHumanPlayerConnection.get(toNms(player)),
createBookOpenPacket()
);*/
if(doubleHands) {
if (doubleHands) {
entityPlayerOpenBook.invoke(
toNms(player),
nmsCopy(book),
@@ -193,24 +194,27 @@ public final class BookReflection {
/**
* 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));
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) };
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
*/
@@ -234,16 +238,19 @@ public final class BookReflection {
*/
public static class UnsupportedVersionException extends RuntimeException {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6835583513394319946L;
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6835583513394319946L;
/**
* The current running version
*/
@Getter
private final String version = BookReflection.version;
public String getVersion() {
return version;
}
public UnsupportedVersionException(Exception e) {
super("Error while executing reflections, submit to developers the following log (version: " + BookReflection.version + ")", e);
}
@@ -252,10 +259,11 @@ public final class BookReflection {
/**
* 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
* @throws IllegalAccessException when some problems are found with the reflection
*/
public static Object toNms(Player player) throws InvocationTargetException, IllegalAccessException {
return craftPlayerGetHandle.invoke(player);
@@ -263,10 +271,11 @@ public final class BookReflection {
/**
* 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
* @throws IllegalAccessException when some problems are found with the reflection
*/
public static Object nmsCopy(ItemStack item) throws InvocationTargetException, IllegalAccessException {
return craftItemStackAsNMSCopy.invoke(null, item);
@@ -275,8 +284,8 @@ public final class BookReflection {
public static Class<?> getNmsClass(String className, boolean log) {
try {
return Class.forName("net.minecraft.server." + version + "." + className);
} catch(ClassNotFoundException e) {
if(log)
} catch (ClassNotFoundException e) {
if (log)
e.printStackTrace();
return null;
}
@@ -290,7 +299,7 @@ public final class BookReflection {
private static Class<?> getCraftClass(String path) {
try {
return Class.forName("org.bukkit.craftbukkit." + version + "." + path);
} catch(ClassNotFoundException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}

View File

@@ -1,8 +1,5 @@
package me.skymc.taboolib.bookformatter.action;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import net.md_5.bungee.api.chat.ClickEvent;
/**
@@ -10,15 +7,17 @@ import net.md_5.bungee.api.chat.ClickEvent;
* @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();
@@ -26,6 +25,7 @@ public interface ClickAction {
/**
* 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
*/
@@ -35,6 +35,7 @@ public interface ClickAction {
/**
* 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
*/
@@ -44,11 +45,12 @@ public interface ClickAction {
/**
* 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://"))
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://");
@@ -56,6 +58,7 @@ public interface ClickAction {
/**
* 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
*/
@@ -63,11 +66,32 @@ public interface ClickAction {
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;
public SimpleClickAction(ClickEvent.Action action, String value) {
this.action = action;
this.value = value;
}
public ClickEvent.Action getAction() {
return action;
}
public String getValue() {
return value;
}
@Override
public ClickEvent.Action action() {
return null;
}
@Override
public String value() {
return null;
}
}
}

View File

@@ -1,18 +1,15 @@
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;
import org.bukkit.Achievement;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
/**
* @author sky
@@ -20,14 +17,17 @@ import net.md_5.bungee.api.chat.TextComponent;
*/
@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();
@@ -35,6 +35,7 @@ public interface HoverAction {
/**
* 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
*/
@@ -44,6 +45,7 @@ public interface HoverAction {
/**
* 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
*/
@@ -53,6 +55,7 @@ public interface HoverAction {
/**
* 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
*/
@@ -62,6 +65,7 @@ public interface HoverAction {
/**
* 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
*/
@@ -71,6 +75,7 @@ public interface HoverAction {
/**
* 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
*/
@@ -80,6 +85,7 @@ public interface HoverAction {
/**
* 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
@@ -95,15 +101,17 @@ public interface HoverAction {
/**
* 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) {
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
*/
@@ -113,15 +121,17 @@ public interface HoverAction {
/**
* 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) {
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
*/
@@ -129,8 +139,6 @@ public interface HoverAction {
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;
@@ -139,5 +147,23 @@ public interface HoverAction {
this.action = action;
this.value = value;
}
public HoverEvent.Action getAction() {
return action;
}
public BaseComponent[] getValue() {
return value;
}
@Override
public HoverEvent.Action action() {
return null;
}
@Override
public BaseComponent[] value() {
return new BaseComponent[0];
}
}
}

View File

@@ -1,8 +1,5 @@
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;
@@ -14,31 +11,54 @@ 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 String getText() {
return text;
}
public ClickAction getClick() {
return onClick;
}
public HoverAction getHover() {
return onHover;
}
public void text(String text) {
this.text = text;
}
public void onClick(ClickAction onClick) {
this.onClick = onClick;
}
public void onHover(HoverAction onHover) {
this.onHover = onHover;
}
public TextBuilder() {
}
public TextBuilder(String text) {
this.text = 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) {
if (onClick != null) {
res.setClickEvent(new ClickEvent(onClick.action(), onClick.value()));
}
if(onHover != null) {
if (onHover != null) {
res.setHoverEvent(new HoverEvent(onHover.action(), onHover.value()));
}
return res;
@@ -46,10 +66,11 @@ public class TextBuilder {
/**
* 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);
return new TextBuilder(text);
}
}