Solved How Could I Change an Entity's Walking Speed?

Discussion in 'Plugin Development' started by Xenthos, Dec 4, 2015.

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

    Xenthos

    Hi, I need help with changing the walking speed of an entity. Not a player's, but perhaps a chicken or zombie or something. Could anybody help me with that? I'm lost.
     
  2. Offline

    Scimiguy

    If you're spawning a custom entity, use
    getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue();

    If not, just give them a speed potion or something
     
  3. Offline

    Xenthos

    @Scimiguy I wouldn't like to use any potion effects, just the code. Do you know how I could do so when any entity is hit?
    Code:
    @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent event) {
            if (!(event.getDamager() instanceof Arrow)) return;
            Arrow arrow = (Arrow) event.getDamager();
            if (arrow.getShooter() instanceof Player) {
                // half walking speed of entity
            }
        }
    This is my code so far.
     
  4. Offline

    mcdorli

  5. Offline

    Xenthos

    @mcdorli I don't know how I could import this, but I will try.

    EDIT: I tried to, and this is what I've come up with.
    Code:
    private static final UUID movementSpeedUID = UUID.fromString("206a89dc-ae78-4c4d-b42c-3b31db3f5a7c");
       
        @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
        public void onEntityDamage(EntityDamageByEntityEvent event) {
            LivingEntity entity = (LivingEntity) event.getEntity();
           
            if (!(event.getDamager() instanceof Arrow)) return;
            Arrow arrow = (Arrow) event.getDamager();
            if (arrow.getShooter() instanceof Player) {
                EntityInsentient nmsEntity = (EntityInsentient) ((CraftLivingEntity) entity).getHandle();
                AttributeInstance attributes = nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED);
               
                AttributeModifier modifier = new AttributeModifier(movementSpeedUID, "movement speed multiplier", 1.0d, 1);
               
                attributes.b(modifier);
            }
        }
    To be quite honest, I do not completely understand the code, although, the entities do go very fast when I hit them.

    Could you explain to me how I could divide their original speed by half? Also, I don't want this to be a "spoonfeed," so if you wouldn't mind, could you also explain to me what this all means? Or anybody?
     
    Last edited: Dec 4, 2015
  6. Offline

    mcdorli

    If ypu want to set the speed to half, set the third argument to 0.5 and the forth to 1. All this does, is changes things inside the entity's class. Read more about nms if you really want to understand it
     
  7. Offline

    Xenthos

    @mcdorli I tried that, but the entity still goes faster!
     
  8. Offline

    mcdorli

    Some things
    You shouldn't use MONITOR priority, except for logger plugins. They shouldn't change anything on the server.
    Try setting the fourth parameter to 0, what happens?
     
  9. Offline

    Xenthos

  10. Offline

    mcdorli

    Try to set it something very low, like 0.01 (not 0) do they go slower? And the fourth to 1
     
  11. Offline

    Xenthos

    @mcdorli I did actually try that before you said to, and it did make them go slower, but I still don't know what the default is.
     
  12. Offline

    mcdorli

    Then experiment
     
  13. Offline

    Scimiguy

    If you're looking for the default movement speed of base mobs (not spiders), then I already did the work for you for my PowerMobs plugin:

    0.23000000417232513
    @Xenthos @mcdorli
     
  14. Offline

    mcdorli

    That's a very interesting number, I'm certain you would get kind of the same thing with 0.23
     
  15. Offline

    Scimiguy

    @mcdorli
    Possibly, but that's the exact default number, so if you want accuracy, just put that in
     
  16. Offline

    mcdorli

    You got that from trial and error, or did you looked up the github page?
     
  17. Offline

    Scimiguy

    @mcdorli
    There's a github page?
    Link?
     
  18. Offline

    Xenthos

    @Scimiguy So I'll put 0.11500000208 for the third argument, but what about for the fourth?
     
  19. Offline

    mcdorli

  20. Offline

    Scimiguy

    Mine has been testing and confirmed.
    The number I gave you is the absolute correct for at least Minecraft 1.8

    Just use mne as a reference so you can easily change it later.

    So when you're setting your attribute, just put in
    0.23000000417232513*0.5

    or something.
    That way you always know how fast you're going in reference to default when you look at your code.
     
  21. Offline

    Xenthos

    @Scimiguy @mcdorli Okay, so I put 1 for the fourth arg, but I can't tell the difference in speed between an entity that has been shot and an entity that hasn't.

    EDIT: I'm getting an error! I'll find it and show you guys
     
  22. Offline

    mcdorli

    Then the best way would be probably to use the .setValue() scimiguy mentioned above. Just put ot right after you're getting the movement speed attribute, and delete everything abput the modifier
     
  23. Offline

    Xenthos

    @mcdorli @Scimiguy This is the error for my code:
    Code:
    ould not pass event EntityDamageByEntityEvent to Test v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEvent(CraftEventFactory.java:87) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:553) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:466) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:585) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.d(EntityLiving.java:1102) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:743) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityMonster.damageEntity(EntityMonster.java:42) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityZombie.damageEntity(EntityZombie.java:167) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityArrow.t_(EntityArrow.java:242) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1607) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.World.tickEntities(World.java:1425) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.WorldServer.tickEntities(WorldServer.java:597) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:786) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-5f38d38-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.IllegalArgumentException: Modifier is already applied on this attribute!
        at net.minecraft.server.v1_8_R3.AttributeModifiable.b(SourceFile:87) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
        at me.xenthos.main.ListenerClass.onEntityDamage(ListenerClass.java:34) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
        ... 21 more
     
  24. Offline

    mcdorli

    You still apply the modofier, delette ot, ypu don't need anything after setting the value of the generic attribute
     
  25. Offline

    Xenthos

Thread Status:
Not open for further replies.

Share This Page