diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index 2b0e2b4..3857f35 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -102,7 +102,9 @@ public class TLib { } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException ignored) { TLocale.Logger.fatal("TLIB.INJECTION-FAILED"); for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - if (plugin != Main.getInst()) TDependencyInjector.inject(plugin, plugin); + if (plugin != Main.getInst()) { + TDependencyInjector.inject(plugin, plugin); + } } } } diff --git a/src/main/java/com/ilummc/tlib/inject/TPluginManager.java b/src/main/java/com/ilummc/tlib/inject/TPluginManager.java index ae5d1ca..f9ead8d 100644 --- a/src/main/java/com/ilummc/tlib/inject/TPluginManager.java +++ b/src/main/java/com/ilummc/tlib/inject/TPluginManager.java @@ -37,8 +37,9 @@ public class TPluginManager implements PluginManager { private List delayedDisable = new ArrayList<>(); public static void delayDisable(Plugin plugin) { - if (!singleton.delayedDisable.contains(plugin)) + if (!singleton.delayedDisable.contains(plugin)) { singleton.delayedDisable.add(plugin); + } } public TPluginManager() { diff --git a/src/main/java/me/skymc/taboolib/csvutils/CsvReader.java b/src/main/java/me/skymc/taboolib/csvutils/CsvReader.java index cfc3477..fda2878 100644 --- a/src/main/java/me/skymc/taboolib/csvutils/CsvReader.java +++ b/src/main/java/me/skymc/taboolib/csvutils/CsvReader.java @@ -500,7 +500,7 @@ public class CsvReader { ++dataBuffer2.Position; if (this.userSettings.SafetySwitch && this.dataBuffer.Position - this.dataBuffer.ColumnStart + this.columnBuffer.Position > 100000) { this.close(); - throw new IOException("Maximum addColumn length of 100,000 exceeded in addColumn " + NumberFormat.getIntegerInstance().format(this.columnsCount) + " in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting addColumn lengths greater than 100,000 characters to" + " avoid this error."); + throw new IOException("Maximum column length of 100,000 exceeded in column " + NumberFormat.getIntegerInstance().format(this.columnsCount) + " in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting column lengths greater than 100,000 characters to" + " avoid this error."); } } } while (this.hasMoreData && this.startedColumn); @@ -695,7 +695,7 @@ public class CsvReader { ++dataBuffer3.Position; if (this.userSettings.SafetySwitch && this.dataBuffer.Position - this.dataBuffer.ColumnStart + this.columnBuffer.Position > 100000) { this.close(); - throw new IOException("Maximum addColumn length of 100,000 exceeded in addColumn " + NumberFormat.getIntegerInstance().format(this.columnsCount) + " in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting addColumn lengths greater than 100,000 characters to" + " avoid this error."); + throw new IOException("Maximum column length of 100,000 exceeded in column " + NumberFormat.getIntegerInstance().format(this.columnsCount) + " in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting column lengths greater than 100,000 characters to" + " avoid this error."); } } } while (this.hasMoreData && this.startedColumn); @@ -868,7 +868,7 @@ public class CsvReader { this.startedColumn = false; if (this.columnsCount >= 100000 && this.userSettings.SafetySwitch) { this.close(); - throw new IOException("Maximum addColumn count of 100,000 exceeded in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting more than 100,000 columns per record to" + " avoid this error."); + throw new IOException("Maximum column count of 100,000 exceeded in record " + NumberFormat.getIntegerInstance().format(this.currentRecord) + ". Set the SafetySwitch property to false" + " if you're expecting more than 100,000 columns per record to" + " avoid this error."); } if (this.columnsCount == this.values.length) { final int n3 = this.values.length * 2; diff --git a/src/main/java/me/skymc/taboolib/entity/EntityUtils.java b/src/main/java/me/skymc/taboolib/entity/EntityUtils.java index 43a5a06..549d52f 100644 --- a/src/main/java/me/skymc/taboolib/entity/EntityUtils.java +++ b/src/main/java/me/skymc/taboolib/entity/EntityUtils.java @@ -32,14 +32,7 @@ public class EntityUtils implements Listener { * @return */ public static Entity getEntityWithUUID(UUID u) { - for (World w : Bukkit.getWorlds()) { - for (Entity e : w.getLivingEntities()) { - if (e.getUniqueId().equals(u)) { - return e; - } - } - } - return null; + return Bukkit.getWorlds().stream().flatMap(w -> w.getLivingEntities().stream()).filter(e -> e.getUniqueId().equals(u)).findFirst().orElse(null); } /** @@ -50,12 +43,7 @@ public class EntityUtils implements Listener { * @return */ public static Entity getEntityWithUUID_World(UUID u, World world) { - for (Entity e : world.getLivingEntities()) { - if (e.getUniqueId().equals(u)) { - return e; - } - } - return null; + return world.getLivingEntities().stream().filter(e -> e.getUniqueId().equals(u)).findFirst().orElse(null); } /** @@ -66,11 +54,7 @@ public class EntityUtils implements Listener { */ public static void addGlow(Player player, Entity entity) { if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) { - try { - throw new PluginNotFoundException(TLocale.asString("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB")); - } catch (Exception e) { - // - } + TLocale.sendToConsole("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB"); } PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA); packet.getIntegers().write(0, entity.getEntityId()); @@ -94,11 +78,7 @@ public class EntityUtils implements Listener { */ public static void delGlow(Player player, Entity entity) { if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) { - try { - throw new PluginNotFoundException(TLocale.asString("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB")); - } catch (Exception e) { - // - } + TLocale.sendToConsole("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB"); } PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA); packet.getIntegers().write(0, entity.getEntityId()); diff --git a/src/main/java/me/skymc/taboolib/entity/VectorUtils.java b/src/main/java/me/skymc/taboolib/entity/VectorUtils.java new file mode 100644 index 0000000..2428757 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/entity/VectorUtils.java @@ -0,0 +1,122 @@ +package me.skymc.taboolib.entity; + +import me.skymc.taboolib.other.NumberUtils; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +import java.util.stream.IntStream; + +/** + * @Author sky + * @Since 2018-06-24 16:32 + */ +public class VectorUtils { + + /** + * 物品丢弃 + * + * 常用参数: + * itemDrop(player, itemStack, 0.2, 0.5) + * + * @param player 玩家 + * @param itemStack 丢弃物品 + * @param bulletSpread 视角偏移 + * @param radius 距离 + * @return {@link Item} + */ + public static Item itemDrop(Player player, ItemStack itemStack, double bulletSpread, double radius) { + Location location = player.getLocation().add(0, 1.5, 0); + Item item = player.getWorld().dropItem(location, itemStack); + + double yaw = Math.toRadians(-player.getLocation().getYaw() - 90.0F); + double pitch = Math.toRadians(-player.getLocation().getPitch()); + double x; + double y; + double z; + + if (bulletSpread > 0) { + double[] spread = {1.0D, 1.0D, 1.0D}; + IntStream.range(0, 3).forEach(t -> spread[t] = ((NumberUtils.getRandom().nextDouble() - NumberUtils.getRandom().nextDouble()) * bulletSpread * 0.1D)); + x = Math.cos(pitch) * Math.cos(yaw) + spread[0]; + y = Math.sin(pitch) + spread[1]; + z = -Math.sin(yaw) * Math.cos(pitch) + spread[2]; + } else { + x = Math.cos(pitch) * Math.cos(yaw); + y = Math.sin(pitch); + z = -Math.sin(yaw) * Math.cos(pitch); + } + + Vector dirVel = new Vector(x, y, z); + item.setVelocity(dirVel.normalize().multiply(radius)); + return item; + } + + /** + * 生物抛射 + * + * 常用参数: + * entityPush(entity, location, 15) + * + * @param entity 目标生物 + * @param to 目标坐标 + * @param velocity 力量 + */ + public static void entityPush(Entity entity, Location to, double velocity) { + Location from = entity.getLocation(); + + Vector test = to.clone().subtract(from).toVector(); + Double elevation = test.getY(); + + Double launchAngle = calculateLaunchAngle(from, to, velocity, elevation, 20.0D); + Double distance = Math.sqrt(Math.pow(test.getX(), 2.0D) + Math.pow(test.getZ(), 2.0D)); + if (distance == 0.0D) { + return; + } + if (launchAngle == null) { + launchAngle = Math.atan((40.0D * elevation + Math.pow(velocity, 2.0D)) / (40.0D * elevation + 2.0D * Math.pow(velocity, 2.0D))); + } + Double hangTime = calculateHangTime(launchAngle, velocity, elevation, 20.0D); + + test.setY(Math.tan(launchAngle) * distance); + test = normalizeVector(test); + + Vector noise = Vector.getRandom(); + noise = noise.multiply(1 / 10.0D); + test.add(noise); + + velocity = velocity + 1.188D * Math.pow(hangTime, 2.0D) + (NumberUtils.getRandom().nextDouble() - 0.8D) / 2.0D; + test = test.multiply(velocity / 20.0D); + + entity.setVelocity(test); + } + + // ********************************* + // + // Private Methods + // + // ********************************* + + private static double calculateHangTime(double launchAngle, double v, double elev, double g) { + double a = v * Math.sin(launchAngle); + double b = -2.0D * g * elev; + return Math.pow(a, 2.0D) + b < 0.0D ? 0.0D : (a + Math.sqrt(Math.pow(a, 2.0D) + b)) / g; + } + + private static Vector normalizeVector(Vector victor) { + double mag = Math.sqrt(Math.pow(victor.getX(), 2.0D) + Math.pow(victor.getY(), 2.0D) + Math.pow(victor.getZ(), 2.0D)); + return mag != 0.0D ? victor.multiply(1.0D / mag) : victor.multiply(0); + } + + private static Double calculateLaunchAngle(Location from, Location to, double v, double elevation, double g) { + Vector vector = from.clone().subtract(to).toVector(); + Double distance = Math.sqrt(Math.pow(vector.getX(), 2.0D) + Math.pow(vector.getZ(), 2.0D)); + double v2 = Math.pow(v, 2.0D); + double v4 = Math.pow(v, 4.0D); + double check = g * (g * Math.pow(distance, 2.0D) + 2.0D * elevation * v2); + return v4 < check ? null : Math.atan((v2 - Math.sqrt(v4 - check)) / (g * distance)); + } +} diff --git a/src/main/java/me/skymc/taboolib/inventory/DropUtils.java b/src/main/java/me/skymc/taboolib/inventory/DropUtils.java index 1ff941f..0da14da 100644 --- a/src/main/java/me/skymc/taboolib/inventory/DropUtils.java +++ b/src/main/java/me/skymc/taboolib/inventory/DropUtils.java @@ -1,5 +1,6 @@ package me.skymc.taboolib.inventory; +import me.skymc.taboolib.entity.VectorUtils; import me.skymc.taboolib.other.NumberUtils; import org.bukkit.Location; import org.bukkit.entity.Item; @@ -9,35 +10,11 @@ import org.bukkit.util.Vector; import java.util.stream.IntStream; +@Deprecated public class DropUtils { public static Item drop(Player player, ItemStack itemStack, double bulletSpread, double radius) { - Location location = player.getLocation(); - location.setY(location.getY() + 1.5); - Item item = player.getWorld().dropItem(location, itemStack); - - double yaw = Math.toRadians(-player.getLocation().getYaw() - 90.0F); - double pitch = Math.toRadians(-player.getLocation().getPitch()); - double x; - double y; - double z; - - if (bulletSpread > 0) { - double[] spread = {1.0D, 1.0D, 1.0D}; - IntStream.range(0, 3).forEach(t -> spread[t] = ((NumberUtils.getRandom().nextDouble() - NumberUtils.getRandom().nextDouble()) * bulletSpread * 0.1D)); - x = Math.cos(pitch) * Math.cos(yaw) + spread[0]; - y = Math.sin(pitch) + spread[1]; - z = -Math.sin(yaw) * Math.cos(pitch) + spread[2]; - } else { - x = Math.cos(pitch) * Math.cos(yaw); - y = Math.sin(pitch); - z = -Math.sin(yaw) * Math.cos(pitch); - } - - Vector dirVel = new Vector(x, y, z); - dirVel.normalize().multiply(radius); - item.setVelocity(dirVel); - return item; + return VectorUtils.itemDrop(player, itemStack, bulletSpread, radius); } } diff --git a/src/main/java/org/javalite/activejdbc/MetaModel.java b/src/main/java/org/javalite/activejdbc/MetaModel.java index 3613a89..55657bf 100644 --- a/src/main/java/org/javalite/activejdbc/MetaModel.java +++ b/src/main/java/org/javalite/activejdbc/MetaModel.java @@ -163,7 +163,7 @@ public class MetaModel implements Serializable { } /** - * @return name of the addColumn for optimistic locking record version + * @return name of the column for optimistic locking record version */ public String getVersionColumn() { return versionColumn; @@ -401,11 +401,11 @@ public class MetaModel implements Serializable { } /** - * FK name is a foreign key name used in relationships as a foreign key addColumn in a child table (table represented by this + * FK name is a foreign key name used in relationships as a foreign key column in a child table (table represented by this * instance is a parent table). * The FK name is derived using {@link org.javalite.common.Inflector}: It is a singular version of this table name plus "_id". * - * @return foreign key name used in relationships as a foreign key addColumn in a child table. + * @return foreign key name used in relationships as a foreign key column in a child table. */ public String getFKName() { return singularize(getTableName()).toLowerCase() + "_id"; @@ -469,10 +469,10 @@ public class MetaModel implements Serializable { } /** - * Provides addColumn metadata map, keyed by attribute names. + * Provides column metadata map, keyed by attribute names. * Table columns correspond to ActiveJDBC model attributes. * - * @return Provides addColumn metadata map, keyed by attribute names. + * @return Provides column metadata map, keyed by attribute names. */ public Map getColumnMetadata() { if (columnMetadata == null || columnMetadata.isEmpty()) {