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

import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.EntityAIRunAroundLikeCrazy;
import net.minecraft.entity.passive.AbstractHorse;
import net.minecraft.entity.player.EntityPlayer;
import org.spongepowered.api.event.cause.entity.dismount.DismountTypes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.entity.EntityBridge;

@Mixin(EntityAIRunAroundLikeCrazy.class)
public abstract class EntityAIRunAroundLikeCrazyMixin extends EntityAIBaseMixin {

    @Shadow @Final @Mutable private AbstractHorse horseHost;

    /**
     * @author rexbut - December 16th, 2016
     *
     * @reason - adjusted to support {@link DismountTypes}
     */
    @Overwrite
    public void updateTask() {
        if (this.horseHost.getRNG().nextInt(50) == 0) {
            Entity entity = this.horseHost.getPassengers().get(0);

            if (entity == null) {
                return;
            }

            if (entity instanceof EntityPlayer) {
                int i = this.horseHost.getTemper();
                int j = this.horseHost.getMaxTemper();

                if (j > 0 && this.horseHost.getRNG().nextInt(j) < i) {
                    this.horseHost.setTamedBy((EntityPlayer)entity);
                    this.horseHost.world.setEntityState(this.horseHost, (byte)7);
                    return;
                }

                this.horseHost.increaseTemper(5);
            }

            // Sponge start - Throw an event before calling entity states
            // this.horseHost.removePassengers(); // Vanilla
            if (((EntityBridge) this.horseHost).bridge$removePassengers(DismountTypes.DERAIL)) {
                // Sponge end
                this.horseHost.makeMad();
                this.horseHost.world.setEntityState(this.horseHost, (byte)6);
            }
        }
    }
}

@@ -1492,16 +1492,219 @@
 Player;%0A
+import org.spongepowered.api.Sponge;%0Aimport org.spongepowered.api.entity.living.animal.Horse;%0Aimport org.spongepowered.api.event.CauseStackManager;%0Aimport org.spongepowered.api.event.SpongeEventFactory;%0A
 import o
@@ -1767,16 +1767,16 @@
 tTypes;%0A
-
 import o
@@ -1984,16 +1984,60 @@
 Shadow;%0A
+import org.spongepowered.common.SpongeImpl;%0A
 import o
@@ -2798,24 +2798,24 @@
 xTemper();%0A%0A
-
             
@@ -2869,24 +2869,412 @@
 t(j) %3C i) %7B%0A
+                    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) %7B%0A                        frame.pushCause(entity);%0A                        if (SpongeImpl.postEvent(SpongeEventFactory.createTameEntityEvent(frame.getCurrentCause(), (Horse) this.horseHost))) %7B%0A                            return;%0A                        %7D%0A                    %7D%0A
             

Support TameEntityEvent for horses