use of net.minecraft.item.ItemLead in project HorsePower by GoryMoon.
the class BlockHPBase method onBlockActivated.
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
ItemStack stack = playerIn.getHeldItem(hand);
TileEntityHPBase te = (TileEntityHPBase) worldIn.getTileEntity(pos);
TileEntityHPHorseBase teH = null;
if (te == null)
return false;
if (te instanceof TileEntityHPHorseBase)
teH = (TileEntityHPHorseBase) te;
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
EntityCreature creature = null;
if (teH != null) {
ArrayList<Class<? extends EntityCreature>> clazzes = Utils.getCreatureClasses();
search: for (Class<? extends Entity> clazz : clazzes) {
for (Object entity : worldIn.getEntitiesWithinAABB(clazz, new AxisAlignedBB((double) x - 7.0D, (double) y - 7.0D, (double) z - 7.0D, (double) x + 7.0D, (double) y + 7.0D, (double) z + 7.0D))) {
if (entity instanceof EntityCreature) {
EntityCreature tmp = (EntityCreature) entity;
if ((tmp.getLeashed() && tmp.getLeashHolder() == playerIn)) {
creature = tmp;
break search;
}
}
}
}
}
if (teH != null && ((stack.getItem() instanceof ItemLead && creature != null) || creature != null)) {
if (!teH.hasWorker()) {
creature.clearLeashed(true, false);
teH.setWorker(creature);
onWorkerAttached(playerIn, creature);
return true;
} else {
return false;
}
} else if (!stack.isEmpty() && te.isItemValidForSlot(0, stack)) {
ItemStack itemStack = te.getStackInSlot(0);
boolean flag = false;
if (itemStack.isEmpty()) {
te.setInventorySlotContents(0, stack.copy());
stack.setCount(stack.getCount() - te.getInventoryStackLimit(stack));
flag = true;
} else if (TileEntityHPBase.canCombine(itemStack, stack)) {
int i = Math.min(te.getInventoryStackLimit(stack), stack.getMaxStackSize()) - itemStack.getCount();
int j = Math.min(stack.getCount(), i);
stack.shrink(j);
itemStack.grow(j);
flag = j > 0;
}
if (flag)
return true;
}
int slot = getSlot(state.getBlock().getExtendedState(state, worldIn, pos), hitX, hitY, hitZ);
ItemStack result = ItemStack.EMPTY;
if (slot > -1) {
result = te.removeStackFromSlot(slot);
} else if (slot > -2) {
result = te.removeStackFromSlot(1);
if (result.isEmpty()) {
result = te.removeStackFromSlot(2);
if (result.isEmpty() && stack.isEmpty() && hand != EnumHand.OFF_HAND) {
result = te.removeStackFromSlot(0);
}
}
if (!result.isEmpty())
emptiedOutput(worldIn, pos);
}
if (result.isEmpty()) {
if (!stack.isEmpty())
return false;
if (teH != null)
teH.setWorkerToPlayer(playerIn);
}
if (!result.isEmpty())
ItemHandlerHelper.giveItemToPlayer(playerIn, result, EntityEquipmentSlot.MAINHAND.getSlotIndex());
te.markDirty();
return true;
}
Aggregations