<commit_before>package me.nallar.patched.world;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import com.google.common.collect.ImmutableSetMultimap;

import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.collections.TreeHashSet;
import me.nallar.tickthreading.minecraft.DeadLockDetector;
import me.nallar.tickthreading.minecraft.ThreadManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.contextaccess.ContextAccess;
import net.minecraft.block.Block;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.IWorldAccess;
import net.minecraft.world.NextTickListEntry;
import net.minecraft.world.SpawnerAnimals;
import net.minecraft.world.Teleporter;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;

@SuppressWarnings ("unchecked") // Can't make the code in WorldServer checked. Yet. // TODO: Add type parameters via prepatcher?
public abstract class PatchWorldServer extends WorldServer implements Runnable {
	public long ticksPerAnimalSpawns;
	public long ticksPerMonsterSpawns;
	private Iterator chunkCoordIterator;
	private ThreadManager threadManager;
	private ThreadLocal<Random> randoms;
	private ArrayList<NextTickListEntry> runningTickListEntries;
	@Declare
	public ThreadLocal<Boolean> worldGenInProgress_;
	private HashSet<ChunkCoordIntPair> chunkTickSet;

	public PatchWorldServer(MinecraftServer par1MinecraftServer, ISaveHandler par2ISaveHandler, String par3Str, int par4, WorldSettings par5WorldSettings, Profiler par6Profiler) {
		super(par1MinecraftServer, par2ISaveHandler, par3Str, par4, par5WorldSettings, par6Profiler);
	}

	public void construct() {
		if (ticksPerAnimalSpawns == 0) {
			ticksPerAnimalSpawns = 1;
		}
		if (ticksPerMonsterSpawns == 0) {
			ticksPerMonsterSpawns = 1;
		}
		randoms = new ThreadLocalRandom();
		threadManager = new ThreadManager(TickThreading.instance.getThreadCount(), "Chunk Updates for " + Log.name(this));
		try {
			field_73064_N = null;
		} catch (NoSuchFieldError ignored) {
			//MCPC+ compatibility - they also remove this.
		}
		pendingTickListEntries = new TreeHashSet();
		runningTickListEntries = new ArrayList<NextTickListEntry>();
		try {
			chunkTickSet = (HashSet<ChunkCoordIntPair>) activeChunkSet;
		} catch (NoSuchFieldError ignored) {
			//Spigot support
			chunkTickSet = new HashSet<ChunkCoordIntPair>();
		}
	}

	@Override
	@Declare
	public Object[] getChunks() {
		List<Chunk> loadedChunks = theChunkProviderServer.getLoadedChunks();
		synchronized (loadedChunks) {
			return loadedChunks.toArray();
		}
	}

	@Override
	@Declare
	public List getEntities() {
		synchronized (loadedEntityList) {
			return Arrays.asList(loadedEntityList.toArray());
		}
	}

	@Override
	@Declare
	public void ttStop() {
		IChunkLoader chunkLoader = theChunkProviderServer.currentChunkLoader;
		if (chunkLoader instanceof AnvilChunkLoader) {
			try {
				((AnvilChunkLoader) chunkLoader).close();
			} catch (NoSuchMethodError ignored) {
				//MCPC+ compatibility
			}
		}
		threadManager.stop();
	}

	@Override
	public void flush() {
		DeadLockDetector.tick("Saving a world before unload", System.nanoTime() + 30000000000L);
		if (saveHandler != null) {
			this.saveHandler.flush();<commit_msg>Fix #312<commit_after>package me.nallar.patched.world;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import com.google.common.collect.ImmutableSetMultimap;

import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.collections.TreeHashSet;
import me.nallar.tickthreading.minecraft.DeadLockDetector;
import me.nallar.tickthreading.minecraft.ThreadManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.contextaccess.ContextAccess;
import net.minecraft.block.Block;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.IProgressUpdate;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.IWorldAccess;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.NextTickListEntry;
import net.minecraft.world.SpawnerAnimals;
import net.minecraft.world.Teleporter;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;

@SuppressWarnings ("unchecked") // Can't make the code in WorldServer checked. Yet. // TODO: Add type parameters via prepatcher?
public abstract class PatchWorldServer extends WorldServer implements Runnable {
	public long ticksPerAnimalSpawns;
	public long ticksPerMonsterSpawns;
	private Iterator chunkCoordIterator;
	private ThreadManager threadManager;
	private ThreadLocal<Random> randoms;
	private ArrayList<NextTickListEntry> runningTickListEntries;
	@Declare
	public ThreadLocal<Boolean> worldGenInProgress_;
	private HashSet<ChunkCoordIntPair> chunkTickSet;

	public PatchWorldServer(MinecraftServer par1MinecraftServer, ISaveHandler par2ISaveHandler, String par3Str, int par4, WorldSettings par5WorldSettings, Profiler par6Profiler) {
		super(par1MinecraftServer, par2ISaveHandler, par3Str, par4, par5WorldSettings, par6Profiler);
	}

	public void construct() {
		if (ticksPerAnimalSpawns == 0) {
			ticksPerAnimalSpawns = 1;
		}
		if (ticksPerMonsterSpawns == 0) {
			ticksPerMonsterSpawns = 1;
		}
		randoms = new ThreadLocalRandom();
		threadManager = new ThreadManager(TickThreading.instance.getThreadCount(), "Chunk Updates for " + Log.name(this));
		try {
			field_73064_N = null;
		} catch (NoSuchFieldError ignored) {
			//MCPC+ compatibility - they also remove this.
		}
		pendingTickListEntries = new TreeHashSet();
		runningTickListEntries = new ArrayList<NextTickListEntry>();
		try {
			chunkTickSet = (HashSet<ChunkCoordIntPair>) activeChunkSet;
		} catch (NoSuchFieldError ignored) {
			//Spigot support
			chunkTickSet = new HashSet<ChunkCoordIntPair>();
		}
	}

	@Override
	@Declare
	public Object[] getChunks() {
		List<Chunk> loadedChunks = theChunkProviderServer.getLoadedChunks();
		synchronized (loadedChunks) {
			return loadedChunks.toArray();
		}
	}

	@Override
	@Declare
	public List getEntities() {
		synchronized (loadedEntityList) {
			return Arrays.asList(loadedEntityList.toArray());
		}
	}

	@Override
	@Declare
	public void ttStop() {
		IChunkLoader chunkLoader = theChunkProviderServer.currentChunkLoader;
		if (chunkLoader instanceof AnvilChunkLoader) {
			try {
				((AnvilChunkLoader) chunkLoader).close();
			} catch (NoSuchMethodError ignored) {
				//MCPC+ compatibility
			}
		}
		threadManager.stop();
	}

	@Override
	public void saveAllChunks(boolean par1, IProgressUpdate par2IProgressUpdate) throws MinecraftException {
		if (this.chunkProvider.canSave()) {
			synchronized (WorldEvent.Save.class) {
				this.saveLevel();
			}

			this.chunkProvider.saveChunks(par1, par2IProgressUpdate);

			synchronized (WorldEvent.Save.class) {
				MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(this));
			}
		}
	}

	@Override
	public void flush() {
		DeadLockDetector.tick("Saving a world before unload", System.nanoTime() + 30000000000L);
		if (saveHandler != null) {
			this.saveHandler.flush();
{'fraction_non_alphanumeric': 0.09655623365300785, 'fraction_numerical': 0.007737576285963383, 'mean_word_length': 5.9, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4261075', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2891, 'n_words': 552}