From 03c8755067b72a754977a02773cb432b08c2abde Mon Sep 17 00:00:00 2001 From: sky Date: Thu, 13 Feb 2020 16:46:26 +0800 Subject: [PATCH] Update NMS & Effects --- build.gradle | 2 +- src/main/scala/io/izzel/taboolib/Version.java | 2 +- .../io/izzel/taboolib/module/nms/NMS.java | 7 ++++ .../io/izzel/taboolib/module/nms/NMSImpl.java | 42 +++++++++++++++++++ .../io/izzel/taboolib/util/lite/Effects.java | 24 +++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 2a96889..7634d3c 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = 'me.skymc' -version = '5.16' +version = '5.17' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/io/izzel/taboolib/Version.java b/src/main/scala/io/izzel/taboolib/Version.java index 996e8bc..90d8970 100644 --- a/src/main/scala/io/izzel/taboolib/Version.java +++ b/src/main/scala/io/izzel/taboolib/Version.java @@ -10,7 +10,7 @@ import java.util.Arrays; */ public enum Version { - v1_7(10700), v1_8(10800), v1_9(10900), v1_10(11000), v1_11(11100), v1_12(11200), v1_13(11300), v1_14(11400), v1_15(11500), vNull(0); + v1_7(10700), v1_8(10800), v1_9(10900), v1_10(11000), v1_11(11100), v1_12(11200), v1_13(11300), v1_14(11400), v1_15(11500), v1_16(11600), vNull(0); private int versionInt; diff --git a/src/main/scala/io/izzel/taboolib/module/nms/NMS.java b/src/main/scala/io/izzel/taboolib/module/nms/NMS.java index 3a7bb58..5b2398a 100644 --- a/src/main/scala/io/izzel/taboolib/module/nms/NMS.java +++ b/src/main/scala/io/izzel/taboolib/module/nms/NMS.java @@ -7,11 +7,14 @@ import io.izzel.taboolib.module.nms.nbt.Attribute; import io.izzel.taboolib.module.nms.nbt.NBTAttribute; import io.izzel.taboolib.module.nms.nbt.NBTCompound; import io.izzel.taboolib.module.nms.nbt.NBTList; +import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -78,4 +81,8 @@ public abstract class NMS { abstract public Position fromBlockPosition(Object blockPosition); abstract public void openSignEditor(Player player, Block block); + + abstract public boolean inBoundingBox(Entity entity, Vector vector); + + abstract public Location getLastLocation(ProjectileHitEvent event); } diff --git a/src/main/scala/io/izzel/taboolib/module/nms/NMSImpl.java b/src/main/scala/io/izzel/taboolib/module/nms/NMSImpl.java index 4d064a1..d6b00c7 100644 --- a/src/main/scala/io/izzel/taboolib/module/nms/NMSImpl.java +++ b/src/main/scala/io/izzel/taboolib/module/nms/NMSImpl.java @@ -16,6 +16,7 @@ import net.minecraft.server.v1_13_R2.IRegistry; import net.minecraft.server.v1_8_R3.NBTBase; import net.minecraft.server.v1_8_R3.*; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.World; import org.bukkit.block.Block; @@ -28,8 +29,10 @@ import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Villager; +import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.util.Vector; import java.lang.reflect.Field; import java.util.List; @@ -404,4 +407,43 @@ public class NMSImpl extends NMS { } return null; } + + @Override + public boolean inBoundingBox(Entity entity, Vector vector) { + if (Version.isAfter(Version.v1_14)) { + return ((org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity) entity).getHandle().getBoundingBox().c(new net.minecraft.server.v1_14_R1.Vec3D(vector.getX(), vector.getY(), vector.getZ())); + } else { + return ((org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity) entity).getHandle().getBoundingBox().b(new net.minecraft.server.v1_12_R1.Vec3D(vector.getX(), vector.getY(), vector.getZ())); + } + } + + @Override + public Location getLastLocation(ProjectileHitEvent event) { + Vector vector = event.getEntity().getVelocity().normalize().multiply(0.1); + Vector vectorEntity = event.getEntity().getLocation().toVector(); + if (event.getHitBlock() != null) { + double i = 0; + double length = event.getHitBlock().getLocation().add(0.5, 0.5, 0.5).distance(event.getEntity().getLocation()) * 2; + while (i < length) { + Location location = vectorEntity.toLocation(event.getHitBlock().getWorld()); + if (location.getBlock().getLocation().equals(event.getHitBlock().getLocation())) { + return location; + } + vectorEntity.add(vector); + i += 0.1; + } + } + if (event.getHitEntity() != null) { + double i = 0; + double length = event.getHitEntity().getLocation().distance(event.getEntity().getLocation()) * 2; + while (i < length) { + if (inBoundingBox(event.getHitEntity(), vectorEntity)) { + return vectorEntity.toLocation(event.getHitEntity().getWorld()); + } + vectorEntity.add(vector); + i += 0.1; + } + } + return null; + } } diff --git a/src/main/scala/io/izzel/taboolib/util/lite/Effects.java b/src/main/scala/io/izzel/taboolib/util/lite/Effects.java index 8ba435d..c4e074a 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/Effects.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/Effects.java @@ -10,11 +10,13 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; import org.bukkit.util.NumberConversions; +import org.bukkit.util.Vector; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Consumer; /** * @Author sky @@ -81,6 +83,28 @@ public class Effects { return Particle.FLAME; } + public static void buildLine(Location locA, Location locB, Consumer action) { + buildLine(locA, locB, action, 0.25); + } + + public static void buildLine(Location locA, Location locB, Consumer action, double interval) { + Vector vectorAB = locB.clone().subtract(locA).toVector(); + double vectorLength = vectorAB.length(); + vectorAB.normalize(); + for (double i = 0; i < vectorLength; i += interval) { + action.accept(locA.clone().add(vectorAB.clone().multiply(i))); + } + } + + public static void buildPolygon(Location center, double range, double interval, Consumer action) { + for (double i = 0; i < 360; i += (360 / interval)) { + double radians = Math.toRadians(i); + double cos = Math.cos(radians) * range; + double sin = Math.sin(radians) * range; + action.accept(center.clone().add(cos, 0, sin)); + } + } + Effects() { }