/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.mixin.core.server.management;

import net.minecraft.server.management.PlayerChunkMap;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.common.interfaces.server.management.IMixinPlayerChunkMap;
import org.spongepowered.common.interfaces.world.IMixinWorldServer;

import javax.annotation.Nullable;

@Mixin(PlayerChunkMap.class)
public abstract class MixinPlayerChunkMap implements IMixinPlayerChunkMap {

    @Shadow @Nullable public abstract PlayerChunkMapEntry getEntry(int chunkX, int chunkZ);

    @Override
    public boolean isChunkInUse(int x, int z) {
        PlayerChunkMapEntry playerInstance = this.getEntry(x, z);
        return playerInstance != null && playerInstance.players.size() > 0;
    }
}

@@ -1420,16 +1420,56 @@
 pEntry;%0A
+import net.minecraft.world.WorldServer;%0A
 import n
@@ -1500,16 +1500,16 @@
 .Chunk;%0A
-
 import n
@@ -1548,24 +1548,66 @@
 iderServer;%0A
+import org.spongepowered.asm.mixin.Final;%0A
 import org.s
@@ -1783,16 +1783,72 @@
 direct;%0A
+import org.spongepowered.common.interfaces.IMixinChunk;%0A
 import o
@@ -2132,16 +2132,72 @@
 kMap %7B%0A%0A
+    @Shadow @Final private WorldServer theWorldServer;%0A%0A
     @Sha
@@ -2489,12 +2489,1181 @@
  0;%0A
-
     %7D%0A
+%0A    @Redirect(method = %22removeEntry%22, at = @At(value = %22INVOKE%22, target = %22Lnet/minecraft/world/gen/ChunkProviderServer;%22%0A            + %22unload(Lnet/minecraft/world/chunk/Chunk;)V%22))%0A    private void onUnloadChunk(ChunkProviderServer chunkProvider, Chunk chunk) %7B%0A        // We remove the ability for a PlayerChunkMap to queue chunks for unload to prevent chunk thrashing%0A        // where the same chunks repeatedly unload and load. This is caused by a player moving in and out of the same chunks.%0A        // Instead, the Chunk GC will now be responsible for going through loaded chunks and queuing any chunk where no player%0A        // is within view distance or a spawn chunk is force loaded. However, if the Chunk GC is disabled then we will fall back to vanilla%0A        // and queue the chunk to be unloaded.%0A        // -- blood%0A%0A        if (((IMixinWorldServer) this.theWorldServer).getChunkGCTickInterval() %3C= 0%0A                %7C%7C ((IMixinWorldServer) this.theWorldServer).getChunkUnloadDelay() %3C= 0) %7B%0A            chunkProvider.unload(chunk);%0A        %7D else %7B%0A            ((IMixinChunk) chunk).setScheduledForUnload(System.currentTimeMillis());%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Move chunk GC unloading mixin to common and convert to redirect