Note that the detail message associated with {@code cause} is
29 | * not automatically incorporated into this exception's detail
30 | * message.
31 | *
32 | * @param message The detail message (which is saved for later retrieval
33 | * by the {@link #getMessage()} method)
34 | * @param cause The cause (which is saved for later retrieval by the
35 | * {@link #getCause()} method). (A null value is permitted,
36 | * and indicates that the cause is nonexistent or unknown.)
37 | * @since 1.6
38 | */
39 | public MemberMappingException(String message, Throwable cause) {
40 | super(message, cause);
41 | }
42 |
43 | /**
44 | * Constructs an {@code IOException} with the specified cause and a
45 | * detail message of {@code (cause==null ? null : cause.toString())}
46 | * (which typically contains the class and detail message of {@code cause}).
47 | * This constructor is useful for IO exceptions that are little more
48 | * than wrappers for other throwables.
49 | *
50 | * @param cause The cause (which is saved for later retrieval by the
51 | * {@link #getCause()} method). (A null value is permitted,
52 | * and indicates that the cause is nonexistent or unknown.)
53 | * @since 1.6
54 | */
55 | public MemberMappingException(Throwable cause) {
56 | super(cause);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/i18n/BaseI18n.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.i18n;
2 |
3 | import java.util.Locale;
4 |
5 | public abstract class BaseI18n implements I18n {
6 | /**
7 | * default locale from {@link Locale#getDefault()}
8 | */
9 | private Locale defaultLocale = Locale.getDefault();
10 |
11 | private I18n parent;
12 |
13 | @Override
14 | public I18n getParent() {
15 | return parent;
16 | }
17 |
18 | @Override
19 | public void setParent(I18n parent) {
20 | this.parent = parent;
21 | }
22 |
23 | @Override
24 | public Locale getDefaultLocale() {
25 | return defaultLocale;
26 | }
27 |
28 | @Override
29 | public void setDefaultLocale(Locale defaultLocale) {
30 | this.defaultLocale = defaultLocale;
31 | }
32 |
33 | @Override
34 | public void setDefaultLocale(String defaultLocale) {
35 | this.defaultLocale = Locale.forLanguageTag(defaultLocale);
36 | }
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/i18n/YamlI18n.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.i18n;
2 |
3 | import org.apache.commons.lang3.ArrayUtils;
4 | import org.bukkit.Bukkit;
5 | import org.bukkit.configuration.Configuration;
6 | import team.unstudio.udpl.UDPLib;
7 | import team.unstudio.udpl.config.ConfigurationHelper;
8 | import javax.annotation.Nonnull;
9 | import java.io.File;
10 | import java.net.URL;
11 | import java.util.Collections;
12 | import java.util.HashMap;
13 | import java.util.Locale;
14 | import java.util.Map;
15 | import java.util.logging.Level;
16 |
17 | public class YamlI18n extends BaseI18n {
18 | protected final Map cache;
19 |
20 | public YamlI18n(@Nonnull Map map) {
21 | this.cache = map;
22 | }
23 |
24 | @Override
25 | public String localize(Locale locale, String key){
26 | if(cache.containsKey(locale))
27 | return cache.get(locale).getString(key, key);
28 | else if(cache.containsKey(getDefaultLocale()))
29 | return cache.get(getDefaultLocale()).getString(key, key);
30 | else if(getParent() != null)
31 | return getParent().localize(locale, key);
32 | else
33 | return key;
34 | }
35 |
36 | @SuppressWarnings("ConstantConditions")
37 | public static YamlI18n fromFile(@Nonnull File path) {
38 | if(!path.exists())
39 | throw new IllegalArgumentException("Path isn't exist.");
40 | if(!path.isDirectory())
41 | throw new IllegalArgumentException("Path isn't directory.");
42 |
43 | Map map = new HashMap<>();
44 |
45 | for(File file:path.listFiles((file,name)->name.endsWith(".yml"))){
46 | Locale locale = Locale.forLanguageTag(file.getName().substring(0, file.getName().lastIndexOf('.')).replaceAll("_", "-"));
47 | Configuration config = ConfigurationHelper.loadConfiguration(file);
48 | if(config != null)
49 | map.put(locale, config);
50 | }
51 | return new YamlI18n(map);
52 | }
53 |
54 | public static YamlI18n fromClassLoader(@Nonnull ClassLoader classLoader, @Nonnull String path) {
55 | Map map = new HashMap<>();
56 |
57 | try {
58 | for (URL url : Collections.list(classLoader.getResources(path))) {
59 | File filePath = new File(url.toURI());
60 | for (File file : (File[]) ArrayUtils.nullToEmpty(filePath.listFiles((file, name) -> name.endsWith(".yml")))) {
61 | Locale locale = Locale.forLanguageTag(file.getName().substring(0, file.getName().lastIndexOf('.')).replaceAll("_", "-"));
62 | Configuration config = ConfigurationHelper.loadConfiguration(file);
63 | if(config != null)
64 | map.put(locale, config);
65 | }
66 | }
67 | } catch (Exception e) {
68 | UDPLib.getLogger().error("Cannot read language file from class path", e);
69 | }
70 |
71 | return new YamlI18n(map);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/i18n/slang/CachedSLang.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.i18n.slang;
2 |
3 | import java.util.HashMap;
4 | import java.util.Locale;
5 | import java.util.Map;
6 |
7 | /**
8 | * Created by trychen on 17/7/11.
9 | */
10 | public class CachedSLang {
11 | /**
12 | * 语言
13 | */
14 | public final Locale locale;
15 |
16 | /**
17 | * 数据
18 | */
19 | protected final Map map = new HashMap<>();
20 |
21 | /**
22 | * 通过语言初始化一个 CachedSLang
23 | */
24 | public CachedSLang(Locale locale) {
25 | this.locale = locale;
26 | }
27 |
28 | /**
29 | * 获取本地化文本
30 | */
31 | public String get(String key){
32 | String s = map.get(key);
33 | if (s == null) return key;
34 | else return s;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/i18n/slang/SLangSpliter.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.i18n.slang;
2 |
3 | import java.util.Arrays;
4 | import java.util.Locale;
5 | import java.util.stream.Collectors;
6 |
7 | /**
8 | * Created by trychen on 17/7/11.
9 | */
10 | public interface SLangSpliter {
11 | /**
12 | * 分隔符
13 | */
14 | String DEFAULT_SPLITER = "|";
15 |
16 | /**
17 | * 使用默认的分割符号分割文本到 CachedSLang
18 | *
19 | * @param list 要分割的文本
20 | */
21 | static CachedSLang[] split(String[] list) {
22 | return split(DEFAULT_SPLITER, list);
23 | }
24 |
25 | /**
26 | * 分割文本到 CachedSLang
27 | *
28 | * @param separator 分割符 (Regex)
29 | * @param list 文本行数据
30 | */
31 | static CachedSLang[] split(String separator, String[] list) {
32 | // 清理注释
33 | list = Arrays.stream(list).filter(it -> !it.startsWith("#")).collect(Collectors.toList()).toArray(new String[0]);
34 |
35 | // clean
36 | if (list.length < 2) return new CachedSLang[0];
37 |
38 | // the first line's string array
39 | String[] locals = list[0].split(separator);
40 | String[][] data = new String[list.length][locals.length];
41 |
42 | data[0] = locals;
43 |
44 | // init data array
45 | for (int i = 1; i < list.length; i++)
46 | data[i] = Arrays.stream(list[i].split(separator)).map(String::trim).toArray(String[]::new);
47 |
48 |
49 | // get the head key or locale
50 | CachedSLang[] langs = new CachedSLang[locals.length];
51 | for (int i = 0; i < locals.length; i++)
52 | if (!locals[i].trim().equalsIgnoreCase("key"))
53 | langs[i] = new CachedSLang(Locale.forLanguageTag(locals[i].trim().replaceAll("_", "-")));
54 |
55 | for (int i = 1; i < data.length; i++)
56 | for (int j = 1; j < data[i].length; j++) {
57 | // check if trim
58 | String lang = data[i][j];
59 | if (lang.endsWith("∞")) lang = lang.substring(0, lang.length() - 1);
60 | langs[j].map.put(data[i][0], lang);
61 | }
62 |
63 |
64 | CachedSLang[] newLangs;
65 | if (langs[0] == null) {
66 | newLangs = new CachedSLang[langs.length - 1];
67 | System.arraycopy(langs, 1, newLangs, 0, langs.length - 1);
68 | } else newLangs = langs;
69 |
70 | return newLangs;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/item/ItemHelper.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.item;
2 |
3 | import static team.unstudio.udpl.util.reflect.NMSReflectionUtils.*;
4 | import org.bukkit.inventory.ItemStack;
5 |
6 | import team.unstudio.udpl.UDPLib;
7 | import javax.annotation.Nullable;
8 |
9 | public interface ItemHelper {
10 |
11 | /**
12 | * 转换为JSON格式
13 | * @param itemStack 物品
14 | */
15 | @Nullable
16 | static String toJson(ItemStack itemStack) {
17 | try {
18 | return ItemStack$save()
19 | .invoke(CraftItemStack$asNMSCopy().invoke(null, itemStack), NBTTagCompound$init().newInstance())
20 | .toString();
21 | } catch (ReflectiveOperationException e) {
22 | UDPLib.debug(e);
23 | }
24 | return null;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/NMSException.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms;
2 |
3 | public class NMSException extends RuntimeException {
4 | public NMSException() {
5 | }
6 |
7 | public NMSException(String message) {
8 | super(message);
9 | }
10 |
11 | public NMSException(Throwable throwable) {
12 | super(throwable);
13 | }
14 |
15 | public NMSException(String message, Throwable throwable) {
16 | super(message, throwable);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/NmsHelper.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms;
2 |
3 | import org.bukkit.block.BlockState;
4 | import org.bukkit.entity.Entity;
5 | import org.bukkit.inventory.ItemStack;
6 |
7 | import team.unstudio.udpl.annotation.Init;
8 | import team.unstudio.udpl.nms.entity.NmsEntity;
9 | import team.unstudio.udpl.nms.inventory.NmsItemStack;
10 | import team.unstudio.udpl.nms.nbt.NmsNBT;
11 | import team.unstudio.udpl.nms.tileentity.NmsTileEntity;
12 |
13 | public final class NmsHelper {
14 |
15 | private NmsHelper(){}
16 |
17 | @Init("nms_manager")
18 | private static NmsManager NMS_MANAGER;
19 |
20 | /**
21 | * 获取 {@link net.minecraft.server} 与 {@link team.unstudio.udpl.nms.nbt} 的帮助类实例
22 | */
23 | public static NmsNBT getNmsNBT(){
24 | checkNmsSupported();
25 | return NMS_MANAGER.getNmsNBT();
26 | }
27 |
28 |
29 | /**
30 | * 创建一个{@link NmsItemStack}对象
31 | */
32 | public static NmsItemStack createNmsItemStack(ItemStack itemStack){
33 | checkNmsSupported();
34 | return NMS_MANAGER.createNmsItemStack(itemStack);
35 | }
36 |
37 | /**
38 | * 创建一个{@link NmsEntity}对象
39 | */
40 | public static NmsEntity createNmsEntity(Entity entity){
41 | checkNmsSupported();
42 | return NMS_MANAGER.createNmsEntity(entity);
43 | }
44 |
45 | /**
46 | * 创建一个{@link NmsTileEntity}对象
47 | */
48 | public static NmsTileEntity createNmsTileEntity(BlockState blockState){
49 | checkNmsSupported();
50 | return NMS_MANAGER.createNmsTileEntity(blockState);
51 | }
52 |
53 | /**
54 | * 获取是否支持Nms操作
55 | */
56 | public static boolean isNmsSupported() {
57 | return NMS_MANAGER != null;
58 | }
59 |
60 | private static void checkNmsSupported() {
61 | if(!isNmsSupported())
62 | throw new NMSException("Unsupported Nms.");
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/NmsManager.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms;
2 |
3 | import org.bukkit.block.BlockState;
4 | import org.bukkit.entity.Entity;
5 | import org.bukkit.inventory.ItemStack;
6 |
7 | import team.unstudio.udpl.nms.entity.NmsEntity;
8 | import team.unstudio.udpl.nms.inventory.NmsItemStack;
9 | import team.unstudio.udpl.nms.nbt.NmsNBT;
10 | import team.unstudio.udpl.nms.tileentity.NmsTileEntity;
11 |
12 | public interface NmsManager {
13 | NmsNBT getNmsNBT();
14 |
15 | NmsItemStack createNmsItemStack(ItemStack itemStack);
16 |
17 | NmsEntity createNmsEntity(Entity entity);
18 |
19 | NmsTileEntity createNmsTileEntity(BlockState state);
20 | }
21 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/entity/NmsEntity.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.entity;
2 |
3 | import java.util.Set;
4 |
5 | import org.bukkit.entity.Entity;
6 | import team.unstudio.udpl.nms.NmsHelper;
7 | import team.unstudio.udpl.nms.nbt.NBTTagCompound;
8 |
9 | public interface NmsEntity {
10 |
11 | Entity getBukkitEntity();
12 |
13 | NBTTagCompound save();
14 |
15 | void load(NBTTagCompound nbt);
16 |
17 | Set getScoreboardTags();
18 |
19 | boolean addScoreboardTag(String tag);
20 |
21 | boolean removeScoreboardTag(String tag);
22 |
23 | static NmsEntity createNmsEntity(Entity entity){
24 | return NmsHelper.createNmsEntity(entity);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/entity/NmsLivingEntity.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.entity;
2 |
3 | import org.bukkit.entity.LivingEntity;
4 |
5 | public interface NmsLivingEntity extends NmsEntity{
6 |
7 | LivingEntity getBukkitEntity();
8 | }
9 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/entity/NmsPlayer.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.entity;
2 |
3 | import org.bukkit.entity.Player;
4 |
5 | import java.util.Locale;
6 |
7 | public interface NmsPlayer extends NmsLivingEntity{
8 |
9 | Player getBukkitEntity();
10 |
11 | Locale getLocale();
12 |
13 | /**
14 | * 发送一个包
15 | */
16 | void sendPacket(Object packet);
17 |
18 | /**
19 | * 发送一个ActionBar
20 | */
21 | void sendActionBar(String message);
22 |
23 | /**
24 | * 发送一个Title
25 | */
26 | void sendTitle(String title, String subtitle);
27 |
28 | /**
29 | * 重置Title
30 | */
31 | void resetTitle();
32 | }
33 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/entity/fake/FakeEntity.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.entity.fake;
2 |
3 | public interface FakeEntity {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/entity/fake/FakeItem.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.entity.fake;
2 |
3 | public interface FakeItem {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/inventory/NmsItemStack.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.inventory;
2 |
3 | import org.bukkit.inventory.ItemStack;
4 | import team.unstudio.udpl.nms.NmsHelper;
5 | import team.unstudio.udpl.nms.nbt.NBTTagCompound;
6 |
7 | /**
8 | * {@link NmsHelper#createNmsItemStack(ItemStack)}
9 | */
10 | public interface NmsItemStack {
11 |
12 | /**
13 | * 获取{@link ItemStack}实例
14 | */
15 | ItemStack getBukkitItemStack();
16 |
17 | /**
18 | * 获取ItemStack的NBT数据
19 | * {@link NmsItemStack#setTag(NBTTagCompound)}
20 | */
21 | NBTTagCompound getTag();
22 |
23 | /**
24 | * 设置ItemStack的NBT数据
25 | * {@link NmsItemStack#getTag()}
26 | */
27 | void setTag(NBTTagCompound nbt);
28 |
29 | /**
30 | * 是否存在NBT数据
31 | * [{@link NmsItemStack#getTag()}
32 | */
33 | boolean hasTag();
34 |
35 | /**
36 | * 从NBT数据载入ItemStack
37 | * {@link NmsItemStack#save()}
38 | */
39 | void load(NBTTagCompound nbt);
40 |
41 | /**
42 | * 保持ItemStack为NBT数据
43 | * {@link NmsItemStack#load(NBTTagCompound)}
44 | */
45 | NBTTagCompound save();
46 |
47 | /**
48 | * 创建一个NmsItemStack对象
49 | */
50 | static NmsItemStack createNmsItemStack(ItemStack itemStack){
51 | return NmsHelper.createNmsItemStack(itemStack);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/mapping/MappingHelper.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.mapping;
2 |
3 | import team.unstudio.udpl.UDPLib;
4 | import team.unstudio.udpl.annotation.Init;
5 | import team.unstudio.udpl.exception.MemberMappingException;
6 | import team.unstudio.udpl.util.ServerUtils;
7 |
8 | public final class MappingHelper {
9 |
10 | private MappingHelper() {
11 | }
12 |
13 | private static MemberMapping memberMapping;
14 |
15 | public static MemberMapping getMemberMapping() {
16 | return memberMapping;
17 | }
18 |
19 | @Init
20 | public static void loadMapping() {
21 | loadMapping(ServerUtils.getMinecraftVersion());
22 | }
23 |
24 | private static void loadMapping(String version) {
25 | try {
26 | memberMapping = MemberMapping.fromClassLoader(version);
27 | UDPLib.getLogger().info("Loaded mapping " + version);
28 | } catch (MemberMappingException e) {
29 | UDPLib.debug(e);
30 | memberMapping = null;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTBase.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import org.bukkit.configuration.serialization.ConfigurationSerializable;
4 |
5 | public abstract class NBTBase implements ConfigurationSerializable{
6 | private NBTBaseType type;
7 |
8 | protected NBTBase(NBTBaseType type) {
9 | this.type = type;
10 | }
11 |
12 | public final NBTBaseType getType() {
13 | return this.type;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTBaseType.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | public enum NBTBaseType {
4 | STRING,
5 | BYTE,
6 | DOUBLE,
7 | FLOAT,
8 | INTEGER,
9 | INTARRAY,
10 | BYTEARRAY,
11 | LONGARRAY,
12 | COMPOUND,
13 | LIST,
14 | LONG,
15 | SHORT,
16 | END;
17 | }
18 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTNumber.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | public abstract class NBTNumber extends NBTBase{
4 |
5 | protected NBTNumber(NBTBaseType type) {
6 | super(type);
7 | }
8 |
9 | public abstract byte getByte();
10 |
11 | public abstract short getShort();
12 |
13 | public abstract int getInt();
14 |
15 | public abstract long getLong();
16 |
17 | public abstract float getFloat();
18 |
19 | public abstract double getDouble();
20 | }
21 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagByte.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagByte extends NBTNumber {
8 | private byte value;
9 |
10 | public NBTTagByte(byte value) {
11 | super(NBTBaseType.BYTE);
12 | this.value = value;
13 | }
14 |
15 | public byte getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Byte.toString(this.value)+"B";
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagByte deserialize(Map map){
62 | return new NBTTagByte(((Number)map.get("value")).byteValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagByteArray.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Lists;
4 | import com.google.common.collect.Maps;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public final class NBTTagByteArray extends NBTBase {
10 | private byte[] value;
11 |
12 | public NBTTagByteArray(byte[] value) {
13 | super(NBTBaseType.BYTEARRAY);
14 | this.value = value;
15 | }
16 |
17 | public NBTTagByteArray(Byte[] value) {
18 | super(NBTBaseType.BYTEARRAY);
19 | this.value = new byte[value.length];
20 | for (int i = 0, size = value.length; i < size; i++)
21 | this.value[i] = value[i];
22 | }
23 |
24 | public byte[] getValue() {
25 | return this.value;
26 | }
27 |
28 | public String toString() {
29 | StringBuilder builder = new StringBuilder("[B;");
30 | for (byte aValue : value) builder.append(aValue).append(',');
31 |
32 | if(builder.charAt(builder.length()-1)==',')
33 | builder.deleteCharAt(builder.length()-1);
34 |
35 | return builder.append(']').toString();
36 | }
37 |
38 | @Override
39 | public Map serialize() {
40 | Map map = Maps.newLinkedHashMap();
41 | map.put("==", getClass().getName());
42 | List values = Lists.newLinkedList();
43 | for(byte value:getValue()) values.add(value);
44 | map.put("value", values);
45 | return map;
46 | }
47 |
48 | @SuppressWarnings("unchecked")
49 | public static NBTTagByteArray deserialize(Map map){
50 | List list = (List) map.get("value");
51 | byte[] bytes = new byte[list.size()];
52 | for (int i = 0, size = list.size(); i < size; i++)
53 | bytes[i] = list.get(i).byteValue();
54 | return new NBTTagByteArray(bytes);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagDouble.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagDouble extends NBTNumber {
8 | private double value;
9 |
10 | public NBTTagDouble(double value) {
11 | super(NBTBaseType.DOUBLE);
12 | this.value = value;
13 | }
14 |
15 | public double getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Double.toString(this.value)+"D";
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return (byte) value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return (short) value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return (int) value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return (long) value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return (float) value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagDouble deserialize(Map map){
62 | return new NBTTagDouble(((Number)map.get("value")).doubleValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagEnd.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public class NBTTagEnd extends NBTBase {
8 |
9 | public static final NBTTagEnd INSTANCE = new NBTTagEnd();
10 |
11 | private NBTTagEnd() {
12 | super(NBTBaseType.END);
13 | }
14 |
15 | public String toString() {
16 | return "End";
17 | }
18 |
19 | @Override
20 | public Map serialize() {
21 | Map map = Maps.newLinkedHashMap();
22 | map.put("==", getClass().getName());
23 | return map;
24 | }
25 |
26 | public static NBTTagEnd deserialize(Map map){
27 | return INSTANCE;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagFloat.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagFloat extends NBTNumber {
8 | private float value;
9 |
10 | public NBTTagFloat(float value) {
11 | super(NBTBaseType.FLOAT);
12 | this.value = value;
13 | }
14 |
15 | public float getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Float.toString(this.value)+"F";
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return (byte) value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return (short) value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return (int) value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return (long) value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagFloat deserialize(Map map){
62 | return new NBTTagFloat(((Number)map.get("value")).floatValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagInt.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagInt extends NBTNumber {
8 | private int value;
9 |
10 | public NBTTagInt(int value) {
11 | super(NBTBaseType.INTEGER);
12 | this.value = value;
13 | }
14 |
15 | public int getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Integer.toString(this.value);
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return (byte) value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return (short) value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagInt deserialize(Map map){
62 | return new NBTTagInt(((Number)map.get("value")).intValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagIntArray.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Lists;
4 | import com.google.common.collect.Maps;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public final class NBTTagIntArray extends NBTBase {
10 | private int[] value;
11 |
12 | public NBTTagIntArray(int[] value) {
13 | super(NBTBaseType.INTARRAY);
14 | this.value = value;
15 | }
16 |
17 | public NBTTagIntArray(Integer[] value) {
18 | super(NBTBaseType.INTARRAY);
19 | this.value = new int[value.length];
20 | for (int i = 0, size = value.length; i < size; i++)
21 | this.value[i] = value[i];
22 | }
23 |
24 | public int[] getValue() {
25 | return this.value;
26 | }
27 |
28 | public String toString() {
29 | StringBuilder builder = new StringBuilder("[I;");
30 | for (int aValue : value) builder.append(aValue).append(',');
31 |
32 | if(builder.charAt(builder.length()-1)==',')
33 | builder.deleteCharAt(builder.length()-1);
34 |
35 | return builder.append(']').toString();
36 | }
37 |
38 | @Override
39 | public Map serialize() {
40 | Map map = Maps.newLinkedHashMap();
41 | map.put("==", getClass().getName());
42 | List values = Lists.newLinkedList();
43 | for(int value:getValue()) values.add(value);
44 | map.put("value", values);
45 | return map;
46 | }
47 |
48 | @SuppressWarnings("unchecked")
49 | public static NBTTagIntArray deserialize(Map map){
50 | return new NBTTagIntArray(((List)map.get("value")).toArray(new Integer[0]));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagList.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Lists;
4 | import com.google.common.collect.Maps;
5 |
6 | import java.util.*;
7 | import java.util.stream.Stream;
8 |
9 | public final class NBTTagList extends NBTBase implements Iterable{
10 | private final List list = Lists.newLinkedList();
11 |
12 | public NBTTagList() {
13 | super(NBTBaseType.LIST);
14 | }
15 |
16 | public NBTTagList(Collection list) {
17 | this();
18 | this.list.addAll(list);
19 | }
20 |
21 | public NBTTagList(NBTBase... bases){
22 | this();
23 | Collections.addAll(list, bases);
24 | }
25 |
26 | public NBTBase get(int index) {
27 | return this.list.get(index);
28 | }
29 |
30 | public NBTTagList add(NBTBase value) {
31 | if(value.getType() != NBTBaseType.END)
32 | this.list.add(value);
33 | return this;
34 | }
35 |
36 | public int size() {
37 | return this.list.size();
38 | }
39 |
40 | public NBTTagList remove(int index) {
41 | this.list.remove(index);
42 | return this;
43 | }
44 |
45 | public NBTTagList set(int index, NBTBase value) {
46 | this.list.set(index, value);
47 | return this;
48 | }
49 |
50 | public NBTTagList clear() {
51 | this.list.clear();
52 | return this;
53 | }
54 |
55 | public Iterator iterator(){
56 | return list.iterator();
57 | }
58 |
59 | public Stream stream(){
60 | return list.stream();
61 | }
62 |
63 | public String toString() {
64 | StringBuilder builder = new StringBuilder("[");
65 | for (NBTBase aList : list) builder.append(aList).append(',');
66 |
67 | if(builder.charAt(builder.length()-1)==',')
68 | builder.deleteCharAt(builder.length()-1);
69 |
70 | return builder.append(']').toString();
71 | }
72 |
73 | @Override
74 | public Map serialize() {
75 | Map map = Maps.newLinkedHashMap();
76 | map.put("==", getClass().getName());
77 | map.put("value", this.list);
78 | return map;
79 | }
80 |
81 | @SuppressWarnings("unchecked")
82 | public static NBTTagList deserialize(Map map){
83 | return new NBTTagList((List) map.get("value"));
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagLong.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagLong extends NBTNumber {
8 | private long value;
9 |
10 | public NBTTagLong(long value) {
11 | super(NBTBaseType.LONG);
12 | this.value = value;
13 | }
14 |
15 | public long getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Long.toString(this.value)+"L";
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return (byte) value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return (short) value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return (int) value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagLong deserialize(Map map){
62 | return new NBTTagLong(((Number)map.get("value")).longValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagLongArray.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Lists;
4 | import com.google.common.collect.Maps;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public final class NBTTagLongArray extends NBTBase {
10 | private long[] value;
11 |
12 | public NBTTagLongArray(long[] value) {
13 | super(NBTBaseType.LONGARRAY);
14 | this.value = value;
15 | }
16 |
17 | public NBTTagLongArray(Long[] value) {
18 | super(NBTBaseType.LONGARRAY);
19 | this.value = new long[value.length];
20 | for (int i = 0, size = value.length; i < size; i++)
21 | this.value[i] = value[i];
22 | }
23 |
24 | public long[] getValue() {
25 | return this.value;
26 | }
27 |
28 | public String toString() {
29 | StringBuilder builder = new StringBuilder("[L;");
30 | for (long aValue : value) builder.append(aValue).append(',');
31 |
32 | if(builder.charAt(builder.length()-1)==',')
33 | builder.deleteCharAt(builder.length()-1);
34 |
35 | return builder.append(']').toString();
36 | }
37 |
38 | @Override
39 | public Map serialize() {
40 | Map map = Maps.newLinkedHashMap();
41 | map.put("==", getClass().getName());
42 | List values = Lists.newLinkedList();
43 | for(long value:getValue()) values.add(value);
44 | map.put("value", values);
45 | return map;
46 | }
47 |
48 | @SuppressWarnings("unchecked")
49 | public static NBTTagLongArray deserialize(Map map){
50 | return new NBTTagLongArray(((List)map.get("value")).toArray(new Long[0]));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagShort.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagShort extends NBTNumber {
8 | private short value;
9 |
10 | public NBTTagShort(short value) {
11 | super(NBTBaseType.DOUBLE);
12 | this.value = value;
13 | }
14 |
15 | public short getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return Short.toString(this.value)+"S";
21 | }
22 |
23 | @Override
24 | public byte getByte() {
25 | return (byte) value;
26 | }
27 |
28 | @Override
29 | public short getShort() {
30 | return value;
31 | }
32 |
33 | @Override
34 | public int getInt() {
35 | return value;
36 | }
37 |
38 | @Override
39 | public long getLong() {
40 | return value;
41 | }
42 |
43 | @Override
44 | public float getFloat() {
45 | return value;
46 | }
47 |
48 | @Override
49 | public double getDouble() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public Map serialize() {
55 | Map map = Maps.newLinkedHashMap();
56 | map.put("==", getClass().getName());
57 | map.put("value", getValue());
58 | return map;
59 | }
60 |
61 | public static NBTTagShort deserialize(Map map){
62 | return new NBTTagShort(((Number)map.get("value")).shortValue());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NBTTagString.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import com.google.common.collect.Maps;
4 |
5 | import java.util.Map;
6 |
7 | public final class NBTTagString extends NBTBase {
8 | private String value;
9 |
10 | public NBTTagString(String value) {
11 | super(NBTBaseType.STRING);
12 | this.value = value;
13 | }
14 |
15 | public String getValue() {
16 | return this.value;
17 | }
18 |
19 | public String toString() {
20 | return "\""+this.value+"\"";
21 | }
22 |
23 | @Override
24 | public Map serialize() {
25 | Map map = Maps.newLinkedHashMap();
26 | map.put("==", getClass().getName());
27 | map.put("value", getValue());
28 | return map;
29 | }
30 |
31 | public static NBTTagString deserialize(Map map){
32 | return new NBTTagString((String) map.get("value"));
33 | }
34 | }
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/nbt/NmsNBT.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.nbt;
2 |
3 | import team.unstudio.udpl.nms.NmsHelper;
4 |
5 | /**
6 | * NBT的转换接口
7 | */
8 | public interface NmsNBT {
9 |
10 | /**
11 | * 转换为Compound
12 | */
13 | NBTTagCompound toCompound(Object nbt);
14 |
15 | /**
16 | * 转换为List
17 | */
18 | NBTTagList toList(Object nbt);
19 |
20 | /**
21 | * 转换为Byte
22 | */
23 | NBTTagByte toByte(Object nbt);
24 |
25 | /**
26 | * 转换为Short
27 | */
28 | NBTTagShort toShort(Object nbt);
29 |
30 | /**
31 | * 转换为Int
32 | */
33 | NBTTagInt toInt(Object nbt);
34 |
35 | /**
36 | * 转换为Long
37 | */
38 | NBTTagLong toLong(Object nbt);
39 |
40 | /**
41 | * 转换为Float
42 | */
43 | NBTTagFloat toFloat(Object nbt);
44 |
45 | /**
46 | * 转换为Double
47 | */
48 | NBTTagDouble toDouble(Object nbt);
49 |
50 | /**
51 | * 转换为String
52 | */
53 | NBTTagString toString(Object nbt);
54 |
55 | /**
56 | * 转换为ByteArray
57 | */
58 | NBTTagByteArray toByteArray(Object nbt);
59 |
60 | /**
61 | * 转换为IntArray
62 | */
63 | NBTTagIntArray toIntArray(Object nbt);
64 |
65 | /**
66 | * 转换为NBTBase
67 | */
68 | NBTBase toNBTBase(Object nbt);
69 |
70 | /**
71 | * 转换为 {@link net.minecraft.server} 的NBT类
72 | */
73 | Object toNmsNBT(NBTBase nbt);
74 |
75 | /**
76 | * 转换为NBT的Json格式
77 | */
78 | String toNBTJson(NBTTagCompound nbt);
79 |
80 | /**
81 | * 从NBT的Json格式读取
82 | */
83 | NBTTagCompound parseNBTJson(String json);
84 |
85 | static NmsNBT getInstance(){
86 | return NmsHelper.getNmsNBT();
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/network/NmsNetwork.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.network;
2 |
3 | import org.bukkit.entity.Player;
4 |
5 | public interface NmsNetwork {
6 |
7 | void inject(Player player);
8 | }
9 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/tileentity/NmsMobSpawner.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.tileentity;
2 |
3 | import org.bukkit.block.CreatureSpawner;
4 | import org.bukkit.entity.Entity;
5 | import team.unstudio.udpl.nms.NmsHelper;
6 | import team.unstudio.udpl.nms.nbt.NBTTagCompound;
7 | import team.unstudio.udpl.nms.nbt.NBTTagList;
8 |
9 | public interface NmsMobSpawner extends NmsTileEntity{
10 |
11 | CreatureSpawner getBukkitBlockState();
12 |
13 | NBTTagList getSpawnEntities();
14 |
15 | void addSpawnEntities(NBTTagCompound nbt, int weight);
16 |
17 | default void addSpawnEntities(Entity entity, int weight){
18 | NBTTagCompound entityNbt = NmsHelper.createNmsEntity(entity).save();
19 | entityNbt.remove("Pos");
20 | entityNbt.remove("Motion");
21 | entityNbt.remove("Rotation");
22 | addSpawnEntities(entityNbt, weight);
23 | }
24 |
25 | NBTTagCompound getSpawnEntity();
26 |
27 | void setSpawnEntity(NBTTagCompound nbt);
28 |
29 | default void setDisplayEntity(Entity entity){
30 | NBTTagCompound entityNbt = NmsHelper.createNmsEntity(entity).save();
31 | entityNbt.remove("Pos");
32 | entityNbt.remove("Motion");
33 | entityNbt.remove("Rotation");
34 | setSpawnEntity(entityNbt);
35 | }
36 |
37 | short getSpawnCount();
38 |
39 | void setSpawnCount(short count);
40 |
41 | short getSpawnRange();
42 |
43 | void setSpawnRange(short range);
44 |
45 | short getDelay();
46 |
47 | void setDelay(short delay);
48 |
49 | short getMinSpawnDelay();
50 |
51 | void setMinSpawnDelay(short minDelay);
52 |
53 | short getMaxSpawnDelay();
54 |
55 | void setMaxSpawnDelay(short maxDelay);
56 |
57 | short getMaxNearbyEntities();
58 |
59 | void setMaxNearbyEntities(short value);
60 |
61 | short getRequiredPlayerRange();
62 |
63 | void setRequiredPlayerRange(short range);
64 | }
65 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/tileentity/NmsTileEntity.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.tileentity;
2 |
3 | import org.bukkit.block.BlockState;
4 | import team.unstudio.udpl.nms.NmsHelper;
5 | import team.unstudio.udpl.nms.nbt.NBTTagCompound;
6 |
7 | public interface NmsTileEntity {
8 |
9 | BlockState getBukkitBlockState();
10 |
11 | NBTTagCompound save();
12 |
13 | void load(NBTTagCompound nbt);
14 |
15 | static NmsTileEntity createNmsTileEntity(BlockState blockState){
16 | return NmsHelper.createNmsTileEntity(blockState);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/nms/util/NmsClassLoader.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.nms.util;
2 |
3 | import team.unstudio.udpl.util.BukkitVersion;
4 | import team.unstudio.udpl.util.ServerUtils;
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.security.SecureClassLoader;
8 |
9 | public class NmsClassLoader extends SecureClassLoader {
10 |
11 | private final NmsClassTransformer transformer;
12 |
13 | public NmsClassLoader(){
14 | this(BukkitVersion.getCurrentBukkitVersion().getPackageName(), ServerUtils.getMinecraftVersion());
15 | }
16 |
17 | public NmsClassLoader(String targetNmsVersion, String targetMinecraftVersion) {
18 | transformer = new NmsClassTransformer(targetNmsVersion, targetMinecraftVersion);
19 | }
20 |
21 | public NmsClassLoader(ClassLoader parent) {
22 | this(parent, BukkitVersion.getCurrentBukkitVersion().getPackageName(), ServerUtils.getMinecraftVersion());
23 | }
24 |
25 | public NmsClassLoader(ClassLoader parent, String targetNmsVersion, String targetMinecraftVersion) {
26 | super(parent);
27 | transformer = new NmsClassTransformer(targetNmsVersion, targetMinecraftVersion);
28 | }
29 |
30 | public Class> loadClass(InputStream input, String bukkitVersion, String minecraftVersion) throws IOException{
31 | byte[] b = transformer.transform(input, bukkitVersion, minecraftVersion);
32 | return loadClass(null, b, 0, b.length);
33 | }
34 |
35 | public Class> loadClass(byte[] bytes, String bukkitVersion, String minecraftVersion) throws IOException{
36 | byte[] b = transformer.transform(bytes, bukkitVersion, minecraftVersion);
37 | return loadClass(null, b, 0, b.length);
38 | }
39 |
40 | public Class> loadClass(String name, byte[] b, int off, int len) {
41 | return defineClass(name, b, off, len);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/scoreboard/BiScoreboard.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.scoreboard;
2 |
3 | import com.google.common.collect.BiMap;
4 | import com.google.common.collect.HashBiMap;
5 | import org.bukkit.Bukkit;
6 | import org.bukkit.scoreboard.DisplaySlot;
7 |
8 | import java.util.Collection;
9 | import java.util.Map;
10 | import java.util.Set;
11 |
12 | /**
13 | * 一个名称与分数唯一对应的计分板
14 | */
15 | public class BiScoreboard extends ScoreboardWrapper {
16 |
17 | private final BiMap keyToScore = HashBiMap.create();
18 |
19 | public BiScoreboard(){
20 | scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
21 | objective = scoreboard.registerNewObjective("ScoreboardAPI", "dummy");
22 | objective.setDisplaySlot(DisplaySlot.SIDEBAR);
23 | }
24 |
25 | @Override
26 | public void put(String key, int score){
27 | String oldKey = getKey(score);
28 | if(oldKey != null)
29 | scoreboard.resetScores(oldKey);
30 | objective.getScore(key).setScore(score);
31 | keyToScore.forcePut(key, score);
32 | }
33 |
34 | public void put(int score, String key){
35 | this.put(key, score);
36 | }
37 |
38 | public void putAll(Map map){
39 | map.forEach(this::put);
40 | }
41 |
42 | public void putAllInverse(Map map){
43 | map.forEach(this::put);
44 | }
45 |
46 | @Override
47 | public void remove(String key){
48 | super.remove(key);
49 | keyToScore.remove(key);
50 | }
51 |
52 | public void remove(int score){
53 | BiMap scoreToKey = keyToScore.inverse();
54 | if(scoreToKey.containsKey(score)){
55 | super.remove(scoreToKey.get(score));
56 | scoreToKey.remove(score);
57 | }
58 | }
59 |
60 | public void removeAllInverse(Collection scores){
61 | scores.forEach(this::remove);
62 | }
63 |
64 | @Override
65 | public Set getKeys(){
66 | return keyToScore.keySet();
67 | }
68 |
69 | public Set getScores(){
70 | return keyToScore.inverse().keySet();
71 | }
72 |
73 | public String getKey(int score){
74 | return keyToScore.inverse().get(score);
75 | }
76 |
77 | public int getScore(String key){
78 | return keyToScore.get(key);
79 | }
80 |
81 | public boolean containKey(String key){
82 | return keyToScore.containsKey(key);
83 | }
84 |
85 | public boolean containScore(int score){
86 | return keyToScore.inverse().containsKey(score);
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/scoreboard/ScoreboardHelper.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.scoreboard;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.entity.Player;
5 | import org.bukkit.scoreboard.Scoreboard;
6 |
7 | public interface ScoreboardHelper {
8 | static void setPlayerScoreboard(Player player, Scoreboard scoreboard){
9 | player.setScoreboard(scoreboard);
10 | }
11 |
12 | static void clear(Player player){
13 | player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
14 | }
15 |
16 | static void reset(Player player){
17 | player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
18 | }
19 |
20 | static Scoreboard getNewScoreboard(){
21 | return Bukkit.getScoreboardManager().getNewScoreboard();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/scoreboard/ScoreboardWrapper.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.scoreboard;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.entity.Player;
5 | import org.bukkit.scoreboard.DisplaySlot;
6 | import org.bukkit.scoreboard.Objective;
7 | import org.bukkit.scoreboard.Scoreboard;
8 |
9 | import java.util.Collection;
10 | import java.util.Map;
11 | import java.util.Set;
12 | import java.util.stream.Collectors;
13 |
14 | public class ScoreboardWrapper {
15 |
16 | protected Scoreboard scoreboard;
17 | protected Objective objective;
18 |
19 | public ScoreboardWrapper() {
20 | }
21 |
22 | public ScoreboardWrapper(Scoreboard scoreboard, Objective objective){
23 | this.scoreboard = scoreboard;
24 | this.objective = objective;
25 | }
26 |
27 | public ScoreboardWrapper(Objective objective) {
28 | this(Bukkit.getScoreboardManager().getNewScoreboard(), objective);
29 | }
30 |
31 | public Scoreboard getScoreboard(){
32 | return scoreboard;
33 | }
34 |
35 | public Objective getObjective(){
36 | return objective;
37 | }
38 |
39 | public void setDisplayerSlot(DisplaySlot slot){
40 | objective.setDisplaySlot(slot);
41 | }
42 |
43 | public String getDisplayName(){
44 | return objective.getDisplayName();
45 | }
46 |
47 | public void setDisplayName(String name){
48 | objective.setDisplayName(name);
49 | }
50 |
51 | public void put(String key,int score){
52 | objective.getScore(key).setScore(score);
53 | }
54 |
55 | public void putAll(Map map){
56 | map.forEach((key, value) -> put(key, value));
57 | }
58 |
59 | public void remove(String key){
60 | scoreboard.resetScores(key);
61 | }
62 |
63 | public void removeAll(Collection keys){
64 | keys.forEach(this::remove);
65 | }
66 |
67 | public Set getKeys(){
68 | return scoreboard.getEntries();
69 | }
70 |
71 | public Set getKeys(int score){
72 | return getKeys().stream().filter(key->getScore(key) == score).collect(Collectors.toSet());
73 | }
74 |
75 | public int getScore(String key){
76 | return objective.getScore(key).getScore();
77 | }
78 |
79 | public void display(Player player){
80 | player.setScoreboard(getScoreboard());
81 | }
82 |
83 | public void reset(Player player){
84 | player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/ui/UIFactory.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.ui;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.event.inventory.InventoryType;
5 |
6 | /**
7 | * 界面的工厂类
8 | * @author AAA
9 | *
10 | */
11 | public interface UIFactory {
12 |
13 | /**
14 | * 创建一个界面
15 | * @param size 界面大小(9的倍数,即槽数量)
16 | * @param title 标题
17 | */
18 | static UI createUI(int size, String title){
19 | return new UI(Bukkit.createInventory(null,size,title));
20 | }
21 |
22 | /**
23 | * 创建一个界面
24 | * @param type 界面类型
25 | * @param title 标题
26 | */
27 | static UI createUI(InventoryType type, String title){
28 | return new UI(Bukkit.createInventory(null, type, title));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/ActionBarUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import com.comphenix.protocol.PacketType;
4 | import com.comphenix.protocol.ProtocolManager;
5 | import com.comphenix.protocol.events.PacketContainer;
6 | import com.comphenix.protocol.wrappers.EnumWrappers;
7 | import com.comphenix.protocol.wrappers.WrappedChatComponent;
8 | import org.bukkit.entity.Player;
9 |
10 | /**
11 | * An action bar helper with ProtocolLib,
12 | * also called overlay message in client.
13 | */
14 | public interface ActionBarUtils {
15 | /**
16 | * sending an action bar to a player
17 | *
18 | * @param player the player to send
19 | * @param text the message to send
20 | */
21 | static Result send(Player player, String text){
22 | PacketContainer container = ProtocolLibUtils.of(PacketType.Play.Server.CHAT);
23 | container.getChatComponents().write(0, WrappedChatComponent.fromJson("{\"text\": \"" + text + "\"}"));
24 | if (container.getBytes().size() > 0) {
25 | container.getBytes().write(0, (byte) 2);
26 | } else if (container.getEnumModifier(EnumWrappers.ChatType.class, 2).size() > 0) {
27 | container.getEnumModifier(EnumWrappers.ChatType.class, 2).write(0, EnumWrappers.ChatType.GAME_INFO);
28 | }
29 |
30 | return ProtocolLibUtils.send(player, container);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/BlockUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import com.comphenix.protocol.PacketType;
4 | import com.comphenix.protocol.ProtocolLibrary;
5 | import com.comphenix.protocol.ProtocolManager;
6 | import com.comphenix.protocol.events.PacketContainer;
7 | import com.comphenix.protocol.wrappers.BlockPosition;
8 | import org.bukkit.Location;
9 | import org.bukkit.entity.Player;
10 |
11 | import java.lang.reflect.InvocationTargetException;
12 |
13 | public interface BlockUtils {
14 | ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary.getProtocolManager();
15 |
16 | /**
17 | * 0–9 are the displayable destroy stages and each other number means that there is no animation on this coordinate.
18 | * Block break animations can still be applied on air; the animation will remain visible although there is no block being broken. However, if this is applied to a transparent block, odd graphical effects may happen, including water losing its transparency. (An effect similar to this can be seen in normal gameplay when breaking ice blocks)
19 | * If you need to display several break animations at the same time you have to give each of them a unique Entity ID.
20 | */
21 | static Result sendBlockBreakAnimation(Player player, Location location, byte state) {
22 | return sendBlockBreakAnimation(player, player.getEntityId(), location, state);
23 | }
24 |
25 | /**
26 | * 0–9 are the displayable destroy stages and each other number means that there is no animation on this coordinate.
27 | * Block break animations can still be applied on air; the animation will remain visible although there is no block being broken. However, if this is applied to a transparent block, odd graphical effects may happen, including water losing its transparency. (An effect similar to this can be seen in normal gameplay when breaking ice blocks)
28 | * If you need to display several break animations at the same time you have to give each of them a unique Entity ID.
29 | */
30 | static Result sendBlockBreakAnimation(Player player, int entityId, Location location, byte state) {
31 | PacketContainer container = PROTOCOL_MANAGER.createPacket(PacketType.Play.Server.BLOCK_BREAK_ANIMATION);
32 | container.getIntegers().write(0, entityId);
33 | container.getBlockPositionModifier().write(0,
34 | new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
35 | container.getIntegers().write(1, (int) state);
36 |
37 | try {
38 | PROTOCOL_MANAGER.sendServerPacket(player, container);
39 | return Result.success();
40 | } catch (InvocationTargetException e) {
41 | return Result.failure(e);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/BukkitVersion.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import team.unstudio.udpl.util.reflect.ReflectionUtils;
4 |
5 | /**
6 | * Bukkit version checker
7 | */
8 | public enum BukkitVersion {
9 |
10 | V1_13_R2("v1_13_R2","1.13.1"),
11 | V1_13_R1("v1_13_R1","1.13"),
12 | V1_12_R1("v1_12_R1","1.12.2"),
13 | V1_11_R1("v1_11_R1","1.11.2"),
14 | V1_10_R1("v1_10_R1","1.10.2"),
15 | V1_9_R2("v1_9_R2","1.9.4"),
16 | V1_9_R1("v1_9_R1","1.9"),
17 | V1_8_R3("v1_8_R3","1.8.7"),
18 | V1_8_R2("v1_8_R2","1.8.3"),
19 | V1_8_R1("v1_8_R1","1.8"),
20 | UNKNOWN(ReflectionUtils.PackageType.getServerVersion(), ServerUtils.getMinecraftVersion());
21 |
22 | private final String packetName;
23 | private final String lastMinecraftVersion;
24 |
25 | BukkitVersion(String packetName,String lastMinecraftVersion) {
26 | this.packetName = packetName;
27 | this.lastMinecraftVersion = lastMinecraftVersion;
28 | }
29 |
30 | /**
31 | * if version equal or newer than the version you gave
32 | * {@code V1_12_R1.isAbove(V1_8_R1) == true}
33 | *
34 | * @param value the version to check
35 | * @return true if newer or the same
36 | */
37 | public boolean isAbove(BukkitVersion value){
38 | return compareTo(value) <= 0;
39 | }
40 |
41 |
42 | /**
43 | * if version equal or older than the version you gave.
44 | * {@code V1_8_R1.isBelow(V1_12_R1) == true}
45 | *
46 | * @param value the version to check
47 | * @return true if older or the same
48 | */
49 | public boolean isBelow(BukkitVersion value){
50 | return compareTo(value) >= 0;
51 | }
52 |
53 | /**
54 | * if version equal the current version.
55 | * In version 1.12.2, you will get
56 | * {@code V1_12_R1.isCurrent() == true}
57 | *
58 | * @return true if equal the current version
59 | */
60 | public boolean isCurrent() {
61 | return this == getCurrentBukkitVersion();
62 | }
63 |
64 | /**
65 | * get package name like "v1_12_R1"
66 | */
67 | public String getPackageName(){
68 | return packetName;
69 | }
70 |
71 | public String getLastMinecraftVersion(){
72 | return lastMinecraftVersion;
73 | }
74 |
75 | private static BukkitVersion CURRENT_BUKKIT_VERSION;
76 |
77 | /**
78 | * get current bukkit version
79 | */
80 | public static BukkitVersion getCurrentBukkitVersion(){
81 | if(CURRENT_BUKKIT_VERSION == null){
82 | CURRENT_BUKKIT_VERSION = valueOf(ReflectionUtils.PackageType.getServerVersion().toUpperCase());
83 | if(CURRENT_BUKKIT_VERSION == null)
84 | CURRENT_BUKKIT_VERSION = BukkitVersion.UNKNOWN;
85 | }
86 | return CURRENT_BUKKIT_VERSION;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/ChatUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import org.apache.commons.lang3.NotImplementedException;
4 | import org.bukkit.ChatColor;
5 | import org.bukkit.command.CommandSender;
6 | import org.bukkit.entity.Player;
7 |
8 | public interface ChatUtils {
9 |
10 | String SPLITTER = translateColorCodes("&l&m----------------------------------------------------------------");
11 | char DEFAULT_COLOR_CHAR = '&';
12 |
13 | /**
14 | * Send splitter line.
15 | * 发送分割线
16 | */
17 | static void sendSplitter(CommandSender sender){
18 | sender.sendMessage(SPLITTER);
19 | }
20 |
21 | /**
22 | * Send blank line.
23 | * 发送空行
24 | */
25 | static void sendEmpty(CommandSender sender){
26 | sender.sendMessage("");
27 | }
28 |
29 | /**
30 | * 发送清屏消息
31 | */
32 | static void sendCleanScreen(CommandSender sender) {
33 | for (int i = 0; i < 20; i++) sendEmpty(sender);
34 | }
35 |
36 | /**
37 | * 将文本中'&'转换为颜色字符
38 | */
39 | static String translateColorCodes(String textToTranslate){
40 | return ChatColor.translateAlternateColorCodes(DEFAULT_COLOR_CHAR, textToTranslate);
41 | }
42 |
43 | /**
44 | * 玩家停止接收消息
45 | * 该方法将会将发送给玩家的消息拦截并缓存,直到调用{@link ChatUtils#startReceiveChat(Player)}方法。
46 | * @see ChatUtils#startReceiveChat(Player)
47 | */
48 | static void stopReceiveChat(Player player) {
49 | throw new NotImplementedException("");
50 | }
51 |
52 | /**
53 | * 玩家开始接收消息
54 | * 该方法将会将已拦截的玩家消息发送,并且该发送是逐步的,使玩家可以阅读未读的消息。
55 | * @see ChatUtils#stopReceiveChat(Player)
56 | */
57 | static void startReceiveChat(Player player) {
58 | throw new NotImplementedException("");
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/EntityUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import com.comphenix.protocol.PacketType;
4 | import com.comphenix.protocol.ProtocolManager;
5 | import com.comphenix.protocol.events.PacketContainer;
6 | import com.comphenix.protocol.wrappers.WrappedDataWatcher;
7 | import com.google.common.base.Strings;
8 | import org.bukkit.Location;
9 | import org.bukkit.entity.Player;
10 | import org.bukkit.inventory.ItemStack;
11 |
12 | import javax.annotation.Nonnull;
13 | import javax.annotation.Nullable;
14 | import java.util.UUID;
15 | import java.util.concurrent.atomic.AtomicInteger;
16 |
17 | public interface EntityUtils {
18 | AtomicInteger nextEntityID = new AtomicInteger(Integer.MAX_VALUE);
19 |
20 | @Deprecated
21 | static Result sendFakeItemEntity(@Nonnull Player player, @Nonnull ItemStack itemStack, @Nonnull Location location, @Nullable String displayName){
22 | int entityID = nextEntityID.getAndDecrement();
23 | PacketContainer spawnEntityLiving = ProtocolLibUtils.of(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
24 | spawnEntityLiving.getIntegers().write(0, entityID); //Entity ID
25 | spawnEntityLiving.getUUIDs().write(0, UUID.randomUUID()); //Entity UUID
26 | spawnEntityLiving.getIntegers().write(1, 2); //Entity Type
27 | spawnEntityLiving.getDoubles().write(0, location.getX())
28 | .write(1, location.getY())
29 | .write(2, location.getZ());
30 | spawnEntityLiving.getIntegers().write(2, 0) //Pitch
31 | .write(3, 0) //Yaw
32 | //Data
33 | .write(4, 1)
34 | //Velocity(X,Y,Z)
35 | .write(5, 0)
36 | .write(6, 0)
37 | .write(7, 0);
38 |
39 | PacketContainer entityMetadata = ProtocolLibUtils.of(PacketType.Play.Server.ENTITY_METADATA);
40 | entityMetadata.getIntegers().write(0, entityID);
41 | WrappedDataWatcher dataWatcher = new WrappedDataWatcher();
42 | dataWatcher.setObject(0, (byte) 0);
43 | dataWatcher.setObject(1, 300);
44 | dataWatcher.setObject(2, Strings.nullToEmpty(displayName));
45 | dataWatcher.setObject(3, !Strings.isNullOrEmpty(displayName));
46 | dataWatcher.setObject(4, false);
47 | dataWatcher.setObject(5, true);
48 | dataWatcher.setObject(6, itemStack);
49 | entityMetadata.getWatchableCollectionModifier().write(0, dataWatcher.getWatchableObjects());
50 |
51 | return ProtocolLibUtils.send(player, spawnEntityLiving, entityMetadata);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/Hologram.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import org.apache.commons.lang3.NotImplementedException;
4 |
5 | /**
6 | * Hologram Util haven't been developed.
7 | */
8 | public interface Hologram {
9 | }
10 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/PluginUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import org.apache.commons.lang.Validate;
4 | import org.bukkit.Bukkit;
5 | import org.bukkit.event.Cancellable;
6 | import org.bukkit.event.Event;
7 | import org.bukkit.event.Listener;
8 | import org.bukkit.plugin.Plugin;
9 | import org.bukkit.plugin.java.JavaPlugin;
10 |
11 | import javax.annotation.Nonnull;
12 | import java.io.IOException;
13 | import java.net.JarURLConnection;
14 | import java.net.URL;
15 | import java.util.Enumeration;
16 | import java.util.jar.JarEntry;
17 | import java.util.jar.JarFile;
18 |
19 | public interface PluginUtils {
20 | /**
21 | * 将 Jar 包中的文件夹,复制到插件的数据目录中
22 | *
23 | * @param plugin 插件实例
24 | * @param resourcePath 文件在 Jar 包中的路径
25 | * @param replace 是否替换文件
26 | */
27 | static void saveDirectory(@Nonnull JavaPlugin plugin, @Nonnull String resourcePath, boolean replace){
28 | Validate.notNull(plugin);
29 | Validate.notEmpty(resourcePath);
30 | resourcePath = resourcePath.replace('\\', '/');
31 |
32 | plugin.getLogger().info("Plugin save directory. Path: " + resourcePath);
33 |
34 | URL url = plugin.getClass().getClassLoader().getResource(resourcePath);
35 | if(url == null)
36 | throw new IllegalArgumentException("Directory isn't found. Path: "+resourcePath);
37 |
38 | JarURLConnection jarConn;
39 | try {
40 | jarConn = (JarURLConnection) url.openConnection();
41 | JarFile jarFile = jarConn.getJarFile();
42 | Enumeration entrys = jarFile.entries();
43 | while(entrys.hasMoreElements()){
44 | JarEntry entry = entrys.nextElement();
45 | if(entry.getName().startsWith(resourcePath)&&!entry.isDirectory())
46 | plugin.saveResource(entry.getName(), replace);
47 | }
48 | } catch (IOException e) {
49 | plugin.getLogger().warning("Plugin save directory failed. Path: " + resourcePath);
50 | e.printStackTrace();
51 | }
52 | }
53 |
54 | static void registerEvents(Listener listener, Plugin plugin){
55 | Bukkit.getPluginManager().registerEvents(listener, plugin);
56 | }
57 |
58 | /**
59 | * Call event.
60 | */
61 | static T callEvent(T event) {
62 | Bukkit.getPluginManager().callEvent(event);
63 | return event;
64 | }
65 |
66 | /**
67 | * Call event.
68 | *
69 | * @param event the event to be call
70 | * @return event.isCancelled() or false
71 | */
72 | static boolean callEventIsCancelled(Event event) {
73 | Bukkit.getPluginManager().callEvent(event);
74 | return event instanceof Cancellable && ((Cancellable) event).isCancelled();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/Result.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import com.google.common.base.Strings;
4 |
5 | import javax.annotation.Nullable;
6 |
7 | public final class Result {
8 |
9 | private static final Result SUCCESS_RESULT = new Result(true);
10 |
11 | public static Result success() {
12 | return SUCCESS_RESULT;
13 | }
14 |
15 | public static Result failure() {
16 | return new Result(false);
17 | }
18 |
19 | public static Result failure(String message) {
20 | return new Result(false, message);
21 | }
22 |
23 | public static Result failure(Throwable throwable) {
24 | return new Result(false, throwable);
25 | }
26 |
27 | public static Result failure(String message, Throwable throwable) {
28 | return new Result(false, message, throwable);
29 | }
30 |
31 | private final boolean success;
32 | private final String message;
33 | private final Throwable throwable;
34 |
35 | private Result(boolean success) {
36 | this(success, null, null);
37 | }
38 |
39 | private Result(boolean success, String message) {
40 | this(success, message, null);
41 | }
42 |
43 | private Result(boolean success, Throwable throwable) {
44 | this(success, null, throwable);
45 | }
46 |
47 | private Result(boolean success, String message, Throwable throwable) {
48 | this.success = success;
49 | this.message = Strings.nullToEmpty(message);
50 | this.throwable = throwable;
51 | }
52 |
53 | public boolean isSuccess() {
54 | return success;
55 | }
56 |
57 | public boolean isFailure() {
58 | return !success;
59 | }
60 |
61 | public String getMessage() {
62 | return message;
63 | }
64 |
65 | @Nullable
66 | public Throwable getThrowable() {
67 | return throwable;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | return String.format("team.unstudio.udpl.util.Result[success: %b, message: %s, throwable: %s]", success, message == null ? "null" : message, throwable == null ? "null" : throwable.toString());
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/ServerUtils.java:
--------------------------------------------------------------------------------
1 | package team.unstudio.udpl.util;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.entity.HumanEntity;
5 | import team.unstudio.udpl.UDPLib;
6 | import team.unstudio.udpl.util.reflect.ReflectionUtils;
7 |
8 | import java.util.List;
9 | import java.util.concurrent.atomic.AtomicReference;
10 | import java.util.function.Predicate;
11 | import java.util.stream.Collectors;
12 |
13 | /**
14 | * Server util to quickly
15 | */
16 | public interface ServerUtils {
17 |
18 | /**
19 | * get online players' name
20 | */
21 | static List getOnlinePlayerNames(){
22 | return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
23 | }
24 |
25 | /**
26 | * get all online players' name with a filter
27 | */
28 | static List getOnlinePlayerNamesWithFilter(Predicate filter){
29 | return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).filter(filter).collect(Collectors.toList());
30 | }
31 |
32 | /**
33 | * cached minecraft version
34 | */
35 | AtomicReference MINECRAFT_VERSION = new AtomicReference<>();
36 |
37 | /**
38 | * get minecraft version like "1.11.2"
39 | */
40 | static String getMinecraftVersion() {
41 | if (MINECRAFT_VERSION.get() == null) {
42 | try {
43 | MINECRAFT_VERSION.set((String) ReflectionUtils.invokeMethod(
44 | ReflectionUtils.getValue(Bukkit.getServer(), true, "console"), "getVersion", false));
45 | } catch (ReflectiveOperationException e) {
46 | UDPLib.debug(e);
47 | String bukkitVersion = Bukkit.getBukkitVersion();
48 | int index = Bukkit.getBukkitVersion().indexOf("-");
49 | MINECRAFT_VERSION.set(bukkitVersion.substring(0, index == -1 ? bukkitVersion.length() : index));
50 | }
51 | }
52 | return MINECRAFT_VERSION.get();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/CurrentFrame.java:
--------------------------------------------------------------------------------
1 | /***
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 |
31 | package team.unstudio.udpl.util.asm;
32 |
33 | /**
34 | * Information about the input stack map frame at the "current" instruction of a
35 | * method. This is implemented as a Frame subclass for a "basic block"
36 | * containing only one instruction.
37 | *
38 | * @author Eric Bruneton
39 | */
40 | class CurrentFrame extends Frame {
41 |
42 | /**
43 | * Sets this CurrentFrame to the input stack map frame of the next "current"
44 | * instruction, i.e. the instruction just after the given one. It is assumed
45 | * that the value of this object when this method is called is the stack map
46 | * frame status just before the given instruction is executed.
47 | */
48 | @Override
49 | void execute(int opcode, int arg, ClassWriter cw, Item item) {
50 | super.execute(opcode, arg, cw, item);
51 | Frame successor = new Frame();
52 | merge(cw, successor, 0);
53 | set(successor);
54 | owner.inputStackTop = 0;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/commons/TableSwitchGenerator.java:
--------------------------------------------------------------------------------
1 | /***
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package team.unstudio.udpl.util.asm.commons;
31 |
32 | import team.unstudio.udpl.util.asm.Label;
33 |
34 | /**
35 | * A code generator for switch statements.
36 | *
37 | * @author Juozas Baliuka
38 | * @author Chris Nokleberg
39 | * @author Eric Bruneton
40 | */
41 | public interface TableSwitchGenerator {
42 |
43 | /**
44 | * Generates the code for a switch case.
45 | *
46 | * @param key
47 | * the switch case key.
48 | * @param end
49 | * a label that corresponds to the end of the switch statement.
50 | */
51 | void generateCase(int key, Label end);
52 |
53 | /**
54 | * Generates the code for the default switch case.
55 | */
56 | void generateDefault();
57 | }
58 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/signature/package.html:
--------------------------------------------------------------------------------
1 |
2 |
31 |
32 | Provides support for type signatures.
33 |
34 | @since ASM 2.0
35 |
36 |
37 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/tree/LabelNode.java:
--------------------------------------------------------------------------------
1 | /***
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package team.unstudio.udpl.util.asm.tree;
31 |
32 | import java.util.Map;
33 |
34 | import team.unstudio.udpl.util.asm.Label;
35 | import team.unstudio.udpl.util.asm.MethodVisitor;
36 |
37 | /**
38 | * An {@link AbstractInsnNode} that encapsulates a {@link Label}.
39 | */
40 | public class LabelNode extends AbstractInsnNode {
41 |
42 | private Label label;
43 |
44 | public LabelNode() {
45 | super(-1);
46 | }
47 |
48 | public LabelNode(final Label label) {
49 | super(-1);
50 | this.label = label;
51 | }
52 |
53 | @Override
54 | public int getType() {
55 | return LABEL;
56 | }
57 |
58 | public Label getLabel() {
59 | if (label == null) {
60 | label = new Label();
61 | }
62 | return label;
63 | }
64 |
65 | @Override
66 | public void accept(final MethodVisitor cv) {
67 | cv.visitLabel(getLabel());
68 | }
69 |
70 | @Override
71 | public AbstractInsnNode clone(final Map labels) {
72 | return labels.get(this);
73 | }
74 |
75 | public void resetLabel() {
76 | label = null;
77 | }
78 | }
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/tree/analysis/AnalyzerException.java:
--------------------------------------------------------------------------------
1 | /***
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package team.unstudio.udpl.util.asm.tree.analysis;
31 |
32 | import team.unstudio.udpl.util.asm.tree.AbstractInsnNode;
33 |
34 | /**
35 | * Thrown if a problem occurs during the analysis of a method.
36 | *
37 | * @author Bing Ran
38 | * @author Eric Bruneton
39 | */
40 | @SuppressWarnings("serial")
41 | public class AnalyzerException extends Exception {
42 |
43 | public final AbstractInsnNode node;
44 |
45 | public AnalyzerException(final AbstractInsnNode node, final String msg) {
46 | super(msg);
47 | this.node = node;
48 | }
49 |
50 | public AnalyzerException(final AbstractInsnNode node, final String msg,
51 | final Throwable exception) {
52 | super(msg, exception);
53 | this.node = node;
54 | }
55 |
56 | public AnalyzerException(final AbstractInsnNode node, final String msg,
57 | final Object expected, final Value encountered) {
58 | super((msg == null ? "Expected " : msg + ": expected ") + expected
59 | + ", but found " + encountered);
60 | this.node = node;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/tree/analysis/Value.java:
--------------------------------------------------------------------------------
1 | /***
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package team.unstudio.udpl.util.asm.tree.analysis;
31 |
32 | /**
33 | * An immutable symbolic value for semantic interpretation of bytecode.
34 | *
35 | * @author Eric Bruneton
36 | */
37 | public interface Value {
38 |
39 | /**
40 | * Returns the size of this value in words.
41 | *
42 | * @return either 1 or 2.
43 | */
44 | int getSize();
45 | }
46 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/tree/analysis/package.html:
--------------------------------------------------------------------------------
1 |
2 |
31 |
32 |
33 |
34 | Provides a framework for static code analysis based on the asm.tree package.
35 |
36 |
37 |
38 | Basic usage:
39 |
40 |
41 |
42 | ClassReader cr = new ClassReader(bytecode);
43 | ClassNode cn = new ClassNode();
44 | cr.accept(cn, ClassReader.SKIP_DEBUG);
45 |
46 | List methods = cn.methods;
47 | for (int i = 0; i < methods.size(); ++i) {
48 | MethodNode method = (MethodNode) methods.get(i);
49 | if (method.instructions.size() > 0) {
50 | Analyzer a = new Analyzer(new BasicInterpreter());
51 | a.analyze(cn.name, method);
52 | Frame[] frames = a.getFrames();
53 | // Elements of the frames arrray now contains info for each instruction
54 | // from the analyzed method. BasicInterpreter creates BasicValue, that
55 | // is using simplified type system that distinguishes the UNINITIALZED,
56 | // INT, FLOAT, LONG, DOUBLE, REFERENCE and RETURNADDRESS types.
57 | ...
58 | }
59 | }
60 |
61 |
62 |
63 | @since ASM 1.4.3
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/udplib-common/src/main/java/team/unstudio/udpl/util/asm/util/ASMifiable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * ASM: a very small and fast Java bytecode manipulation framework
3 | * Copyright (c) 2000-2011 INRIA, France Telecom
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | * 3. Neither the name of the copyright holders nor the names of its
15 | * contributors may be used to endorse or promote products derived from
16 | * this software without specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package team.unstudio.udpl.util.asm.util;
31 |
32 | import java.util.Map;
33 |
34 | import team.unstudio.udpl.util.asm.Label;
35 |
36 | /**
37 | * An {@link team.unstudio.udpl.util.asm.Attribute Attribute} that can print the ASM code
38 | * to create an equivalent attribute.
39 | *
40 | * @author Eugene Kuleshov
41 | */
42 | public interface ASMifiable {
43 |
44 | /**
45 | * Prints the ASM code to create an attribute equal to this attribute.
46 | *
47 | * @param buf
48 | * a buffer used for printing Java code.
49 | * @param varName
50 | * name of the variable in a printed code used to store attribute
51 | * instance.
52 | * @param labelNames
53 | * map of label instances to their names.
54 | */
55 | void asmify(StringBuffer buf, String varName, Map