Freezing Ghasts

Discussion in 'Plugin Development' started by The Fancy Whale, Jul 22, 2014.

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

    The Fancy Whale

    I am trying to make a plugin where you spawn ghasts that are stuck in place and just shoot at people. In this code this ghast spawns but moves at normal speed. If anyone knows how to freeze them it would be greatly appreciated. Thanks!
    Code:java
    1. Ghast entity = (Ghast) Locations.ghast(i).getWorld().spawnEntity(Locations.ghast(i), EntityType.GHAST);
    2. entity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 10)); //also tried 30 instead of 10 which had no effect
     
  2. Offline

    stormneo7

    First thing that came to my mind. Don't quote me on this.
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    2. public void run(){
    3. for(World w : Bukkit.getWorlds())
    4. for(Entity e : w.getEntities())
    5. if(e.getType() == EntityType.GHAST)
    6. e.teleport(e.getLocation());
    7. }
    8. }, 0L, 1L);
     
  3. Offline

    artish1

    What you can do is create a BukkitRunnable, (there might be another way to it though) and for each amount of tick... teleport the ghast to its same location. You can also check if entity.getLocation.getBlockX() == loc.getBlockX(), and if its not tp (Don't forget getBlockY() and getBlockZ() )... just a cleaner way i think.
     
  4. Offline

    The Fancy Whale

    Yeah to be honest that just sounds like a lag machine waiting to happen :p
    Also, not sure they would be able to aim and shoot if they are getting repeatedly teleported. But thanks for the idea!
     
  5. Offline

    artish1

    stormneo7
    But the problem with that is, that it will loop through all worlds, and all entities, and it will freeze all the poor Ghasts out there. It makes the plugin do more work on the server if you know what i mean.
     
  6. Offline

    The Fancy Whale

    stormneo7 Whoops, just noticed your post. It is essentially the same ideas as artish1 and as I said, look slike it could lag alot and I don't think the ghasts would be able to aim if they are getting teleporting every tick.
     
  7. Offline

    stormneo7

    Unfortunately, there isn't an EntityMoveEvent yet :(
     
  8. Offline

    fireblast709

    artish1 likes this.
  9. Offline

    The Fancy Whale

    But the thing that doesn't move would have to be floating in mid air? Then the same issue occurs where I would be repeatedly teleporting the entity right?
     
  10. Offline

    artish1

    The Fancy Whale
    No... what you can do is let them ride an Item(Entity). And Spawn it before the Ghast gets spawned... then put the Ghast on it
     
  11. Offline

    The Fancy Whale

    But where would I spawn the item? In the air? Wouldn't it just fall to the ground?
     
  12. Offline

    fireblast709

    The Fancy Whale holograms use wither skulls (to keep horses in the air)
    Though you would have to check how you can properly freeze those (I know it is possible through NMS, but I am not sure how well a Bukkit only approach works out)
     
  13. Offline

    artish1

  14. Offline

    The Fancy Whale

  15. Offline

    artish1

    The Fancy Whale
    Hmmm... or you can create your own custom Ghast, (MyGhast extends EntityGhast)
    Then look through the methods and check which one fires when the ghast moves...you can do something around there... then you can either 1)Make your own GhastMoveEvent and cancel it there or 2) Cancel it (return;) at the method itself... so it will stay where u spawned it in forever... then initialize the Custom Ghast into the Bukkit server.

    Edit: i meant to say "Register" the Ghast into the server.
     
  16. Offline

    The Fancy Whale

  17. Offline

    fireblast709

  18. Offline

    Onlineids

    The Fancy Whale Made a class with the help from the resource by Goblom where you can tp ghasts back to there location every half second with no lag unless you run the command a crap ton:
    Code:java
    1. package me.online.AdvancedPVP;
    2.  
    3. import java.util.HashMap;
    4. import java.util.HashSet;
    5. import java.util.Map;
    6. import java.util.Set;
    7. import java.util.UUID;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Location;
    11. import org.bukkit.World;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.EntityType;
    15. import org.bukkit.entity.Ghast;
    16. import org.bukkit.entity.LivingEntity;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.server.PluginDisableEvent;
    21. import org.bukkit.event.world.WorldUnloadEvent;
    22. import org.bukkit.plugin.PluginManager;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24. import org.bukkit.scheduler.BukkitScheduler;
    25. import org.bukkit.scheduler.BukkitTask;
    26.  
    27. public class Commands extends JavaPlugin implements Listener{
    28.  
    29. public void onEnable() {
    30. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    31. getConfig().options().copyDefaults(true);
    32. saveDefaultConfig();
    33. tasks.add(bs.runTaskTimer(this, new Freeze(), 0, 10L)); //plugin represents your JavaPlugin
    34. pm.registerEvents(this, this);
    35. }
    36.  
    37. @Override
    38. public boolean onCommand(CommandSender sender, Command command,
    39. String label, String[] args) {
    40. if(command.getName().equalsIgnoreCase("ghast")){
    41. if(sender instanceof Player){
    42. Player pl = (Player) sender;
    43. Ghast ghast = (Ghast) pl.getWorld().spawnEntity(pl.getLocation(), EntityType.GHAST);
    44. frozen.put(ghast.getUniqueId(), ghast.getLocation());
    45.  
    46. }
    47. }
    48. return false;
    49. }
    50. public static Map<UUID, Location> frozen = new HashMap<UUID, Location>();
    51. private Set<BukkitTask> tasks = new HashSet<BukkitTask>();
    52.  
    53. private BukkitScheduler bs = Bukkit.getScheduler();
    54. private PluginManager pm = Bukkit.getPluginManager();
    55. @EventHandler
    56. public void onPluginDisable(PluginDisableEvent event) {
    57. for (BukkitTask task : tasks) {
    58. task.cancel();
    59. }
    60. }
    61.  
    62. @EventHandler
    63. public void onWorldUnload(WorldUnloadEvent event) {
    64. for (LivingEntity ent : event.getWorld().getLivingEntities()) {
    65. UUID id = ent.getUniqueId();
    66. if (frozen.containsKey(id)) frozen.remove(id);
    67. }
    68. }
    69.  
    70. private class Freeze implements Runnable {
    71. @Override
    72. public void run() {
    73. for (World world : Bukkit.getWorlds()) {
    74. for (LivingEntity ent : world.getLivingEntities()) {
    75. UUID id = ent.getUniqueId();
    76. if (frozen.containsKey(id)) ent.teleport(frozen.get(id));
    77. }
    78. }
    79. }
    80. }
    81.  
    82. }
     
Thread Status:
Not open for further replies.

Share This Page