Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games.coob.skywars.model;
- import lombok.experimental.UtilityClass;
- import org.bukkit.Chunk;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Player;
- import org.mineacademy.fo.Common;
- import org.mineacademy.fo.Valid;
- import org.mineacademy.fo.collection.StrictSet;
- import org.mineacademy.fo.model.ChunkedTask;
- import org.mineacademy.fo.region.Region;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- /**
- * Enable us to reset arena world by
- * 1) Stopping the autosave when the arena starts
- * 2) Restoring back chunks to their previous state on arena end
- */
- @UtilityClass
- public final class ArenaWorldManager {
- /**
- * A list of worlds that are being processes
- */
- private final StrictSet<World> processedWorlds = new StrictSet<>();
- /**
- * Disable autosave for the given world
- *
- * @param arena
- */
- public void disableAutoSave(final Arena arena) {
- checkApplicable(arena);
- final World world = arena.getSettings().getRegion().getWorld();
- world.setAutoSave(false);
- Common.log("Disabled world save for " + world.getName());
- }
- /**
- * Attempts to restore all chunks in the world before autosave was disabled
- *
- * @param arena
- */
- public void restoreWorld(final Arena arena) {
- checkApplicable(arena);
- final Region region = arena.getSettings().getRegion();
- final World world = region.getWorld();
- for (final Player player : world.getPlayers())
- player.kickPlayer("The game has ended you should not be here.");
- processedWorlds.add(world);
- final List<Block> blocks = region.getBlocks();
- new ChunkedTask(500_000) {
- final Set<Chunk> chunks = new HashSet<>();
- @Override
- protected void onProcess(final int index) {
- final Block block = blocks.get(index);
- chunks.add(block.getChunk());
- }
- @Override
- protected boolean canContinue(final int index) {
- return index < blocks.size();
- }
- @Override
- protected String getLabel() {
- return "blocks";
- }
- @Override
- protected void onFinish() {
- Common.log("Arena " + arena.getName() + " finished converting blocks to chunks.");
- new ChunkedTask(50) {
- final List<Chunk> chunksCopy = new ArrayList<>(chunks);
- @Override
- protected void onProcess(final int index) {
- final Chunk chunk = chunksCopy.get(index);
- chunk.unload(false);
- chunk.load();
- }
- @Override
- protected boolean canContinue(final int index) {
- return index < chunksCopy.size();
- }
- @Override
- protected String getLabel() {
- return "chunks";
- }
- @Override
- protected void onFinish() {
- Common.log("Arena " + arena.getName() + " finished resetting world " + world.getName() + ".");
- processedWorlds.remove(world);
- }
- }.startChain();
- }
- }.startChain();
- }
- /**
- * Return if the given world is being processed right now
- *
- * @param world
- * @return
- */
- public boolean isWorldBeingProcessed(final World world) {
- return processedWorlds.contains(world);
- }
- /*
- * Check a few settings if we can proceed
- */
- private void checkApplicable(final Arena arena) {
- final ArenaSettings settings = arena.getSettings();
- Valid.checkBoolean(!isWorldBeingProcessed(settings.getRegion().getWorld()), "Arena " + arena.getName() + " world is already being processed!");
- Valid.checkBoolean(arena.getSettings().isWorldResetEnabled(), "Cannot use world restore, arena does not support it!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement