/*
 * 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.entity.boss;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import net.minecraft.entity.MultiPartEntityPart;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.item.EntityEnderCrystal;
import net.minecraft.world.GameRules;
import org.spongepowered.api.entity.EnderCrystal;
import org.spongepowered.api.entity.living.complex.EnderDragon;
import org.spongepowered.api.entity.living.complex.EnderDragonPart;
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.entity.IMixinGriefer;
import org.spongepowered.common.mixin.core.entity.MixinEntityLiving;

import java.util.Optional;
import java.util.Set;

@Mixin(EntityDragon.class)
public abstract class MixinEntityDragon extends MixinEntityLiving implements EnderDragon {

    @Shadow public MultiPartEntityPart[] dragonPartArray;
    @Shadow public EntityEnderCrystal healingEnderCrystal;

    @Override
    public Set<EnderDragonPart> getParts() {
        Builder<EnderDragonPart> builder = ImmutableSet.builder();

        for (MultiPartEntityPart part : this.dragonPartArray) {
            builder.add((EnderDragonPart) part);
        }

        return builder.build();
    }

    @Redirect(method = "destroyBlocksInAABB", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameRules;getBoolean(Ljava/lang/String;)Z"))
    private boolean onCanGrief(GameRules gameRules, String rule) {
        return gameRules.getBoolean(rule) && ((IMixinGriefer) this).canGrief();
    }
    
    @Override
    public Optional<EnderCrystal> getHealingCrystal() {
        return Optional.ofNullable((EnderCrystal) this.healingEnderCrystal);
    }
}

@@ -1529,31 +1529,181 @@
 ity.
-item.EntityEnderCrystal
+boss.dragon.phase.IPhase;%0Aimport net.minecraft.entity.boss.dragon.phase.PhaseHover;%0Aimport net.minecraft.entity.item.EntityEnderCrystal;%0Aimport net.minecraft.util.math.Vec3d
 ;%0Aim
@@ -2294,16 +2294,51 @@
 l.Set;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 @Mixin(E
@@ -3229,24 +3229,24 @@
 Crystal() %7B%0A
-
         retu
@@ -3315,11 +3315,659 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Fixes a hidden divide-by-zero error when %7B@link PhaseHover%7D returns the%0A     * current location as the target location.%0A     *%0A     * @author JBYoshi%0A     */%0A    @Redirect(method = %22onLivingUpdate%22, at = @At(value = %22INVOKE%22, target = %22Lnet/minecraft/entity/boss/dragon/phase/IPhase;getTargetLocation()Lnet/minecraft/util/math/Vec3d;%22))%0A    @Nullable%0A    private Vec3d getTargetLocationOrNull(IPhase iPhase) %7B%0A        Vec3d target = iPhase.getTargetLocation();%0A        if (target != null && target.x == this.posX && target.z == this.posZ) %7B%0A            return null; // Skips the movement code%0A        %7D%0A        return target;%0A    %7D
 %0A%7D%0A

Fix a divide-by-zero error in EntityDragon that only plugins can expose.