Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package musaddict.snowgrow;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Biome;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockFace;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerTeleportEvent;
- import org.bukkit.event.weather.WeatherChangeEvent;
- public class AutoGrowListeners implements Listener
- {
- private SnowGrow plugin;
- private int playerUpdateTaskID; //Master player update taskID.
- private int snowUpdateTaskID; //Auto-Snow update taskID.
- private HashMap<String, Location> playerPositions;
- private SnowGrowSchedule updateThread;
- public AutoGrowListeners(SnowGrow plugin) {
- super();
- this.plugin = plugin;
- playerUpdateTaskID = -1;
- snowUpdateTaskID = -1;
- playerPositions = new HashMap<String, Location>();
- }
- @EventHandler
- public void onWeatherChange(WeatherChangeEvent event) {
- if (SnowGrow.worldData.list.contains(event.getWorld())) {
- /* 1: Enable for all chunks within radius of any players, check regularly and add/remove chunks as players move.
- * Problems: could be many chunks to update. With many players there would be a fuck ton of block updates per second.
- * Need config option for max number of chunks to snow in, would prevent the server from getting overloaded while auto-snowing.
- * Add chunks searching outwards from the players;; Would need to add nearest chunk to a given player then move onto the next player.
- *
- * 2: Fuck chunks. Do everything on a per-block radius from the player
- */
- if (event.toWeatherState()) //If true it's starting to rain/snow for the world.
- //Start scheduler to re-check player position (every 30 seconds)
- playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
- else {//Cancel all running tasks.
- Bukkit.getServer().getScheduler().cancelTask(playerUpdateTaskID);
- Bukkit.getServer().getScheduler().cancelTask(snowUpdateTaskID);
- playerUpdateTaskID = -1;
- snowUpdateTaskID = -1;
- }
- }
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent event) {
- if (playerUpdateTaskID == -1 && event.getPlayer().getWorld().hasStorm())
- playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
- }
- @EventHandler
- public void onPlayerTeleport(PlayerTeleportEvent event) {
- if (playerUpdateTaskID == -1 && event.getTo().getWorld().hasStorm())
- playerUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new UpdatePlayerBlockInfo(plugin), 2L, (long) (plugin.getConfig().getDouble("storm-snow.player-check-delay") * 20));
- }
- public HashSet<Location> getAffectedBlocks(Player player) {
- HashSet<Location> affectedBlocks = new HashSet<Location>();
- double sourceX = player.getLocation().getBlockX(), sourceZ = player.getLocation().getBlockZ();
- sourceX += (sourceX >= 0)? 0.5 : -0.5;
- sourceZ += (sourceZ >= 0)? 0.5 : -0.5;
- int radius = plugin.getConfig().getInt("storm-snow.max-range") / 2;
- double minX = sourceX - radius,
- maxX = sourceX + radius,
- minZ = sourceZ - radius,
- maxZ = sourceZ + radius;
- World world = player.getWorld();
- for (double x = minX; x < maxX; x++)
- {
- for (double z = minZ; z < maxZ; z++)
- {
- int highesyBlockY = world.getHighestBlockYAt((int) x, (int) z); //Gets the highest non-air coordinate at the given coordinates
- Block temp = world.getBlockAt((int) x, highesyBlockY, (int) z);
- while (temp.getType() == Material.AIR) //For some reason getHighestBlockYAt sometimes returns a block that has air beneath it.
- temp = temp.getRelative(BlockFace.DOWN);
- if (temp.getBiome() != Biome.TAIGA
- && temp.getBiome() != Biome.TAIGA_HILLS
- && temp.getBiome() != Biome.TUNDRA
- && temp.getBiome() != Biome.ICE_DESERT
- && temp.getBiome() != Biome.ICE_MOUNTAINS
- && temp.getBiome() != Biome.ICE_PLAINS
- && temp.getBiome() != Biome.SEASONAL_FOREST) //There are more; I'm not sure if they're needed.
- continue;
- //blocks to replace with snow
- if (SnowGrowCommands.ToReplace.contains(temp.getType()))
- affectedBlocks.add(temp.getLocation());
- else if (!SnowGrowCommands.NotAllowed.contains(temp.getType()))
- affectedBlocks.add(temp.getRelative(BlockFace.UP).getLocation());
- }
- }
- return affectedBlocks;
- }
- public class UpdatePlayerBlockInfo implements Runnable { //Update the chunk info for players as they move (call this when they move)
- private SnowGrow plugin;
- public UpdatePlayerBlockInfo(SnowGrow plugin) {
- super();
- this.plugin = plugin;
- }
- @Override
- public void run() { //Re-check the auto-snow block list based upon the players new location.
- HashSet<Location> newBlocks = new HashSet<Location>();
- boolean update = false;
- for (World world : SnowGrow.worldData.list)
- if (world.hasStorm()) {
- for (Player player : world.getPlayers()) { //For each player on the stormy world.
- if (playerPositions.containsKey(player.getName())) {
- int newX = player.getLocation().getBlockX(), oldX = playerPositions.get(player.getName()).getBlockX(),
- newZ = player.getLocation().getBlockZ(), oldZ = playerPositions.get(player.getName()).getBlockZ(),
- maxDistance = plugin.getConfig().getInt("storm-snow.max-range") / 3;
- if (newX > oldX + maxDistance || newX < oldX - maxDistance || newZ > oldZ + maxDistance || newZ < oldZ - maxDistance) {
- playerPositions.put(player.getName(), player.getLocation());
- update = true;
- }
- }
- else {
- playerPositions.put(player.getName(), player.getLocation());
- update = true;
- }
- }
- for (Player player : world.getPlayers()) { //For each player on the stormy world.
- if (update) {
- HashSet<Location> affectedBlocks = getAffectedBlocks(player); //Get any snow biome blocks around the player.
- if (!affectedBlocks.isEmpty())
- newBlocks.addAll(affectedBlocks);
- }
- }
- }
- if (update) {
- if (updateThread == null)
- updateThread = new SnowGrowSchedule(plugin, new ArrayList<Location>(newBlocks), false);
- if (snowUpdateTaskID == -1)
- snowUpdateTaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, updateThread, 2L, (long) (plugin.getConfig().getDouble("storm-snow.growth-delay") * 20));
- 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.
- updateThread.affectedBlocks = new ArrayList<Location>(newBlocks);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment