Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.thegamecube.Physics.regen;
- import java.util.List;
- import org.bukkit.block.BlockState;
- import org.bukkit.scheduler.BukkitRunnable;
- import eu.thegamecube.Physics.Physics;
- import eu.thegamecube.Physics.util.Utilities;
- public class RegenTask extends BukkitRunnable {
- private List<BlockState> blocks;
- private int delayBetween;
- public RegenTask(List<BlockState> blocks, int delayBetween,
- long delayToStart) {
- Physics.log("Scheduling "
- + blocks.size()
- + " Blocks for regeneration. Please do not shut down the server, or reload it.");
- this.blocks = blocks;
- this.delayBetween = delayBetween;
- runTaskLater(Physics.getInstance(), delayToStart);
- }
- @Override
- public void run() {
- Physics.log("Regenerating " + blocks.size() + " Blocks.");
- Utilities.regen(blocks, true, delayBetween);
- }
- }
- //////////////////// different class ////////////////////
- package eu.thegamecube.Physics.util;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import org.bukkit.Bukkit;
- import org.bukkit.Effect;
- import org.bukkit.block.BlockState;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.util.Vector;
- import eu.thegamecube.Physics.Physics;
- /**
- * @author TryB4
- */
- public class Utilities {
- private static List<BlockState> waiting = new ArrayList<BlockState>();
- private static List<Integer> tasks = new ArrayList<Integer>();
- /**
- * Taken from
- * http://forums.bukkit.org/threads/lib-regenerating-blocks.209765/
- */
- public static void regen(final List<BlockState> blocks,
- final boolean effect, final int speed) {
- for (BlockState blockState : blocks) {
- waiting.add(blockState);
- }
- tasks.add(new BukkitRunnable() {
- int i = -1;
- @SuppressWarnings("deprecation")
- public void run() {
- if (i != blocks.size() - 1) {
- i++;
- BlockState bs = blocks.get(i);
- bs.getBlock().setType(bs.getType());
- bs.getBlock().setData(bs.getData().getData());
- if (effect)
- bs.getBlock()
- .getWorld()
- .playEffect(bs.getLocation(),
- Effect.STEP_SOUND,
- bs.getBlock().getType());
- if (waiting.contains(bs))
- waiting.remove(bs);
- } else {
- for (BlockState bs : blocks) {
- bs.getBlock().setType(bs.getType());
- bs.getBlock().setData(bs.getBlock().getData());
- }
- blocks.clear();
- Physics.log("Successfully regenerated a total of "
- + (i + 1) + " Blocks.");
- this.cancel();
- }
- }
- }.runTaskTimer(Physics.getInstance(), speed, speed).getTaskId());
- }
- @SuppressWarnings("deprecation")
- /**
- * Incase the server stops during regeneration proccess.
- */
- public static void regenAll() {
- for (BlockState state : waiting) {
- state.getBlock().setType(state.getType());
- state.getBlock().setData(state.getData().getData());
- }
- for (Integer i : tasks) {
- Bukkit.getScheduler().cancelTask(i);
- }
- tasks.clear();
- waiting.clear();
- tasks = null;
- waiting = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement