[Resource] Entity Freeze

Discussion in 'Resources' started by Goblom, Oct 30, 2013.

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

    Goblom

    (sorry im not very good with descriptions here)
    To make the entity frozen just put their UUID into the frozen HashMap along with their location and the entity will stay frozen.

    Code:java
    1. import java.util.HashMap;
    2. import java.util.HashSet;
    3. import java.util.Map;
    4. import java.util.Set;
    5. import java.util.UUID;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.entity.LivingEntity;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.server.PluginDisableEvent;
    13. import org.bukkit.event.world.WorldUnloadEvent;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.scheduler.BukkitScheduler;
    16. import org.bukkit.scheduler.BukkitTask;
    17.  
    18. /**
    19. *
    20. * @author Goblom
    21. */
    22. public class EntityFreeze implements Listener {
    23.  
    24. public static Map<UUID, Location> frozen = new HashMap();
    25. private Set<BukkitTask> tasks = new HashSet<BukkitTask>();
    26.  
    27. private BukkitScheduler bs = Bukkit.getScheduler();
    28. private PluginManager pm = Bukkit.getPluginManager();
    29.  
    30. public EntityFreeze() {
    31. tasks.add(bs.runTaskTimer(plugin, new Freeze(), 0, 20L)); //plugin represents your JavaPlugin
    32. pm.registerEvents(this, plugin);
    33. }
    34.  
    35. @EventHandler
    36. public void onPluginDisable(PluginDisableEvent event) {
    37. for (BukkitTask task : tasks) {
    38. task.cancel();
    39. }
    40. }
    41.  
    42. @EventHandler
    43. public void onWorldUnload(WorldUnloadEvent event) {
    44. for (LivingEntity ent : event.getWorld().getLivingEntities()) {
    45. UUID id = ent.getUniqueId();
    46. if (frozen.containsKey(id)) frozen.remove(id);
    47. }
    48. }
    49.  
    50. private class Freeze implements Runnable {
    51. @Override
    52. public void run() {
    53. for (World world : Bukkit.getWorlds()) {
    54. for (LivingEntity ent : world.getLivingEntities()) {
    55. UUID id = ent.getUniqueId();
    56. if (frozen.containsKey(id)) ent.teleport(frozen.get(id));
    57. }
    58. }
    59. }
    60. }
    61. }

    Updates:
    • November 1, 2013
      • Cancel all tasks when plugin gets disabled
      • If world gets unloaded check to see if frozen contains entities from that world and remove them
      • removed init() in place of EntityFreeze()
     
  2. Offline

    hubeb

    nice!
     
  3. Offline

    user_43347

    Or just apply Slowness 6...
     
    Proudyy likes this.
  4. Offline

    zachoooo

    Why constantly teleport them. You could just use a listener.
     
  5. Offline

    xTrollxDudex

    .__.

    There is no EntityMoveEvent by the way.
     
  6. Offline

    zachoooo

    You can make your own events
     
  7. Offline

    xTrollxDudex

    zachoooo
    I don't think anyone would listen to all the entities to a world. Just saying.

    I would work however.
     
  8. Offline

    zachoooo

    True, that's not necessarily the way I would do it. I just feel that constantly teleporting them is not a very elegant solution.
     
  9. Offline

    Goblom

    zachoooo Its a lot less laggy then an EntityMoveEvent... This library will only teleport entities that are in the hashmap.

    If an EntityMoveEvent is created it will be called every time an entity moves and that cause much lag.
     
  10. Offline

    jimuskin

    zachoooo EntityMoveEvent will be ticking every tick to check if the entity moved which will lag the server heaps
     
  11. Offline

    Garris0n

    It already has to tick every tick and move the entity as well as calculate pathfinding. People greatly overestimate what will cause lag. That said, an actual EntityMoveEvent would require some custom entities to implement and this is far simpler.
     
    bobacadodl, jimuskin and zachoooo like this.
  12. Offline

    Ultimate_n00b

    Add a slowness potion effect. It's the best way.
     
  13. Offline

    Techcable

    Goblom
    couldn't you just give them like slowness 10 or something?
     
  14. Offline

    Goblom

    Techcable Yes but i felt the need to do it this way. Also, i wanted the entities to have no particles coming out of them ;)
     
  15. Offline

    Techcable

    Goblom
    i'm pretty sure you can block the particle packet with protocol-lib
     
  16. Offline

    blablubbabc

    Another thing: I wouldn't store the Location, because it holds a reference to the World and will therefore prevent GarbageCollection, when the world gets unloaded (correct me if I am wrong). I would store the world's name instead and get the actual World object everytime you need it. Or update your Location object on every world unload/load (I think there are events for this).
     
  17. Offline

    Goblom

    Techcable That is true, But i am here creating a simple class that can do a simple task. As soon as i add protocol lib this class gets over crowded.

    blablubbabc Will update class in a moment to add support for that.


    Edit: Update Completed. Check the Update Log.
     
  18. Offline

    Mathias Eklund

    set the movement speed to 0 perhaps?
     
    Ultimate_n00b likes this.
  19. Offline

    Pocketkid2

    I have done this before, you can use the classes from Metadatable too. I am not sure if they have a performance differ, but using some simple methods it is a lot easier! If you would like to see my code, let me know.

    nope, this won't quite work, not only as modifying speed is very tricky, but it doesnt protect from jumping. Although you could give a negative jump boost, which in turn would complicate it even more...

    The simplest way known to bukkit is to just cancel teleport/move events. If you want to make it less laggy, you could decrease the movement amount until cancel.

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

    TheUpdater

    this is a good idea why don't make a custom one then use this code to make them freeze =D
     
Thread Status:
Not open for further replies.

Share This Page