EntityMoveEvent Class

Discussion in 'Resources' started by _Belknap_, May 16, 2014.

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


    Hi, I have seen some threads about creating an EntityMoveEvent, so here is one for you with some steps:

    1. Paste this code into a class named EntityMoveEvent:
    2. package (packagename)
    6. import java.util.HashMap;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Location;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.event.Event;
    12. import org.bukkit.event.HandlerList;
    13. import org.bukkit.event.Cancellable;
    15. public class EntityMoveEvent extends Event implements Cancellable{
    17. private static final HandlerList handlers = new HandlerList();
    18. private boolean cancelled = false;
    19. private Entity entity;
    22. public boolean isCancelled() {
    23. return cancelled;
    24. }
    26. public void setCancelled(boolean cancelled) {
    27. this.cancelled = cancelled;
    28. }
    30. public HandlerList getHandlers() {
    31. return handlers;
    32. }
    34. public static HandlerList getHandlerList() {
    35. return handlers;
    36. }
    37. public Entity getEntity() {
    38. return entity;
    40. }
    41. public Location getLocation() {
    42. return entity.getLocation();
    43. }
    44. }

    2: Put this as a public HashMap in your class extending JavaPlugin:
    1. HashMap<Entity,Location> previous = new HashMap<Entity,Location>();

    3: In your main class extending JavaPlugin, put this in your onEnable() method:
    2. for (World w : this.getServer().getWorlds()) {
    3. for (Entity e : w.getEntities()) {
    4. previous.put(e,e.getLocation());
    5. }}
    6. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    8. public void run() {
    9. for (World w : this.getServer().getWorlds()) {
    10. for (Entity e : w.getEntities()) {
    11. if (e.getLocation().equals(previous.get(e))) {
    13. }else{
    14. EntityMoveEvent event = new EntityMoveEvent();
    15. Bukkit.getPluginManager().callEvent(event);
    16. previous.put(e, getLocation());
    17. }
    19. }}}}, 0L, 1L);

    4: If you want to be able to cancel the event, put this in your Listener class:
    1. @EventHandler
    2. public void onMoveCancel(EntityMoveEvent e) {
    3. if (e.isCancelled()) {
    4. e.getEntity().teleport(previous.get(e));
    5. }else{
    7. }
    8. }

    Basically, what this does is on the start of your server, it puts all the entities and their associated locations in a HashMap. Next, it runs through all the entities and checks if their location still equals their location, and if not, adds their new location to the HashMap, and calls the EntityMoveEvent class. WARNING: This might be very laggy for your server. Although, if you want to just target specific entities, change it so that the repeating task gathers all of a type if entities and their locations.
  2. Offline

    The Fancy Whale

    You should only trigger the event if they move a whole block. Would help to decrease lag.
  3. Offline


    How exactly is this useful? You may as well just run the code, no need for the event.
  4. Offline


    This has terrible efficiency, will lag a server like hell.
    GrandmaJam, Cirno, jimuskin and 2 others like this.
  5. Offline


    That's why I put the warning. If you want to just get let's say all the skeletons for a single world, then that would be a lot less laggy, and much more efficient. I was just posting the main idea.

    You have the event so that you can cancel the entity from moving if you want to.

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


    But you could do that without using the fairly resource intensive Event API at an insanely spammy level for no apparent reason.
  7. Offline


    This will turn into a memory leak very, VERY quickly
    Cirno and bigteddy98 like this.
  8. Offline


    there are 200 mobs per player on server, dont you understand how much load this will cause?
  9. Offline


    This is not EntityMoveEvent, this is LagBomb. It can't be usable. Much better to make custom entities.
    bobacadodl likes this.
  10. Offline


    If you want to stop entities moving, just set their movement speed to 0. It required NMS, thought, but will not lag. And it's even possible to store old movement speed and apply it back later very easily.

    Also, BKCommonLib has optimized version of entity move event.
  11. Offline


    bensku or you can give them slowness.
  12. Offline


    _Belknap_ Garris0n xTrollxDudex RawCode Quaro
    Atleast this library is better than this:
    1. public void onEnable(){
    2. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    3. public void run(){
    4. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    5. for(Entity e : p.getWorld().getEntities){
    6. p.getWorld().spawnEntity(Entity.SQUID, spawnPoint);
    7. }
    8. }
    9. }, 0L, 1L);
    10. }
    jimuskin likes this.
  13. Offline


    I think it's better to use server.shutdown(); instead of crashing it :)
  14. Offline


    I've brought it up because I'm a huge fan of it:

    This class doesn't make any sense; if you want to check movement, just track the specific entity and use a sync repeating task; don't track every single item on the ground or EXP Orb flying around.
Thread Status:
Not open for further replies.

Share This Page