|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.forge.mixin.core.minecraftforge.event.entity; |
| 26 | + |
| 27 | +import net.minecraft.world.entity.item.ItemEntity; |
| 28 | +import net.minecraft.world.entity.player.Player; |
| 29 | +import net.minecraftforge.event.entity.item.ItemEvent; |
| 30 | +import net.minecraftforge.event.entity.item.ItemTossEvent; |
| 31 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 32 | +import org.spongepowered.api.entity.Entity; |
| 33 | +import org.spongepowered.api.event.EventContextKeys; |
| 34 | +import org.spongepowered.api.event.SpongeEventFactory; |
| 35 | +import org.spongepowered.api.event.cause.entity.SpawnTypes; |
| 36 | +import org.spongepowered.api.event.item.inventory.DropItemEvent; |
| 37 | +import org.spongepowered.asm.mixin.Final; |
| 38 | +import org.spongepowered.asm.mixin.Mixin; |
| 39 | +import org.spongepowered.asm.mixin.Shadow; |
| 40 | +import org.spongepowered.common.event.tracking.PhaseTracker; |
| 41 | +import org.spongepowered.forge.launch.bridge.event.ForgeEventBridge_Forge; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | + |
| 45 | +@Mixin(ItemTossEvent.class) |
| 46 | +public abstract class ItemTossEventMixin_Forge extends ItemEvent implements ForgeEventBridge_Forge<DropItemEvent.Dispense> { |
| 47 | + |
| 48 | + //@formatter:off |
| 49 | + @Shadow private @Final Player player; |
| 50 | + //@formatter:on |
| 51 | + |
| 52 | + private ItemTossEventMixin_Forge(ItemEntity itemEntity) { |
| 53 | + super(itemEntity); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void bridge$syncFrom(DropItemEvent.Dispense event) { |
| 58 | + event.entities().stream() |
| 59 | + .filter(e -> e instanceof ItemEntity) |
| 60 | + .map(e -> (ItemEntity) e) |
| 61 | + .findFirst() |
| 62 | + .ifPresent(e -> this.getEntity().setItem(e.getItem())); |
| 63 | + this.setCanceled(event.isCancelled()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void bridge$syncTo(DropItemEvent.Dispense event) { |
| 68 | + event.setCancelled(this.isCanceled()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public DropItemEvent.@Nullable Dispense bridge$createSpongeEvent() { |
| 73 | + if (this.player.level().isClientSide()) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + try (final var frame = PhaseTracker.getInstance().pushCauseFrame()) { |
| 77 | + frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM); |
| 78 | + final var cause = frame.currentCause(); |
| 79 | + final var toSpawn = (Entity) this.getEntity(); |
| 80 | + final var list = new ArrayList<Entity>(); |
| 81 | + list.add(toSpawn); |
| 82 | + return SpongeEventFactory.createDropItemEventDispense(cause, list); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments