1.14 supported

This commit is contained in:
坏黑 2019-06-24 21:53:36 +08:00
parent 483bba8f0a
commit 9a364bb7bc
15 changed files with 1127 additions and 640 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
'version': 2
'version': 3
'entity_null': '实体'
'entity_ArmorStand_name': '盔甲架'
'entity_Arrow_name': '箭'

View File

@ -90,7 +90,21 @@ public class NMSHandlerImpl extends NMSHandler {
@Override
public String getName(Entity entity) {
if (TabooLib.getVersionNumber() < 11300) {
if (TabooLib.getVersionNumber() >= 11400) {
net.minecraft.server.v1_14_R1.MinecraftKey minecraftKey = net.minecraft.server.v1_14_R1.EntityTypes.getName(((org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity) entity).getHandle().getEntityType());
return "entity.minecraft." + minecraftKey.getKey();
} else if (TabooLib.getVersionNumber() == 11300) {
try {
String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) entityTypesField.get(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle())).getKey();
if (entity instanceof Villager && ((CraftVillager) entity).getCareer() != null) {
name += "." + String.valueOf(((CraftVillager) entity).getCareer()).toLowerCase();
}
return name;
} catch (Throwable t) {
t.printStackTrace();
}
return "entity.null";
} else {
try {
if (entity instanceof Player) {
return "entity.Player.name";
@ -150,17 +164,6 @@ public class NMSHandlerImpl extends NMSHandler {
t.printStackTrace();
}
return "entity.null";
} else {
try {
String name = "entity.minecraft." + IRegistry.ENTITY_TYPE.getKey((net.minecraft.server.v1_13_R2.EntityTypes<?>) entityTypesField.get(((org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity) entity).getHandle())).getKey();
if (entity instanceof Villager && ((CraftVillager) entity).getCareer() != null) {
name += "." + String.valueOf(((CraftVillager) entity).getCareer()).toLowerCase();
}
return name;
} catch (Throwable t) {
t.printStackTrace();
}
return "entity.null";
}
}

View File

@ -37,7 +37,7 @@ public class SimpleI18n {
} else {
lang = ConfigUtils.load(TabooLib.instance(), localeFile);
}
if (lang.getInt("version") < 2 && !released) {
if (lang.getInt("version") < 3 && !released) {
released = true;
FileUtils.deleteAllFile(new File(Main.getInst().getDataFolder(), "simpleI18n"));
init();

View File

@ -15,10 +15,25 @@ public class CronusParser {
public static Location toLocation(Object in) {
String str = String.valueOf(in);
if (str.toLowerCase().startsWith("area=")) {
String[] area = str.substring("area=".length()).split("~");
return new Location(Location.Mode.AREA, new org.bukkit.Location[] {toBukkitLocation(area[0]), toBukkitLocation(area[area.length > 1 ? 1 : 0])}, null);
} else {
// 区域
// world:0,80,0~0,90,0
if (str.contains(":") && str.contains("~")) {
String[] area = str.split("~");
try {
return new Location(Location.Mode.AREA, new org.bukkit.Location[] {toBukkitLocation(area[0].replace(":", ",")), toBukkitLocation(area[0].split(":")[0] + "," + area[1])}, null);
} catch (Throwable ignored) {
return new Location(Location.Mode.AREA, null, null);
}
}
// 范围
// world:0,80,0 r:10
else if (str.contains("r:")) {
String[] range = str.split("r:");
return new Location(Location.Mode.RANGE, new org.bukkit.Location[] {toBukkitLocation(range[0].replace(":", ","))}, NumberConversions.toInt(range[1]));
}
// 单项
// world,0,80,0;world,0,90,0
else {
return new Location(Location.Mode.POINT, null, Arrays.stream(str.split(";")).map(CronusParser::toBukkitLocation).toArray(org.bukkit.Location[]::new));
}
}

View File

@ -30,6 +30,10 @@ public class CronusUtils {
return player.getItemInHand().getType() == material ? player.getItemInHand() : player.getInventory().getItemInOffHand();
}
public static boolean next(int page, int size, int entry) {
return size / (double) entry > page + 1;
}
public static boolean isInt(String in) {
try {
Integer.parseInt(in);

View File

@ -1,6 +1,8 @@
package me.skymc.taboolib.cronus.bukkit;
import me.skymc.taboolib.inventory.ItemUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
/**
* @Author 坏黑
@ -46,6 +48,40 @@ public class ItemStack {
return isType(itemStack) && isName(itemStack) && isLore(itemStack) && isDamage(itemStack) && isAmount(itemStack);
}
public boolean hasItem(Player player) {
int checkAmount = amount;
for (org.bukkit.inventory.ItemStack itemStack : player.getInventory().getContents()) {
if (itemStack != null && !itemStack.getType().equals(Material.AIR) && isItem(itemStack)) {
checkAmount -= itemStack.getAmount();
if (checkAmount <= 0) {
return true;
}
}
}
return false;
}
public boolean takeItem(Player player) {
int takeAmount = amount;
org.bukkit.inventory.ItemStack[] contents = player.getInventory().getContents();
for (int i = 0; i < contents.length; i++) {
org.bukkit.inventory.ItemStack itemStack = contents[i];
if (itemStack != null && !itemStack.getType().equals(Material.AIR) && isItem(itemStack)) {
takeAmount -= itemStack.getAmount();
if (takeAmount < 0) {
itemStack.setAmount(itemStack.getAmount() - (takeAmount + itemStack.getAmount()));
return true;
} else {
player.getInventory().setItem(i, null);
if (takeAmount == 0) {
return true;
}
}
}
}
return false;
}
public String getType() {
return type;
}

View File

@ -1,5 +1,7 @@
package me.skymc.taboolib.cronus.bukkit;
import org.bukkit.Bukkit;
import java.util.Arrays;
/**
@ -11,6 +13,7 @@ public class Location {
private Mode mode;
private org.bukkit.Location[] area;
private org.bukkit.Location[] points;
private int range;
public Location(Mode mode, org.bukkit.Location[] area, org.bukkit.Location[] points) {
this.mode = mode;
@ -18,8 +21,14 @@ public class Location {
this.points = points;
}
public Location(Mode mode, org.bukkit.Location[] points, int range) {
this.mode = mode;
this.points = points;
this.range = range;
}
public org.bukkit.Location toBukkit() {
return points[0];
return points != null && points.length > 0 ? points[0] : new org.bukkit.Location(Bukkit.getWorlds().get(0), 0, 0, 0);
}
public boolean isBukkit() {
@ -30,36 +39,65 @@ public class Location {
return false;
}
public boolean isSelect(org.bukkit.Location locationA, org.bukkit.Location locationB) {
return locationA.getWorld().equals(locationB.getWorld()) && locationA.getX() == locationB.getX() && locationA.getY() == locationB.getY() && locationA.getZ() == locationB.getZ();
}
public boolean inSelect(org.bukkit.Location location) {
if (!isSelectWorld(location)) {
return false;
}
if (mode == Mode.AREA) {
return location.toVector().isInAABB(area[0].toVector(), area[1].toVector());
} else {
return Arrays.asList(points).contains(location);
switch (mode) {
case AREA:
return area != null && location.toVector().isInAABB(area[0].toVector(), area[1].toVector());
case POINT:
return points != null && Arrays.stream(points).anyMatch(p -> isSelect(p, location));
case RANGE:
return points != null && toBukkit().distance(location) <= range;
default:
return false;
}
}
public boolean isSelectWorld(org.bukkit.Location location) {
if (mode == Mode.AREA) {
return location.getWorld().equals(area[0].getWorld());
} else {
return Arrays.stream(points).anyMatch(p -> p.getWorld().equals(location.getWorld()));
switch (mode) {
case AREA:
return area != null && location.getWorld().equals(area[0].getWorld());
default:
return points != null && Arrays.stream(points).anyMatch(p -> p.getWorld().equals(location.getWorld()));
}
}
public enum Mode {
AREA, POINT
}
@Override
public String toString() {
return "Location{" +
"mode=" + mode +
", area=" + Arrays.toString(area) +
", points=" + Arrays.toString(points) +
", range=" + range +
'}';
}
// *********************************
//
// Getter and Setter
//
// *********************************
public Mode getMode() {
return mode;
}
public org.bukkit.Location[] getArea() {
return area;
}
public org.bukkit.Location[] getPoints() {
return points;
}
public enum Mode {
AREA, POINT, RANGE
}
}

View File

@ -0,0 +1,103 @@
package me.skymc.taboolib.cronus.util;
import com.ilummc.tlib.logger.TLogger;
import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.common.inject.TInject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author 坏黑
* @Since 2019-06-07 23:51
*/
public class StringExpression {
@TInject
private static TLogger logger;
private static Pattern pattern = Pattern.compile("(?<symbol>>|>=|<|<=|==|=|!=|≈≈|≈|!≈)[ ]?(?<number>.+)");
private String symbol;
private StringNumber number;
public StringExpression(Object in) {
Matcher matcher = pattern.matcher(String.valueOf(in));
if (!matcher.find()) {
logger.error("StringExpression \"" + in + "\" parsing failed.");
return;
}
symbol = matcher.group("symbol");
number = new StringNumber(matcher.group("number"));
}
public boolean isSelect(String string) {
switch (symbol) {
case "=":
case "==":
return number.getSource().equals(string);
case "!=":
return !number.getSource().equals(string);
case "":
case "≈≈":
return number.getSource().equalsIgnoreCase(string);
case "!≈":
return !number.getSource().equalsIgnoreCase(string);
default:
return false;
}
}
public boolean isSelect(double number) {
if (!this.number.isNumber()) {
return false;
}
double v = this.number.getNumber().doubleValue();
switch (symbol) {
case ">":
return number > v;
case ">=":
return number >= v;
case "<":
return number < v;
case "<=":
return number <= v;
case "=":
case "==":
return number == v;
default:
return false;
}
}
public String translate() {
switch (symbol) {
case ">":
return TLocale.asString("translate-expression-0") + " " + number.getSource();
case ">=":
return TLocale.asString("translate-expression-1") + " " + number.getSource();
case "<":
return TLocale.asString("translate-expression-2") + " " + number.getSource();
case "<=":
return TLocale.asString("translate-expression-3") + " " + number.getSource();
case "=":
case "==":
return TLocale.asString("translate-expression-4") + " " + number.getSource();
}
return symbol + " " + number.getSource();
}
@Override
public String toString() {
return "StringExpression{" +
"symbol='" + symbol + '\'' +
", number=" + number +
'}';
}
public String getSymbol() {
return symbol;
}
public StringNumber getNumber() {
return number;
}
}

View File

@ -0,0 +1,135 @@
package me.skymc.taboolib.cronus.util;
/**
* @Author 坏黑
* @Since 2019-05-29 21:43
*/
public class StringNumber {
private NumberType type;
private Number number;
private String source;
public StringNumber(long number) {
this.number = number;
this.type = NumberType.INT;
}
public StringNumber(double number) {
this.number = number;
this.type = NumberType.DOUBLE;
}
public StringNumber(String source) {
this.source = source;
try {
number = Long.valueOf(source);
type = NumberType.INT;
} catch (Throwable ignored) {
try {
number = Double.valueOf(source);
type = NumberType.DOUBLE;
} catch (Throwable ignored2) {
type = NumberType.STRING;
}
}
}
public StringNumber add(String v) {
StringNumber numberFormat = new StringNumber(v);
if (isNumber() && numberFormat.isNumber()) {
if (type == NumberType.INT && numberFormat.getType() == NumberType.INT) {
number = number.longValue() + numberFormat.getNumber().longValue();
} else {
number = number.doubleValue() + numberFormat.getNumber().doubleValue();
type = NumberType.DOUBLE;
}
} else {
source += numberFormat.getSource();
type = NumberType.STRING;
}
return this;
}
public StringNumber subtract(String v) {
StringNumber numberFormat = new StringNumber(v);
if (isNumber() && numberFormat.isNumber()) {
if (type == NumberType.INT && numberFormat.getType() == NumberType.INT) {
number = number.longValue() - numberFormat.getNumber().longValue();
} else {
number = number.doubleValue() - numberFormat.getNumber().doubleValue();
type = NumberType.DOUBLE;
}
}
return this;
}
public StringNumber multiply(String v) {
StringNumber numberFormat = new StringNumber(v);
if (isNumber() && numberFormat.isNumber()) {
if (type == NumberType.INT && numberFormat.getType() == NumberType.INT) {
number = number.longValue() * numberFormat.getNumber().longValue();
} else {
number = number.doubleValue() * numberFormat.getNumber().doubleValue();
type = NumberType.DOUBLE;
}
}
return this;
}
public StringNumber division(String v) {
StringNumber numberFormat = new StringNumber(v);
if (isNumber() && numberFormat.isNumber()) {
if (type == NumberType.INT && numberFormat.getType() == NumberType.INT) {
number = number.longValue() / numberFormat.getNumber().longValue();
} else {
number = number.doubleValue() / numberFormat.getNumber().doubleValue();
type = NumberType.DOUBLE;
}
}
return this;
}
public Object get() {
switch (type) {
case INT:
return number.longValue();
case DOUBLE:
return number.doubleValue();
default:
return source;
}
}
public boolean isNumber() {
return type == NumberType.INT || type == NumberType.DOUBLE;
}
public Number getNumber() {
return number;
}
public NumberType getType() {
return type;
}
public String getSource() {
return source;
}
public enum NumberType {
DOUBLE, INT, STRING
}
@Override
public String toString() {
return "StringNumber{" +
"type=" + type +
", number=" + number +
", source='" + source + '\'' +
'}';
}
}

View File

@ -5,12 +5,13 @@ import me.clip.placeholderapi.PlaceholderAPI;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.common.function.TFunction;
import me.skymc.taboolib.common.nms.NMSHandler;
import me.skymc.taboolib.common.nms.nbt.NBTBase;
import me.skymc.taboolib.common.nms.nbt.NBTCompound;
import me.skymc.taboolib.common.nms.nbt.NBTList;
import me.skymc.taboolib.common.util.SimpleI18n;
import me.skymc.taboolib.fileutils.ConfigUtils;
import me.skymc.taboolib.itemnbtapi.NBTItem;
import me.skymc.taboolib.itemnbtapi.NBTList;
import me.skymc.taboolib.itemnbtapi.NBTListCompound;
import me.skymc.taboolib.itemnbtapi.NBTType;
import me.skymc.taboolib.other.NumberUtils;
import org.bukkit.Color;
import org.bukkit.Material;
@ -27,6 +28,7 @@ import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.NumberConversions;
import java.io.File;
import java.util.Arrays;
@ -361,47 +363,46 @@ public class ItemUtils {
// 元数据
item.setItemMeta(meta);
// 数据
NBTItem nbt = new NBTItem(item);
NBTCompound nbt = NMSHandler.getHandler().loadNBT(item);
// 物品标签
if (section.contains("nbt")) {
for (String name : section.getConfigurationSection("nbt").getKeys(false)) {
Object obj = section.get("nbt." + name);
if (obj instanceof String) {
nbt.setString(name, obj.toString());
nbt.put(name, new NBTBase(obj.toString()));
} else if (obj instanceof Double) {
nbt.setDouble(name, Double.valueOf(obj.toString()));
nbt.put(name, new NBTBase(NumberConversions.toDouble(obj)));
} else if (obj instanceof Integer) {
nbt.setInteger(name, Integer.valueOf(obj.toString()));
nbt.put(name, new NBTBase(NumberConversions.toInt(obj)));
} else if (obj instanceof Long) {
nbt.setLong(name, Long.valueOf(obj.toString()));
} else {
nbt.setObject(name, obj);
nbt.put(name, new NBTBase(NumberConversions.toLong(obj)));
}
}
}
// 物品属性
if (section.contains("attributes")) {
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
NBTList attr = new NBTList();
for (String hand : section.getConfigurationSection("attributes").getKeys(false)) {
for (String name : section.getConfigurationSection("attributes." + hand).getKeys(false)) {
if (asAttribute(name) != null) {
try {
NBTListCompound _attr = attr.addCompound();
Object num = section.get("attributes." + hand + "." + name);
if (num.toString().contains("%")) {
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
_attr.setInteger("Operation", 1);
NBTCompound a = new NBTCompound();
String num = section.getString("attributes." + hand + "." + name);
if (num.endsWith("%")) {
a.put("Amount", new NBTBase(NumberConversions.toDouble(num.substring(0, num.length() - 1)) / 100D));
a.put("Operation", new NBTBase(1));
} else {
_attr.setDouble("Amount", Double.valueOf(num.toString()));
_attr.setInteger("Operation", 0);
a.put("Amount", new NBTBase(NumberConversions.toDouble(num) / 100D));
a.put("Operation", new NBTBase(0));
}
_attr.setString("AttributeName", asAttribute(name));
_attr.setInteger("UUIDMost", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setInteger("UUIDLeast", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setString("Name", asAttribute(name));
if (!"all".equals(hand)) {
_attr.setString("Slot", hand);
a.put("AttributeName", new NBTBase(asAttribute(name)));
a.put("UUIDMost", new NBTBase(NumberUtils.getRandom().nextInt(Integer.MAX_VALUE)));
a.put("UUIDLeast", new NBTBase(NumberUtils.getRandom().nextInt(Integer.MAX_VALUE)));
a.put("Name", new NBTBase(asAttribute(name)));
if (!hand.equals("all")) {
a.put("Slot", new NBTBase(hand));
}
attr.add(a);
} catch (Exception ignored) {
}
} else {
@ -409,44 +410,9 @@ public class ItemUtils {
}
}
}
nbt.put("AttributeModifiers", attr);
}
return nbt.getItem();
}
public static NBTItem setAttribute(NBTItem nbt, String name, Object num, String hand) {
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
if (asAttribute(name) != null) {
try {
NBTListCompound _attr = null;
for (int i = 0; i < attr.size(); i++) {
NBTListCompound nlc = attr.getCompound(i);
if (nlc.getString("AttributeName").equals(asAttribute(name))) {
_attr = nlc;
}
}
if (_attr == null) {
_attr = attr.addCompound();
}
if (num.toString().contains("%")) {
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
_attr.setInteger("Operation", 1);
} else {
_attr.setDouble("Amount", Double.valueOf(num.toString()));
_attr.setInteger("Operation", 0);
}
_attr.setString("AttributeName", asAttribute(name));
_attr.setInteger("UUIDMost", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setInteger("UUIDLeast", NumberUtils.getRandom().nextInt(Integer.MAX_VALUE));
_attr.setString("Name", asAttribute(name));
if (!"all".equals(hand)) {
_attr.setString("Slot", hand);
}
} catch (NumberFormatException ignored) {
}
} else {
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", name);
}
return nbt;
return NMSHandler.getHandler().saveNBT(item, nbt);
}
// *********************************
@ -485,6 +451,11 @@ public class ItemUtils {
//
// *********************************
@Deprecated
public static NBTItem setAttribute(NBTItem nbt, String name, Object num, String hand) {
return nbt;
}
@Deprecated
public static FileConfiguration getItemdir() {
return itemDir;

View File

@ -9,7 +9,7 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent;
* @Author 坏黑
* @Since 2019-05-21 22:04
*/
@TListener
@TListener(version = ">=10900")
class ClickListener1_9 implements Listener {
@EventHandler

View File

@ -14,31 +14,28 @@ import java.lang.annotation.Target;
public @interface TListener {
/**
* 注册时执行方法
*
* @return 方法名
* 注册时执行的方法名
*/
String register() default "";
/**
* 注销时执行方法
*
* @return 方法名
* 注销时执行的方法名
*/
String cancel() default "";
/**
* 注册时判断条件
*
* @return 方法名
* 注册时判断的方法名需返回布尔值
*/
String condition() default "";
/**
* 注册前判断依赖插件
*
* @return 依赖插件
*/
String[] depend() default "";
/**
* 注册前判断依赖版本
*/
String version() default ">0";
}

View File

@ -4,6 +4,7 @@ import com.ilummc.tlib.logger.TLogger;
import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.TabooLibLoader;
import me.skymc.taboolib.cronus.util.StringExpression;
import me.skymc.taboolib.events.TPluginEnableEvent;
import me.skymc.taboolib.methods.ReflectionUtils;
import org.bukkit.Bukkit;
@ -50,6 +51,10 @@ public class TListenerHandler implements Listener {
if (Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
try {
TListener tListener = pluginClass.getAnnotation(TListener.class);
// 检查版本
if (!new StringExpression(tListener.version()).isSelect(TabooLib.getVersionNumber())) {
continue;
}
// 检查注册条件
if (tListener.depend().length > 0 && !Strings.isBlank(tListener.depend()[0])) {
if (Arrays.stream(tListener.depend()).anyMatch(depend -> Bukkit.getPluginManager().getPlugin(depend) == null)) {