Search in sources :

Example 1 with ItemFrameAccessor

use of org.spongepowered.common.accessor.world.entity.decoration.ItemFrameAccessor in project SpongeCommon by SpongePowered.

the class EntityTickPhaseState method postBlockTransactionApplication.

@Override
public void postBlockTransactionApplication(final EntityTickContext context, final BlockChange blockChange, final BlockTransactionReceipt transaction) {
    if (blockChange == BlockChange.BREAK) {
        final Entity tickingEntity = context.getSource(Entity.class).get();
        final BlockPos blockPos = VecHelper.toBlockPos(transaction.originalBlock().position());
        final List<HangingEntity> hangingEntities = ((ServerLevel) tickingEntity.world()).getEntitiesOfClass(HangingEntity.class, new AABB(blockPos, blockPos).inflate(1.1D, 1.1D, 1.1D), entityIn -> {
            if (entityIn == null) {
                return false;
            }
            final BlockPos entityPos = entityIn.getPos();
            // Hanging Neighbor Entity
            if (entityPos.equals(blockPos.offset(0, 1, 0))) {
                return true;
            }
            // Check around source block
            final Direction entityFacing = entityIn.getDirection();
            if (entityFacing == Direction.NORTH) {
                return entityPos.equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_NORTH));
            } else if (entityFacing == Direction.SOUTH) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_SOUTH));
            } else if (entityFacing == Direction.WEST) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_WEST));
            } else if (entityFacing == Direction.EAST) {
                return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_EAST));
            }
            return false;
        });
        for (final HangingEntity entityHanging : hangingEntities) {
            if (entityHanging instanceof ItemFrame) {
                final ItemFrame itemFrame = (ItemFrame) entityHanging;
                if (!itemFrame.removed) {
                    ((ItemFrameAccessor) itemFrame).invoker$dropItem((net.minecraft.world.entity.Entity) tickingEntity, true);
                }
                itemFrame.remove();
            }
        }
    }
}
Also used : HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(org.spongepowered.api.entity.Entity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ServerLevel(net.minecraft.server.level.ServerLevel) BlockPos(net.minecraft.core.BlockPos) ItemFrame(net.minecraft.world.entity.decoration.ItemFrame) Direction(net.minecraft.core.Direction) AABB(net.minecraft.world.phys.AABB) ItemFrameAccessor(org.spongepowered.common.accessor.world.entity.decoration.ItemFrameAccessor)

Aggregations

BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 HangingEntity (net.minecraft.world.entity.decoration.HangingEntity)1 ItemFrame (net.minecraft.world.entity.decoration.ItemFrame)1 AABB (net.minecraft.world.phys.AABB)1 Entity (org.spongepowered.api.entity.Entity)1 ItemFrameAccessor (org.spongepowered.common.accessor.world.entity.decoration.ItemFrameAccessor)1