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

import net.minecraft.entity.item.EntityItemFrame;
import net.minecraft.util.DamageSource;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.hanging.ItemFrame;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.entity.AttackEntityEvent;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.rotation.Rotation;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.common.SpongeImpl;
import org.spongepowered.common.item.inventory.util.ItemStackUtil;
import org.spongepowered.common.mixin.core.entity.MixinEntityHanging;

import java.util.ArrayList;
import java.util.Optional;

import javax.annotation.Nullable;

@Mixin(EntityItemFrame.class)
public abstract class MixinEntityItemFrame extends MixinEntityHanging implements ItemFrame {

    @Shadow @Nullable public abstract net.minecraft.item.ItemStack getDisplayedItem();
    @Shadow public abstract void setDisplayedItem(@Nullable net.minecraft.item.ItemStack p_82334_1_);

    @Shadow(prefix = "shadow$")
    public abstract int shadow$getRotation();

    @Shadow
    public abstract void setItemRotation(int p_82336_1_);

    @Inject(method = "attackEntityFrom", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/item/EntityItemFrame;dropItemOrSelf(Lnet/minecraft/entity/Entity;Z)V"))
    private void onAttackEntityFrom(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(source);
            AttackEntityEvent event = SpongeEventFactory.createAttackEntityEvent(frame.getCurrentCause(), new ArrayList(), this, 0, amount);
            SpongeImpl.postEvent(event);
            if (event.isCancelled()) {
                cir.setReturnValue(true);
            }
        }
    }

    public Optional<ItemStack> getItem() {
        return Optional.ofNullable(ItemStackUtil.fromNative(getDisplayedItem()));
    }

    public void setItem(@Nullable ItemStack item) {
        setDisplayedItem(ItemStackUtil.toNative(item));
    }

    public Rotation getItemRotation() {
        return SpongeImpl.getGame().getRegistry().getRotationFromDegree(shadow$getRotation() * 45).get();
    }

    public void setRotation(Rotation itemRotation) {
        setItemRotation(itemRotation.getAngle() / 45);
    }
}

@@ -1946,16 +1946,84 @@
 Inject;%0A
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;%0A
 import o
@@ -3534,32 +3534,594 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Fixes MC-124833%0A     *%0A     * @author Meronat - April 4th, 2018%0A     * @reason Fixes a vanilla dupe in 1.12.2 - MC-124833 - Which resulted due%0A     *     to the displayed item not being properly unset.%0A     *%0A     * @param ci The callback info%0A     */%0A    @Inject(method =%22removeFrameFromMap%22, at = @At(value = %22INVOKE%22, target = %22Lnet/minecraft/item/ItemStack;setItemFrame(Lnet/minecraft/entity/item/EntityItemFrame;)V%22))%0A    private void postOnSetItemFrame(CallbackInfo ci) %7B%0A        setDisplayedItem(net.minecraft.item.ItemStack.EMPTY);%0A    %7D%0A%0A
     public Optio

Fix vanilla item frame dupe - MC-124833 (#1858)