Guest User

AutoGrowListeners.java

a guest
Feb 11th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. package musaddict.snowgrow;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.Material;
  10. import org.bukkit.World;
  11. import org.bukkit.block.Biome;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.BlockFace;
  14. import org.bukkit.entity.Player;
  15. import org.bukkit.event.EventHandler;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.player.PlayerJoinEvent;
  18. import org.bukkit.event.player.PlayerTeleportEvent;
  19. import org.bukkit.event.weather.WeatherChangeEvent;
  20.  
  21. public class AutoGrowListeners implements Listener
  22. {
  23.     private SnowGrow plugin;
  24.     private int playerUpdateTaskID; //Master player update taskID.
  25.     private int snowUpdateTaskID; //Auto-Snow update taskID.
  26.     private HashMap<String, Location> playerPositions;
  27.     private SnowGrowSchedule updateThread;
  28.  
  29.     public AutoGrowListeners(SnowGrow plugin) {
  30.         super();
  31.         this.plugin = plugin;
  32.         playerUpdateTaskID = -1;
  33.         snowUpdateTaskID = -1;
  34.         playerPositions = new HashMap<String, Location>();
  35.     }
  36.  
  37.     @EventHandler
  38.     public void onWeatherChange(WeatherChangeEvent event) {
  39.         if (SnowGrow.worldData.list.contains(event.getWorld())) {
  40.             /* 1: Enable for all chunks within radius of any players, check regularly and add/remove chunks as players move.
  41.              *      Problems: could be many chunks to update. With many players there would be a fuck ton of block updates per second.
  42.              * Need config option for max number of chunks to snow in, would prevent the server from getting overloaded while auto-snowing.
  43.              * Add chunks searching outwards from the players;; Would need to add nearest chunk to a given player then move onto the next player.
  44.              *
  45.              * 2: Fuck chunks. Do everything on a per-block radius from the player
  46.              */
  47.             if (event.toWeatherState()) //If true it's starting to rain/snow for the world.
  48.                 //Start scheduler to re-check player position (every 30 seconds)
  49.                 playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
  50.             else {//Cancel all running tasks.
  51.                 Bukkit.getServer().getScheduler().cancelTask(playerUpdateTaskID);
  52.                 Bukkit.getServer().getScheduler().cancelTask(snowUpdateTaskID);
  53.  
  54.                 playerUpdateTaskID = -1;
  55.                 snowUpdateTaskID = -1;
  56.             }
  57.         }
  58.     }
  59.  
  60.     @EventHandler
  61.     public void onPlayerJoin(PlayerJoinEvent event) {
  62.         if (playerUpdateTaskID == -1 && event.getPlayer().getWorld().hasStorm())
  63.             playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
  64.     }
  65.  
  66.     @EventHandler
  67.     public void onPlayerTeleport(PlayerTeleportEvent event) {
  68.         if (playerUpdateTaskID == -1 && event.getTo().getWorld().hasStorm())
  69.             playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
  70.     }
  71.  
  72.     public HashSet<Location> getAffectedBlocks(Player player) {
  73.         HashSet<Location> affectedBlocks = new HashSet<Location>();
  74.         double sourceX = player.getLocation().getBlockX(), sourceZ = player.getLocation().getBlockZ();
  75.  
  76.         sourceX += (sourceX >= 0)? 0.5 : -0.5;
  77.         sourceZ += (sourceZ >= 0)? 0.5 : -0.5;
  78.  
  79.         int radius = plugin.getConfig().getInt("storm-snow.max-range") / 2;
  80.  
  81.         double minX = sourceX - radius,
  82.             maxX = sourceX + radius,
  83.             minZ = sourceZ - radius,
  84.             maxZ = sourceZ + radius;
  85.  
  86.         World world = player.getWorld();
  87.  
  88.         for (double x = minX; x < maxX; x++)
  89.         {
  90.             for (double z = minZ; z < maxZ; z++)
  91.             {
  92.                 int highesyBlockY = world.getHighestBlockYAt((int) x, (int) z); //Gets the highest non-air coordinate at the given coordinates
  93.  
  94.                 Block temp = world.getBlockAt((int) x, highesyBlockY, (int) z);
  95.  
  96.                 while (temp.getType() == Material.AIR) //For some reason getHighestBlockYAt sometimes returns a block that has air beneath it.
  97.                     temp = temp.getRelative(BlockFace.DOWN);
  98.  
  99.                 if (temp.getBiome() != Biome.TAIGA
  100.                     && temp.getBiome() != Biome.TAIGA_HILLS
  101.                     && temp.getBiome() != Biome.TUNDRA
  102.                     && temp.getBiome() != Biome.ICE_DESERT
  103.                     && temp.getBiome() != Biome.ICE_MOUNTAINS
  104.                     && temp.getBiome() != Biome.ICE_PLAINS
  105.                     && temp.getBiome() != Biome.SEASONAL_FOREST) //There are more; I'm not sure if they're needed.
  106.                     continue;
  107.  
  108.                 //blocks to replace with snow
  109.                 if (SnowGrowCommands.ToReplace.contains(temp.getType()))
  110.                     affectedBlocks.add(temp.getLocation());
  111.                 else if (!SnowGrowCommands.NotAllowed.contains(temp.getType()))
  112.                     affectedBlocks.add(temp.getRelative(BlockFace.UP).getLocation());
  113.             }
  114.         }
  115.  
  116.         return affectedBlocks;
  117.     }
  118.  
  119.     public class UpdatePlayerBlockInfo implements Runnable { //Update the chunk info for players as they move (call this when they move)
  120.         private SnowGrow plugin;
  121.  
  122.         public UpdatePlayerBlockInfo(SnowGrow plugin) {
  123.             super();
  124.             this.plugin = plugin;
  125.         }
  126.  
  127.         @Override
  128.         public void run() { //Re-check the auto-snow block list based upon the players new location.
  129.             HashSet<Location> newBlocks = new HashSet<Location>();
  130.             boolean update = false;
  131.  
  132.             for (World world : SnowGrow.worldData.list)
  133.                 if (world.hasStorm()) {
  134.                     for (Player player : world.getPlayers()) { //For each player on the stormy world.
  135.                         if (playerPositions.containsKey(player.getName())) {
  136.                             int newX = player.getLocation().getBlockX(), oldX = playerPositions.get(player.getName()).getBlockX(),
  137.                                 newZ = player.getLocation().getBlockZ(), oldZ = playerPositions.get(player.getName()).getBlockZ(),
  138.                                 maxDistance = plugin.getConfig().getInt("storm-snow.max-range") / 3;
  139.                             if (newX > oldX + maxDistance || newX < oldX - maxDistance || newZ > oldZ + maxDistance || newZ < oldZ - maxDistance) {
  140.                                 playerPositions.put(player.getName(), player.getLocation());
  141.                                 update = true;
  142.                             }
  143.                         }
  144.                         else {
  145.                             playerPositions.put(player.getName(), player.getLocation());
  146.                             update = true;
  147.                         }
  148.                     }
  149.  
  150.                     for (Player player : world.getPlayers()) { //For each player on the stormy world.
  151.                         if (update) {
  152.                             HashSet<Location> affectedBlocks = getAffectedBlocks(player); //Get any snow biome blocks around the player.
  153.  
  154.                             if (!affectedBlocks.isEmpty())
  155.                                 newBlocks.addAll(affectedBlocks);
  156.                         }
  157.                     }
  158.                 }
  159.  
  160.             if (update) {
  161.                 if (updateThread == null)
  162.                     updateThread = new SnowGrowSchedule(plugin, new ArrayList<Location>(newBlocks), false);
  163.  
  164.                 if (snowUpdateTaskID == -1)
  165.                     snowUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, updateThread, 2L, (long) (plugin.getConfig().getDouble("storm-snow.growth-delay") * 20));
  166.  
  167.                 if (!newBlocks.isEmpty()) //Start the schedule with our new global auto-snow block update list and save it's task ID so we can recreate it later with a new one when needed.
  168.                     updateThread.affectedBlocks = new ArrayList<Location>(newBlocks);
  169.             }
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment