Solved Creating fake projectiles

Discussion in 'Plugin Development' started by Unica, Oct 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Unica

    Hello,

    I am trying to create 'Shuriken Stars' (ninja stars) and for that I am using nether stars.
    To throw it I 'drop' an item with a specific velocity, so it looks like a player threw it.
    However, I am having trouble to check if the 'ninja star' hit a player.
    Currently I am damaging the player in my crosshair, but the ninja star doesn't go directly as my crosshair. So I want to check when my item 'hit' a player.
    Example: If you know The Shotbow networks's Annihilation there is this 'Scorpio class' which has an Hook, and there they pull the player towards you when this hook hits you. So I know this is possible.


    Any suggestions?

    Current Code
    Code:java
    1. public void throwStar(Player p, Material mat, ParticleEffect effect){
    2. Location loc = p.getEyeLocation();
    3. final Item item = p.getWorld().dropItem(loc, new ItemStack(mat, 1));
    4. item.setVelocity(loc.getDirection().multiply(2.1));
    5.  
    6. //Get nearest player in front of player
    7. Location observerPos = loc;
    8. Vector3D observerDir = new Vector3D(observerPos.getDirection());
    9.  
    10. Vector3D observerStart = new Vector3D(observerPos);
    11. Vector3D observerEnd = observerStart.add(observerDir.multiply(13));
    12. Player hit = null;
    13. p.playSound(p.getLocation(), Sound.ARROW_HIT, 30, 1);
    14. for (Player target : p.getWorld().getPlayers()) {
    15. Vector3D targetPos = new Vector3D(target.getLocation());
    16. Vector3D minimum = targetPos.add(-0.5, 0, -0.5);
    17. Vector3D maximum = targetPos.add(0.5, 1.67, 0.5);
    18.  
    19. if (target != p && hasIntersection(observerStart, observerEnd, minimum, maximum)) {
    20. if (hit == null ||
    21. hit.getLocation().distanceSquared(observerPos) >
    22. target.getLocation().distanceSquared(observerPos)) {
    23.  
    24. hit = target;
    25. }
    26. }
    27. }
    28. if (hit != null) {
    29. damage(hit, p, 5.0, effect);
    30. }
    31. new BukkitRunnable(){
    32. public void run(){
    33. item.remove();
    34. }
    35. }.runTaskLater(m, 25);
    36. }
    37.  
    38. private void damage(final Player p, final Player thrower, final double damage, final ParticleEffect effect){
    39. new BukkitRunnable(){
    40. public void run(){
    41. p.damage(damage, thrower);
    42. effect.display(p.getLocation(), 1, 1, 1, 0, 50);
    43. p.playSound(p.getLocation(), Sound.BAT_HURT, 40, 1);
    44. }
    45. }.runTaskLater(m, 5);
    46. }
    47.  
    48. private boolean hasIntersection(Vector3D p1, Vector3D p2, Vector3D min, Vector3D max) {
    49. final double epsilon = 0.0001f;
    50.  
    51. Vector3D d = p2.subtract(p1).multiply(0.5);
    52. Vector3D e = max.subtract(min).multiply(0.5);
    53. Vector3D c = p1.add(d).subtract(min.add(max).multiply(0.5));
    54. Vector3D ad = d.abs();
    55.  
    56. if (Math.abs(c.x) > e.x + ad.x)
    57. return false;
    58. if (Math.abs(c.y) > e.y + ad.y)
    59. return false;
    60. if (Math.abs(c.z) > e.z + ad.z)
    61. return false;
    62.  
    63. if (Math.abs(d.y * c.z - d.z * c.y) > e.y * ad.z + e.z * ad.y + epsilon)
    64. return false;
    65. if (Math.abs(d.z * c.x - d.x * c.z) > e.z * ad.x + e.x * ad.z + epsilon)
    66. return false;
    67. if (Math.abs(d.x * c.y - d.y * c.x) > e.x * ad.y + e.y * ad.x + epsilon)
    68. return false;
    69.  
    70. return true;
    71. }
    72.  
    73.  


    Vector3D (lib)
    Code:java
    1. package me.dubehh.GamePlay.Specials;
    2.  
    3. /*
    4. * Attack hidden players
    5. *
    6. * Copyright 2012 Kristian S. Stangeland (Comphenix)
    7. *
    8. * This library is free software; you can redistribute it and/or
    9. * modify it under the terms of the GNU Lesser General Public
    10. * License as published by the Free Software Foundation; either
    11. * version 2.1 of the License, or (at your option) any later version.
    12. *
    13. * This library is distributed in the hope that it will be useful,
    14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    16. * Lesser General Public License for more details.
    17. *
    18. * You should have received a copy of the GNU Lesser General Public
    19. * License along with this library. If not, see <[url]http://www.gnu.org/licenses/>[/url].
    20. */
    21.  
    22. import org.bukkit.Location;
    23. import org.bukkit.util.Vector;
    24.  
    25. public class Vector3D {
    26. /**
    27.   * Represents the null (0, 0, 0) origin.
    28.   */
    29. public static final Vector3D ORIGIN = new Vector3D(0, 0, 0);
    30.  
    31. // Use protected members, like Bukkit
    32. public final double x;
    33. public final double y;
    34. public final double z;
    35.  
    36. /**
    37.   * Construct an immutable 3D vector.
    38.   */
    39. public Vector3D(double x, double y, double z) {
    40. this.x = x;
    41. this.y = y;
    42. this.z = z;
    43. }
    44.  
    45. /**
    46.   * Construct an immutable floating point 3D vector from a location object.
    47.   * @param location - the location to copy.
    48.   */
    49. public Vector3D(Location location) {
    50. this(location.toVector());
    51. }
    52.  
    53. /**
    54.   * Construct an immutable floating point 3D vector from a mutable Bukkit vector.
    55.   * @param vector - the mutable real Bukkit vector to copy.
    56.   */
    57. public Vector3D(Vector vector) {
    58. if (vector == null)
    59. throw new IllegalArgumentException("Vector cannot be NULL.");
    60. this.x = vector.getX();
    61. this.y = vector.getY();
    62. this.z = vector.getZ();
    63. }
    64.  
    65. /**
    66.   * Convert this instance to an equivalent real 3D vector.
    67.   * @return Real 3D vector.
    68.   */
    69. public Vector toVector() {
    70. return new Vector(x, y, z);
    71. }
    72.  
    73. /**
    74.   * Adds the current vector and a given position vector, producing a result vector.
    75.   * @param other - the other vector.
    76.   * @return The new result vector.
    77.   */
    78. public Vector3D add(Vector3D other) {
    79. if (other == null)
    80. throw new IllegalArgumentException("other cannot be NULL");
    81. return new Vector3D(x + other.x, y + other.y, z + other.z);
    82. }
    83.  
    84. /**
    85.   * Adds the current vector and a given vector together, producing a result vector.
    86.   * @param other - the other vector.
    87.   * @return The new result vector.
    88.   */
    89. public Vector3D add(double x, double y, double z) {
    90. return new Vector3D(this.x + x, this.y + y, this.z + z);
    91. }
    92.  
    93. /**
    94.   * Substracts the current vector and a given vector, producing a result position.
    95.   * @param other - the other position.
    96.   * @return The new result position.
    97.   */
    98. public Vector3D subtract(Vector3D other) {
    99. if (other == null)
    100. throw new IllegalArgumentException("other cannot be NULL");
    101. return new Vector3D(x - other.x, y - other.y, z - other.z);
    102. }
    103.  
    104. /**
    105.   * Substracts the current vector and a given vector together, producing a result vector.
    106.   * @param other - the other vector.
    107.   * @return The new result vector.
    108.   */
    109. public Vector3D subtract(double x, double y, double z) {
    110. return new Vector3D(this.x - x, this.y - y, this.z - z);
    111. }
    112.  
    113. /**
    114.   * Multiply each dimension in the current vector by the given factor.
    115.   * @param factor - multiplier.
    116.   * @return The new result.
    117.   */
    118. public Vector3D multiply(int factor) {
    119. return new Vector3D(x * factor, y * factor, z * factor);
    120. }
    121.  
    122. /**
    123.   * Multiply each dimension in the current vector by the given factor.
    124.   * @param factor - multiplier.
    125.   * @return The new result.
    126.   */
    127. public Vector3D multiply(double factor) {
    128. return new Vector3D(x * factor, y * factor, z * factor);
    129. }
    130.  
    131. /**
    132.   * Divide each dimension in the current vector by the given divisor.
    133.   * @param divisor - the divisor.
    134.   * @return The new result.
    135.   */
    136. public Vector3D divide(int divisor) {
    137. if (divisor == 0)
    138. throw new IllegalArgumentException("Cannot divide by null.");
    139. return new Vector3D(x / divisor, y / divisor, z / divisor);
    140. }
    141.  
    142. /**
    143.   * Divide each dimension in the current vector by the given divisor.
    144.   * @param divisor - the divisor.
    145.   * @return The new result.
    146.   */
    147. public Vector3D divide(double divisor) {
    148. if (divisor == 0)
    149. throw new IllegalArgumentException("Cannot divide by null.");
    150. return new Vector3D(x / divisor, y / divisor, z / divisor);
    151. }
    152.  
    153. /**
    154.   * Retrieve the absolute value of this vector.
    155.   * @return The new result.
    156.   */
    157. public Vector3D abs() {
    158. return new Vector3D(Math.abs(x), Math.abs(y), Math.abs(z));
    159. }
    160.  
    161. @Override
    162. public String toString() {
    163. return String.format("[x: %s, y: %s, z: %s]", x, y, z);
    164. }
    165. }

     
  2. Offline

    fireblast709

    I would just use a snowball and use modify the packets with ProtocolLib to make it look like a netherstar
     
  3. Offline

    Unica

    fireblast709

    Mm. Never used ProtocolLib before, is there a tutorial?
     
  4. Offline

    fireblast709

    Unica Check the BukkitDev page, the author has a ton of examples ;3.
     
  5. Offline

    Unica

    fireblast709

    hey,
    I found
    Code:java
    1. public void testPackets() {
    2. ProtocolLibrary.getProtocolManager().addPacketListener(
    3. new PacketAdapter(m,
    4. ConnectionSide.SERVER_SIDE,
    5. Packets.Server.ENTITY_METADATA) {
    6. @Override
    7. public void onPacketSending(PacketEvent event) {
    8. PacketContainer packet = event.getPacket();
    9.  
    10. // See if we are modifying an item stack
    11. if (packet.getEntityModifier(event).read(0) instanceof Item) {
    12. WrappedDataWatcher watcher = new WrappedDataWatcher(
    13. packet.getWatchableCollectionModifier()
    14. .read(0));
    15. ItemStack stack = watcher.getItemStack(10);
    16.  
    17. if (stack != null) {
    18. // You have to clone the watcher, unfortunately
    19. watcher = watcher.deepClone();
    20. watcher.removeObject(10); // Not needed after
    21. // build #148
    22. watcher.setObject(10, processItemStack(stack));
    23.  
    24. // Save the modification
    25. packet.getWatchableCollectionModifier().write(
    26. 0, watcher.getWatchableObjects());
    27. }
    28. }
    29. }
    30. });
    31. }
    32.  
    33. private ItemStack processItemStack(ItemStack stack) {
    34. // Otherwise you'll modify the stack on the server
    35. ItemStack cloned = stack.clone();
    36.  
    37. cloned.setType(Material.STONE);
    38. return cloned;
    39. }


    But I cannot see what to change in order to get the result I want,
    I looked for different tutorials, and I have no idea where to start.
     
  6. Offline

    TheCwispyOne

    Unica I think you could have the nether star "hit" a player by just checking if they pick it up. Basically, you just drop the nether star and set the vector. Then, if it hits the ground, remove it. You also have a pickup item event or something like that. If the item getting picked up is a nether star, deal damage to the player who is picking up the nether star and cancel the event and remove the nether star.
     
Thread Status:
Not open for further replies.

Share This Page