[Resource] Ropes (leads)

Discussion in 'Resources' started by Desle, Mar 2, 2014.

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

    Desle

    I've been looking all over, to find out how to spawn ropes (leads). I did not find any solutions, so I decided to make my own class, with a cheaty way, since that is the only way I know of right now.

    You can use this for decoration, ziplines, etcetera. Enough to play around with.


    Here is the class.
    Code:java
    1. public class Rope {
    2.  
    3. private EntityWitherSkull start;
    4. private EntityWitherSkull end;
    5.  
    6. public Rope() {
    7. }
    8.  
    9. public Location getEnd() {
    10. return this.end.getBukkitEntity().getLocation();
    11. }
    12.  
    13. public Location getStart() {
    14. return this.start.getBukkitEntity().getLocation();
    15. }
    16.  
    17. public void move(Location newStart, Location newEnd) {
    18. delete();
    19. spawn(newStart, newEnd);
    20. }
    21.  
    22. public void delete() {
    23. start.getBukkitEntity().getPassenger().remove();
    24. start.getBukkitEntity().remove();
    25. end.getBukkitEntity().getPassenger().remove();
    26. end.getBukkitEntity().remove();
    27. this.start = null;
    28. this.end = null;
    29. }
    30.  
    31. public void spawn(Location start, Location end) {
    32. if (start.getWorld().getName().equals(end.getWorld().getName())) {
    33.  
    34. WorldServer world = ((CraftWorld) start.getWorld()).getHandle();
    35.  
    36. EntityWitherSkull skullStart = new EntityWitherSkull(world);
    37. EntityWitherSkull skullEnd = new EntityWitherSkull(world);
    38.  
    39. skullStart.setLocation(start.getX(), start.getY(), start.getZ(), 0, 0);
    40. skullEnd.setLocation(end.getX(), end.getY(), end.getZ(), 0, 0);
    41.  
    42. world.addEntity(skullStart);
    43. world.addEntity(skullEnd);
    44.  
    45.  
    46. Bat ropeStart = (Bat) world.getWorld().spawnEntity(start, EntityType.BAT);
    47. Bat ropeEnd = (Bat) world.getWorld().spawnEntity(end, EntityType.BAT);
    48.  
    49. skullStart.getBukkitEntity().setPassenger(ropeStart);
    50. skullEnd.getBukkitEntity().setPassenger(ropeEnd);
    51.  
    52. ropeStart.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 9999999, 1));
    53. ropeEnd.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 9999999, 1));
    54.  
    55. ropeStart.setLeashHolder(ropeEnd);
    56.  
    57. this.start = skullStart;
    58. this.end = skullEnd;
    59. }
    60. }
    61. }
    62.  


    And you could use it by doing something like this;
    Code:java
    1. Rope rope = new Rope();
    2. rope.spawn(startLocation, endLocation);
    3. rope.delete();
    4. rope.move(newStartLocation, newEndLocation);
    5. rope.getEnd();
    6. rope.getStart();


    Note that this is not fully working the way I wanted it to, since you can still damage the mobs that are being attached to eachother.
    For me, that's not a problem since I don't use mobs, so I can cancel all damage to bats. But maybe for anyone using this, it is, so be aware and mess some with this code to your needs.

    [​IMG]



    If packets does the job for you, Goblom improved this by much and made it use packets, which can be found right here.
     
    Skyost, stirante, Phasesaber and 11 others like this.
  2. Offline

    97WaterPolo

    Desle
    Awesome resource! Imagine if you could detect if a player is on the lead, then you could make zip lines :3
     
    DSH105, Desle and bennie3211 like this.
  3. Offline

    Windy Day

    You could calculate a vector between the start and end point then set that as the player's velocity and it might look like they are zip lining :). And only do this when they toggle crouch at the start location.
     
  4. Offline

    97WaterPolo

    Windy Day

    Vectors = :'( for me

    Just cool to speculate though :3 would be nice for buildings, add a pretty cool touch.
     
  5. Offline

    Ultimate_n00b

    hmm.. I might turn this into using packets, so that the WitherSkulls aren't actually there. There's quite a few things you could do with this.
     
    Desle likes this.
  6. Offline

    Windy Day

    Agreed. Just like what Ultimate_n00b said. There id quite a few things you could do with this.

    As for calculating a vector between two locations, with Bukkit its super easy. Just do:
    Code:
    Vector vector = end.toVector().subtract(start.toVector());
    
     
    97waterpolo likes this.
  7. Offline

    LCastr0

    I can't import the CraftWorld :(

    How am I so dumb... :p
     
    Desle likes this.
  8. Offline

    Goblom

    I converted it to Packets. I have no idea if it works though...

    https://gist.github.com/Goblom/9354411

    Code:
    Rope rope = new Rope(Location start, Location end);
    rope.setStart(Location location);
    rope.setEnd(Location location);
    rope.getStart();
    rope.getEnd();
    rope.spawn();
    rope.deSpawn();
    
     
  9. Offline

    Desle

    Goblom
    I totally forgot about the attach packets and whatnot.. Nice. May I use bits of your code and improve both classes of ours? I'll give you credit and all that ;)
     
    Goblom likes this.
  10. Offline

    ItsLeoFTW

    Would it be possible to "connect" a player to the rope, and then make them move along? I suppose vectors would be involved.
     
  11. Offline

    Ultimate_n00b

    You'd have to calculate the curve of the rope then correct the player's velocity every tick. In theory, it could be done.
     
  12. Offline

    LCastr0

    I did calculate the vector and change the player's velocity to it's "distance". The player went completly right at the first try (going in a line that doesn't make any curve), but when I tried with curves, the player went completly wrong xD I guess your way would work ;) Maybe I can try this tomorrow
     
  13. Offline

    Goblom

    I updated source so that it updated every time you set start or end location as well as added a new constructor to make it so that it can automatically spawn at creation.
     
  14. Offline

    ccrama

    Very cool. Is it simple to add a vector to the rope (like a throwing animation), or does the entity just teleport to the new location from the starting location?

    Cheers
     
  15. Offline

    Desle

    ccrama
    I think, if you change the velocity of the witherskulls, they won't stop and blowup.
     
  16. Offline

    Phasesaber

    I can't seem to get it to work, keep having an error at:

    WorldServer world = ((CraftWorld) start.getWorld()).getHandle();

    in the 'spawn()' method.
     
  17. Offline

    Desle

    Phasesaber
    What kind of error? You probably didint import CraftWorld
     
  18. Offline

    Phasesaber


    Well, it was an error on my part(got wrong locations), but now when I spawn them in, they stay there for a second then disappear!
     
  19. Offline

    viper_monster

    Phasesaber maybe you have mobs disabled in that world?
     
  20. Offline

    Phasesaber

    Again, derp. I got it working. :p
    BUT:
    When they spawn in, the bats just fly around. They arent still.

    I was using it on my main world, where I did have them disabled, then I went to my survival world, and it worked(sort of) fine.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  21. Offline

    TeeePeee

    Fixed up and changed Goblom 's code to work with packets. This version is more about sticking the ends to entities. Now, you provide it with an entity to glue the start to and can provide it with another entity to glue the end to.

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8.  
    9. import net.minecraft.server.v1_7_R1.EntityBat;
    10. import net.minecraft.server.v1_7_R1.PacketPlayOutAttachEntity;
    11. import net.minecraft.server.v1_7_R1.PacketPlayOutEntityDestroy;
    12. import net.minecraft.server.v1_7_R1.PacketPlayOutSpawnEntityLiving;
    13. import net.minecraft.server.v1_7_R1.WorldServer;
    14.  
    15. import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
    16. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftEntity;
    17. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    18.  
    19. /**
    20. * Credits to [USER=90856537]Desle[/USER] for the original resource. (Uses entities, not packets)
    21. *
    22. * @author Goblom
    23. * @author Jordan
    24. */
    25. public class Rope {
    26.  
    27. private static final List<Rope> ropes = new ArrayList<Rope>();
    28.  
    29. private Location loc_end;
    30. private EntityBat ent_bat_end;
    31. private Entity holder;
    32.  
    33. public Rope(Location end, Entity holder) {
    34. this.loc_end = end;
    35. this.holder = holder;
    36. for (Rope r : new ArrayList<Rope>(ropes)) {
    37. if (r.holder != null && r.holder.equals(holder)) {
    38. r.holder = null;
    39. r.despawn();
    40. }
    41. }
    42. ropes.add(this);
    43.  
    44. spawn();
    45. }
    46.  
    47. public void setEnd(Location end) {
    48. this.loc_end = end;
    49. spawn();
    50. }
    51.  
    52. public Location getEnd() {
    53. return loc_end;
    54. }
    55.  
    56. private void makeEnt() {
    57. WorldServer world = ((CraftWorld) loc_end.getWorld()).getHandle();
    58.  
    59. if (ent_bat_end == null) {
    60. this.ent_bat_end = new EntityBat(world);
    61. ent_bat_end.setInvisible(true);
    62. }
    63.  
    64. this.ent_bat_end.setLocation(loc_end.getX(), loc_end.getY(), loc_end.getZ(), 0, 0);
    65.  
    66. ent_bat_end.setLeashHolder(((CraftEntity) holder).getHandle(), true);
    67. }
    68.  
    69. public void spawn() {
    70. if (holder == null) {
    71. despawn();
    72. return;
    73. }
    74. makeEnt();
    75. PacketPlayOutSpawnEntityLiving bat_end = new PacketPlayOutSpawnEntityLiving(ent_bat_end);
    76. PacketPlayOutAttachEntity attach = new PacketPlayOutAttachEntity(1, ent_bat_end, ((CraftPlayer) holder).getHandle());
    77.  
    78. for (Player player : Bukkit.getOnlinePlayers()) {
    79. CraftPlayer cP = (CraftPlayer) player;
    80. cP.getHandle().playerConnection.sendPacket(bat_end);
    81. cP.getHandle().playerConnection.sendPacket(attach);
    82. }
    83. }
    84.  
    85. public void despawn() {
    86. PacketPlayOutEntityDestroy destroy = new PacketPlayOutEntityDestroy(ent_bat_end.getId());
    87.  
    88. for (Player player : Bukkit.getOnlinePlayers()) {
    89. CraftPlayer cP = (CraftPlayer) player;
    90. cP.getHandle().playerConnection.sendPacket(destroy);
    91. }
    92. ropes.remove(this);
    93. }
    94.  
    95. public void glueEndTo(Entity att) {
    96. PacketPlayOutAttachEntity attach = new PacketPlayOutAttachEntity(0, ent_bat_end, ((CraftEntity) att).getHandle());
    97. for (Player player : Bukkit.getOnlinePlayers()) {
    98. CraftPlayer cP = (CraftPlayer) player;
    99. cP.getHandle().playerConnection.sendPacket(attach);
    100. }
    101. }
    102. }
    103.  


    A practical use of this might be as follows:

    To throw a rope, listen for a right-click event and do something like so:
    Code:java
    1. private void throwRope(Player thrower) {
    2. Rope rope = new Rope(thrower.getLocation(), thrower);
    3. Snowball snowball = thrower.launchProjectile(Snowball.class);
    4. rope.glueEndTo(snowball);
    5. }

    To glue the end of the rope to the snowball. Once the snowball lands, it is automatically un-glued from the rope and so the rope sticks where it lands. The other end follows the player.
     
    Goblom likes this.
  22. Offline

    Phasesaber

    TeeePeee likes this.
  23. Offline

    TeeePeee

    Goblom likes this.
  24. Offline

    Goblom


    Updated March 13, 2014
    • You can now Spawn & Despawn the rope for a Specific Player
      • rope.spawn(Player player);
      • rope.deSpawn(Player player);
     
  25. Offline

    Aephout14

    ANYTHING IS POSSIBLE IN CODING
     
  26. Offline

    MineStein

    That's really cool.

    Thread officially hijacked :3
     
  27. Offline

    ChipDev

    Seriously, invisibility? I'd take velocity 0 and direction 0
     
Thread Status:
Not open for further replies.

Share This Page