Teleport mobs to own location every tick?

Discussion in 'Plugin Development' started by TheFluffey, Dec 14, 2012.

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

    TheFluffey

    I already have made it so that mobs can't move by overriding the class. But they can still get pushed. I need a plugin which teleports mobs to their own location every tick. Please provide the complete code! Thanks.
     
  2. Offline

    ahuby09

    id imagine you would use a scdualar
     
  3. Offline

    RealDope

    Code:JAVA
    1.  
    2. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this,
    3. new BukkitRunnable() {
    4. public void run() {
    5. something.teleport(someLocation);
    6. }
    7. }, 0, 1);
    8.  


    I think teleportong once every tick (1/20th of a second) is going to be EXTREMELY resource intensive..
     
  4. Offline

    fireblast709

    mostly depending on what you do
     
  5. Offline

    TheFluffey

    There will be a max of 49 zombies spawned in at 1 time, so it shouldn't be too bad?
     
  6. Offline

    RealDope

    You might want to do a check to see if they have actually moved before you teleport them. That would probably be worth something atleast.
     
  7. Offline

    TheFluffey

    Can you provide code. Sorry I neglected to mention, but I am extremely new to code so full code with some comments is nice.

    Edit: Also worth mentioning that they can only be pushed as I already overrode the class and made it so they cannot move. I am adding this as an extra feature to prevent people from pushing the mobs.
     
  8. Offline

    RealDope

    Make a hashmap storing the entityID and the location of it. Then on the next tick, check if the mobs current location is different from the one stored in the hashmap.
     
  9. Offline

    Barinade

    When a creature spawns, save it's spawn location
    Every x ticks, check if they are 20 blocks from their spawn location
    If so, teleport them back to their spawn
     
  10. Offline

    TheFluffey

    This is good, but please do it with 1 block. If they are outside of the one block, they are teleported back. Can you provide code? I am a noob.
     
  11. Offline

    fireblast709

    check the current x, y and z. If they do not match the spawn x, y and z, you teleport them to the spawn x, y z
     
  12. Offline

    TheFluffey

    Can you write the code for me? I can compile and import of needed but once again I am really new to coding.
     
  13. Offline

    fireblast709

    this means if you never try, you never learn ;3. Besides, you have the whole code layout there, just need to code it. Everyone was on that point at some time
     
  14. Offline

    TheFluffey

    I know this. If you wrote it and marked it up, I would learn the most. I learn by example best by far.
     
  15. Offline

    NinjaW0lf

    Dont ask for code, its called spoon-feading. U know, how u feed a baby for them. Instead ask for ideas or some hints in the right direction. Then go search the javadocs and related java development wikis to help.

    As for the problem, do this. Create a class to check ever so-so ticks(every tick i wouldnt reccomend because alot of mobs might make it resource intensive on server.) then check if the entity has moved, if they have, fire a custom event. then in another class, listen to the event, and if the entity has moved, teleport to original position

    Better to learn by trial and error, work on your own code, if it doesnt work, post it and others can help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  16. Offline

    TheFluffey

    I literally know none. Some pointers to code snippets would help.
     
  17. Offline

    RealDope

    Just ask someone to code it for you at this point lol
     
  18. Offline

    TheFluffey

    Could you write anything up? I would be very grateful. Comments would be nice but aren't necessary by any means. Basically, on spawn of zombie store location in a hashmap. Every 5 ticks the plugin checks if the zombies location does not match the spawn block. If it doesn't, the plugin teleports the zombie to the spawn block.
     
  19. Offline

    Barinade

    pseudo

    entity spawn event:
    hashmap.put(uuid, location);

    scheduler (10s?):
    collect monsters by classes (better method?)
    iterate monsters
    hashmap.get(uuid) //returns location where they spawned
    if currentx - spawnx >= -10 && <= 10
    or
    currentz - spawnz >= -10 && <= 10

    creature out of bounds, teleport to spawn location
     
  20. Offline

    ahuby09

    i was there at one point lol i ask for text tuts or vid tuts not directly for the code so i totaly agree with u
     
  21. Offline

    Barinade

    What you just described will basically keep a zombie stationary... Is that what you want :confused:
     
  22. Offline

    TheFluffey

    I already have the code that overrides the zombie class and makes him not able to move. However, they can still be pushed which is a problem. This simply prevents that.
     
  23. Offline

    fireblast709

    in that override class, just have a final location in the class body. In the update method you just check if the current location matches that. If not, teleport
     
  24. Offline

    TheFluffey

    I know extremely little java (None, basically without tutorial). Thanks for that though, it is nice to know I can merge the code.



    Here is the current code: http://pastie.org/5536295

    Can anyone edit it to store entity data and do this?
    Basically, on spawn of zombie store location in a hashmap. Every 5 ticks the plugin checks if the zombies location does not match the spawn block. If it doesn't, the plugin teleports the zombie to the spawn block.

    Thanks! Please return completed code on pastebin or pastie.org!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  25. Offline

    lenis0012

    dpends on what you wanna make.
    if you want to prevent mobs form moving you shall learn protocal (packet management)
    example, one of my plugins: http://dev.bukkit.org/server-mods/statues/
     
  26. Offline

    TheFluffey

    That would work great. However, I need players to be able to interact with the bodies by right clicking on them as if they were normal mobs, so I can have players right click on them. Also, I need zombies.

    Edit: Basically, would it be possible to send packets and make the statues act as if they were real mobs, so players can right click them?
     
  27. Offline

    fireblast709

    Why not create your own EntityZombie class and make every spawned zombie one?
     
  28. Offline

    TheFluffey

    Those statues are just packets, a mirror of the real zombies. I need zombies that won't move (Already done), can't be pushed (what I really need help with), and can be interacted with by right click.
     
  29. Offline

    fireblast709

    TheFluffey that and overriding the update method
     
  30. Offline

    Barinade

    Code:
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class StationaryZombies extends JavaPlugin{
        Map<Integer, Location> zspawns = new HashMap<Integer, Location>();
     
        public void onEnable() {
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
     
                @Override
                public void run() {
                    for (World w : getServer().getWorlds()) {
                        if (w.getEntitiesByClass(Zombie.class) != null) {
                            Collection<Zombie> zombies = w.getEntitiesByClass(Zombie.class);
                            for (Zombie z : zombies) {
                                if (zspawns.containsKey(z.getEntityId())) {
                                    if (zspawns.get(z.getEntityId()) != z.getLocation()) {
                                        z.teleport(zspawns.get(z.getEntityId()));
                                    }
                                } else {
                                    z.remove();
                                }
                            }
                        }
                    }
                   
                }
               
            }, 20L, 20L);
            getServer().getPluginManager().registerEvents(new Listener() {
                @EventHandler
                public void entitySpawn(CreatureSpawnEvent e) {
                    if (e.getEntityType() == EntityType.ZOMBIE) {
                        zspawns.put(e.getEntity().getEntityId(), e.getEntity().getLocation());
                    }
                }
            }, this);
           
        }
        public void onDisable() {
           
        }
     
    }
    
    This, plus your code that make it so they don't start moving, would do the trick
     
Thread Status:
Not open for further replies.

Share This Page