[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

    Garris0n

    By reading the method and writing a similar method in the relevant class. In other words, by copy-pasting it and then fixing whatever's wrong.
     
    Flybelette likes this.
  2. Offline

    Likaos

    Hey,
    Works very well for all Animals, but.. cannot apply speed for spider, any idea ?
    found it

    Code:
    @Override
    protected boolean bj() {
        return true;
      }
     
  3. Offline

    KingFaris11

    I'm getting the following error:
    I know this is for 1.7.5 but any chance of this being 1.7.9?

    Edit: I'm using it for IronGolem - would this not work?
    Code:
            Field jump = null;
            try {
                jump = EntityIronGolem.class.getDeclaredField("bc");
            } catch (NoSuchFieldException ex) {
                ex.printStackTrace();
                return;
            }
            jump.setAccessible(true);
    
    Edit: Fixed, I changed it to EntityLiving.

    Edit 2: I might as well post the full method for 1.7.9:
    Code:
        public void e(float sideMot, float forMot) {
            if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
                super.e(sideMot, forMot);
                this.W = 0.5F;    // Make sure the entity can walk over half slabs, instead of jumping
                return;
            }
     
            this.lastYaw = this.yaw = this.passenger.yaw;
            this.pitch = this.passenger.pitch * 0.5F;
     
            // Set the entity's pitch, yaw, head rotation etc.
            this.b(this.yaw, this.pitch);
            this.aO = this.aM = this.yaw;
     
            this.W = 1.0F;    // The custom entity will now automatically climb up 1 high blocks
     
            sideMot = ((EntityLiving) this.passenger).bd * 0.5F;
            forMot = ((EntityLiving) this.passenger).be;
     
            if (forMot <= 0.0F) {
                forMot *= 0.25F;    // Make backwards slower
            }
            sideMot *= 0.75F;    // Also make sideways slower
     
            float speed = 0.35F;    // 0.2 is the default entity speed. I made it slightly faster so that riding is better than walking
            this.i(speed);    // Apply the speed
            super.e(sideMot, forMot);    // Apply the motion to the entity
     
     
            Field jump = null;
            try {
                jump = EntityLiving.class.getDeclaredField("bc");
            } catch (NoSuchFieldException ex) {
                ex.printStackTrace();
                return;
            }
            jump.setAccessible(true);
     
            if (jump != null && this.onGround) {    // Wouldn't want it jumping while on the ground would we?
                try {
                    if (jump.getBoolean(this.passenger)) {
                        double jumpHeight = 0.5D;
                        this.motY = jumpHeight;    // Used all the time in NMS for entity jumping
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    
     
  4. Offline

    Phasesaber

  5. Offline

    DSH105 Retired Staff

    What are you trying to do with it? Player entities usually don't work the same way as other entity types :).
     
  6. Offline

    Phasesaber

    I am trying to use WASD to control it! :p
     
  7. Offline

    lenis0012

    Phasesaber DSH105
    the velocity is currently not applied to the entity.
    Because EntityPlayer doesn't call EntityLiving tick methods.

    Working on it
     
    Phasesaber and DSH105 like this.
  8. Offline

    Phasesaber

    Velocity? Is that how all entities move?

    lenis0012 DSH105 I just noticed it says it's a 1.7.5 tutorial, and I'm using 1.7.9 (That Bukkit,getPlayer(UUIUD id); is to good to pass up). Are there any differences?

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

    bigteddy98

    Just spawn a normal WASD rideable mob, send the PacketPlayOutEntityDestroy packet and send a PacketPlayOutNamedEntitySpawn to make it look like a player, pretty easy to make. This way you will be able to use pathfinders, and so the WASD option too.
     
    DSH105 likes this.
  10. Offline

    DSH105 Retired Staff

    Nothing has changed in regards to NMS (or obfuscation) in regards to this tutorial. This will work from 1.7.5 through to 1.7.9.
     
  11. Offline

    Phasesaber

    Mkay, it just doesn't seem to work for some reason. lenis0012 said the velocity wasn't being applied, so is movement just velocity?
     
  12. Offline

    DSH105 Retired Staff

    Yep. You can make use of bigteddy98 's suggestion until he fixes it though. I currently have that implemented in one of my plugins, EchoPet :)
     
  13. Offline

    lenis0012

    WRONG!
     
  14. Offline

    DSH105 Retired Staff

    I meant in regards to this post :3
     
  15. Offline

    Phasesaber

    DSH105 lenis0012 I am making a PlayerPet plugin, and when you ride the pet, you should be able to control him. Right now I just have the pet going to where the player is looking.
     
  16. Offline

    Fhbgsdhkfbl

    DSH105
    I don't understand how I would do this with my brooms plugin I'm making, I'm trying to, when they select a "broom", which in my case, will be a hoe.
    It will spawn a horse (which will be textured into a broom :p)
    then make it fly and be controled with WASD and space bar
    Code:
    @EventHandler(priority=EventPriority.HIGH)
      public void onClick(PlayerInteractEvent event) {
        Player p = event.getPlayer();
     
        if (p.getItemInHand().getType() == Material.WOOD_HOE) {
          p.setFlySpeed(0.1F);
          p.sendMessage(ChatColor.GOLD + "[DPBrooms]" + ChatColor.BLUE + " Shooting Star Selected!");
          p.getAllowFlight();
          p.setVelocity(new Vector(0, 2, 0));
          p.setAllowFlight(true);
          p.setFlying(true);
          Entity arrow = Bukkit.getWorld(p.getWorld().getName()).spawnEntity(p.getLocation(), EntityType.ARROW);
          arrow.setPassenger(p);
        }
        else if (p.getItemInHand().getType() == Material.STONE_HOE) {
          p.setFlySpeed(0.2F);
          p.sendMessage(ChatColor.GOLD + "[DPBrooms]" + ChatColor.BLUE + " Comet 140 Selected!");
          p.getAllowFlight();
          p.setVelocity(new Vector(0, 2, 0));
          p.setAllowFlight(true);
          p.setFlying(true);
        }
        else if (p.getItemInHand().getType() == Material.IRON_HOE) {
          p.setFlySpeed(0.4F);
          p.sendMessage(ChatColor.GOLD + "[DPBrooms]" + ChatColor.BLUE + " Nimbus 2000 Selected!");
          p.getAllowFlight();
          p.setVelocity(new Vector(0, 2, 0));
          p.setAllowFlight(true);
          p.setFlying(true);
        }
        else if (p.getItemInHand().getType() == Material.GOLD_HOE) {
          p.setFlySpeed(0.6F);
          p.sendMessage(ChatColor.GOLD + "[DPBrooms]" + ChatColor.BLUE + " Nimbus 2001 Selected!");
          p.getAllowFlight();
          p.setVelocity(new Vector(0, 2, 0));
          p.setAllowFlight(true);
          p.setFlying(true);
        }
        else if (p.getItemInHand().getType() == Material.DIAMOND_HOE) {
          p.setFlySpeed(0.8F);
          p.sendMessage(ChatColor.GOLD + "[DPBrooms]" + ChatColor.BLUE + " Firebolt selected!");
          p.getAllowFlight();
          p.setVelocity(new Vector(0, 2, 0));
          p.setAllowFlight(true);
          p.setFlying(true);
        }
        else if (p.getItemInHand().getType() == Material.BAKED_POTATO) {
          p.setFlying(false);
          p.setAllowFlight(false);
        }
      }
    }
     
  17. Offline

    MrStrauss

    DSH105
    Hi, sorry for bump.
    With this "system", my entity isn't detected as a vehicle and the VehicleExitEvent isn't called but I need to use this event for my entity (I want to cancel it).
    Do you know how I could solve it ?
    Thanks !
     
  18. Offline

    DSH105 Retired Staff


    Yep, it sure is possible.
    1. 'Mimic' a 'Craft' class the represents your custom entity. For example: If you are extending the functionality of a Squid, your craft class would extend CraftSquid.
    2. Make this class a Vehicle ('implements Vehicle').
    3. Override getBukkitEntity() in your NMS/custom entity class so that it returns an instance of your custom Craft class.
    e.g.
    Code:java
    1.  
    2. @Override
    3. public CraftSquid getBukkitEntity() {
    4. if (this.bukkitEntity == null) {
    5. this.bukkitEntity = new CustomCraftSquid(this.world.getServer(), this);
    6. }
    7. return this.bukkitEntity;
    8. }
    9.  


    The event should now be fired for your custom entity :)
     
  19. Offline

    MrStrauss

    Hi ! First, thanks to reply too quickly !
    I've a trouble, vehicle interface and the Craft class aren't compatible : I can't implement vehicle and extend the Craft class at the same time, that makes errors :/

    Thanks again !
     
  20. Offline

    DSH105 Retired Staff

    It's certainly possible. I did it over here.
     
  21. Offline

    erez9901

    Hey ! Nice Tutorial!
    But i have a problem..
    When i ride the zombie it won't call the e() method.. (I try to broadcast message and more but it just not seems to call that), But what the zombie does is that he is walking ALWAYS in the same direction by himself.. I spawned some zombie to test that.. and yea...
    Please Help me..
     
  22. Offline

    Garris0n

    Post your code using code tags, http://www.pastebin.com, or http://gist.github.com
     
  23. Offline

    erez9901

  24. Offline

    Garris0n

    And the code to spawn the zombie?

    Also, you're getting the jump field every single tick. You only have to do it once, in the constructor, and keep it as an instance variable.
     
  25. Offline

    erez9901

    Well thats it actually.. I don't know how to spawn it currectly... Garris0n
     
  26. Offline

    Garris0n

  27. Offline

    erez9901

  28. Offline

    Garris0n

    How exactly do you know the method is not being called?
     
  29. Offline

    erez9901

    Garris0n
    I fixed it.. thx for the help any way :)
     
  30. Offline

    ChroniclerBat

    I understand a large part of java, but i am having trouble with this.
    the custom entity spawns, but i cannot mount it. here is my code:
    Main Class
    Code:java
    1.  
    2. package me.zaxdas247.CreeperKart;
    3.  
    4. import java.util.logging.Logger;
    5.  
    6. import me.zaxdas247.CreeperKart.handlers.ConfigurationHandler;
    7. import me.zaxdas247.CreeperKart.handlers.FileHandler;
    8. import net.minecraft.server.v1_7_R1.Counter;
    9. import me.zaxdas247.CreeperKart.CustomCreeper.CustomEntityType;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.Material;
    14. import org.bukkit.block.Sign;
    15. import org.bukkit.entity.Entity;
    16. import org.bukkit.entity.EntityType;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.block.Action;
    21. import org.bukkit.event.block.SignChangeEvent;
    22. import org.bukkit.event.player.PlayerInteractEntityEvent;
    23. import org.bukkit.event.player.PlayerInteractEvent;
    24. import org.bukkit.plugin.PluginManager;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27. @SuppressWarnings("unused")
    28. public class CreeperKart extends JavaPlugin implements Listener {
    29.  
    30. private ConfigurationHandler configHandler = new ConfigurationHandler(this);
    31. private FileHandler fh = new FileHandler(this);
    32.  
    33. public void onDisable() {
    34. CustomEntityType.unregisterEntities();
    35. getLogger().info("PLUGIN is now disabled.");
    36. }
    37.  
    38. public void onEnable() {
    39. //save config//
    40. this.getConfig().options().copyDefaults(true);
    41. this.getFh().getArena().options().copyDefaults(true);
    42. this.saveConfig();
    43. this.getFh().saveArena();
    44. CustomEntityType.registerEntities();
    45. getLogger().info("CreeperKart enabled");
    46. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    47.  
    48.  
    49. //initiators//
    50. configHandler.init();
    51.  
    52. PluginManager pm = getServer().getPluginManager();
    53.  
    54. getLogger().info("PLUGIN is now enabled.");
    55.  
    56.  
    57. }
    58.  
    59. @EventHandler
    60. public void onPlayerInteract(PlayerInteractEntityEvent event){
    61.  
    62. if(event.getRightClicked() instanceof CustomCreeper){
    63. Entity creeper = event.getRightClicked();
    64. Player player = event.getPlayer();
    65. creeper.setPassenger(player);
    66.  
    67. }
    68.  
    69. }
    70.  
    71.  
    72.  
    73.  
    74.  
    75.  
    76. public FileHandler getFh() {
    77. return fh;
    78. }
    79.  
    80. public void setFh(FileHandler fh) {
    81. this.fh = fh;
    82. }
    83.  
    84. public ConfigurationHandler getConfighandler(){
    85. return configHandler;
    86. }
    87.  
    88. public void setConfigHandler(ConfigurationHandler configHandler){
    89. this.configHandler = configHandler;
    90. }
    91.  
    92. public void sendMessage(Player player, String message){
    93. player.sendMessage(ChatColor.GOLD + "[ " + ChatColor.GREEN + this.getConfighandler().getPrefix() + ChatColor.GOLD + " ] " + ChatColor.GRAY + message);
    94. }
    95.  


    Custom Entity:
    Code:java
    1. package me.zaxdas247.CreeperKart;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.util.List;
    5.  
    6. import org.bukkit.entity.EntityType;
    7.  
    8. import java.util.Map;
    9.  
    10. import net.minecraft.server.v1_7_R1.BiomeBase;
    11. import net.minecraft.server.v1_7_R1.BiomeMeta;
    12.  
    13. import net.minecraft.server.v1_7_R1.EntityHuman;
    14. import net.minecraft.server.v1_7_R1.EntityInsentient;
    15. import net.minecraft.server.v1_7_R1.EntityLiving;
    16. import net.minecraft.server.v1_7_R1.EntityTypes;
    17. import net.minecraft.server.v1_7_R1.World;
    18.  
    19.  
    20.  
    21. public class CustomCreeper extends net.minecraft.server.v1_7_R1.EntityCreeper{
    22. public CustomCreeper(World world) {
    23. super(world);
    24. // TODO Auto-generated constructor stub
    25. }
    26. public enum CustomEntityType {
    27.  
    28. CREEPER("Creeper", 50, EntityType.CREEPER, net.minecraft.server.v1_7_R1.EntityCreeper.class, CustomCreeper.class);
    29.  
    30. private String name;
    31. private int id;
    32. private EntityType entityType;
    33. private Class<? extends EntityInsentient> nmsClass;
    34. private Class<? extends EntityInsentient> customClass;
    35.  
    36. private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass,
    37. Class<? extends EntityInsentient> customClass) {
    38. this.name = name;
    39. this.id = id;
    40. this.entityType = entityType;
    41. this.nmsClass = nmsClass;
    42. this.customClass = customClass;
    43. }
    44.  
    45. public String getName() {
    46. return name;
    47. }
    48.  
    49. public int getID() {
    50. return id;
    51. }
    52.  
    53. public EntityType getEntityType() {
    54. return entityType;
    55. }
    56.  
    57. public Class<? extends EntityInsentient> getNMSClass() {
    58. return nmsClass;
    59. }
    60.  
    61. public Class<? extends EntityInsentient> getCustomClass() {
    62. return customClass;
    63. }
    64.  
    65. /**
    66.   * Register our entities.
    67.   */
    68. public static void registerEntities() {
    69. for (CustomEntityType entity : values())
    70. a(entity.getCustomClass(), entity.getName(), entity.getID());
    71.  
    72. // BiomeBase#biomes became private.
    73. BiomeBase[] biomes;
    74. try {
    75. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    76. } catch (Exception exc) {
    77. // Unable to fetch.
    78. return;
    79. }
    80. for (BiomeBase biomeBase : biomes) {
    81. if (biomeBase == null)
    82. break;
    83.  
    84. // This changed names from J, K, L and M.
    85. for (String field : new String[] { "as", "at", "au", "av" })
    86. try {
    87. Field list = BiomeBase.class.getDeclaredField(field);
    88. list.setAccessible(true);
    89. @SuppressWarnings("unchecked")
    90. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    91.  
    92. // Write in our custom class.
    93. for (BiomeMeta meta : mobList)
    94. for (CustomEntityType entity : values())
    95. if (entity.getNMSClass().equals(meta.b))
    96. meta.b = entity.getCustomClass();
    97. } catch (Exception e) {
    98. e.printStackTrace();
    99. }
    100. }
    101. }
    102.  
    103. /**
    104.   * Unregister our entities to prevent memory leaks. Call on disable.
    105.   */
    106. public static void unregisterEntities() {
    107. for (CustomEntityType entity : values()) {
    108. // Remove our class references.
    109. try {
    110. ((Map<?, ?>) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
    111. } catch (Exception e) {
    112. e.printStackTrace();
    113. }
    114.  
    115. try {
    116. ((Map<?, ?>) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
    117. } catch (Exception e) {
    118. e.printStackTrace();
    119. }
    120. }
    121.  
    122. for (CustomEntityType entity : values())
    123. try {
    124. // Unregister each entity by writing the NMS back in place of the custom class.
    125. a(entity.getNMSClass(), entity.getName(), entity.getID());
    126. } catch (Exception e) {
    127. e.printStackTrace();
    128. }
    129.  
    130. // Biomes#biomes was made private so use reflection to get it.
    131. BiomeBase[] biomes;
    132. try {
    133. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    134. } catch (Exception exc) {
    135. // Unable to fetch.
    136. return;
    137. }
    138. for (BiomeBase biomeBase : biomes) {
    139. if (biomeBase == null)
    140. break;
    141.  
    142. // The list fields changed names but update the meta regardless.
    143. for (String field : new String[] { "as", "at", "au", "av" })
    144. try {
    145. Field list = BiomeBase.class.getDeclaredField(field);
    146. list.setAccessible(true);
    147. @SuppressWarnings("unchecked")
    148. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    149.  
    150. // Make sure the NMS class is written back over our custom class.
    151. for (BiomeMeta meta : mobList)
    152. for (CustomEntityType entity : values())
    153. if (entity.getCustomClass().equals(meta.b))
    154. meta.b = entity.getNMSClass();
    155. } catch (Exception e) {
    156. e.printStackTrace();
    157. }
    158. }
    159. }
    160.  
    161. /**
    162.   * A convenience method.
    163.   * @param clazz The class.
    164.   * @param f The string representation of the private static field.
    165.   * @return The object found
    166.   * @throws Exception if unable to get the object.
    167.   */
    168. private static Object getPrivateStatic(Class<?> clazz, String f) throws Exception {
    169. Field field = clazz.getDeclaredField(f);
    170. field.setAccessible(true);
    171. return field.get(null);
    172. }
    173.  
    174. /*
    175.   * Since 1.7.2 added a check in their entity registration, simply bypass it and write to the maps ourself.
    176.   */
    177. @SuppressWarnings("unchecked")
    178. private static void a(Class<?> paramClass, String paramString, int paramInt) {
    179. try {
    180. ((Map<String, Class<?>>) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    181. ((Map<Class<?>, String>) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    182. ((Map<Integer, Class<?>>) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
    183. ((Map<Class<?>, Integer>) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    184. ((Map<String, Integer>) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    185. } catch (Exception exc) {
    186. // Unable to register the new class.
    187. }
    188. }
    189. }
    190. public void e(float sideMot, float forMot) {
    191. if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
    192. super.e(sideMot, forMot);
    193. return;
    194. }
    195.  
    196.  
    197. this.lastYaw = this.yaw = this.passenger.yaw;
    198. this.pitch = this.passenger.pitch * 1F;
    199.  
    200. // Set the entity's pitch, yaw, head rotation etc.
    201. this.b(this.yaw, this.pitch); //[url]https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/Entity.java#L163-L166[/url]
    202. this.aO = this.aM = this.yaw;
    203.  
    204.  
    205. sideMot = ((EntityLiving) this.passenger).be * 0.5F;
    206. forMot = ((EntityLiving) this.passenger).be;
    207.  
    208. if (forMot <= 0.0F) {
    209. forMot *= 0.25F; // Make backwards slower
    210. }
    211. sideMot *= 0.75F; // Also make sideways slower
    212.  
    213. float speed = 0.35F; // 0.2 is the default entity speed. I made it slightly faster so that riding is better than walking
    214. this.i(speed); // Apply the speed
    215. super.e(sideMot, forMot); // Apply the motion to the entity
    216.  
    217.  
    218. /*Field jump = null;
    219.   try {
    220.   jump = EntityLiving.class.getDeclaredField("bc");
    221.   } catch (NoSuchFieldException e1) {
    222.   // TODO Auto-generated catch block
    223.   e1.printStackTrace();
    224.   } catch (SecurityException e1) {
    225.   // TODO Auto-generated catch block
    226.   e1.printStackTrace();
    227.   }
    228.   jump.setAccessible(true);
    229.  
    230.   if (jump != null && this.onGround) { // Wouldn't want it jumping while on the ground would we?
    231.   try {
    232.   if (jump.getBoolean(this.passenger)) {
    233.   double jumpHeight = 0.5D;
    234.   this.motY = jumpHeight; // Used all the time in NMS for entity jumping
    235.   }
    236.   } catch (IllegalAccessException e) {
    237.   e.printStackTrace();
    238.   }
    239.   }
    240.   }*/
    241. }
    242. }
    243.  
    244.  
    245.  
     
Thread Status:
Not open for further replies.

Share This Page