[Tutorial] [1.7.5] WASD Entity Riding

Discussion in 'Resources' started by DSH105, Jul 26, 2013.

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

    Ultimate_n00b

    I tried your enderdragon code, "kinda" works. He moves side to side, but up and down it's as the dragon wishes.
     
  2. Offline

    DSH105 Retired Staff

    It works fine in EchoPet. Remember that the up and down movement is also based on the player's yaw. It will fly down if the rider is looking below a certain angle.
     
    Garris0n likes this.
  3. Offline

    Ultimate_n00b

    No I get that. However, the dragon itself seems to still control going up and down. Do you want to see my example code?
     
  4. Offline

    Garris0n

    I'd be curious to see it.
     
  5. Offline

    Ultimate_n00b

    Code:java
    1. package me.paulbgd.bukkit.TST;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import net.minecraft.server.v1_7_R1.Entity;
    6. import net.minecraft.server.v1_7_R1.EntityEnderDragon;
    7. import net.minecraft.server.v1_7_R1.EntityHuman;
    8. import net.minecraft.server.v1_7_R1.EntityLiving;
    9. import net.minecraft.server.v1_7_R1.MathHelper;
    10. import net.minecraft.server.v1_7_R1.Vec3D;
    11. import net.minecraft.server.v1_7_R1.World;
    12.  
    13. import org.bukkit.Location;
    14. import org.bukkit.util.Vector;
    15.  
    16. public class DragonRider extends EntityEnderDragon {
    17.  
    18. Field jump = null;
    19.  
    20. public DragonRider(World world) {
    21. super(world);
    22. try {
    23. jump = EntityLiving.class.getDeclaredField("bd");
    24. e.printStackTrace();
    25. }
    26. jump.setAccessible(true);
    27. }
    28.  
    29. @Override
    30. public void e(float sideMot, float forMot) {
    31. float f;
    32. float f1;
    33.  
    34. if (this.passenger != null && (this.passenger instanceof EntityHuman)) {
    35. EntityHuman human = (EntityHuman) this.passenger;
    36.  
    37. float side = human.be;
    38. float forw = human.bf;
    39.  
    40. Vector v = new Vector();
    41. Location l = new Location(this.world.getWorld(), this.locX, this.locY, this.locZ);
    42.  
    43. if (side < 0.0F) {
    44. l.setYaw(this.passenger.yaw - 90);
    45. v.add(l.getDirection().normalize().multiply(-0.5));
    46. } else if (side > 0.0F) {
    47. l.setYaw(this.passenger.yaw + 90);
    48. v.add(l.getDirection().normalize().multiply(-0.5));
    49. }
    50.  
    51. if (forw < 0.0F) {
    52. l.setYaw(this.passenger.yaw);
    53. v.add(l.getDirection().normalize().multiply(0.5));
    54. } else if (forw > 0.0F) {
    55. l.setYaw(this.passenger.yaw);
    56. v.add(l.getDirection().normalize().multiply(0.5));
    57. }
    58.  
    59. this.lastYaw = this.yaw = this.passenger.yaw - 180;
    60. this.pitch = this.passenger.pitch * 0.5F;
    61. this.b(this.yaw, this.pitch);
    62. this.aP = this.aN = this.yaw;
    63.  
    64. if (this.jump != null) {
    65. try {
    66. if (this.jump.getBoolean(this.passenger)) {
    67. v.setY(0.5F);
    68. } else {
    69. if (((EntityLiving) this.passenger).pitch >= 50) {
    70. v.setY(-0.4F);
    71. }
    72. }
    73. } catch (Exception e) {
    74. e.printStackTrace();
    75. }
    76. }
    77.  
    78. l.add(v.multiply(2.25));
    79. this.setPosition(l.getX(), l.getY(), l.getZ());
    80. return;
    81. }
    82.  
    83. if (this.world.isStatic) {
    84. f = MathHelper.cos(this.by * 3.1415927F * 2.0F);
    85. f1 = MathHelper.cos(this.bx * 3.1415927F * 2.0F);
    86. if (f1 <= -0.3F && f >= -0.3F) {
    87. this.world.a(this.locX, this.locY, this.locZ, "mob.enderdragon.wings", 5.0F, 0.8F + this.random.nextFloat() * 0.3F, false);
    88. }
    89. }
    90.  
    91. this.bx = this.by;
    92. float f2;
    93.  
    94. if (this.getHealth() <= 0.0F) {
    95. f = (this.random.nextFloat() - 0.5F) * 8.0F;
    96. f1 = (this.random.nextFloat() - 0.5F) * 4.0F;
    97. f2 = (this.random.nextFloat() - 0.5F) * 8.0F;
    98. this.world.addParticle("largeexplode", this.locX + (double) f, this.locY + 2.0D + (double) f1, this.locZ + (double) f2, 0.0D, 0.0D, 0.0D);
    99. } else {
    100. f = 0.2F / (MathHelper.sqrt(this.motX * this.motX + this.motZ * this.motZ) * 10.0F + 1.0F);
    101. f *= (float) Math.pow(2.0D, this.motY);
    102. if (this.bA) {
    103. this.by += f * 0.5F;
    104. } else {
    105. this.by += f;
    106. }
    107.  
    108. this.yaw = MathHelper.g(this.yaw);
    109. if (this.bo < 0) {
    110. for (int d05 = 0; d05 < this.bn.length; ++d05) {
    111. this.bn[d05][0] = (double) this.yaw;
    112. this.bn[d05][1] = this.locY;
    113. }
    114. }
    115.  
    116. if (++this.bo == this.bn.length) {
    117. this.bo = 0;
    118. }
    119.  
    120. this.bn[this.bo][0] = (double) this.yaw;
    121. this.bn[this.bo][1] = this.locY;
    122. double d0;
    123. double d1;
    124. double d2;
    125. double d3;
    126. float f3;
    127.  
    128. if (this.world.isStatic) {
    129. if (this.bh > 0) {
    130. d0 = this.locX + (this.bi - this.locX) / (double) this.bh;
    131. d1 = this.locY + (this.bj - this.locY) / (double) this.bh;
    132. d2 = this.locZ + (this.bk - this.locZ) / (double) this.bh;
    133. d3 = MathHelper.g(this.bl - (double) this.yaw);
    134. this.yaw = (float) ((double) this.yaw + d3 / (double) this.bh);
    135. this.pitch = (float) ((double) this.pitch + (this.bm - (double) this.pitch) / (double) this.bh);
    136. --this.bh;
    137. this.setPosition(d0, d1, d2);
    138. this.b(this.yaw, this.pitch);
    139. }
    140. } else {
    141. d0 = this.h - this.locX;
    142. d1 = this.i - this.locY;
    143. d2 = this.j - this.locZ;
    144. d3 = d0 * d0 + d1 * d1 + d2 * d2;
    145.  
    146. try {
    147. Field bD = this.getClass().getDeclaredField("bD");
    148. bD.setAccessible(true);
    149.  
    150. if (bD.get(this) != null) {
    151. Entity ent = (Entity) bD.get(this);
    152. this.h = ent.locX;
    153. this.j = ent.locZ;
    154. double d4 = this.h - this.locX;
    155. double d5 = this.j - this.locZ;
    156. double d6 = Math.sqrt(d4 * d4 + d5 * d5);
    157. double d7 = 0.4000000059604645D + d6 / 80.0D - 1.0D;
    158.  
    159. if (d7 > 10.0D) {
    160. d7 = 10.0D;
    161. }
    162.  
    163. this.i = ent.boundingBox.b + d7;
    164. } else {
    165. this.h += this.random.nextGaussian() * 2.0D;
    166. this.j += this.random.nextGaussian() * 2.0D;
    167. }
    168. } catch (Exception e) {
    169. e.printStackTrace();
    170. }
    171.  
    172. d1 /= (double) MathHelper.sqrt(d0 * d0 + d2 * d2);
    173. f3 = 0.6F;
    174. if (d1 < (double) (-f3)) {
    175. d1 = (double) (-f3);
    176. }
    177.  
    178. if (d1 > (double) f3) {
    179. d1 = (double) f3;
    180. }
    181.  
    182. this.motY += d1 * 0.10000000149011612D;
    183. this.yaw = MathHelper.g(this.yaw);
    184. double d8 = 180.0D - Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D;
    185. double d9 = MathHelper.g(d8 - (double) this.yaw);
    186.  
    187. if (d9 > 50.0D) {
    188. d9 = 50.0D;
    189. }
    190.  
    191. if (d9 < -50.0D) {
    192. d9 = -50.0D;
    193. }
    194.  
    195. Vec3D vec3d = this.world.getVec3DPool().create(this.h - this.locX, this.i - this.locY, this.j - this.locZ).a();
    196. Vec3D vec3d1 = this.world.getVec3DPool().create((double) MathHelper.sin(this.yaw * 3.1415927F / 180.0F), this.motY, (double) (-MathHelper.cos(this.yaw * 3.1415927F / 180.0F))).a();
    197. float f4 = (float) (vec3d1.b(vec3d) + 0.5D) / 1.5F;
    198.  
    199. if (f4 < 0.0F) {
    200. f4 = 0.0F;
    201. }
    202.  
    203. this.bg *= 0.8F;
    204. float f5 = MathHelper.sqrt(this.motX * this.motX + this.motZ * this.motZ) * 1.0F + 1.0F;
    205. double d10 = Math.sqrt(this.motX * this.motX + this.motZ * this.motZ) * 1.0D + 1.0D;
    206.  
    207. if (d10 > 40.0D) {
    208. d10 = 40.0D;
    209. }
    210.  
    211. this.bg = (float) ((double) this.bg + d9 * (0.699999988079071D / d10 / (double) f5));
    212. this.yaw += this.bg * 0.1F;
    213. float f6 = (float) (2.0D / (d10 + 1.0D));
    214. float f7 = 0.06F;
    215.  
    216. this.a(0.0F, -1.0F, f7 * (f4 * f6 + (1.0F - f6)));
    217. if (this.bA) {
    218. this.move(this.motX * 0.800000011920929D, this.motY * 0.800000011920929D, this.motZ * 0.800000011920929D);
    219. } else {
    220. this.move(this.motX, this.motY, this.motZ);
    221. }
    222.  
    223. Vec3D vec3d2 = this.world.getVec3DPool().create(this.motX, this.motY, this.motZ).a();
    224. float f8 = (float) (vec3d2.b(vec3d1) + 1.0D) / 2.0F;
    225.  
    226. f8 = 0.8F + 0.15F * f8;
    227. this.motX *= (double) f8;
    228. this.motZ *= (double) f8;
    229. this.motY *= 0.9100000262260437D;
    230. }
    231.  
    232. }
    233. }
    234.  
    235. public void setPosition(double d0, double d1, double d2) {
    236. this.locX = d0;
    237. this.locY = d1;
    238. this.locZ = d2;
    239. float f = this.width / 2.0F;
    240. float f1 = this.length;
    241.  
    242. this.boundingBox.b(d0 - (double) f, d1 - (double) this.height + (double) this.X, d2 - (double) f, d0 + (double) f, d1 - (double) this.height + (double) this.X + (double) f1, d2 + (double) f);
    243. }
    244.  
    245. }
    246.  
     
  6. Offline

    ColaCraft

    How can I make it trigger VehicleExitEvent when the user leaves the CustomEntity??
     
  7. Offline

    DSH105 Retired Staff

    You have to implement something similar to that used in CraftBukkit - a 'Craft' class that represents the custom NMS entity needs to implement Vehicle for the event to be fired. An example of this can be found here.
     
  8. Offline

    ColaCraft


    I had it set up like this

    Code:java
    1. public class CustomEntitySpider extends EntitySpider implements Vehicle{


    will it work just as well if I make it

    Code:java
    1. public class CustomEntitySpider extends CraftEnitity implements Vehicle{


    or can I make it extends both EntitySpider and CraftEntity??
     
  9. Offline

    Garris0n

    http://forums.bukkit.org/threads/exit-custom-entity.223996/#post-2213322
     
  10. Ive extended EntityEnderDragon , its all working fine, but the dragon can pass thru blocks... anyway to stop him from doing it?
     
  11. Offline

    DSH105 Retired Staff

  12. DSH105 I cant set the field Y to false, (I mean, I can, but its doesnt change anything :/ ) any idea? haha
     
  13. Offline

    DSH105 Retired Staff

    Works perfectly fine in EchoPet, and for vanilla EnderDragons. How doesn't it work?
     
  14. DSH105 Im doing this.Y = false; at the constructor, do I need to set it anywhere else?... :confused:
     
  15. Offline

    DSH105 Retired Staff

    Would you be able to show me your entity class?
     
  16. DSH105
    Code:java
    1. public class Dragon extends EntityEnderDragon{
    2.  
    3. public Plane(World world) {
    4. super(world);
    5. this.a(0.5F, 0.9F);
    6. this.Y = false;
    7.  
    8. try {
    9. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    10. bField.setAccessible(true);
    11. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    12. cField.setAccessible(true);
    13. bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    14. bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    15. cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    16. cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. }

    I cut off all the movement code so its will be more clear, if you would like to see the riding code aswell, just tell :p and how can I make the dragon stop moving? (just stay in place)
     
  17. Offline

    MoeMix

    How would you do this for a non-living entity? Such as a minecart?
     
  18. Offline

    Garris0n

    I would assume it works in a similar manner but you'd have to manually move the minecart.
     
  19. Offline

    MoeMix

    there isn't an e method in the EntityMinecartAbstract class... So I get an error upon trying to override the e method :confused:
     
  20. Offline

    Garris0n

    Look at what the actual e method does in EntityLiving.
     
    DSH105 likes this.
  21. Offline

    DSH105 Retired Staff

    Updated the OP to 1.7.5
    Changes to note:
    • X is now W (in relation to allowing mobs to auto-walk up half slabs and blocks)
    • aP is now aO (for setting yaw in the tutorial)
    • aN is now aM (for setting yaw in the tutorial)
    • The jump field bd, accessed via reflection is now bc (in the extended section of the tutorial.

    As Garris0n said, the e method is used mainly for movement calculations. Instead of calling super.e(...) you would have to move the entity yourself. An example of this can be found in one of my plugins.
     
    Garris0n and ZeusAllMighty11 like this.
  22. Offline

    Buizelfan2

    How come whenever i spawn in a chicken (the entity im using) with a spawn egg it just starts to sink down into the ground?

    Code:java
    1. @Override
    2. public void e(float sideMot, float forMot) {
    3. if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
    4. super.e(sideMot, forMot);
    5. this.X = 0.5F;
    6. return;
    7. }
    8.  
    9. EntityHuman human = (EntityHuman) this.passenger;
    10. if (human.getBukkitEntity() == Bukkit.getPlayer(getName())) {
    11. super.e(sideMot, forMot);
    12. this.W = 0.5F;
    13. return;
    14. }
    15.  
    16. this.lastYaw = this.yaw = this.passenger.yaw;
    17. this.pitch = this.passenger.pitch * 0.5F;
    18.  
    19. this.b(this.yaw, this.pitch);
    20. this.aO = this.aM = this.yaw;
    21.  
    22. this.X = 1.0F;
    23.  
    24. sideMot = ((EntityLiving) this.passenger).bf * 0.5f;
    25. forMot = ((EntityLiving) this.passenger).be;
    26.  
    27. if (forMot <= 0.0F) {
    28. forMot *= 0.25F;
    29. }
    30. sideMot *= 0.75F;
    31.  
    32. float speed = 0.35F;
    33. this.i(speed);
    34. super.e(sideMot, forMot);
    35.  
    36. }
     
  23. Offline

    Squid_Boss

    Buizelfan2 It has to be a CustomEntity.


    Btw DSH105 You never did change the X to a W in the post, it just causes an error if you keep it at X.

    Btw DSH105 , when trying to override a Squid's e method, it doesn't work. I was looking at the EntitySquid class, and the e method already seems different. Any ideas ?

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

    Garris0n

    Use the code from EntityLiving or wherever instead of calling the superclass's method.
     
    DSH105 likes this.
  25. Offline

    Squid_Boss

    Garris0n Yeah, I am using the code from EntityLiving, I was comparing it to the Squid's e method for some reason...but it's definitely from EntityLiving. Either way, I can't control the Custom Squid.
     
  26. Offline

    Garris0n

    Post your code I guess...or make a thread in Plugin Development and post it there.
     
  27. Offline

    Squid_Boss

    Garris0n
    Code:java
    1. public class CustomEntitySquid extends EntitySquid {
    2.  
    3. public CustomEntitySquid(World world) {
    4. super(world);
    5. }
    6.  
    7. @Override
    8. public void e(float sideMot, float forMot) {
    9. if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
    10. super.e(sideMot, forMot);
    11. return;
    12. }
    13.  
    14. this.lastYaw = this.yaw = this.passenger.yaw;
    15. this.pitch = this.passenger.pitch * 0.5F;
    16.  
    17. this.b(this.yaw, this.pitch);
    18. this.aO = this.aM = this.yaw;
    19.  
    20. this.W = 1.0F;
    21.  
    22. sideMot = ((EntityLiving) this.passenger).bd * 0.5F;
    23. forMot = ((EntityLiving) this.passenger).be;
    24.  
    25. if (forMot <= 0.0F) {
    26. forMot *= 0.25F;
    27. }
    28. sideMot *= 0.75F;
    29.  
    30. float speed = 0.35F;
    31. this.i(speed);
    32. super.e(sideMot, forMot);
    33. }
    34.  
    35. }


    Same thing as DSH105 's, nothing has changed.
     
  28. Offline

    Garris0n

    Squid_Boss As I said, you can't call the super because it doesn't do anything. You have to do what the EntityLiving class does manually.
     
    DSH105 likes this.
  29. Offline

    Flybelette

    Hello ! How should we do that ? :)
     
Thread Status:
Not open for further replies.

Share This Page