/*
 *  This file is part of Cubic Chunks Mod, licensed under the MIT License (MIT).
 *
 *  Copyright (c) 2015 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 io.github.opencubicchunks.cubicchunks.core.asm.mixin.fixes.common;

import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Mixin(EntityLivingBase.class)
public abstract class MixinEntityLivingBase extends Entity {

    public MixinEntityLivingBase(World worldIn) {
        super(worldIn);
    }

    @ModifyArg(
            method = "travel",
            index = 1,
            at = @At(
                    target = "Lnet/minecraft/util/math/BlockPos$PooledMutableBlockPos;setPos(DDD)"
                            + "Lnet/minecraft/util/math/BlockPos$PooledMutableBlockPos;",
                    value = "INVOKE", ordinal = 1)
    )
    public double moveEntityWithHeading_getReplacedY(double y) {
        return this.posY;
    }
    
    @ModifyConstant(method = "attemptTeleport", constant = @Constant(expandZeroConditions = Constant.Condition.GREATER_THAN_ZERO))
    private int getMinHeight(int orig) {
        return ((ICubicWorld) world).getMinHeight();
    }
}

@@ -1435,166 +1435,60 @@
 aft.
-world.World;%0Aimport org.spongepowered.asm.mixin.Mixin;%0Aimport org.spongepowered.asm.mixin.injection.At;%0Aimport org.spongepowered.asm.mixin.injection.ModifyArg
+util.math.BlockPos;%0Aimport net.minecraft.world.World
 ;%0Aim
@@ -1524,26 +1524,13 @@
 xin.
-injection.Constant
+Mixin
 ;%0Aim
@@ -1572,30 +1572,17 @@
 jection.
-ModifyConstant
+*
 ;%0Aimport
@@ -2371,20 +2371,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Mo
@@ -2602,11 +2602,469 @@
 );%0A    %7D
+%0A%0A    @Redirect(method = %22attemptTeleport%22, at = @At(value = %22INVOKE%22, target = %22Lnet/minecraft/util/math/BlockPos;getY()I%22))%0A    private int isBlockLoadedTeleportCheck(BlockPos blockPos) %7B%0A        // in this check:%0A        // while (!flag1 && blockpos.getY() %3E 0)%0A        // return very high Y if the block isn't loaded to avoid potentially infinite loop%0A        return world.isBlockLoaded(blockPos.down()) ? blockPos.getY() : (Integer.MIN_VALUE + 1);%0A    %7D
 %0A%7D%0A

Fix Minecraft hanging when entities teleport in cubic chunks end.