package com.yolp900.itsjustacharm.common.events;
import com.yolp900.itsjustacharm.api.ItsJustaCharmAPI;
import com.yolp900.itsjustacharm.api.affinities.AffinityInteractionHandler;
import com.yolp900.itsjustacharm.api.affinities.IAffinityHolderItem;
import com.yolp900.itsjustacharm.api.affinities.IAffinityHolderTile;
import com.yolp900.itsjustacharm.util.RayTraceHelper;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class EventAffinityHolderInteract {
@SubscribeEvent
public void onAffinityHolderInteract(PlayerInteractEvent.RightClickBlock event) {
EntityPlayer player = event.getEntityPlayer();
World world = event.getWorld();
ItemStack stack;
IAffinityHolderItem affinityHolderItem;
EnumHand hand = event.getHand();
ItemStack heldItem = player.getHeldItem(hand);
if (heldItem != null && heldItem.getItem() instanceof IAffinityHolderItem) {
stack = heldItem;
affinityHolderItem = (IAffinityHolderItem) heldItem.getItem();
} else {
return;
}
RayTraceResult rtr = RayTraceHelper.rayTraceFromEntity(world, player, true, 24);
if (rtr != null) {
BlockPos pos = rtr.getBlockPos();
Block block = world.getBlockState(pos).getBlock();
TileEntity tile = world.getTileEntity(pos);
if (tile == null || !(tile instanceof IAffinityHolderTile)) {
return;
}
IAffinityHolderTile affinityHolderTile = (IAffinityHolderTile) tile;
for (AffinityInteractionHandler handler : ItsJustaCharmAPI.Affinities.affinityInteractionHandlers) {
handler.onInteraction(stack, affinityHolderItem, tile, affinityHolderTile, block, pos, player, world);
}
}
}
}